1 / 66

Data Structures

Data Structures. Balanced Trees. CSCI 2720 Spring 2007. Outline. Balanced Search Trees 2-3 Trees 2-3-4 Trees Red-Black Trees. Why care about advanced implementations?. Same entries, different insertion sequence:.  Not good! Would like to keep tree balanced. 2-3 Trees. Features.

faris
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures Balanced Trees CSCI 2720 Spring 2007

  2. Outline • Balanced Search Trees • 2-3 Trees • 2-3-4 Trees • Red-Black Trees

  3. Why care about advanced implementations? Same entries, different insertion sequence:  Not good! Would like to keep tree balanced.

  4. 2-3 Trees Features • each internal node has either 2 or 3 children • all leaves are at the same level

  5. 2-3 Trees with Ordered Nodes 2-node 3-node • leaf node can be either a 2-node or a 3-node

  6. Example of 2-3 Tree

  7. Traversing a 2-3 Tree inorder(in ttTree: TwoThreeTree) if(ttTree’s root node r is a leaf) visit the data item(s) else if(r has two data items) { inorder(left subtree of ttTree’s root) visit the first data item inorder(middle subtree of ttTree’s root) visit the second data item inorder(right subtree of ttTree’s root) } else { inorder(left subtree of ttTree’s root) visit the data item inorder(right subtree of ttTree’s root) }

  8. Searching a 2-3 Tree retrieveItem(in ttTree: TwoThreeTree, in searchKey:KeyType, out treeItem:TreeItemType):boolean if(searchKey is in ttTree’s root node r) { treeItem = the data portion of r return true } else if(r is a leaf) return false else { return retrieveItem( appropriate subtree, searchKey, treeItem) }

  9. What did we gain? What is the time efficiency of searching for an item?

  10. Gain: Ease of Keeping the Tree Balanced Binary Search Tree both trees after inserting items 39, 38, ... 32 2-3 Tree

  11. Inserting Items Insert 39

  12. Inserting Items Insert 38 divide leaf and move middle value up to parent result insert in leaf

  13. Inserting Items Insert 37

  14. Inserting Items Insert 36 divide leaf and move middle value up to parent insert in leaf overcrowded node

  15. Inserting Items ... still inserting 36 divide overcrowded node, move middle value up to parent, attach children to smallest and largest result

  16. Inserting Items After Insertion of 35, 34, 33

  17. Inserting so far

  18. Inserting so far

  19. Inserting Items How do we insert 32?

  20. Inserting Items • creating a new root if necessary • tree grows at the root

  21. Inserting Items Final Result

  22. 70 80 Deleting Items Delete70

  23. Deleting Items Deleting70: swap 70 with inorder successor (80)

  24. Deleting Items Deleting70: ... get rid of 70

  25. Deleting Items Result

  26. Deleting Items Delete 100

  27. Deleting Items Deleting 100

  28. Deleting Items Result

  29. Deleting Items Delete 80

  30. Deleting Items Deleting 80 ...

  31. Deleting Items Deleting 80 ...

  32. Deleting Items Deleting 80 ...

  33. Deleting Items Final Result comparison with binary search tree

  34. Deletion Algorithm I Deleting item I: • Locate node n, which contains item I • If node n is not a leaf  swap I with inorder successor • deletion always begins at a leaf • If leaf node n contains another item, just delete item Ielsetry to redistribute nodes from siblings (see next slide) if not possible, merge node (see next slide)

  35. Deletion Algorithm II Redistribution • A sibling has 2 items: • redistribute itembetween siblings andparent Merging • No sibling has 2 items: • merge node • move item from parentto sibling

  36. Deletion Algorithm III Redistribution • Internal node n has no item left • redistribute Merging • Redistribution not possible: • merge node • move item from parentto sibling • adopt child of n If n's parent ends up without item, apply process recursively

  37. Deletion Algorithm IV If merging process reaches the root and root is without item  delete root

  38. all operations have time complexity of log n Operations of 2-3 Trees

  39. 2-3-4 Trees • similar to 2-3 trees • 4-nodes can have 3 items and 4 children 4-node

  40. 2-3-4 Tree Example

  41. 2-3-4 Tree: Insertion • Insertion procedure: • similar to insertion in 2-3 trees • items are inserted at the leafs • since a 4-node cannot take another item,4-nodes are split up during insertion process • Strategy • on the way from the root down to the leaf:split up all 4-nodes "on the way" •  insertion can be done in one pass(remember: in 2-3 trees, a reverse pass might be necessary)

  42. 2-3-4 Tree: Insertion Inserting 60, 30, 10, 20, 50, 40, 70, 80, 15, 90, 100

  43. 2-3-4 Tree: Insertion Inserting 60, 30, 10, 20 ... ...50, 40 ...

  44. 2-3-4 Tree: Insertion Inserting 50, 40 ... ...70, ...

  45. 2-3-4 Tree: Insertion Inserting 70 ... ...80, 15 ...

  46. 2-3-4 Tree: Insertion Inserting 80, 15 ... ...90 ...

  47. 2-3-4 Tree: Insertion Inserting 90 ... ...100 ...

  48. 2-3-4 Tree: Insertion Inserting 100 ...

  49. 2-3-4 Tree: Insertion Procedure Splitting 4-nodes during Insertion

  50. 2-3-4 Tree: Insertion Procedure Splitting a 4-node whose parent is a 2-node during insertion

More Related