Data Structures Balanced Tree Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology Bombay http://www.ee.iitb.ac.in/~viren/ E-mail: viren@ee.iitb.ac.in EE-717/453:Advance Computing for Electrical Engineers Lecture 7 (12 Aug 2013) CADSL
Binary Search Tree - Analysis The running time of these operations is O(d) , where d is the depth of the node containing the accessed item. What is the average depth of the nodes in a binary search tree? It depends on how well balanced the tree is. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Average Depth of Nodes Data Order: 10, 5, 1, 8, 20, 13, 34 1 0 N=7 5 2 0 1 8 1 3 3 4 Consider this very well-balanced binary search tree. What is the depth of its leaf nodes? CADSL 12 Aug 2012 EE-717/EE-453@IITB
A Better Analysis The analysis on the previous slide was for a particularly well-balanced binary search tree. However, not all binary search trees will be this well balanced. In particular, binary search trees are created via insertions of data. Depending on the order of the data, various trees will emerge. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Effect of Data Order 1 4 2 3 3 2 1 4 Obtained if data is 4, 3, 2 1 Obtained if data is 1, 2, 3, 4 Note in these cases the average depth of nodes is about N/2 , not log(N)! CADSL 12 Aug 2012 EE-717/EE-453@IITB
Depth of Nodes In the best case the depth will be about O(log N). In the worst case, if the data are already ordered, the depth will be about O(N). CADSL 12 Aug 2012 EE-717/EE-453@IITB
Effects of Data Order… So, if the input data are randomly ordered, what is the average depth of the nodes? The analysis is beyond the scope, but it can be shown that the average depth is O(log N), which is a very nice result. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Binary Search Tree For an average binary search tree, the average depth of the nodes is O(log N). This is quite amazing, indicating that the bad situations, which are O(N), don’t occur very often. However, for those who are still concerned about the very bad situations, we can try to “balance” the trees. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Balancing Trees What does it mean to “balance” trees? The basic idea is to make sure that the trees aren’t right-heavy or left-heavy. When they are right-heavy or left-heavy, the trees need to be adjusted. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Example 1 2 Here is a right-heavy tree. How can we adjust it to be 3 more balanced??? 4 5 6 7 CADSL 12 Aug 2012 EE-717/EE-453@IITB
Rule #1 4 5 3 6 2 1 7 Rule #1: Require that the left and right subtrees of the root node have the same height. We can do better. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Rule #2 4 2 6 1 3 5 7 Rule #2: Require that every node have left and right subtrees of the same height. Too restrictive. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Rule #3 Rule 3 violated at node 4 4 4 2 2 6 6 1 3 5 1 3 0 0 Rule #3: Require that, for every node, the height of the left and right subtrees can differ by most one. The example on the left satisfies rule #3, while the one on the right does not. Why? This rule is a nice compromise between too lax and too restrictive. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Repair Suppose the tree violates a balance condition. How and when can it be repaired? ● Repair is accomplished via “tree rotations”. ● Repair is done either during insertions, or after access of a node (because during access one notices the node is very deep and should be made more shallow). CADSL 12 Aug 2012 EE-717/EE-453@IITB
AVL Trees AVL (Adelson-Velskii and Landis) trees are binary search trees that follow rule #3. When a tree violates rule #3 a repair is done. ● The repair is done during insertions, as soon as rule #3 is violated. ● The repair is accomplished via “single” and “double” rotations. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Single Rotation k k 2 1 k k 1 2 h Z X X Y Y Z New item uppose an item is added at the bottom of subtree X, thus causing n imbalance at k2. Then pull k1 up. Note that after the rotation, e height of the tree is the same as it was before the insertion. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Example 4 2 4 2 6 1 3 6 1 3 0 0 Imbalance at node 4 solved with single rotation. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Another Single Rotation k k 1 2 k k 1 h 2 Z X X Y Z Y pose an item is added at the bottom of subtree X, thus causing mbalance at k2. Then pull k1 up. Note that after the rotation, height of the tree is the same as it was before the insertion. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Another Example 6 4 8 2 4 6 2 1 5 5 8 0 1 0 Imbalance at node 4 solved with single rotation. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Single Rotations After single rotations, the new height of the entire subtree is exactly the same as the height of the original subtree prior to the insertion of the new data item that caused X to grow. Thus no further updating of heights on the path to the root is needed, and consequently no further rotations are needed. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Double Rotation k k 2 3 k k k 1 1 3 h k D 2 C B A A D B C o r o r Suppose an item is added below k2. This causes an imbalance at k3. Then pull k2 up. Note that after the rotation, the height of the ree is the same as it was before the insertion. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Another Double Rotation k k 2 3 k k k 3 1 h 1 A k 2 B C D A D B C o r o r Suppose an item is added below k2. This causes an imbalance at k3. Then pull k2 up. Note that after the rotation, the height of the tree is the same as it was before the insertion. CADSL 12 Aug 2012 EE-717/EE-453@IITB
An Example 4 4 2 2 6 6 1 5 1 5 8 9 1 1 8 0 0 9 Imbalance at node 8 solved with double rotation. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Which Rotation Do I Use? Recognizing which rotation you have to use is the hardest part. ● Find the imbalanced node. ● Go down two nodes towards the newly inserted node. ● If the path is straight, use single rotation. ● If the path zig-zags, use double rotation. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Double Rotation= 2 Single Rotations k 3 k 1 D k 2 A C B First do a single rotation of k2 and k1. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Double Rotation= 2 Single Rotation s k 3 k 2 D k 1 C A B But k3 still imbalanced, so do a single rotation of k2 and k3 . CADSL 12 Aug 2012 EE-717/EE-453@IITB
Double Rotation= 2 Single Rotations k 2 k k 1 3 C A B D Now we are done. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Double Rotations As with the single rotations, double rotations restore the height of the subtree to what it was before the insertion. This guarantees that all rebalancing and height updating is complete. CADSL 12 Aug 2012 EE-717/EE-453@IITB
Conclusions: AVL Tree AVL trees maintain balance of binary search trees while they are being created via insertions of data. An alternative approach is to have trees that readjust themselves when data is accessed, making often accessed data items move to the top of the tree. We won’t be covering these (splay trees). CADSL 12 Aug 2012 EE-717/EE-453@IITB
Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes CADSL 12 Aug 2012 EE-717/EE-453@IITB
Recommend
More recommend