BBM 202 - ALGORITHMS B ALANCED S EARCH T REES ‣ 2-3 search trees ‣ Red-black BSTs D EPT . OF C OMPUTER E NGINEERING ‣ B-trees ‣ Geometric applications of BSTs B ALANCED T REES Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University. Text B ALANCED S EARCH T REES worst-case cost average case ‣ 2-3 search trees (after N inserts) (after N random inserts) ordered key ‣ Red-black BSTs implementation iteration? interface search insert delete search hit insert delete ‣ B-trees ‣ Geometric applications of BSTs sequential search N N N N/2 N N/2 no equals() (unordered list) binary search compareTo() lg N N N lg N N/2 N/2 yes (ordered array) compareTo() BST N N N 1.39 lg N 1.39 lg N ? yes goal log N log N log N log N log N log N yes compareTo() ‣ Challenge. Guarantee performance. 3
2-3 tree 2-3 tree You can read it as 2 or 3 children tree Allow 1 or 2 keys per node. • 2-node: one key, two children. Allow 1 or 2 keys per node. • 3-node: two keys, three children. • 2-node: one key, two children. • 3-node: two keys, three children. Our Aim is Perfect balance. Every path from root to null link has same length. M M 3-node 2-node 3-node 2-node R R E J E J A C H L P S X A C H L P S X null link null link 5 6 2-3 tree 2-3 tree demo Allow 1 or 2 keys per node. Search. • 2-node: one key, two children. • Compare search key against keys in node. • 3-node: two keys, three children. • Find interval containing search key. • Follow associated link (recursively). Perfect balance. Every path from root to null link has same length. Symmetric order. Inorder traversal yields keys in ascending order. search for H H is less than M (go left) M H M R R E J E J smaller than E larger than J A C H L P S X A C H L P S X between E and J 7
2-3 tree demo 2-3 tree demo Search. Search. • Compare search key against keys in node. • Compare search key against keys in node. • Find interval containing search key. • Find interval containing search key. • Follow associated link (recursively). • Follow associated link (recursively). search for H search for H M M H is between E and J (go middle) R R E J E J H A C H L P S X A C H H L P S X found H (search hit) 2-3 tree demo 2-3 tree demo Search. Search. • Compare search key against keys in node. • Compare search key against keys in node. • Find interval containing search key. • Find interval containing search key. • Follow associated link (recursively). • Follow associated link (recursively). search for B search for B B is less than M (go left) B M M B is less than E (go left) R R E J E J B A C H L P S X A C H L P S X
2-3 tree demo 2-3 tree demo Search. Search. • Compare search key against keys in node. • Compare search key against keys in node. • Find interval containing search key. • Find interval containing search key. • Follow associated link (recursively). • Follow associated link (recursively). search for B search for B M M R R E J E J B is between A and C (go middle) B A C H L P S X A C H L P S X B link is null (search miss) Insert Operation 2-3 tree demo Insert into a 2-node at bottom. • Search for key, as usual. ‣ Problem with Binary Search Tree: when the tree grows from leaves, it • Replace 2-node with 3-node. is possible to always insert to same branch. (worst-case) ‣ Instead of growing the tree from bottom, try to grow upwards. ‣ If there is space in a leaf, simply insert it K is less than M insert K ‣ Otherwise push nodes from bottom to top, if done recursively the tree will be (go left) balanced as it grows (increasing the height by introducing a new root) K M ‣ If we keep on inserting to same branch; R E J BST: 2 or 3 Tree: 9 8 8 9 A C H L P S X 6,7 7 6 15
2-3 tree demo 2-3 tree demo Insert into a 2-node at bottom. Insert into a 2-node at bottom. • Search for key, as usual. • Search for key, as usual. • Replace 2-node with 3-node. • Replace 2-node with 3-node. insert K insert K M M K is greater than J (go right) R R K E J E J A C H L P S X A C H K L P S X search ends here 2-3 tree demo 2-3 tree demo Insert into a 2-node at bottom. Insert into a 2-node at bottom. • Search for key, as usual. • Search for key, as usual. • Replace 2-node with 3-node. • Replace 2-node with 3-node. insert K insert K M M M R R R E J E J E J A C H K L P S X A C A C H H K LL P P S X S X replace 2-node with 3-node containing K
2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. Z is greater than M insert Z insert Z (go right) M Z M Z is greater than R (go right) R R Z E J E J A C H K L P S X A C H K L P S X 2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. insert Z insert Z M M R R E J E J A C H K L P S X Z A C H K L P S X Z search ends here replace 3-node with temporary 4-node containing Z
2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. insert Z insert Z M M R R E J E J A C H K L P S X Z A C H K L P S X Z split 4-node into two 2-nodes (pass middle key to parent) 2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. insert Z insert Z M M R X R X E J E J A C H K L P S Z A C H K L P S Z
2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. • Repeat up the tree, as necessary. • Repeat up the tree, as necessary. • If you reach the root and it's a 4-node, split it into three 2-nodes. • If you reach the root and it's a 4-node, split it into three 2-nodes. insert L insert L E R E R A C H P L S X A C H L P S X convert 3-node into 4-node 2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. • Repeat up the tree, as necessary. • Repeat up the tree, as necessary. • If you reach the root and it's a 4-node, split it into three 2-nodes. • If you reach the root and it's a 4-node, split it into three 2-nodes. insert L insert L E R E L R A C H L P S X A C H P S X split 4-node (move L to parent)
2-3 tree demo 2-3 tree demo Insert into a 3-node at bottom. Insert into a 3-node at bottom. • Add new key to 3-node to create temporary 4-node. • Add new key to 3-node to create temporary 4-node. • Move middle key in 4-node into parent. • Move middle key in 4-node into parent. • Repeat up the tree, as necessary. • Repeat up the tree, as necessary. • If you reach the root and it's a 4-node, split it into three 2-nodes. • If you reach the root and it's a 4-node, split it into three 2-nodes. height of tree increases by 1 insert L insert L L split 4-node (move L to parent) E L R E R A C H P S X A C H P S X 2-3 tree demo Search in a 2-3 tree • Compare search key against keys in node. Insert into a 3-node at bottom. • Find interval containing search key. • Add new key to 3-node to create temporary 4-node. • Follow associated link (recursively). • Move middle key in 4-node into parent. • Repeat up the tree, as necessary. • If you reach the root and it's a 4-node, split it into three 2-nodes. successful search for H unsuccessful search for B H is less than M so B is less than M so look to the left look to the left M M R R E J E J A C H L P S X A C H L P S X insert L L B is less than E H is between E and L so M so look to the left M look in the middle R R E J E J P S X P S X A C H L A C H L E R M M R R E J E J P S X P S X A C H L A C H L A C H P S X found H so return value ( search hit ) B is between A and C so look in the middle link is null so B is not in the tree ( search miss ) 36
Recommend
More recommend