This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assuming graph is connected | |
// and a graph node is defined by this structure | |
// struct GraphNode { | |
// T item; | |
// vector<GraphNode*> children; | |
// } | |
// WARNING: untested code | |
void BreadthFirstSearch(GraphNode* node) // start node | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reminder: this is pseudocode, no bother with "const&", "std::" or others | |
// forgive me C++ fellows | |
template <typename BlaBla> | |
class BST | |
{ | |
public: | |
// other code ... | |
vector<BlaBla*> ReverseInOrderProduceVector(int offset, int limit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename BlaBla> | |
class BST | |
{ | |
public: | |
// other code ... | |
vector<BlaBla*> InOrderProduceVector() | |
{ | |
vector<BlaBla*> result; | |
result.reserve(1000); // magic number, reserving a space to avoid reallocation on inserts | |
InOrderProduceVectorHelper_(root_, result); // passing vector by reference |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is a pseudocode, that's why I didn't bother with "const&"'s and "std::"'s | |
// though it could have look better, forgive me C++ fellows | |
vector<Item*> GetItemsByKeywordInSortedOrder(string keyword) | |
{ | |
// assuming IndexTable is a big hashtable mapping keywords to Item BSTs | |
BST<Item*> items = IndexTable[keyword]; | |
// suppose BST has a function InOrderProduceVector(), which creates a vector and | |
// inserts into it items fetched via in-order traversing the tree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cached representation of an Item | |
// Full Item object (with title, description, comments etc.) | |
// could be fetched from the database | |
struct Item | |
{ | |
// ID_TYPE is the type of Item's unique id, might be an integer, or a string | |
ID_TYPE id; | |
int rating; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// struct TreeNode { | |
// T item; | |
// TreeNode* left; | |
// TreeNode* right; | |
// } | |
// you cann pass a callback function to do whatever you want to do with the node's value | |
// in this particular example we are just printing its value. | |
// node is the "starting point", basically the first call is done with the "root" node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// struct ListNode { | |
// ListNode* next; | |
// T item; | |
// }; | |
void TraverseRecursive(ListNode* node) // starting node, most commonly the list 'head' | |
{ | |
if (!node) return; // stop | |
std::cout << node->item; | |
TraverseRecursive(node->next); // recursive call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Warning: a bunch of pseudocode ahead | |
void RangeInsertIntoTimelines(vector<long> user_ids, long tweet_id) | |
{ | |
for (auto id : user_ids) { | |
InsertIntoUserTimeline(id, tweet_id); | |
} | |
} | |
void DeliverATweet(User* author, Tweet* tw) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 'author' represents the User object, at this point we are interested only in author.id | |
// | |
// 'tw' is a Tweet object, at this point we are interested only in 'tw.id' | |
void DeliverATweet(User* author, Tweet* tw) | |
{ | |
// we assume that 'tw' object is already stored in a database | |
// 1. Get the list of user's followers (author of the tweet) | |
vector<User*> user_followers = GetUserFollowers(author->id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 'author' represents the User object, at this point we are interested only in author.id | |
// | |
// 'tw' is a Tweet object, at this point we are interested only in 'tw.id' | |
void DeliverATweet(User* author, Tweet* tw) | |
{ | |
// we assume that 'tw' object is already stored in a database | |
// 1. Get the list of user's followers (author of the tweet) | |
vector<User*> user_followers = GetUserFollowers(author->id); |
NewerOlder