CSC 213 Lecture 8: (2,4) Trees
Review of Last Lecture Binary Search Tree – plain and tall No balancing, no splaying, no speed AVL Tree – liberté, égalité, fraternité Traverse up the tree & check for balance When a node’s children’s heights differ by 2 or more Each restructure uses node, taller child, tallest grandchild
Review of Last Lecture Splay Tree – Greed is Good Splay a node until it becomes the root Each splay operation involves the node, its parent, and its grandparent Only exception is if a node’s parent is the root
Multi-Way Search Tree (§ 9.4.1) A multi-way search tree is an ordered tree Each internal node in a multi-way search tree: Has at least 2 children (can have more!) Has 1 fewer entries than it has children Keeps its entries and children in sorted order
Multi-Way Search Tree (§ 9.4.1) For a node with children v 1 v 2 … v d storing entries with keys k 1 k 2 … k d 1 Keys in the subtree of v 1 are smaller than k 1 Keys in the subtree of v i are between k i 1 and k i Keys in the subtree of v d are greater than k d
Multi-Way Inorder Traversal Can traverse multi-way search trees in-order Alternate visiting child i then entry e i Like with a BST, inorder traversal visits keys in increasing order
Multi-Way Searching Similar to search in a BST At each with children v 1 v 2 … v d and entries e 1 e 2 … e d 1 for i = 1 to d – 1 if k k i : return result of search in child v i if k = k i : return e i Return result of search in child v d Throw exception if we reach an external node Example: find(30)
Multi-Way Searching Similar to search in a BST At each with children v 1 v 2 … v d and entries e 1 e 2 … e d 1 for i = 1 to d – 1 if k k i : return result of search in child v i if k = k i : return e i Return result of search in child v d Throw exception if we reach an external node Example: find(1)
(2,4) Trees (§ 9.4.2) (2,4) tree is multi-way search tree with 2 properties: Node-Size Property: internal nodes have at most 4 children Depth Property: all the external nodes have the same depth Nodes can be a 2-node, 3-node or 4-node
Height of a (2,4) Tree (2,4) tree of size n has height O(log n) In the largest tree, each node has 2 children But external nodes must be at same depth This property automatically balances the tree! This worst case scenario: a balanced binary tree! 1 2 2h12h1 0 entries 0 1 h1h1 h depth
Insertion Begin with a search for the key k If k does not exist within the tree, add entry to last internal node we searched in… …thereby preserving the depth property Example: insert(30) v v
Insertion This insertion can cause an overflow! Node v became a 5-node This violates Node-Size property v v
Overflow and Split Handle overflow using split: Split node v into 2 nodes ( v' and v" ) A 3-node ( v’ ) with keys e 1 e 2 and children v 1 v 2 v 3 A 2-node ( v’’ ) with key e 4 and children v 4 v 5 Promote e 3 to parent node u (this may create new root) This may cause parent node ( u ) to overflow v u v1v1 v2v2 v3v3 v4v4 v5v v'v' u v1v1 v2v2 v3v3 v4v4 v5v5 35 v"v"
Overflow and Split Splitting a node when the parent also overflows is similar, but takes a little more work Example: insert(29)
Overflow and Split Example: insert(29)
Overflow and Split Example: insert(29)
Overflow and Split Example: insert(29)
Overflow and Split Example: insert(29)
Overflow and Split Example: insert(29) But this promotion could also cause an overflow…
Analysis of Insertion Algorithm insert(k, o) 1.Search for key k to find insert node v 2.Add the new entry (k, o) at node v 3. while overflow(v) if isRoot(v) create a new empty root above v split(v) v parent(v) What is big-Oh time for: Step 1? Step 2? Checking overflow(v)? Calling isRoot(v)? Calling split(v)? Step 3 ? insert(k,o) big-Oh time?
Deletion Deletion from a leaf node is simple: Just remove the entry and the null child Example: delete(27)
Deletion If entry is not at a leaf node, replace it with inorder successor Go to right child and then go as far left as possible Example: delete(24)
Underflow and Fusion Deleting an entry may cause underflow Cause a node to be a 1-node (has one child, but no entries) This has two solutions, the solution depends on the situation Example: remove(15)
Fusion Case 1: v has adjacent siblings that is a 2-node Make new node ( v’ ) by merging node ( v ) with adjacent sibling ( w ) Steal entry from parent ( u ) that was between v & w This may propagate underflow to the parent! Example: remove(15) u v u v'v'w
6 8 Transfer Case 2: v has adjacent sibling w that is 3- or 4-node Move entry in u that is between w & v into v Promote entry in w that is closest to v into u Make child of w that is next to v be a child of v No more underflows can exist Example: remove(10) u vw 2 u vw
Analysis of Deletion What is big-Oh time needed for deletion? Height of tree = O(log n) Time to find entry to delete Time needed for each fusion Maximum possible number of fusions Time needed for transfer Maximum possible number of transfers Total time needed for deletion
Your Turn Insert 1, 2, 3, 4, 5, 6, 7, 8 into a (2,4) tree Delete 3, 6, 5, 2, 8, 4, 1, 7 from your tree
Daily Quiz Write the Node class for a (2,4)-tree. What public methods should you define (does it make sense to include all getters and setters)? You will want to consider using other data structures in your Node.