1 / 65

Advanced Implementation of Tables

This chapter introduces various search trees that remain balanced in all situations, improving efficiency and maintaining balance in tree structures.

maygoins
Download Presentation

Advanced Implementation of Tables

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. Advanced Implementation of Tables Chapter 12

  2. Although we described the advantages of using the binary search tree, the efficiency of this implementation suffers when the tree loses its balance. • This chapter introduces various search trees, which remain balanced in all situations. Chapter 12 -- Advanced Implementations of Trees

  3. Balanced Search Trees • As we saw in a previous chapter, the efficiency of the binary search tree is related to the tree’s height. • The operations Retrieve, Insert, and Delete follow the path from the root of the tree to the node that contains the desired item (or to the parent of the item in the case of insert) • As we learned, the height of a BST is sensitive to the order in which you insert the items. Chapter 12 -- Advanced Implementations of Trees

  4. Consider a tree with the following nodes: • 10,20, 30, 40, 50, 60, and 70 • Depending upon the insertion order you could end up with: Chapter 12 -- Advanced Implementations of Trees

  5. 2-3 Trees Chapter 12 -- Advanced Implementations of Trees

  6. Nodes are similar to a BST node Chapter 12 -- Advanced Implementations of Trees

  7. A 2-3 Tree could look like this: Chapter 12 -- Advanced Implementations of Trees

  8. Traversal • Similar to a BST Chapter 12 -- Advanced Implementations of Trees

  9. Searching • Similar to a BST Chapter 12 -- Advanced Implementations of Trees

  10. Inserting • Quite different • Let’s start with the following trees • To the left is a BST, to the right is a 2-3 tree with the same elements Chapter 12 -- Advanced Implementations of Trees

  11. Insert 39, 38, 37, … , 32 (in that order) • Here is what the trees will look like, Chapter 12 -- Advanced Implementations of Trees

  12. So, let’s walk through the insertions and figure out how it works. • Insert 39 – Fairly straightforward • Now, what happens when you insert 38? Chapter 12 -- Advanced Implementations of Trees

  13. This is a little more difficult • You can’t insert a third value into a node, so you have to split it (and push up the middle value) • So, split 38,39,40 and push up 39 Chapter 12 -- Advanced Implementations of Trees

  14. Insert 37 – fairly straightforward Chapter 12 -- Advanced Implementations of Trees

  15. Insert 36 • This also creates a 3 value node, so you have to split it, and push up the middle, • Which creates a 3 value node, so you split it… Chapter 12 -- Advanced Implementations of Trees

  16. And push up the middle Chapter 12 -- Advanced Implementations of Trees

  17. Insert 35, 34, and 33 • Now Insert 32 Chapter 12 -- Advanced Implementations of Trees

  18. Insert 32 Chapter 12 -- Advanced Implementations of Trees

  19. The Algorithm • Locate the leaf node to insert into • If leaf node contains only one item before insert (two after) you are done. • If it contains two before (three after) you need to split the node and push the middle up Chapter 12 -- Advanced Implementations of Trees

  20. This process (pushing up) continues until a node is reached that has only one node in it (before push up) • Here is the illustration of splitting an internal node: Chapter 12 -- Advanced Implementations of Trees

  21. Sometimes you may have to split all the way up to the root node Chapter 12 -- Advanced Implementations of Trees

  22. Deletion • This is the inverse of the insertion • Start with the following: • Now delete 70 Chapter 12 -- Advanced Implementations of Trees

  23. Step 1 • Make the node a leaf – swap with the inorder successor Chapter 12 -- Advanced Implementations of Trees

  24. Step 2 – delete and fix from there. • Now delete 100 Chapter 12 -- Advanced Implementations of Trees

  25. Delete 100 • Now delete 80 Chapter 12 -- Advanced Implementations of Trees

  26. Delete 80 Chapter 12 -- Advanced Implementations of Trees

  27. The results of deleting 70, 100, and 80 from a BST and a 2-3 tree Chapter 12 -- Advanced Implementations of Trees

  28. Deletion Overview Chapter 12 -- Advanced Implementations of Trees

  29. Chapter 12 -- Advanced Implementations of Trees

  30. 2-3-4 trees Chapter 12 -- Advanced Implementations of Trees

  31. Insertion • Insert 60, 30, 10, and 20 into an empty tree • Now insert 50, 40, 70, 80, 15, 90, 100 Chapter 12 -- Advanced Implementations of Trees

  32. 50 and 40 are easy • Now insert 70 Chapter 12 -- Advanced Implementations of Trees

  33. Insert 70 • Split the 40-50-60 node (push middle up) • Then Insert 70 • Insert 80 and 15 (easy) • Now insert 90 Chapter 12 -- Advanced Implementations of Trees

  34. Insert 90 • Split 60-70-80 node and push middle up • Then insert 90 • Now Insert 100 • There is a trick here…. Chapter 12 -- Advanced Implementations of Trees

  35. Insert 100 • Split the 4 node at the root. • Then do the insertion as before • The thing to remember – • You split every 4 node on your way down for an insertion. Chapter 12 -- Advanced Implementations of Trees

  36. Splitting a 4 node • At the Root Chapter 12 -- Advanced Implementations of Trees

  37. Splitting a 4 node • Whose parent is a 2-node Chapter 12 -- Advanced Implementations of Trees

  38. Splitting a 4 node • Whose parent is a 3-node Chapter 12 -- Advanced Implementations of Trees

  39. Deleting from a 2-3-4 tree • The deletion algorithm has the same beginning as the deletion fro a 2-3 tree. • Locate the node with item I • Locate the inorder successor • Swap • If the leaf with I is a 3-node or a 4-node, just remove it. • Otherwise restructure • Combine nodes and then remove I Chapter 12 -- Advanced Implementations of Trees

  40. Red-Black Trees • Represent a 2-3-4 tree as a BST with colored links • 4 nodes are easy Chapter 12 -- Advanced Implementations of Trees

  41. 3 nodes have options Chapter 12 -- Advanced Implementations of Trees

  42. Example • 2-3-4 Tree • Red-Black Tree Chapter 12 -- Advanced Implementations of Trees

  43. Code for a Red-Black Tree Node enum Color {RED, BLACK}; class TreeNode{ private: TreeItemType Item; TreeNode *left, *right; Color leftColor, rightColor; friend class RedBlackTree; }; Chapter 12 -- Advanced Implementations of Trees

  44. Searching and traversing a Red-Black Tree • Since a red-black tree is a binary search tree you can traverse it by using the algorithms for a binary search tree (you simply ignore the color of the pointers) • Inserting • Since a red-black tree actually represents a 2-3-4 tree, you simply need to adjust the 2-3-4 insertion algorithms to accommodate the red-black representation. Chapter 12 -- Advanced Implementations of Trees

  45. Inserting (cont) • Recall that you split 4 nodes you encounter on insertion. Chapter 12 -- Advanced Implementations of Trees

  46. If you split a 4 node whose parent is a 2 node you push up Chapter 12 -- Advanced Implementations of Trees

  47. Splitting a 4-node whose parent is a 3-node is a little more complicated – there are a few cases • Case 1 Chapter 12 -- Advanced Implementations of Trees

  48. Case 2 Chapter 12 -- Advanced Implementations of Trees

  49. Case 3 Chapter 12 -- Advanced Implementations of Trees

  50. This chapter also introduces AVL Trees • Named for its inventors: Adel’son-Vel’skii and Landis Chapter 12 -- Advanced Implementations of Trees

More Related