5 stars based on
Sets and maps are important and useful abstractions. We've seen various ways to implement longest path binary tree abstract data type for sets and maps, since data structures that implement sets can be used to implement maps as well. Today we will look at an longest path binary tree of sets that is asymptotically efficient and useful in practice. This implementation is one of several balanced binary tree schemes. Binary trees have two advantages above the asymptotically more efficient hash table: Second, they store their values or longest path binary tree, in the case of a map in order, which makes range queries and in-order iteration possible.
An important property of a longest path binary tree tree is that it can be used to implement an ordered set or ordered map easily: Although we will not consider such operations today, ordered sets generally provide operations for finding the minimum and maximum elements of longest path binary tree set, for iterating over all the elements between two elements, and for extracting or iterating over ordered subsets of the elements between a range.
A binary search tree is a binary tree with the following representation invariant: For any node nevery node in the left subtree of n has a value less than n 's value, and every node in the right subtree of n has a value greater than n 's value. Given such a tree, how do you perform a lookup operation?
Start from the root. At every node, if the value of the node is the value you are looking for, you are done. Otherwise, recursively search in the left or right subtree depending on whether the value you are looking for is less than or greater than the one stored at the current longest path binary tree, respectively. Note the use of the keyword function so that the variable used in the pattern matching need not be named.
This is equivalent to unneccessarily naming a variable and then using match:. Adding an element is similar: This is a nondestructive update, so as the recursion completes, a new tree longest path binary tree constructed that is just like the old one except that it has a new node if needed:. What is the running time of those operations? Since add is just a lookup with an extra constant-time node creation, we focus on the lookup operation. Clearly, the run time of lookup is O hwhere h is the height of the tree.
What's the worst-case height of a tree? It occurs with a tree of n nodes all in a single long branch imagine adding the numbers 1,2,3,4,5,6,7 in order into a binary search tree. So the worst-case running time of lookup is still O n for n the number of nodes in the tree. What is a good shape for a tree that would allow for fast lookup? A longest path binary tree binary tree has the largest number of nodes n for a given height h: If unspecified, logs are always base 2, although it doesn't really matter because of the O.
If a tree with n nodes is kept balanced, its height is O log nwhich leads to a lookup operation running in time O log n. How can we keep a tree balanced? It can become unbalanced during element addition or deletion. Most balanced tree schemes involve adding or deleting an element just like in a normal binary search tree, followed by some kind longest path binary tree tree surgery to rebalance the tree.
Some examples of balanced binary search tree data structures include. In each of these, we ensure asymptotic complexity O log n by enforcing a stronger invariant on the data structure than just the binary search tree invariant.
Red-black trees are a fairly simple and very efficient data structure for maintaining a balanced binary tree. The idea is to strengthen the representation invariant so a tree has height logarithmic in n. To help enforce the invariant, we color each node of the tree either red or black.
Where it matters, we consider the color of an empty tree to be black. If a tree satisfies these two conditions, it must also be the case that every subtree of the tree also satisfies the conditions. If a subtree violated either of the conditions, longest path binary tree whole tree would also. Additionally, we require that the root of the tree be colored black. This can always be enforced by simply setting longest path binary tree color to black; doing this does not cause any other invariants to be violated.
With these invariants, the longest possible path from the root to an empty node would alternately contain red and black nodes; therefore it is at most twice as long as the shortest possible path, which only contains black nodes.
If n is the number of nodes in the tree, the longest path cannot have a length greater than twice the length of the paths in a perfect binary tree: Therefore, the tree has height O log n and the operations are all asymptotically logarithmic in the number of nodes. Another way to see this is to think about just the black nodes in the tree. Suppose we snip all the red nodes out of the trees and reconnecting each black node to its closest black ancestor. Then we have a tree whose leaves are all at depth BH, and whose branching factor ranges between 2 and 4.
How do we check for membership in red-black trees? Exactly the same way as for general binary trees. More interesting is the insert operation. As with standard binary trees, we add a node by replacing the leaf found by the search procedure. We also color the new node red to ensure that invariant 2 is preserved.
However, this may destroy invariant 1 by producing two adjacent red nodes. In order to restore the invariant, we consider not only the new red node and its longest path binary tree parent, but also its black grandparent. The next figure shows the four possible cases that can arise. Notice that in each of these trees, the values of the nodes in a, b, c, d must have the same relative ordering with respect to x, y, and z: Therefore, we can transform the tree to restore the invariant locally by replacing any of the above four cases with: In addition, there is the case where the tree is left unchanged locally.
This balancing transformation possibly breaks invariant 1 one level up in the tree, but it can be restored again at that level in the same way, and so on up the tree. In the worst case, the process cascades all the way up to the root, resulting in two adjacent red nodes, one of them the root. But if this happens, we can just recolor the root black, which increases the BH by one.
The amount of work is O log n. The insert code using balance is as follows:. Removing an element from a red-black tree works analogously. We start with BST element removal longest path binary tree then do rebalancing.
When an interior nonleaf node is removed, we simply splice it out if it has fewer than two nonleaf children; if it has two nonleaf children, we find the next value in the tree, which must be found inside its right child. Balancing the trees during removal from red-black tree requires considering more cases. Deleting a black element from the tree creates the possibility that some path in the tree has too few black nodes, breaking the black-height invariant 2.
The solution is to consider that path to contain a "doubly-black" node. A series of tree rotations can then eliminate the doubly-black node by propagating the "blackness" up until a red node can be converted to a black node, or until the longest path binary tree is reached and it can be changed from doubly-black to black without breaking the invariant.