int iterative_search(vector vec, int val) for i = 0 to v.size - 1 if vec[i] == val return i return -1 int recursive_search(vector vec, int i, int val) if i >= vec.size return -1 else if vec[i] == val return i else return recursive_search(vec, i + 1, val)
node * recursive_search(node * root, val) if root == null or root.val == val return root node * found = recursive_search(root->left, val) if found == null found = recursive_search(root->right, val) return found node * iterative_search(node * root, int val) stack s s.push(root) do root = s.pop() if root != null if root->val == val return root s.push(root->left) s.push(root->right) while not s.empty() return null
node * fold(node * list, unsigned size) if size > 1 unsigned h = size/2 list = advance(list, h) list->next = fold(list->next, size - h - 1) list->prev = fold(list, h) return list
(node *, node *) flatten(node * root) if root = null return (root, root) node * left_b, * right_e left_b, root->left = flatten(node->left) root->right, right_e = flatten(node->right) if root->left root->left->right = root else left_b = root if root->right root->right->left = root else right_e = root return (left_b, right_e)
See the complete code.
This page last modified on 3 December 2003.