Sorted vectors allow search in O(log n), but insert is O(n) (because of moving elements after the insert position). Linked lists allow insert in O(1) at any position, but lookup takes O(n) because there is no way to go to an element other than by going sequentially from the start.
Binary search trees allow going along the binary search directions.
So, a binary search tree contains keys in all nodes and has property that, for each node, the left subtree contains only keys smaller than the key in the parent node, and the right subtree contains only keys larger than the key in the parent node.
Goal: to avoid trees to degenerate to linked lists. In other words, to keep the height of the tree in O(n), where n is the number of nodes.
An AVL tree is a binary search tree where, for each node, the difference between the heights of the left and right of sub-trees is at most one.
Minimum number of nodes for a given height - denote it by N(h).
We have:
We prove by induction that N(h) ≥ ᵩh. For 0 and 1, this is immediate: N(0)=1= ᵩ0 and N(1)=2>ᵩ1≃1.6
N(h+1)=1+N(h)+N(h-1)>ᵩh+ᵩh-1= ᵩh(1+1/ᵩ) = ᵩhᵩ = ᵩh+1
A red-black tree has each node labeled "red" or black". Important: the tree is considered as having "null" nodes, without keys, as leaves; in practice, those nodes are not actually represented.
Restrictions:
There are 3 cases:
This section deals with how an iterator is represented. Possibilities:
The first approach makes the iterator a full-blown collection (vector or linked list). This is undesirable because it makes parsing slow.
The second approach increases the size of a node by adding the pointer to parent. Parsing is done as follows:
Input-output: p - pointer to the current node; at exit, it will point to the next node Algorithm: if p->right != null: p = p->right while p->left != null: p = p->left else: while p->parent != null and p == p->parent->right: p = p->parent if p->parent != null: p = p->parent else: p = &end
The third approach requires only two bits per node in order to mark if the left and right links are to children or to previous/next node. Those links have to be maintained on insertions and deletions of nodes. Parsing is done as follows:
Input-output: p - pointer to the current node; at exit, it will point to the next node Algorithm: if p->hasRight: p = p->right while p->hasLeft: p = p->left else: p = p->rightRadu-Lucian LUPŞA