You are on page 1of 21
M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur ibus: Search Trees- AVL Trees, Definition, Height of AVL Tree, Operations-, Insertion, Deletion and Introduction to Red-Black and Splay Trees, B-Trees, Height of B-Tree, Insertion, Deletion and Searching, Comparison of Search Trees. IT -V SEARCH TREES (1) AVL Trees & De > AVL tree is a self-balancing binary search tree invented by G.M. Adelson-Velsky and E.M. Landis in 1962 > In an AVL tree the heights of the two sub trees of a node may differ by at most one. > Due to this property, the AVL tree is also known as a height-balanced tree. > The advantage of using AVL tree is, it takes O(log n) time for search, insert and delete operations in an average case as well as the worst case because the height of the tree is limited to O(log n). > The structure of an AVL tree is the same as that of a binary search tree, but every node has a balance factor associated with it > The balance factor of a node is given by Balance factor = Height of left sub tree — Height of right sub tree > Abinary search tree in which every node has a balance factor of -1,0,1 is said to be height balanced. A node with any other balance factor is considered to be unbalanced and requires rebalancing of the tree > Ifthe balance factor of a node is 1, then that tree is called a left-heavy tree. If the balance factor of anode is 0, then the height of the left sub-tree is equal to the height of the right sub-tree. > If the balance factor of a node is -1, then that tree is called a right-heavy tree > The left-heavy AVL tree, right-heavy AVL tree and balanced tree is shown below. (a) left-heavy AVL tree (b) right-heavy AVL tree (c) balanced tree > The insertions and deletions from an AVL tree may disturb the balance factor of the nodes and thus, rebalancing of the tree may have to be done. > The tree is rebalanced by performing the following four types of rotations LL rotation, RR rotation, LR rotation and RL rotation. > The type of rotation that has to be done will vary depending on the particular situation. i) Various rotations of AVL Trees(Associated with insertion operation): > AVL Rotations are used to convert an unbalanced AVL Tree into a balanced AVL Tree for insertion. There are 4 types of AVL Rotations for insertion. (a)LL Rotation —_(b)RR Rotation (LR Rotation (d)RL Rotation > The first two are called as single rotations and next two are called as double rotations. ALL Rotation(left to left rotation): > LL Rotation is applied on unbalanced AVL Tree if the unbalance occurred due to the insertion of a new element into left sub tree of left child of pivot element. 1® 2a insert new oO, wy fg] siomentino After LL Rotation, ] Pa not] Page 1 M-Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur > The following actions are done in LL rotation 1. Node B occupies the position of node A along with left sub tree of BL 2. Node A is attached as right child of node B along with right sub tree of Ar 3. Bris attached to left child of node A. Example: > Consider the following tree 18) insert 2) pivot 1 Oe @ After LL Rotation (D)RR Rotation(right to right rotation): > RR Rotation is applied on unbalanced AVL Tree if the unbalance occurred due to the insertion of a new element into right sub tree of right child of pivot element. a 0 __insert new %B) _element into Br —~ h Bal » The following actions are done in LL rotation 1. Node B occupies the position of node A along with right sub tree of Br 2. Node A is attached as left child of node B along with left sub tree of AL 3. Bi is attached to right child of node A Exam > Consider the following tree A insert 30-2) pivot ® 4 After RR rotation 2° 0 28) ——— 0 o Oo (c)LR Rotation(left to t rotation): LR Rotation is applied on unbalanced AVL Tree if the unbalance occurred due to the insertion of a new element into right sub tree of left child of pivot element (2 insert new element into Cis, [Ag] After LR Rotation ————— nfP rf} nef > The following actions are done in LL rotation 1. Node C occupies the position of node A 2. Node B is attached as left child of node C along with left sub tree of By 3. Node A is attached as right child of node C along with right sub tree of Ar 4. C: is attached to right child of node B & Cz is attached to left child of node A Page2 M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Example: > Consider the following tree 149 insert 12 248) Pivot @ _After LR rotation we » RL Rotation is ee Reet ae on unbalanced AVL Tree if the unbalance occurred due to the insertion of a new element into left sub tree of right child of pivot element. insert new element into Cry le] fer le] [er Q > The following actions are done in LL rotation 1. Node C occupies the position of node A 2. Node B is attached as right child of node C along with right sub tree of Br 3. Node A is attached as left child of node C along with left sub tree of AL 4. C. is attached to right child of node A & Cx is attached to left child of node B Example: Consider the following tree 4 insert 20 248) pivot P 3 Alter RL rotation, 0 0 di)Insertion Operation in AVL Tree > In an AVL tree, the insertion operation is performed with O(log n) time complexity > In AVL Tree, new node is always inserted as a leaf node. > The insertion algorithm is given below. Step 1: Insert the new element into the tree using Binary Search Tree insertion logic. Step 2: After insertion, check the Balance Factor of every node. Step 3: If the Balance Factor of every node is 0 or 1 or -1 then go for next operation Step 4: If the Balance Factor of any node is other than 0 or 1 or -1 then tree is said to be imbalanced. Then perform the suitable Rotation to make it balanced. And go for next nf het h operation. Example: > Construct an AVL Tree by inserting numbers 63,9,19,27,18,108,99,81 inset6s @° inset9 @1 insert 19 @? 0 Tree is balanced (9) , After LR Rotation ° ——+ 9 Tree is Balanced © ® Tree is imbalanced ‘Tree is balanced Page3 M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur st 27 inser insert 18 4 insert 108 , insert 99, we s ‘ a 0 ge eB Tree is balanced Tree is balanced Tree is balanced Tree is balanced 8 Tree is imbalanced i ‘ Tree is balanced (iii) Various rotations of AVL Trees(Associated with deletion operation): > AVL Rotations are used to convert an unbalanced AVL Tree into a balanced AVL Tree for deletion. > There are 6 types of AVL Rotations for deletion (a)RO Rotation (b)R1 Rotation equal to LL rotation (c) R-1 rotation --equal to LR rotation (d)LO Rotation —_(e)L-1 Rotation equal to RR rotation (f) L1 rotation --equal to RL rotation ‘a)RO Rotation(equal to LL rotation): » RO Rotation is applied on unbalanced AVL Tree if the deletion is done on right sub tree of pivot element and balance factor of left child of pivot is zero. 10) 2@) °e Tag _elete node t [Ag| After RO Rotation R| > hel} “| ——> Py ae nF} PR) Bie » The following actions are done in LL rotation 1. Node B occupies the position of node A along with left sub tree of BL 2. Node A is attached as right child of node B along with right sub tree of Ar 3. Br is attached to left child of node A. Example: > Consider the following tree Tree is balanced Tree is balanced Tree is balan Page 4 I M-Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur (b)R1 Rotation(equal to LL rotation) > R1 Rotation is applied on unbalanced AVL Tree if the deletion is done on right sub tree of pivot element and balance factor of left child of pivot is one. » The following actions are done in LL rotation 1. Node B occupies the position of node A along with left sub tree of BL 2. Node A is attached as right child of node B along with right sub tree of Ar 3. Br is attached to left child of node A Examp! » Consider the following tree Ou Ater Ri rotation at 10 Rot: (equal to LR rotation): R1 Rotation is applied on unbalanced AVL Tree if the deletion is done on right sub tree of pivot element and balance factor of left child of pivot is -1. » The following actions are done in LL rotation 1. Node C occupies the position of node A 2. Node B is attached as left child of node C along with left sub tree of BL 3. Node A is attached as right child of node C along with right sub tree of Ar 4. Cis attached to right child of node B & Cz is attached to left child of node A ‘xample: > Consider the following tree " at 10 ‘Tree is balanced ‘Tree is balanced (d)LO Rotation(equal to RR_ LO Rotation is applied on unbalanced AVL Tree if the deletion is done on left sub tree of pivot element and balance factor of right child of pivot is zero. Page 5 I M.Tech I Sem CSE ADS &A Unit-5 (R19) @ 9 AL (B) delete elementt h > bet B B, B B, @ ,) | L | la, h t h h rae Prepared by: Dr. Md. Umar khan, KHIT, Guntur > The following actions are done in LL rotation 1. Node B occupies the position of node A along with right sub tree of Br 2. Node A is attached as left child of node B along with left sub tree of AL 3. Bi is attached to right child of node A Example: Consider the following tree 0 \ After LO rotation 4\ __at1o 4, —alte, , Tree is balanced Tree is imbalanced Tréé is balanced (@)L-1 Rotation(equal to RR rotation): » L-1 Rotation is applied on unbalanced AVL Tree if the deletion is done on left sub tree of pivot element and balance factor of right child of pivot is -4 4 delete elementt ) After L-1 Rotation h —— ®& Ba h B, B t RI | he h nae] h-aBe > The following actions are done in LL rotation 1. Node B occupies the position of node A along with right sub tree of Br 2. Node A is attached as left child of node B along with left sub tree of A. 3. Bi is attached to right child of node A Examp! > Consider the following tree Tree is balanced Tree is imbalanced Tree is balanced (OLLI Rotation(equal to RL rotation) > R1 Rotation is applied on unbalanced AVL Tree if the deletion is done on left sub tree of pivot element and balance factor of right child of pivot is one. Page 6 I M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur © After L1 Rotation (Ay ‘B) 4 YB) delete element — ICL, ICI byt) ha] hey] hed > The following actions are done in LL rotation 1. Node C occupies the position of node A 2. Node B is attached as right child of node C along with right sub tree of Br 3. Node A is attached as left child of node C along with left sub tree of A 4. C. is attached to right child of node A & Cr is attached to left child of node B Example: ¥ Consider the following tree Tree is balanced Tree is imbalanced Tree is balanced {lii)Deletion Operation in AVL Tree Algorithm: 1. First find the deleted node. If that node is leaf node then delete the node directly. 2. If node to be deleted has one child then swap deleted node and left child or right child of that node. Next delete the node. Otherwise find in order successor of that node and swap that deleted node with in order ‘successor and then delete. 3, If balance factors of all nodes are -1,0,1 then deletion is successful and stop. 4. Otherwise perform an appropriate rotation to balance the AVL Tree. 1.The node to be deleted is a leaf node(i.c. node has no children) > First find the leaf node say 18 and delete. For imbalance nodes do required rotations. The node to be deleted has exactly one child node find the node that has exactly one child say 9. Swap 9 and 18. Delete 9. > For imbalance nodes do required rotations M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur ‘The node to be deleted is a root node(or node to be deleted has left & right children) > First find the root node 19. Find in order successor of 19 that is 27. Swap 19 and 27. Delete 19 > For imbalance nodes do required rotations. “ ul (iv) Search Operation in AVL Tree > In an AVL tree, the search operation is performed with O(log n) time complexity. > The search operation is performed similar to Binary search tree search operation. > We use the following steps to search an element in AVL tree. Step 1: Read the search element from the user ‘Step 2: Compare, the search element with the value of root node in the tree. Step 3: If both are matching, then display “Given node is found!!!" and terminate the function Step 4: If both are not matching, then check whether search element is smaller or larger than that node value. Step 5: If search element is smaller, then continue the search process in left subtree. Step 6: If search element is larger, then continue the search process in right subtree. ‘Step 7: Repeat the same until we found exact element or we completed with a leaf node Step 8: If we reach to the node with search value, then display "Element is found" and terminate the function. Step 9: If we reach to a leaf node and it is also not matching, then display "Element not found" and terminate the function (v)Height of an AVL Tree: > AVL tree is a type of binary search tree in which at any given node, the difference between heights of left sub-tree and right sub-tree cannot be greater than 1 » This property of the AVL tree helps to keep the tree height balanced. » The height of a tree is the number of nodes on the longest path from the root to a leaf. > We will say that an empty tree has height 0. Height of tree with only one node has height 1 » Let us consider the following five AVL Trees. To Ti T2 NES 14 Ad > For TO the height of AVL Tree » For T1 the height of AVL Tree: » For TO the height of AVL Tre » For TO the height of AVL Tree: » For TO the height of AVL Tree: (2) Red-Black Trees: (i) Definition: > Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK > Red-Black Trees are efficient to use for searching, insertion and deletion in O(log n) time where n is the number of nodes in the tree. the number of nodes on the longest path from the root to a leaf =1 the number of nodes on the longest path from the root to a leaf=2, the number of nodes on the longest path from the root to a leaf=3 the number of nodes on the longest path from the root to a leaf=4 the number of nodes on the longest path from the root to a leaf=5 Page 8 TM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur {ii) Representation of a Red-Black > Itis convenient to include external nodes when defining red-black trees. > With each node we need to store only its color or the color of the two pointers to its children > Node colors require just one additional bit per node, while pointer colors require two. > In insert and delete operations, the needed color changes only for the nodes. > The following figure shows a red-black tree. Red-black tree > The properties are given by 1. The color of a node is either red or black 2. The color of the root node is always black 3. All leaf nodes (external nodes) are black 4. Every red node has both the children colored black. 5. Every path from a root node to any of its leaf (or external) nodes has an equal number of black nodes. > The rules of red-black tree are given by 1. The color of root node is black 2. There should be no two consecutive red nodes on any path from root to external nodes 3. The number of black nodes on any paths from root node to external nodes must be same Various rotations of Red-Black Trees: > Red-Black Rotations are used to rotate Red-Black Trees when two consecutive red nodes occurred and one red parent red node sibling is absent. There are 4 types of rotations (a)LLb imbalance (or) rotation (b)RRb imbalance (or) rotation (c)LRb imbalance (or) rotation (d)RLb imbalance (or) rotation > The first two are called as single rotations and next two are called as double rotations. @)LLD imbalance (or) rotation: > LLb Rotation is applied if two consecutive red nodes occurred due to the insertion of a new element into left sub tree of left child of pivot (gu) element and red parent red node (pu) sibling is absent or black. bE pivot b r, LLb Rotation . . —_s> Sur é \ color change PuR ML UR ply SUR UL UR > The following actions are done in LLb rotation 1. Apply LL rotation at gu 2. Change the color of pu to black & Change the color of gu to red Example: ¥ Consider the following tree 68 insert § (3) pivot Y LL b rotation ag? —_—_—_> color changer r Page 9 I M-Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur (b)RRb imbalance (or) rotatio > RRb Rotation is applied if two consecutive red nodes occurred due to the insertion of a new element into right sub tree of right child of pivot (gu) element and red parent red node (pu) sibling is, absent or black RRb Rotation aes r color change OUR Pl UL OR » The following actions are done in RRb rotation 1. Apply RR rotation at gu 2. Change the color of pu to black & Change the color of gu to red Example: > Consider the following tree b insert 30 b(@B) pivot ARBRE r color change (c)LRb imbalance (or) rotatio1 > LRb Rotation is applied if two consecutive red nodes occurred due to the insertion of a new element into right sub tree of left child of pivot (gu) element and red parent red node (pu) sibling is absent or black. \b LRb Rotation 7 aes r color change pl % Po UL UR gue uL » The following actions are done in LRb rotation 1. Apply LR rotation at gu 2. Change the color of u to black & Change the color of gu to red Examp! > Consider the following tree Le) insert 12 b@a)pivot b : © LRb rotation 2 ——— ) color change Gy Ou a (@RLb imbalance (or) rotatio > RLb Rotation is applied if two consecutive red nodes occurred due to the insertion of a new element into right sub tree of left child of pivot (gu) element and red parent red node (pu) sibling is absent or black. pivot " r RLb Rotation t ga —> F color change r Buy S gH tL oR pug uy Ur Page 10 M-Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur > The following actions are done in RLb rotation 1. Apply RL rotation at gu 2. Change the color of u to black & Change the color of gu to red Example: > Consider the following tree b insert 20 b, pivot b p @ RLb rotation 20} —<— > color change @® ea)" » It is used to change Red-Black Trees node colors when two consecutive red nodes occurred and ‘one red parent red node sibling is red. There are 4 types of Red-Black Trees color changes. (@)LLr imbalance (or) color change (b)RRr imbalance (or) color change (©)LRr imbalance (or) color change (@)RLr imbalance (or) color change @)LLr imbalance (or) color change: ® LLr color change is applied if two consecutive red nodes occurred due to the insertion of a new element into left sub tree of left child of pivot (gu) element and red parent red node (pu) sibling is red b, @a) pivot LLr color change Pur giRL gURR “LOR UL UR » The following actions are done in LLr color change 1. Change the color of pu to black & Change the color of guR to black 2. If gu is not root, then change the color of gu to red PUR gGR_ gURR Example: } Consider the following tree. b, (8) Insert 5 (8) pivot Insert 20 678 pivot Reg LRr color change —> (b)RRr imbalance (or) color chang RRr color change is applied if two consecutive red nodes occurred due to the insertion of a new element into right sub tree of right child of pivot (gu) element and red parent red node (pu) sibling is red. pivot RRr color change —=s color change Up > The following actions are done in RRr color change 1. Change the color of pu to black & Change the color of guR to black 2. If gu is not root, then change the color of gu to red Page 1 M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Example: > Consider the following tree bi Insert 30 4778) pivot Insert 10 . r i) (¢)LRr imbalance (or) color change: > LRr color change is applied if two consecutive red nodes occurred due to the insertion of a new element into right sub tree of left child of pivot (gu) element and red parent red node (pu) sibling is red. 1, pivor RRr color change > RRr color change ——_> color change Pu gURL = guRR Pa gURL — guRR ub UR UL UR > The following actions are done in LRr color change 1. Change the color of pu to black & Change the color of guR to black 2. If gu is not root, then change the color of gu to red Example: ¥ Consider the following tree. b(3) Insert 12 *® pivot Insert 20 a pivot : i C) et C) (@RLr imbalance (or) color change: > RLr color change is applied if two consecutive red nodes occurred due to the insertion of anew element into right sub tree of left child of (gu) pivot element and red parent red node (pu) sibling is red RLr color change —_—> color change > The following actions are done in LRr color change 1. Change the color of pu to black & Change the color of guR to black 2 tt 1u is not root, then change the color of gu to red > Corser the following tree Insert 20 (6) gut Insert 10 e pivot 199) Pur colr change e pivot) Page 12 TM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur [Insertion Operation in Red-Black Tree > The insertion algorithm is given below. Step 1: If tree is empty, create black root node. ‘Step 2: Insert a new leaf node as red. Step 3: After insertion, if its parent is black then we have done. Step 4: If parent is red then goto Step 5 or Step 6 Step 5: If black or absent sibling rotate, recolor is done Step 6: If red sibling is present then recolor & check again. Examp! Construct a Red-Black Tree for the following 50, 10, 80, 90, 70, 60, 65, 62 insert wae b RR rr color change LL rook E ae lor LR b rotation at 70 and change color RL b Rotation at 50 and cange color IM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur (vi) Deletion from a Red-Black Tre Algorithm: 1. If deleted node is red or black with one or two children then find in order successor of deleted node. Swap deleted node & successor node. Now delete node. Check for imbalances. 2. If deleted node is black with child external nodes then delete node & check for imbalances. 3. If deleted node is red with child external nodes then delete node directly. Example(i): [Deleted node is red or black with two children] Delete 35 inorder successor is 65 so swap 35,65 & delete > We can search a red-black tree with the code we used to search an ordinary binary search tree. > This code has complexity O(h), which is O(log n) for a red black tree. > We use the following steps to search an element in Red-Black tree. Step 1: Read the search element from the user Step 2: Compare, the search element with the value of root node in the tree. Step 3: If both are matching, then display "Given node found!!!" and terminate the function Step 4: If both are not matching, then check whether search element is smaller or larger than that node value. Step 5: If search element is smaller, then continue the search process in left subtree. Step 6: If search element is larger, then continue the search process in right subtree. Step 7: Repeat the same until we found exact element or we completed with a leaf node Page 14 TM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Step 8: If we reach to the node with search value, then display "Element is found” and terminate the function. Step 9: If we reach to a leaf node and itis also not matching, then display "Element not found" and terminate the function Q)Splay Tree: ® Splay Tree is a self - adjusted Binary Search Tree in which every operation on an element rearrange the tree so that the element is placed at the root position of the tree > Ina splay tree, every operation is performed at root of the tree. All the operations on a splay tree are involved with a common operation called "Splaying” > Ina splay tree, splaying an element rearrange all the elements in the tree so that splayed element is placed at root of the tree. > With the help of splaying an element we can bring most frequently used element closer to the root of the tree so that any operation on those element performed quickly. That means the splaying operation automatically brings more frequently used elements closer to the root of the tree. > Every operation on a splay tree performs the splaying operation. For example, the insertion operation first inserts the new element as it inserted into the binary search tree, after insertion the newly inserted element is splayed so that itis placed at root of the tree. > The search operation in a splay tree is search the element using binary search process then splay the searched element so that it placed at the root of the tree. > Ina splay tree, to splay any element we use the following rotation operations Rotations in Splay Tree 41. Zig Rotation 2. Zag Rotation 3. Zig - Zig Rotation 4. Zag - Zag Rotation 5. Zig - Zag Rotation 6. Zag - Zig Rotation Example: 1.Zig Rotation: > The Zig Rotation in a splay tree is similar to the single right rotation in AVL Tree rotations. In zig rotation every node moves one position to thr righ from its current position. > Consider the following example @ Splay (3) gg © mw So Zig Rotation @) (@) Single ight Rotation Zag Rotation > The Zag Rotation in a splay tree is similar to the single left rotation in AVL Tree rotations. In zag rotation every node moves one position to the left from its current position > Consider the following example Q > 5) @ = Rotation Page 15 IM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Rotation + The Zig-Zig Rotation in a splay tree is a double zig rotation. In zig-zig rotation every node moves two position to the right from its current position. Consider the following example ® 3) © Zig-Zig Rotation @ (4) Double Rohe Rotation Zag-Zag Rotation > The Zag-Zag Rotation in a splay tree is a double zag rotation. In zag-zag rotation every node moves two position to the left from its current position. > Consider the following example Splay (2) Splay (6 ) > Zag-Zag Rotation (©) ouste rte rotation Zig-Zag Rotation > The Zig-Zag Rotation in a splay tree is a sequence of zig rotation followed by zag rotation. In zig- zag rotation every node moves one position to the right followed by one position to the left from its current position. Consider the following example Splay ( 4 ) Zag-Zig Rotation The Zag-Zig Rotation in a splay tree is a sequence of zag rotation followed by zig rotation. In zag- zig rotation every node moves one position to the left followed by one position to the right from its current position. Consider the following example Splay (4) Zag Rotation at 3 Zig Rotation ats Page 16 TM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur (4) B-Trees: (Definition and Properties: > B-Tree is a self-balanced search tree with multiple keys in every node and more than two children for every node. Here, number of keys in a node and number of children for a node is depending on the order of the B-Tree. For example, B-Tree of Order 4 contains maximum 3 key values in a node and maximum 4 children for a node (2) Number of Elements in a B-tree: > The number of elements held in B-Tree is the average number of elements that fit in one node, times the number of leaf nodes. > The number of leaf nodes is the number of children that fit in a non leaf node > Ina 2-3 Bree known as 2-3 tree each internal node may have only 2 or 3 child nodes. When two children one data element, 3-children two data elements. > Ina 2-3-4 B-tree known as 2-3-4 tree each internal node may have only 2 or 3 or 4 child nodes. > When 2- children one data element, 3-children two data elements and 4-children three data elements G) Insertion into a ‘Algorithm: > All the insertions are done into leaf nodes only. > The process of inserting new element is given below. Step-1:Search for the leaf node into which the new element should be inserted. Step-2:If the leaf node contains less than m-1 elements, then insert this new element into the leaf node by maintaining increasing order of elements in the node. Step-3:If the leaf node already contains m-1 elements then ‘Step-3.1: Increase the element into the node by maintaining increasing order of elements Step-3.2: Split the node into two nodes at median such that all elements in first half are less than median and all the elements in second half are greater than median Step-3.3: Push the median up to the parent and insert it into the parent node. If the parent node also contains m-1 elements then repeat the above steps until the median element is properly inserted. Examp! Create B-Tree using the values 25,10,15,35,90,80,60,65,100,6 Assume order of B-Tree m=4 Minimum no. of keys in internal nodes=4/2-1= Maximum no. of keys in internal nodes=: Insert 25 Insert 10 Insert 15 25 10 | 25 10 [15 | 25 Insert 35 15 to [15 [25 |35 split 10 25 [35 Insert 90 7 10 25 | 35 | 90 Insert 80 16] 35 lit SES 10 25 80 | 90 Page 17 ITM.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur Insert 60 715] 35 10 25 60 | 80 | 90 Insert 65 15 | 35 | 65 mai 10 25 [60 80/90) Insert 100 45 135 | 65 10| 25) [60 [80 [90/100 Insert 6 15 | 35 | 65 6 |10 25 [60 (80 [9010 (4) Deletion from a B-tree: > To delete an element from a B-Tree of order m, the following steps are used Step-1: If the element to be deleted is in a leaf node then perform the following steps. Step-1.1: Locate the leaf node in which the element to be deleted is present. Step-1.2: If the leaf node contains more than minimum no. of elements(>(m/2-1)) then simply delete the value Step-1.3: If the leaf node does not contain minimum no. of elements after deletion then fill the node by taking an element either from the left sibling or right sibling If the left sibling has more than minimum no. of elements then push its largest element into its parent node and pull down the intervening element from the parent node into the leaf node where the element is deleted. If the right sibling has more than minimum no. of elements then push its smallest element into its parent node and pull down the intervening element from the parent node into the leaf node where the element is deleted. Step-1.4: If both left and right sibling contains only the minimum no. of elements then create a new leaf node by combining the leaf node in which element is deleted with either left sibling or right sibling Step-2: If the element to be deleted is in an intemal node then replace the element to be deleted with either in order successor or in order predecessor and then delete that in order successor or in order predecessor by using step-1 Step-3: If any violations after deletion then merge and proceed the above steps. Example: > Consider the following tree with order=5. Minimum no. of keys in internal nodes=5/2-1=3-1=2 > Maximum no. of keys in internal nodes=5-1=4 108 63 | 81 4117] 201 36 | 45 72] 79 go | 93 | 101 441 [114 151 |180 243] 266|333 [450 Page 18 I M-Tech I Sem CSE ADS &A Unit-5 (R19) Delete 93: Prepared by: Dr. Md. Umar khan, KHIT, Guntur > The leaf node contains more than minimum no. of elements(>(5/2-1)) so simply delete the value > After deleting 93 to the above tree it becomes [106] 63 | 81 117| 204] 36 [45 7279 90 | 101 Delete 201: 111 [114 151 |180, 243] 256] 333/450 » The element to be deleted is in an internal node so replace the element to be deleted with in order ‘successor 243 and delete. After deleting 201 to the above tree it becomes 108) 63 | 81 117|243 36 | 45 7279 90 [101 Delete 180: ana [1414 151 |180 256) 333 | 450) ® The leaf node does not contain minimum no. of elements (2) after deletion so fill the node by taking an element either from right sibling. Now the right sibling has more than minimum no. of elements so push its largest element (256) into its parent node and pull down the intervening element (243) from the parent node into the leaf node where the element is deleted > After deleting 180 to the above tree it becomes 108) 63 | 81 117| 256 36 [45 72 | 79 90 | 101 4444444, 151 | 243) 333}.450 Delete 72: » The leaf node does not contain minimum no. of elements (2) after deletion of 72 so fill the node by taking an element either from left sibling or right sibling. But the left sibling or right sibling has not more than minimum no. of elements the parent node into the leaf node where so merge it and pull down the intervening element (81) from the element is deleted » After deleting 72 to the above tree it becomes 108) 81 117| 256 36 | 45 | 63| 79 90 [104 414 [114 151 | 243 333] 450 Page 19 I M.Tech I Sem CSE ADS &A Unit-5 (R19) Prepared by: Dr. Md. Umar khan, KHIT, Guntur > Again the property is violated so merge with right sibling of 81. > Now it becomes 81 | 108] 117] 256| 36 [45 [63/79] [90 [101 444 [114 151 | 243) 333] 450) Delete 114: > The leaf node does not contain minimum no. of elements (2) after deletion of 114 so fill the node by taking an element from root node. So pull down the intervening element (108) from the parent node into the leaf node where the element is deleted and merge. » After deleting 114 to the above tree it becomes 81 [117/256 36 | 45 | 63| 79} [90 |101]108]111] [151 | 243} 333] 450) (5) Height of ® Consider a B-tree of height h with minimal number of keys: 1 key at the root and t~ 1 keys at each other node. > Number of keys in nodes of depth i > 1 is 2t"\(t - 1). depth no of nodes oO 1 4 a ut 1 2 tt] oct [tt t-1 . 1 2 2t ti]--(tt] fer) --fer] faa]... Per] fea] --- fev] 2 22 > The number of keys in a minimal B-tree =2t" -1 (G) Searching of a B-tree: > B-TREE-SEARCH is a straightforward generalization of the TREE-SEARCH procedure defined for binary search trees. B-TREE-SEARCH takes as input a pointer to the root node x of a subtree and a key k to be searched for in that subtree. > The top-level call is thus of the form B-TREE-SEARCH(roof{ 7], k). > If kis in the B-tree, B-TREE-SEARCH returns the ordered pair (y,/) consisting of a node y and an index i such that keyiy| = k. Otherwise, the value NIL is returned. > The algorithm is given by B-TREE-SEARCH(x, k) thet 2 while i= nfx] and k > key] 3doini+1 4 if i n[x] and k = keyfx] 5 then return (x, /) 6 if leaf(x] 7 then return NIL 8 else DISK-READ(ci]) 9 retum B-TREE-SEARCH(o[x], k) Page 20 I M-Tech I Sem CSE ADS &A Unit-5 (R19) (2) Comparison of Search Tre Prepared by: Dr. Md. Umar khan, KHIT, Guntur > There are many different search trees. They are different in nature. The basic search tree is Binary Search Tree (BST). Some other search trees are AVL tree, B tree, Red-Black tree, splay tree et. ¥ These trees can be compares based on their operations. We will see the time complexity of these trees Search Tree Binary Search Tree AVL tree BTree Red-Black Tree Splay Tree Search Tree Binary Search Tree AVL tree B Tree Red-Black Tree Splay Tree Insert O(log n) O(log n) Ovlog n) O(log n) O(log n) Insert On) O(log n) O(log n) Ovlog n) O(log n) Average Case Delete Search O(log n) O(log n) Ofloge n) O(log n) Oflog n) O(log n) O(log n) O(log n) Ofloge n) O(log n) Worst Case Delete Search O(n) O(n) Ofloge n) O(log n) Otlog n) O(log n) O(log n) O(log n) O(log2 n) O(log n) Page 21

You might also like