60 likes | 251 Views
2-3 Trees. Goals Design decisions Design Insertion. 2-3 Trees: Goals. Goal – Almost Balanced Tree with leaves sorted Why? Balanced -> O(log n) speed Sorted speeds INSERT/DELETE often required by the application, anyway. 2-3 Trees: Design.
E N D
2-3 Trees Goals Design decisions Design Insertion CS 303 – 2-3 Trees Lecture 10
2-3 Trees: Goals • Goal – Almost Balanced Tree with leaves sorted • Why? • Balanced -> O(log n) speed • Sorted • speeds INSERT/DELETE • often required by the application, anyway CS 303 – 2-3 Trees Lecture 10
2-3 Trees: Design • Values stored at leaves (not at internal nodes!) • Interior nodes have either 2 or 3 children (never only 1) • Restructure as needed to keep all paths from root to leaf the same length (or nearly so) CS 303 – 2-3 Trees Lecture 10
2-3 Tree: Definition < a a > a, < b b >b CS 303 – 2-3 Trees Lecture 10
2-3 Trees: Insertion • To insert x • Search for x • At lowest interior level, there are two cases: • 1) the easy case a b a x b plus two other cases, x < a < b, and a < b < x CS 303 – 2-3 Trees Lecture 10
2-3 Trees: Insertion... And now, we need to insert the new node into the Parent, where there are 2 cases... • 2) the hard case Parent a b c a x b c plus three other cases: x < a < b < c, a < b < x < c, and a < b < c < x Do an example, e.g. insert 5,2,7,0,3,4,6,1,8,9... CS 303 – 2-3 Trees Lecture 10