650 likes | 671 Views
This chapter introduces various search trees that remain balanced in all situations, improving efficiency and maintaining balance in tree structures.
E N D
Advanced Implementation of Tables Chapter 12
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
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
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
2-3 Trees Chapter 12 -- Advanced Implementations of Trees
Nodes are similar to a BST node Chapter 12 -- Advanced Implementations of Trees
A 2-3 Tree could look like this: Chapter 12 -- Advanced Implementations of Trees
Traversal • Similar to a BST Chapter 12 -- Advanced Implementations of Trees
Searching • Similar to a BST Chapter 12 -- Advanced Implementations of Trees
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
Insert 39, 38, 37, … , 32 (in that order) • Here is what the trees will look like, Chapter 12 -- Advanced Implementations of Trees
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
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
Insert 37 – fairly straightforward Chapter 12 -- Advanced Implementations of Trees
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
And push up the middle Chapter 12 -- Advanced Implementations of Trees
Insert 35, 34, and 33 • Now Insert 32 Chapter 12 -- Advanced Implementations of Trees
Insert 32 Chapter 12 -- Advanced Implementations of Trees
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
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
Sometimes you may have to split all the way up to the root node Chapter 12 -- Advanced Implementations of Trees
Deletion • This is the inverse of the insertion • Start with the following: • Now delete 70 Chapter 12 -- Advanced Implementations of Trees
Step 1 • Make the node a leaf – swap with the inorder successor Chapter 12 -- Advanced Implementations of Trees
Step 2 – delete and fix from there. • Now delete 100 Chapter 12 -- Advanced Implementations of Trees
Delete 100 • Now delete 80 Chapter 12 -- Advanced Implementations of Trees
Delete 80 Chapter 12 -- Advanced Implementations of Trees
The results of deleting 70, 100, and 80 from a BST and a 2-3 tree Chapter 12 -- Advanced Implementations of Trees
Deletion Overview Chapter 12 -- Advanced Implementations of Trees
2-3-4 trees Chapter 12 -- Advanced Implementations of Trees
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
50 and 40 are easy • Now insert 70 Chapter 12 -- Advanced Implementations of Trees
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
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
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
Splitting a 4 node • At the Root Chapter 12 -- Advanced Implementations of Trees
Splitting a 4 node • Whose parent is a 2-node Chapter 12 -- Advanced Implementations of Trees
Splitting a 4 node • Whose parent is a 3-node Chapter 12 -- Advanced Implementations of Trees
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
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
3 nodes have options Chapter 12 -- Advanced Implementations of Trees
Example • 2-3-4 Tree • Red-Black Tree Chapter 12 -- Advanced Implementations of Trees
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
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
Inserting (cont) • Recall that you split 4 nodes you encounter on insertion. Chapter 12 -- Advanced Implementations of Trees
If you split a 4 node whose parent is a 2 node you push up Chapter 12 -- Advanced Implementations of Trees
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
Case 2 Chapter 12 -- Advanced Implementations of Trees
Case 3 Chapter 12 -- Advanced Implementations of Trees
This chapter also introduces AVL Trees • Named for its inventors: Adel’son-Vel’skii and Landis Chapter 12 -- Advanced Implementations of Trees