170 likes | 319 Views
Balanced Trees Abs(depth( leftChild ) – depth( rightChild )) <= 1 Depth of a tree is it’s longest path length. Red-black trees – Restructure the tree when rules among nodes of the tree are violated as we follow the path from root to the insertion point.
E N D
Balanced TreesAbs(depth(leftChild) – depth(rightChild)) <= 1Depth of a tree is it’s longest path length • Red-black trees – Restructure the tree when rules among nodes of the tree are violated as we follow the path from root to the insertion point. • AVL Trees – Maintain a three way flag at each node (-1,0,1) determining whether the left sub-tree is longer, shorter or the same length. Restructure the tree when the flag would go to –2 or +2. • Splay Trees – Don’t require complete balance. However, N inserts and deletes can be done in NlgN time. Rotates are done to move accessed nodes to the top of the tree.
RotatesAnalyze possible tree depths after rotates • LL, RR Rotates • Child node is raised one level • RL, LR Rotates • Child node is raised two levels in two steps • Splay Tree Rotates • Outer nodes of grandparent nodes are raised two levels.
Outer RR and LL rotates LL RR X Y X Y C X A Y C X A Y B A B A B C B C Note: The squares are subtrees, the circles are nodes
Inner LR and RL rotates X Z X Y A Y RL A B Z D C D B C Note: LR is the mirror image
Outer Splay Rotates Z X Y D A Y C X B Z A B C D Note: There is also a rotate for the mirror image
AVL and Splay TreesAdelson-Velskii and Landis AVL Insertion Algorithm • Insert using the same algorithm as the binary search • Traverse back up the tree and for each node on the path to the root • Update the balance weight • IF weight becomes zero, break out of traversal • IF weight becomes +2 or -2, perform an appropriate rotation; break AVL Removal Algorithm • Add the following logic to a standard binary search tree deletionAfter the deleting node having less than two children, traverse up the treeWHILE traversing IF the path to the left was reduced in length, add 1 to the weight IF the path to the right was reduced in length, subtract 1 from the weight IF the tree did not shrink (new weight = ±1) THEN BREAK IF the weight becomes +2 or -2 Find a path in the sibling direction that we can rotate Perform an appropriate rotation and adjust weights
Splay Tree Algorithm Splaying a node (bringing it up to the top) Find the node and double rotate it to the top. A single rotation might be needed at the last step Splay Tree Insertion Algorithm Perform a normal binary search tree insertion and then splay Deletion Algorithm Find and Splay node, n, to delete Remove n leaving two trees (one with nodes with smaller keys) Splay the smallest node in the tree with larger nodes Link the root of the tree with smaller nodes to the root of the tree with the larger nodes
Red Black Trees • Red-black trees are tress that conform to the following rules: • Every node is colored (either red or black) • The root is always black • If a node is red, its children must be black • Every path from the root to leaf, or to a null child, must contain the same number of black nodes. • During insertions and deletions, these rules must be maintained
Red-black Insertion Algorithm current node = root node parent = grandParent = null While current <> null If current is black, and current’s children are red Change current to red (If current<>root)and current’s children to black Call rotateTree() grandParent = parent parent = current current is set to the child node in the binary search sequence If parent is null root = node to insert; color it black ElseConnect the node to insert to the leaf node; it was instantiated to red Call rotateTree() Link the new node to the parent (or create new root if there is no parent)
Rotate Tree() Algorithm Ifcurrent <> root and Parent is red If current is an outer child of the grandParent node Set color of grandParent nodeto red Set color of parent node to black Raise current by rotating parent with grandParent If current is an inner child of the grandParent node Set color of grandParent node to red Set color of current to black Raise current by rotating current with parent Raise current by rotating current with grandParent node
Red Black Example Before adding 99 After adding 99 Note:Color change at 92 led to an outer rotation involving 52, 67, 92
Red Black Deletion • A standard Binary Search Tree removal reduces to removing a node with less than two children • A node with a single child must be black, with a red child (otherwise there would be either a black imbalance or two consecutive red nodes). To delete, simply connect the child to the parent and change its color black. • Leafs can be red or black. If red, simply remove it. If black, removal causes an imbalance of black nodes, which must be restored. • Weapons at our disposal • Color flips • Traversal upward • Rotations
Red Black Deletion (cont.) Explanation The general case • X, Y, Z represent sub-trees • Path from P to the left has K black nodes; the path to the right has K+1 black nodes • P is the parent node, S is the sibling node (A sibling must exist, because of step 2) • To restore balance Case A: IF Head of X is red; turn it black Case B: IF S black with red children; rotate Case C: IF S black with no red children • IF P red, set P black and S red • ELSE color S red restoring balance. Traversal up because both paths now have only K black nodes Case D: IF S red, perform one rotate, two if one or three of S's grandchildren are red. P K black nodes K+1 black nodes X S Y Z
Example Case B (Balance Restored) P S S X P G G Y X Y Z Q Z Q Note: P=parent, S=sibling, G = grandchild, Green node can be either black or red
Case D Examples Case D (An S grandchild is red) Case D (An S grandchild is red) G P S P P S S P G S X X Y G G G X Z X A G G B G Q Y Z Q Q A B A A B B Q Y Z Y Z Note: These examples require another rotation because a double red occurs
Another Case D Example Case D (All of S's Grandchildren are Red) Balance has been restored G P P S S X X G G G B A C D E F G H E F G H A B C D
Which algorithm is best? • Advantages • AVL: relatively easy to program. Insert requires only one rotation. • Splay: No extra storage, high frequency nodes near the top • RedBlack: Fastest in practice, no traversal back up the tree on insert • Disadvantages • AVL: Repeated rotations are needed on deletion, must traverse back up the tree. • SPLAY: Can occasionally have O(N) finds, multiple rotates on every search • RedBlack: Multiple rotates on insertion, delete algorithm difficult to understand and program