Successfully reported this slideshow.
Upcoming SlideShare
×

# 358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11

1,411 views

Published on

Reema Thareja Data Structure on efficient binary tree

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### 358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11

1. 1. © Oxford University Press 2011. All rights reserved. Data Structures Using C Reema Thareja, Assistant Professor, Institute of Information Technology and Management, New Delhi
3. 3. © Oxford University Press 2011. All rights reserved. BINARY SEARCH TREES • A binary search tree, also known as an ordered binary tree is a variant of binary tree in which the nodes are arranged in order. In a binary search tree, all the nodes in the left sub-tree have a value less than that of the root node. Correspondingly, all the nodes in the right sub-tree have a value either equal to or greater than the root node. The same rule is applicable to every sub-tree in the tree. • The average running time of a search operation is O(log2n) as at every step, we eliminate half of the sub-tree from the search process. Due to its efficiency in searching elements, binary search trees are widely used in dictionary problems where the code always inserts and searches the elements that are indexed by some key value. 3 9 4 5 2 7 1 8 9 2 9 4 0 5 4 5 9 2 8 3 6 2 1 1 0 1 9 6 5 6
4. 4. © Oxford University Press 2011. All rights reserved. Create a binary search trees using the following data values 45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67 45 45 39 45 39 56 45 39 56 12 45 39 56 12 34 4 5 39 56 12 34 78 4 5 39 56 12 34 78 32 45 39 56 12 34 78 10 32 45 39 56 12 34 78 32 10 89 54 45 39 56 12 34 78 32 10 89 54 67
5. 5. © Oxford University Press 2011. All rights reserved. Searching a Value in BST • The search function is used to find whether a given value is present in the tree or not. The searching process begins at the root node. The function first checks if the binary search tree is empty. If it is, then it means that the value we are searching for is not present in the tree. So, the search algorithm terminates by displaying an appropriate message. • However, if there are nodes in the tree then the search function checks to see if the key value of the current node is equal to the value to be searched. If not, it checks if the value to be searched for is less than the value of the node, in which case it should be recursively called on the left child node. In case the value is greater than the value of the node, it should be recursively called on the right child node. Algorithm to search in a BST Step 1: IF TREE->DATA = VAL OR TREE = NULL, then Return TREE ELSE IF VAL < TREE->DATA Return searchElelement(TREE->LEFT, VAL) ELSE Return searchElelement(TREE->RIGHT, VAL) [END OF IF] [END OF IF] Step 2: End
6. 6. © Oxford University Press 2011. All rights reserved. Insertion in a BST Algorithm to insert a value in the binary search tree- Step 1: IF TREE = NULL, then Allocate memory for TREE SET TREE->DATA = VAL SET TREE->LEFT = TREE ->RIGHT = NULL ELSE IF VAL < TREE->DATA Insert(TREE->LEFT, VAL) ELSE Insert(TREE->RIGHT, VAL) [END OF IF] [END OF IF] Step 2: End
7. 7. © Oxford University Press 2011. All rights reserved. Deletion from a BST • The delete function deletes a node from the binary search tree. However, utmost care should be taken that the properties of the binary search tree does not get violated and nodes are not lost in the process. The deletion of a node can be done in any of the three cases. • Case 1: Deleting a node that has no children. 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 5 4 5 5
8. 8. © Oxford University Press 2011. All rights reserved. • Case 2: Deleting a node with one child (either left or right). To handle the deletion, the node’s child is set to be the child of the node’s parent. In other words, replace the node with its child. Now, if the node was the left child of its parent, the node’s child becomes the left child of the node’s parent. Correspondingly, if the node was the right child of its parent, the node’s child becomes the right child of the node’s parent. 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 7 8 5 4 5 5 4 5 3 9 5 6 7 8 5 5 Case 3: Deleting a node with two children. To handle this case of deletion, replace the node’s value with its in-order predecessor (right most child of the left sub-tree) or in- order successor (leftmost child of the right sub-tree). The in-order predecessor or the successor can then be deleted using any of the above cases.
9. 9. © Oxford University Press 2011. All rights reserved. 45 39 56 7854 55 80 45 39 56 7854 55 80 45 39 55 7854 55 80 45 39 55 7854 80 Algorithm to delete a value in the binary search tree Step 1: IF TREE = NULL, then Write “VAL not found in the tree” ELSE IF VAL < TREE->DATA Delete(TREE->LEFT, VAL) ELSE IF VAL > TREE->DATA Delete(TREE->RIGHT, VAL) ELSE IF TREE->LEFT AND TREE->RIGHT SET TEMP = findLargestNode(TREE->LEFT) SET TREE->DATA = TEMP->DATA Delete(TREE->LEFT, TEMP->DATA) ELSE SET TEMP = TREE IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL SET TREE = NULL ELSE IF TREE->LEFT != NULL SET TREE = TREE->LEFT ELSE SET TREE = TREE->RIGHT FREE TEMP Step 2: End
10. 10. © Oxford University Press 2011. All rights reserved. Determining the Height of the Tree • In order to determine the height of the binary search tree, we will calculate the height of the left sub-tree and the right sub- tree. Whichever height is greater, 1 is added to it. • Since height of right sub-tree is greater than the height of the left sub-tree, the height of the tree = height (right sub-tree) + 1= 2 + 1 = 3 45 39 78 54 80 Algorithm to determine height of the binary search tree- Height (TREE) Step 1: IF TREE = NULL, then Return 0 ELSE SET LeftHeight = Height(TREE->LEFT) SET RightHeight = Height(TREE->RIGHT) IF LeftHeight > RightHeight Return LeftHeight + 1 ELSE Return RightHeight + 1 [END OF IF] [END OF IF] Step 2: End
11. 11. © Oxford University Press 2011. All rights reserved. Determining the Number of Nodes • Determining the number of nodes in a binary search tree is similar to determining its height. To calculate the total number of elements/ nodes in the tree, we will count the number of nodes in the left sub-tree and the right sub- tree. • Number of nodes = totalNodes(left sub-tree) + totalNodes(right sub-tree) + 1 45 39 78 7954 55 80 Total nodes of left sub-tree = 1 Total nodes of left sub-tree = 5 Total nodes of tree = Total nodes of left sub-tree + Total nodes of right sub-tree + 1 = 1 + 5 + 1 = 7 Algorithm to determine number of nodes in the binary search tree- totalNodes (TREE) Step 1: IF TREE = NULL, then Return 0 ELSE Return totalNodes(TREE->LEFT) + totalNodes(TREE->RIGHT) + 1 [END OF IF] Step 2: End
12. 12. © Oxford University Press 2011. All rights reserved. Mirror Image • Mirror image of a binary search tree is obtained by interchanging the left sub-tree with the right sub-tree at every node of the tree. For example, given the tree T, the mirror image of T can be obtained as T’. 4 5 3 9 7 8 7 9 5 4 4 5 7 8 3 9 T T’ 5 4 7 9 Algorithm to obtain the mirror image of the binary search tree- Step 1: IF TREE != NULL , then MirrorImage(TREE->LEFT) MirrorImage(TREE->RIGHT) SET TEMP = TREE->LEFT SET TREE->LEFT = TREE->RIGHT SET TREE_>RIGHT = TEMP [END OF IF] Step 2: End
13. 13. © Oxford University Press 2011. All rights reserved. Removing the Tree • To delete/remove the entire binary search tree from the memory, we will first delete the elements/nodes in the left sub-tree and then delete the right sub-tree. Algorithm to remove the binary search tree- Step 1: IF TREE != NULL , then deleteTree (TREE->LEFT) deleteTree (TREE->RIGHT) Free (TREE) [END OF IF] Step 2: End Finding the Smallest Node The very basic property of the binary search tree states that the smaller value will occur in the left sub-tree. If the left sub-tree is NULL, then the value of root node will be smallest as compared with nodes in the right sub-tree. So, to find the node with the smallest value, we will find the value of the leftmost node of the left sub-tree. However, if the left sub-tree is empty then we will find the value of the root node. Algorithm to find the smallest node of the binary search tree- Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then Return TREE ELSE Return findSmallestElement(TREE->LEFT) [END OF IF] Step 2: End
16. 16. © Oxford University Press 2011. All rights reserved. AVL TREES • AVL tree is a self-balancing binary search tree in which the heights of the two sub- trees of a node may differ by at most one. Because of the property, AVL tree is also known as a height-balanced tree. • The key advantage of using an AVL tree is that it takes O(logn) time to perform search, insert and delete operations in average case as well as worst case (because the height of the tree is limited to O(logn).. • The structure of an AVL tree is same as that of a binary search tree but with a little difference. In its structure, it stores an additional variable called the BalanceFactor. • The balance factor of a node is calculated by subtracting the height of its right sub- tree from the height of the left sub-tree. A binary search tree in which every node has a balance factor of -1, 0 or 1 is said to be height balanced. A node with any other balance factor is considered to be unbalanced and requires rebalancing the tree. Balance factor = Height (left sub-tree) – Height (right sub-tree) • If the balance factor of a node is 1, then it means that the left sub-tree of the tree is one level higher than that of the right sub-tree. Such a tree is therefore called Left- heavy tree. • If the balance factor of a node is 0, then it means that the height of the left sub-tree (longest path in the left sub-tree) is equal to the height of the right sub-tree. • If the balance factor of a node is -1, then it means that the left sub-tree of the tree is one level lower than that of the right sub-tree. Such a tree is therefore called Right- heavy tree.
17. 17. © Oxford University Press 2011. All rights reserved. 4 5 6 3 3 6 2 7 1 8 3 9 5 4 7 2 0 0 0 1 0 1 0 1 Left heavy AVL tree 4 5 6 3 3 6 2 7 7 0 3 9 5 4 7 2 1 0 -1 -1 0 0 0 0 Right heavy AVL tree 45 63 27 39 54 72 0 0 0 0 0 0 0 36 Balanced AVL tree Search Operation Searching in an AVL tree is performed exactly the same way as it is performed in a binary search tree. Because of the height-balancing of the tree, the search operation takes O(log n) time to complete. Since the operation does not modify the structure of the tree, no special provisions needs to be taken.
18. 18. © Oxford University Press 2011. All rights reserved. Insertion in an AVL Tree • Since AVL tree is also a variant binary search tree, insertion is also done in the same way as it is done in case of a binary search tree. Like in binary search tree, the new node is always inserted as the leaf node. But the step of insertion is usually followed by an additional step of rotation. • Rotation is done to restore the balance of the tree. However, if insertion of the new node does not disturb the balance factor, that is, if the balance factor of every node is still -1, 0 or 1, then rotations are not needed. • During insertion, the new node is inserted as the leaf node, so it will always have balance factor equal to zero. The only nodes whose balance factors will change are those which lie on the path between the root of the tree and the newly inserted node. The possible changes which may take place in any node on the path are as follows: • Initially the node was either left or right heavy and after insertion has become balanced. • Initially the node was balanced and after insertion has become either left or right heavy. • Initially the node was heavy (either left or right) and the new node has been inserted in the heavy sub-tree thereby creating an unbalanced sub-tree. Such a node is said to be a critical node.
19. 19. © Oxford University Press 2011. All rights reserved. Rotations to Balance the AVL Tree After Insertion of a New Node • To perform rotation, our first work is to find the critical node. Critical node is the nearest ancestor node on the path from the root to the inserted node whose balance factor is neither -1, 0 nor 1. • The second task in rebalancing the tree is to determine which type of rotation has to be done. There are four types of rebalancing rotations and application of these rotations depends on the position of the inserted node with reference to the critical node. • LL rotation: the new node is inserted in the left sub-tree of the left sub-tree of the critical node • RR rotation: the new node is inserted in the right sub-tree of the right sub-tree of the critical node. • LR rotation: the new node is inserted in the right sub-tree of the left sub-tree of the critical node • RL rotation: the new node is inserted in the left sub-tree of the right sub-tree of the critical node
20. 20. © Oxford University Press 2011. All rights reserved. Example: Consider the AVL tree given below and insert 9 into it. 4 5 6 3 3 6 2 7 3 9 5 4 7 2 0 0 0 1 1 0 1 1 8 0 4 5 6 3 3 6 2 7 3 9 5 4 7 2 0 0 0 2 2 0 2 1 8 9 1 0 4 5 6 3 2 7 1 8 3 6 5 4 7 2 0 0 0 1 1 0 0 9 0 3 9 0 The tree is balanced using LL rotation Consider the AVL tree given below and insert 91 into it. 4 5 6 3 3 6 2 7 3 9 5 4 7 2 -1 0 -1 -1 0 0 0 8 9 0 4 5 6 3 3 6 2 7 3 9 5 4 7 2 -2 -2 -2 0 0 0 8 9 -1 9 1 0 4 5 7 2 3 6 2 7 3 9 6 3 8 9 -1 -1 0 -1 0 0 0 90 3 9 0 The tree is balanced using RR rotation
21. 21. © Oxford University Press 2011. All rights reserved. Consider the AVL tree given below and insert 37 into it. 45 6336 27 39 0 1 0 0 0 45 6336 27 39 0 2 0 1 -1 370 39 4536 27 37 63 0 -1 0 0 0 0 The tree is balanced using LR rotation Consider the AVL tree given below and insert 37 into it. 45 6336 72 0 0 0 -1 0 45 6336 54 72 0 1 1 -2 0 0 51 54 6345 72 -1 0 0 36 51 0 0 0 The tree is balanced using RL rotation
22. 22. © Oxford University Press 2011. All rights reserved. Deletion of a node from an AVL Tree • Deletion of a node in the AVL tree is similar to that of binary search trees. But it goes one step ahead. Deletion may disturb the AVLness of the tree, so to re- balance the AVL tree we need to perform rotations. There are two classes of rotation that can be performed on an AVL tree after deleting a given node. These rotations are- R rotation and L rotation. • If the node to be deleted is present in the left sub-tree of the critical node, then L rotation is applied else if X is on the right sub-tree, R rotation is performed. • Further there are three categories of L and R rotations. The variations of L rotation are- L-1, L0 and L1 rotation. Correspondingly for R rotation, there are R0, R-1 and R1 rotations. • R0 Rotation • Let B be the root of the left or right sub-tree of A (critical node). R0 rotation is applied if the balance factor of B is 0. • Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 7 2 -1 1 1 -1 0 1 8 0 4 0 0 0 4 5 6 3 3 6 2 7 3 9 0 2 1 -1 0 1 8 4 0 0 0 3 6 4 5 2 7 1 8 3 9 1 -1 0 -1 1 4 0 6 3 0 0
23. 23. © Oxford University Press 2011. All rights reserved. R1Rotation Let B be the root of the left or right sub-tree of the critical node. R1 rotation is applied if the balance factor of B is 1. Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 7 2 1 1 1 0 1 1 8 0 0 4 5 6 3 3 6 2 7 3 9 0 2 1 0 1 1 8 0 3 6 4 5 2 7 1 8 3 9 0 0 0 0 1 6 3 0 R-1Rotation Let B be the root of the left or right sub-tree of the (critical node. R-1 rotation is applied if the balance factor of B is -1. Consider the AVL tree given below and delete 72 from it. 4 5 6 3 3 6 2 7 3 9 -1 1 0 0 -1 3 7 4 1 0 0 7 2 0 4 5 6 3 3 6 2 7 3 9 0 2 0 0 -1 3 7 4 1 0 0 3 9 4 5 3 6 2 7 3 7 4 1 1 1 1 0 1 0 6 3 0
24. 24. © Oxford University Press 2011. All rights reserved. M-WAY SEARCH TREES In such a tree M is called the degree of the tree. Note in a binary search tree M = 2, so it has one value and 2 sub-trees. In other words, every internal node of an M-way search tree consists of pointers to M sub-trees and contains M – 1 keys, where M > 2. P0 K0 P1 K1 P2 K2 …… … Pn-1 Kn-1 Pn In the structure, P0, P1, P2,…, Pn are pointers to the node’s sub-trees and K0, K1, K2,…, Kn-1 are the key values of the node. All the key values are stored in ascending order. That is, Ki < Ki+1 for 0 ≤ i ≤ n-2. In an M-way search tree, it is not compulsory that every node ha s exactly (M-1) values and have exactly M sub-trees. Rather, the node can have anywhere from 1 to (M-1) values, and the number of sub-trees may vary from 0 (for a leaf node) to 1 + i, where i is the number of key values in the node. M is thus a fixed upper limit that defines how much key values can be stored in the node.
25. 25. © Oxford University Press 2011. All rights reserved. 18 45 9 11 2 7 36 54 63 29 30 72 81 Using the above 3-way search tree given below, let us make out some basic properties of an M-way search tree. Key values in the sub-tree pointed by P0 are less than the key value K0. Similarly, all key values in the sub-tree pointed by P1 are less than K1, so on and so forth. Thus, the generalized rule is that all key values in the sub-tree pointed by Pi are less than Ki, where 0 ≤ i ≤ n-1. Kkey values in the sub-tree pointed by P1 are greater than the key value K0. Similarly, all key values in the sub-tree pointed by P2 are greater than K1, so on and so forth. Thus, the generalized rule is that all key values in the sub-tree pointed by Pi are greater than Ki-1, where 0 ≤ i ≤ n-1. In an M-way search tree, every sub-tree is also an M-way search tree and follows the same rules.
26. 26. © Oxford University Press 2011. All rights reserved. B TREES • A B-tree is a specialized m- way tree that is widely used for disk access. A B tree of order m can have maximum m-1 keys and m pointers to its sub-trees. A B-tree may contain a large number of key values and pointers to sub-trees. Storing a large number of keys in a single node keeps the height of the tree relatively small. • A B-tree is designed to store sorted data and allows search, insert, and delete operations to be performed in logarithmic amortized time. A B-tree of order m (the maximum number of children that each node can have) is a tree with all the properties of an m-way search tree and in addition has the following properties: • Every node in the B-tree has at most (maximum) m children. • Every node in the B-tree except the root node and leaf nodes have at least (minimum) m⁄2 children. This condition helps to keep the tree bushy so that the path from the root node to the leaf is very short even in a tree that stores a lot of data. • The root node has at least two children if it is not a terminal (leaf) node. • All leaf nodes are at the same level. • An internal node in the B tree can have n number of children, where 0 ≤n ≤ m. it is not necessary that every node has the same number of children, but the only restriction is that the node should have at least m/2 children.
27. 27. © Oxford University Press 2011. All rights reserved. O 45 O O 29 O 32 O O 49 O 63 O O 18 O 27 O O 30 O 31 O O 36 O 39 O O 46 O 47 O O 54 O 59 O 61 O O 67 O 72 O Insert Operation In a B tree all insertions are done at the leaf node level. A new value is inserted in the B tree using the algorithm given below. Search the B tree to find the leaf node where the new key value should be inserted. If the leaf node is not full, that is it contains less than m-1 key values then insert the new element in the node, keeping the node's elements ordered. if the leaf node is full, that is the leaf node already contain m-1 key values then insert the new value in order into the existing set of keys split the node at its median into two nodes. note that the split nodes are half full. push the median element up to its parent’s node. If the parent’s node is already full, then split the parent node by following the same steps.
28. 28. © Oxford University Press 2011. All rights reserved. O 18 O 45 O 72 O Example: Look at the B tree of order 5 given below and insert 8, 9, 39 into it. O 7 O 11 O O 21 O 27 O 3 6 O 42 O O 54 O 63 O O 81 O 89 O 90 O O 18 O 45 O 72 O O 7 O 8 O 1 1 O O 21 O 27 O 36 O 42 O O 54 O 63 O O 81 O 89 O 90 O O 18 O 45 O 72 O O 7 O 8 O 9 O 1 1 O O 2 1 O 2 7 O 3 6 O 4 2 O O 5 4 O 6 3 O O 81 O 89 O 90 O O 18 O 36 O 45 O 72 O O 7 O 8 O 9 O 1 1 O O 21 O 27 O O 39 O 42 O O 54 O 63 O O 81 O 89 O 90 O
29. 29. © Oxford University Press 2011. All rights reserved. Deletion • Like insertion, deletion is also done from the leaf nodes. There are two cases of deletion. First, a leaf node has to be deleted. Second, an internal node has to be deleted. Let us first see the steps involved in deleting a leaf node. • Locate the leaf node which has to be deleted • If the leaf node contains more than minimum number of key values (more than m/2 elements), then delete the value. • Else, if the leaf node does not contain even m/2 elements then, fill the node by taking an element either from the left or from the right sibling – If the left sibling has more than the minimum number of key values (elements), push its largest key into its parent’s node and pull down the intervening element from the parent node to the leaf node where the key is deleted. – Else if the right sibling has more than the minimum number of key values (elements), push its smallest key into its parent node and pull down the intervening element from the parent node to the leaf node where the key is deleted. • Else, if both left and right siblings contain only minimum number of elements, then create a new leaf node by combining the two leaf nodes and the intervening element of the parent node (ensuring that the number of elements do not exceed the maximum number of elements a node can have, that is, m). If pulling the intervening element from the parent node leaves it with less than minimum number of keys in the node, then propagate the process upwards thereby reducing the height of the B tree. • To delete an internal node, promote the successor or predecessor of the key to be deleted to occupy the position of the deleted key. This predecessor or successor will always be in the leaf node. So further the processing will be done as if a value from the leaf node has been deleted.
30. 30. © Oxford University Press 2011. All rights reserved. Consider the B tree of order 5 given below and delete the values- 93, 201 from it. O 36 O 45 O O 72 O 79 O O 90 O 93 O 101 O O 108 O O 63 O 81 O O 117 O 201 O O 111 O 114 O O 151 O 180 O O 243 O 256 O 333 O 450 O O 108 O O 63 O 81 O O 117 O 243 O O 36 O 45 O O 72 O 79 O O 90 O 101 O O 11 1 O 11 4 O O 151 O 180 O O 256 O 333 O 450 O
31. 31. © Oxford University Press 2011. All rights reserved. B+ TREES • A B+ tree is a variant of a B tree which stores sorted data in a way that allows for efficient insertion, retrieval and removal of records, each of which is identified by a key. While a B tree can store both keys and records in its interior nodes, a B+ tree, in contrast, stores all records at the leaf level of the tree; only keys are stored in interior nodes. • The leaf nodes of the B+ tree are often linked to one another in a linked list. This has an added advantage of making the queries simpler and more efficient. • Typically B+-trees are used to store large amounts of data that cannot be stored in the main memory. With B+ trees, the secondary storage (magnetic disk) is used to store the leaf nodes of the tree and the internal nodes of the tree are stored in the main memory. • B+-tree stores data only in the leaf nodes. All other nodes (internal nodes) are called index nodes or i-nodes and store index values which allow us to traverse the tree from the root down to the leaf node that stores the desired data item. O 13 O O 4 O 7 O O 17 O 20 O O 1 O 3 O O 5 O 6 O O 8 O 11 O O 1 4 O 16 O O 18 O 19 O O 23 O 24 O
32. 32. © Oxford University Press 2011. All rights reserved. TRIE • The term trie has been taken from the word "retrieval". A trie is an ordered tree data structure introduced in the 1960's by Fredkin. Trie stores keys that are usually strings. It is basically a k-ary position tree. • In contrast to the binary search trees, node in the trie does not store the key associated with that node. Rather, its position in the tree represents the key associated with that node. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. a I d a m a r ar c ar e ar m art d o do t a d I m o t r m c e 3 11 9 12 5 8 15 A trie is very commonly used to store a dictionary (for ex, on a mobile telephone). These applications take advantage of a trie’s ability to quickly search, insert, and delete entries. Tries are also used to implement approximate matching algorithms, including those used in spell checking software.
33. 33. © Oxford University Press 2011. All rights reserved. RED BLACK TREE • A red-black tree is a self-balancing binary search tree. Although a red black tree is complex, but it has good worst-case running time for its operations and is efficient to use as searching, insertion, and deletion can all be done in O(log n) time, where n is the number of nodes in the tree. Properties of a Red- Black Tree • A red-black tree is a binary search tree in which every node has a color which is either red or black. Apart from the other restrictions of a binary search trees, the red black tree has some additional requirements that can be given as follows: • The color of a node is either red or black. • The color of the root node is always black. • All leaf nodes are black. • Every red node has both the children colored in black. • Every simple path from a given node to any of its leaf nodes has equal number of black nodes.
34. 34. © Oxford University Press 2011. All rights reserved. 1 6 2 7 9 7 1 1 NULL NULL NULL1 1 NULL NULL 2 1 4 5 NULL NULL 3 6 6 3 NULL NULL NULL NULL These constraints enforce a critical property of red-black trees that is the longest path from the root node to any leaf node is no more than twice as long as the shortest path from the root to any other leaf in that tree. This results in a roughly balanced tree. Since operations such as insertion, deletion, and searching require worst-case time proportional to the height of the tree, this theoretical upper bound on the height allows red-black trees to be efficient in the worst-case, unlike ordinary binary search trees.
35. 35. © Oxford University Press 2011. All rights reserved. Insertion in a Red- Black Tree • Once the new node is added, one or the other property of the red-black tree may have got violated. So in order to restore their property, we will check for certain cases and will do restoration of the property depending on the case that turns up after insertion. But before learning about these cases in detail, first let us have a look at certain important terms that will be used. • Case 1: The new node N is added as the root of the tree. In this case, N is repainted black as the root of the tree is always black (property 2). Since N adds one black node to every path at once, Property 5 that says all paths from any given node to its leaf nodes has equal number of black nodes is not violated. The C code for case 1 can be given as: void case1(struct node *n) { if (n->parent == NULL) // Root node n->color = BLACK; else case2(n); }
36. 36. © Oxford University Press 2011. All rights reserved. • Case 2: The new node's parent P is black In this case, both children of every red node are black, so property 4 is not invalidated. Property 5 which says that all paths from any given node to its leaf nodes have equal number of black nodes is also not threatened. This is because the new node N has two black leaf children, but because N is red, the paths through each of its children have the same number of black nodes. The C code to check for case 2 can be given as follows. void case2(struct node *n) { if (n->parent->color == BLACK) return; /* Red black tree property is not violated*/ else case3(n); } Case 3: If both the parent (P) and the uncle (U) are red In this case property 5 which says all paths from any given node to its leaf nodes have equal number of black nodes is violated. G P U N A B C D E G P U N A B C D E
37. 37. © Oxford University Press 2011. All rights reserved. • So in order to restore property 5, both nodes (P and U) are repainted black and the grandparent G becomes is repainted red. Now, the new red node N has a black parent. Since any path through the parent or uncle must pass through the grandparent, the number of black nodes on these paths has not changed. • However, the grandparent G may now violate property 2 which says that the root node is always black or property 4 which states that both children of every red node are black. Property 4 will be violated when G has a red parent. So in order to fix this problem, this entire procedure is recursively performed on G from case 1. The C code to deal with case 3 insertion is given below. Void case3(struct node *n) { struct node *u, *g; u = uncle (n), g = grand_parent(n); if ((u != NULL) && (u->color == RED)) { n->parent->color = BLACK; u->color = BLACK; g->color = RED; case1(g); } else insert_case4(n); }
38. 38. © Oxford University Press 2011. All rights reserved. • Case 4: The parent P is red but the uncle U is black and N is the right child of P and P is the left child of G In order to fix this problem; a left rotation is done to switch the roles of the new node N and its parent P. After the rotation, note that in the C code we have re-labeled N and P and then case 5 is called to deal with the new node’s parent. This is done because property 4 which says both children of every red node should be black is still violated. G A ? N B ? C D ? E N U P A ? B C E G P U void case4(struct node *n) { struct node *g = grand_parent(n); if ((n == n->parent->right) && (n->parent == g->left)) { rotate_left(n->parent); n = n->left; } else if ((n == n->parent->left) && (n->parent == g->right)) { rotate_right(n->parent); n = n->right; } case5(n); }
39. 39. © Oxford University Press 2011. All rights reserved. Case 5: The parent P is red but the uncle U is black and the new node N is the left child of P, and P is the left child of its parent G. • In order to fix this problem, a right rotation on G (the parent of parent of N) is performed. After this rotation, the former parent P is now the parent of both the new node N and the former grandparent G. • We know that the color of G is black (because otherwise its former child P could not have been red) so now switch the colors of P and G so that the resulting tree satisfies Property 4 which states that both children of a red node are black. G P U N A ? B C D E P N G A ? B C U D E void case5(struct node *n) { struct node *g = grandparent(n); if ((n == n->parent->left) && (n->parent == g->left)) rotate_right(g); else if ((n == n->parent->right) && (n->parent == g->right)) rotate_left(g); n->parent->color = BLACK; g->color = RED; }
40. 40. © Oxford University Press 2011. All rights reserved. Deletion of a node from a Red Black Tree • While deleting a node, if its color is red then we can simply replace it with its child, which must be black. All paths through the deleted node will simply pass through one less red node, and both the deleted node's parent and child must be black, so none of the properties will be violated. • Another simple case is when we delete a black node that has a red child. In this case property 4 (both children of every red node are black) and property 5 (all paths from any given node to its leaf nodes has equal number of black nodes), could be violated, so to restore them just repaint the deleted node’s child with black. • However, complex situation arises when both the node to be deleted as well as its child is black. In this case, we begin by replacing the node to be deleted with its child. This will violate property 5 which states that all paths from any given node to its leaf nodes have equal number of black nodes. Therefore, the tree needs to be rebalanced. There are several cases to consider: • Case 1: N is the new root. In this case, we have removed one black node from every path, and the new root is black, so none of the properties are violated. void del_case1(struct node *n) { if (n->parent != NULL) del_case2(n); }
41. 41. © Oxford University Press 2011. All rights reserved. • Case 2: Sibling S is red. In this case, interchange the colors of P and S, and then rotate left at P. in the resultant tree, S will become N's grandparent. P N S A ? B SL C SR D E S P S R A ? B D E N SL C D void del_case2(struct node *n) { struct node *s = sibling(n); if (s->color == RED) { if (n == n->parent->left) rotate_left(n->parent); else rotate_right(n->parent); n->parent->color = RED; s->color = BLACK; } del_case3(n); }
42. 42. © Oxford University Press 2011. All rights reserved. • Case 3: P, S, and S's children are black. EDGFSRSLBA?SNP In this case simply repaint S with red color. The resultant tree will have all paths passing through S, have one less black node. Therefore, all paths that pass through P now have one fewer black node than paths that do not pass through P, so Property 5 which states that all paths from any given node to its leaf nodes have equal number of black nodes is still violated. To fix this problem, we perform the rebalancing procedure on P, starting at case 1. P N S A ? B SL SR F GD E P N S A ? B SL SR F GD E void del_case3(struct node *n) { struct node *s = sibling(n); if ((n->parent->color == BLACK) && (s->color == BLACK) && (s- >left->color == BLACK) && (s->right->color == BLACK)) { s->color = RED; del_case1(n->parent); } else del_case4(n); }
43. 43. © Oxford University Press 2011. All rights reserved. • Case 4: If S and S's children are black, but P is red. In this case, we interchange the colors of S and P. Although this will not affect the number of black nodes on paths going through S, but it will add one black node to the paths going through N, making up for the deleted black node on those paths. P N S A ? B SL SR F GD E P N S A ? B SL SR F GD E void del_case4(struct node *n) { struct node *s = sibling(n); if ((n->parent->color == RED) && (s->color == BLACK) && (s->left- >color == BLACK) && (s->right->color == BLACK)) { s->color = RED; n->parent->color = BLACK; } else del_case5(n); }
44. 44. © Oxford University Press 2011. All rights reserved. • Case 5: If N is the left child of P and S is black, S’s left child is red, S's right child is black. In this case, perform a right rotation at S. After the rotation, S's left child becomes S's parent and N's new sibling. Also interchange the colors of S and its new parent. Note that now all paths still have equal number of black nodes, but N has a black sibling whose right child is red, so we fall into case 6. S SL SR F GD E SL D SR F G S E void del_case5(struct node *n) { struct node *s = sibling(n); if (s->color == BLACK) { if ((n == n->parent->left) && (s->right->color == BLACK) && (s->left- >color == RED)) rotate_right(s); else if ((n == n->parent->right) && (s->left->color == BLACK) && (s- >right->color == RED)) rotate_left(s); s->color = RED; s->right->color = BLACK; } del_case6(n); }
45. 45. © Oxford University Press 2011. All rights reserved. • Case 6: S is black, S's right child is red, and N is the left child of its parent P. In this case, a left rotation is done at P to make S the parent of P and S's right child. After the rotation, the colors of P and S are interchanged and S’s right child is colored black. Once these steps are followed, you will observe that property 4 which says both children of every red node are black and property 5 that states all paths from any given node to its leaf nodes have equal number of black nodes remain valid. P N S A ? B SR D E C S P A ? B D E N C S R void del_case6(struct node *n) { struct node *s = sibling(n); s->color = n->parent->color; n->parent->color = BLACK; if (n == n->parent->left) { s->right->color = BLACK; rotate_left(n->parent); } else { s->left->color = BLACK; rotate_right(n->parent); } }
46. 46. © Oxford University Press 2011. All rights reserved. SPLAY TREE • A splay tree is a self-balancing binary search tree with an additional property that recently accessed elements can be re-accessed fast. It is said to be an efficient binary tree because it performs basic operations such as insertion, search and deletion operations in O(log(n)) amortized time. For many non-uniform sequences of operations, splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown. • Splay tree consists of a binary tree, with no additional fields. When a node in a splay tree is accessed, it is rotated or "splayed" to the root thereby changing the structure of the tree. Since the most frequently accessed node is always moved closer to the starting point of the search (or the root node), those nodes are therefore located faster. A simple idea behind it is that if an element is accessed, it is likely that it will be accessed again. • In a splay tree, operations like insert, search and delete are combined with one basic operation, called splaying. Splaying the tree for a particular node rearranges the tree to make that node placed at the root of the tree.
47. 47. © Oxford University Press 2011. All rights reserved. Advantages and disadvantages • A splay tree gives good performance for search, insert and delete operations. This advantage centers on the fact that the splay tree is self-balancing, and a self optimizing data structure in which the frequently accessed nodes are moved closer to the root so that they can be accessed more quickly. This advantage is particularly useful for implementing caches and garbage collection algorithms. • Splay trees are considerably simpler to implement than other self-balancing binary search trees, such as red-black trees or AVL trees, while their average-case performance is just as efficient. • Splay tree minimizes memory requirements as they do not store any bookkeeping data. • Unlike other types of self balancing trees, splay trees gives good performance (amortized O(log n)) with nodes containing identical keys. • However, the negative side of splay tree includes: • While sequentially accessing all the nodes of the tree in a sorted order, the resultant tree becomes completely unbalanced. This takes n accesses of the tree in which each access takes O(log n) time. • For uniform access, the performance of a splay tree will be considerably worse than a somewhat balanced simple binary search tree.
48. 48. © Oxford University Press 2011. All rights reserved. Splaying When we access a node N, splaying is performed on N to move it to the root. To perform a splay operation, certain splay steps are performed where each step moves N closer to the root. Splaying a particular node of interest after every access ensures that the recently accessed nodes are kept closer to the root and the tree remains roughly balanced, so that the desired amortized time bounds can be achieved. • Each splay step depends on three factors: • Whether N is the left or right child of its parent P • Whether P is the root or not, and if not • Whether P is the left or right child of its parent, G (N’s grandparent). Depending on the three factors we have one splay step based on each factor. • Zig Step: The zig operation is done when P (the parent of N) is the root of the splay tree. In the zig step, the tree is rotated on the edge between N and P. Zig step is usually performed as the last step in a splay operation and only when N has odd depth at the beginning of the operation. P N T 1 T 2 T 3 N T 1 P T 2 T 2
49. 49. © Oxford University Press 2011. All rights reserved. • Zig-zig Step: The zig- zig operation is performed when P is not the root. In addition to this, N and P are either both right children or are both left children of their parent’s. Figure shows the case where N and P are the left children. During the zig- zig step, first the tree is rotated on the edge joining P and its parent G, and then again rotated on the edge joining N and P. G P T 4 N T 1 T 2 T 3 P G T 4 N T 1 T 2 N T 1 P G T 4 T 2 T 3 Zig-zag Step: The zig-zag operation is performed when P is not the root. In addition to this, N is a right child of P and P is a left child of G or vice versa. In zig-zag step, the tree is first rotated on the edge between N and P, and then rotated on the edge between P and G.
50. 50. © Oxford University Press 2011. All rights reserved. G P T 4 T 1 N T 2 T 3 G N T 4 P T 1 T 2 T 3 N G P T 1 T 2 T 1 T 2 Insertion Although the process of inserting a new node N into a splay tree begins in the same way as we insert a node in a binary search tree, but after the insertion, N is made the new root of the splay tree. The steps performed to insert a new node N in a splay tree can be given as: Step 1: Search N in the splay tree. If the search is successful, splay at the node N. Step 2: If the search is unsuccessful, add the new node N in such a way that it replaces the NULL pointer reached during the search by a pointer to a new node N. Splay the tree at N
51. 51. © Oxford University Press 2011. All rights reserved. Consider the splay tree given on the left. Observe the change in structure of the tree when 81 is added to it 5 4 3 9 6 3 9 4 5 1 8 9 0 7 2 9 9 27 5 4 3 9 6 3 9 4 5 2 7 1 8 9 0 7 2 9 9 8 1 8 1 6 3 5 4 7 2 3 9 9 4 5 2 7 1 8 9 0 9 9 Search If a particular node N is present in the splay tree then a pointer to the N is returned; otherwise a pointer to the null node is returned. The steps performed to search a node N in a splay tree includes: Search down the root of the splay tree looking for N If the search is successful, and we reach N then splay the tree at N and return a pointer to N If the search is unsuccessful, i.e., the splay tree does not contain N, then we reach the null node. Splay the tree at the last non-null node reached during the search and return a pointer to null.
52. 52. © Oxford University Press 2011. All rights reserved. 5 4 3 9 6 3 9 4 5 2 7 1 8 9 0 7 2 9 9 7 2 9 0 9 9 5 4 3 9 6 3 9 1 8 2 7 4 5 Consider the splay tree given on the left. Observe the change in structure of the tree when a node containing 81 is searched in the tree. Delete To delete a node N from a splay tree, perform the steps given below. Search for the N that has to be deleted. If the search is unsuccessful, splay the tree at the last non-null node encountered during search. If the search is successful, and N is not the root node then let P be the parent of N. Replace N by an appropriate descendent of P (as we do in binary search tree). Finally splay the tree at P.
53. 53. © Oxford University Press 2011. All rights reserved. Consider the splay tree at the left. When we delete node 39 from it, the new structure of the tree can be given as shown in the right side of the figure. 8 1 6 3 9 0 5 4 7 2 9 9 3 9 9 4 5 2 7 1 8 8 1 6 3 9 0 5 4 7 2 9 9 2 7 9 4 5 1 8 P 5 4 2 7 6 3 8 1 7 2 9 0 9 9 9 4 5 1 8