Difference between revisions of "Algorithm Problems"
From Hawk Wiki
(Created page with "==Binary search== <pre> def search_binary_tree(node, key): if node is None: return None # key not found if key < node.key: return search_binary_tree(...") |
(No difference)
|
Revision as of 23:53, 21 February 2012
Binary search
def search_binary_tree(node, key): if node is None: return None # key not found if key < node.key: return search_binary_tree(node.leftChild, key) elif key > node.key: return search_binary_tree(node.rightChild, key) else: # key is equal to node key return node.value # found key
Binary tree insert
/* Inserts the node pointed to by "newNode" into the subtree rooted at "treeNode" */ void InsertNode(Node* &treeNode, Node *newNode) { if (treeNode == NULL) treeNode = newNode; else if (newNode->key < treeNode->key) InsertNode(treeNode->left, newNode); else InsertNode(treeNode->right, newNode); }