binary search trees chapter 19kmartin/cop3530/weiss/ch19b_weiss.pdf · figure 19.39 a color flip at...
TRANSCRIPT
![Page 1: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/1.jpg)
Binary Search Trees
Data Structures & Problem SolvingUsing JAVA
Second Edition
Mark Allen Weiss
Chapter 19
© 2002 Addison Wesley
![Page 2: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/2.jpg)
Figure 19.39A color flip at 50 induces a violation; because the violation is outside, a singlerotation fixes it.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 3: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/3.jpg)
Figure 19.40Result of single rotation that fixes the violation at node 50
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 4: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/4.jpg)
Figure 19.41Insertion of 45 as a red node
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 5: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/5.jpg)
Figure 19.50X has two black children, and both of its sibling’s children are black; do a color flip.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 6: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/6.jpg)
Figure 19.51X has two black children, and the outer child of its sibling is red; do asingle rotation.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 7: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/7.jpg)
Figure 19.52X has two black children, and the inner child of its sibling is red; do a doublerotation.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 8: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/8.jpg)
Figure 19.53X is black, and at least one child is red; if we fall through to the next level and landon a red child, fine; if not, we rotate a sibling and parent.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 9: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/9.jpg)
Figure 19.54AA-tree resulting from the insertion of 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40,5, 55, and 35
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 10: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/10.jpg)
Figure 19.55The skew procedure is a simple rotation between X and P.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 11: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/11.jpg)
Figure 19.56The split procedure is a simple rotation between X and R; note that R’slevel increases.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 12: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/12.jpg)
Figure 19.57After insertion of 45 in the sample tree; consecutive hori-zontal links are introduced,starting at 35.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 13: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/13.jpg)
Figure 19.58After split at 35; a left horizontal link at 50 is introduced.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 14: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/14.jpg)
Figure 19.59After skew at 50; consecutive horizontal nodes are introduced starting at 40.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 15: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/15.jpg)
Figure 19.60After split at 40; 50 is now on the same level as 70, inducing an illegal lefthorizontal link.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 16: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/16.jpg)
Figure 19.61After skew at 70; consecutive horizontal links are introduced, starting at 30.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 17: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/17.jpg)
Figure 19.62After split at 30; the insertion is complete.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 18: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/18.jpg)
Figure 19.63When 1 is deleted, all nodes become level 1, thereby introducing horizontalleft links.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 19: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/19.jpg)
Figure 19.82A 5-ary tree of 31 nodes has only three levels
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 20: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/20.jpg)
Figure 19.83A B-tree of order 5
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 21: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/21.jpg)
Figure 19.84The B-tree after insertion of 57 in the tree shown in Figure 19.83.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 22: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/22.jpg)
Figure 19.85Insertion of 55 in the B-tree shown in Figure 19.84 causes a split into two leaves.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
![Page 23: Binary Search Trees Chapter 19kmartin/cop3530/weiss/Ch19b_Weiss.pdf · Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes](https://reader035.vdocument.in/reader035/viewer/2022062923/5f0abd8b7e708231d42d1c45/html5/thumbnails/23.jpg)
Figure 19.86Insertion of 40 in the B-tree shown in Figure 19.85 causes a split into two leaves andthen a split of the parent node.
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley