Presentation is loading. Please wait.

Presentation is loading. Please wait.

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é,

Similar presentations


Presentation on theme: "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é,"— Presentation transcript:

1 CSC 213 Lecture 8: (2,4) Trees

2 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

3 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

4 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 11 24 2 6 815 30 27 32

5 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  1 11 24 2 6 815 30 27 32 11 2 6 815 11 24 24

6 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 11 24 2 6 815 30 27 32 12379 46 5 8

7 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) 11 24 2 6 815 30 27 32 30 27 32

8 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) 11 24 2 6 815 30 27 32 2 6 8

9 (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 10 15 24 2 81227 3218

10 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 2h12h1 0 entries 0 1 h1h1 h depth

11 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) 27 32 35 10 15 24 2 81218 10 15 24 2 81227 30 32 3518 27 32 35 v v

12 Insertion This insertion can cause an overflow! Node v became a 5-node This violates Node-Size property 27 32 35 15 24 1218 15 24 1227 30 32 3518 v v 12 345

13 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 15 24 12 27 30 32 35 18 v u v1v1 v2v2 v3v3 v4v4 v5v5 15 24 32 12 27 30 18 v'v' u v1v1 v2v2 v3v3 v4v4 v5v5 35 v"v"

14 Overflow and Split Splitting a node when the parent also overflows is similar, but takes a little more work Example: insert(29) 15 24 32 12 27 28 29 30 1835 27 28 30

15 Overflow and Split Example: insert(29) 15 24 29 32 12 27 28 1835 30

16 Overflow and Split Example: insert(29) 15 24 29 32 12 27 28 1835 30

17 Overflow and Split Example: insert(29) 15 24 12 27 28 1835 30 29 32

18 Overflow and Split Example: insert(29) 15 24 12 27 28 1835 30 29 32

19 Overflow and Split Example: insert(29) But this promotion could also cause an overflow… 15 24 12 27 28 1835 30 29 32

20 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?

21 Deletion Deletion from a leaf node is simple: Just remove the entry and the null child Example: delete(27) 27 32 35 10 15 24 2 81218 32 35 10 15 24 2 81218 27 32 35

22 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) 27 32 35 10 15 24 2 81218 32 35 10 15 27 2 81218 27 32 35

23 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) 9 14 2 5 710 15 9 14

24 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) 9 14 2 5 710 15 u v 9 14 10 14 u v'v'w 2 5 7 10 99 14

25 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) 4 9 6 82 10 u vw 2 u vw 4 9 9 4 6 8 4 8 6

26 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

27 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

28 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.


Download ppt "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é,"

Similar presentations


Ads by Google