Download presentation
Presentation is loading. Please wait.
Published byDevi Darmali Modified over 6 years ago
1
CS202 - Fundamental Structures of Computer Science II
lec06-balancedtrees November 20, 2018 Balanced Search Trees The height of a binary search tree is sensitive to the order of insertions and deletions. The height of a binary search tree is between log2(N+1) and N. So, the worst case behavior of some BST operations are O(N). There are various search trees that can retain their balance despite insertions and deletions. AVL Trees 2-3 Trees 2-3-4 Trees Red-Black Trees In these height balance search trees, the run time complexity of insertion,deletion and retrieval operations will be O(log2N) at worst case. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
2
CS202 - Fundamental Structures of Computer Science II
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates the ideal tree (completely balanced tree). AVL Tree maintains a height close to the minimum. Definition: An AVL tree is a binary search tree such that for any node in the tree, the height of the left and right subtrees can differ by at most 1. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
3
CS202 - Fundamental Structures of Computer Science II
AVL Tree Examples NOT an AVL tree (unbalanced nodes are darkened) an AVL tree 11/20/2018 CS202 - Fundamental Structures of Computer Science II
4
A Minimum Tree of height H
11/20/2018 CS202 - Fundamental Structures of Computer Science II
5
CS202 - Fundamental Structures of Computer Science II
AVL Tree Properties The depth of a typical node in an AVL tree is very close to the optimal log2N. Consequently, all searching operations in an AVL tree have logarithmic worst-case bounds. An update (insert or delete) in an AVL tree could destroy the balance. It must then be rebalanced before the operation can be considered complete. After an insertion, only nodes that are on the path from the insertion point to the root can have their balances altered. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
6
CS202 - Fundamental Structures of Computer Science II
Balance Operations Balance is restored by tree rotations. There are 4 cases that we might have to fix. Single Right Rotation Single Left Rotation Double Right-Left Rotation Double Left-Right Rotation 11/20/2018 CS202 - Fundamental Structures of Computer Science II
7
CS202 - Fundamental Structures of Computer Science II
Single Rotation A single rotation switches the roles of the parent and child while maintaining the search order. We rotate between a node and its child (left or right). Child becomes parent. Parent becomes right child in case 1, left child in case 2. The result is a binary search tree that satisfies the AVL property. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
8
Case 1 – Single Right Rotation
11/20/2018 CS202 - Fundamental Structures of Computer Science II
9
Case 1 – Single Right Rotation – Example
11/20/2018 CS202 - Fundamental Structures of Computer Science II
10
Case 2 – Single Left Rotation
Before Rotation After Rotation 11/20/2018 CS202 - Fundamental Structures of Computer Science II
11
Case 3 –Double Right-Left Rotation
The height of B (or C) is same as the height of D 11/20/2018 CS202 - Fundamental Structures of Computer Science II
12
Case 4 –Double Left-Right Rotation
The height of B (or C) is same as the height of A 11/20/2018 CS202 - Fundamental Structures of Computer Science II
13
Case 4 –Double Left-Right Rotation – Example
11/20/2018 CS202 - Fundamental Structures of Computer Science II
14
CS202 - Fundamental Structures of Computer Science II
Insertion – Analysis Single rotation preserves the original height: The new height of the entire subtree is exactly the same as the height of the original subtree before the insertion. Therefore it is enough to do rotation only at the first node, where imbalance exists, on the path from inserted node to root. Thus the rotation takes O(1) time. Hence insertion is O(logN) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
15
CS202 - Fundamental Structures of Computer Science II
AVL Tree -- Deletion Deletion is more complicated. Deletion of a node x from an AVL tree requires the same basic ideas, including single and double rotations, that are used for insertion. We may need more than one rebalance on the path from deleted node to root. Deletion is O(logN) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
16
AVL Tree – Deletion Method
First delete the node (to be deleted) same as the deletion in a binary search tree. Leaf Node : Delete the node Node with Single Child: Delete the node, the parent of the deleted node points the single child of the deleted node after deletion. Node with Two Children: Find the inorder successor of the node (to be deleted) and swap them. Then delete the inorder successor. Walk through from the deleted node back to the root, and rebalance the nodes on the path if it is required. We’ll trace the effects of this deletion on height through all the nodes on the path from the deleted node x back to the root. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
17
AVL Tree – Deletion Method (cont.)
A boolean variable shorter shows if the height of a subtree has been shortened. Each node of the AVL tree is associated a balance factor. left high -- left subtree has height greater than right subtree right high -- right subtree has height greater than left subtree equal -- left and right subtrees have same height The action to be taken at each node depends on the value of shorter balance factor of the node sometimes the balance factor of a child of the node. shorter is initially true. The following steps are to be done for each node p on the path from the parent of x to the root, provided shorter remains true. When shorter becomes false, the algorithm terminates. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
18
CS202 - Fundamental Structures of Computer Science II
Case 1 Case 1: The current node p has balance factor equal. Change the balance factor of p to right-high. shorter becomes false \ p T2 T1 p deleted T2 T1 No rotations Height unchanged 11/20/2018 CS202 - Fundamental Structures of Computer Science II
19
CS202 - Fundamental Structures of Computer Science II
Case 2 Case 2: The balance factor of p is not equal, and the taller subtree was shortened. Change the balance factor of p to equal Leave shorter true. / p deleted T2 T1 p T2 T1 No rotations Height reduced 11/20/2018 CS202 - Fundamental Structures of Computer Science II
20
CS202 - Fundamental Structures of Computer Science II
Case 3 Case 3: The balance factor of p is not equal, and the shorter subtree was shortened. Rotation is needed. Let q be the root of the taller subtree of p. We have three sub-cases according to the balance factor of q. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
21
CS202 - Fundamental Structures of Computer Science II
Case 3a Case 3a: The balance factor of q is equal. Apply a single rotation shorter becomes false. height unchanged / q T3 T2 T1 \ p h-1 h q T3 T2 deleted T1 \ p h-1 h 11/20/2018 CS202 - Fundamental Structures of Computer Science II
22
CS202 - Fundamental Structures of Computer Science II
Case 3b Case 3b: The balance factor of q is the same as that of p. Apply a single rotation Set the balance factors of p and q to equal leave shorter as true. height reduced p \ q T3 T2 deleted T1 h-1 h q - T3 T1 p h-1 h T2 11/20/2018 CS202 - Fundamental Structures of Computer Science II
23
CS202 - Fundamental Structures of Computer Science II
Case 3c Case 3c: The balance factors of p and q are opposite. Apply a double rotation set the balance factors of the new root to equal leave shorter as true. height reduced h-1 q T3 T1 r T2 h-1 or h-2 p - T4 / q T3 T1 r h-1 T2 h-1 or h-2 p \ h-1 T4 11/20/2018 CS202 - Fundamental Structures of Computer Science II
24
CS202 - Fundamental Structures of Computer Science II
Example Delete o. m p e c j n s k d h r u b o a g i l t f 11/20/2018 CS202 - Fundamental Structures of Computer Science II
25
Worst Cases of AVL Trees
H 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 30 40 50 minN 33 54 88 143 232 376 609 986 1.596 2.583 4.180 6.764 10.945 17.710 logN 2,81 3,58 4,32 5,04 5,75 6,46 7,16 7,86 8,55 9,25 9,95 10,64 11,33 12,03 12,72 13,42 14,11 21,05 28,00 34,94 H / logN 1,42 1,39 1,40 1,41 1,43 What is the minumum number of nodes in a height N AVL tree? minN(0) = 0 minN(1) = 1 minN(2) = 2 minN(3) = 4 . minN(h) = minN(h-1) + minN(h-2) + 1 Maximum height of a N-node AVL tree is less than 1.44 logN 11/20/2018 CS202 - Fundamental Structures of Computer Science II
26
CS202 - Fundamental Structures of Computer Science II
2-3 Trees Definition : A 2-3 tree is a tree in which each internal node has either two or three children, and all leaves are at the same level. A node with two children is called a 2-node. A node with three children is called a 3-node. A 2-3 is not a binary tree. A 2-3 tree is never taller than a minimum-height binary tree. A 2-3 tree with N nodes never has height greater than log2(N+1) A 2-3 tree of height h always has at least 2h-1 nodes. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
27
2-3 Tree Example (height 3)
11/20/2018 CS202 - Fundamental Structures of Computer Science II
28
CS202 - Fundamental Structures of Computer Science II
2-3 Tree T is a 2-3 tree of height h if T is empty (a 2-3 tree of height 0), or T is of the form r TL TR where r is a node that contains one data item and TL and TR are both 2-3 trees, each of height h-1.The search key in r must be greater than each key in TL and smaller than each key in TR, or TL TM TR where r is a node that contains two data items and TL , TM and TR are 2-3 trees, each of height h-1.The smaller search key in r must be greater than each key in TL and smaller than each key in TM, the larger search key in r must be greater than each key in TM and smaller than each key in TR. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
29
Nodes in a 2-3 tree: (a) a 2-node; (b) a 3-node
11/20/2018 CS202 - Fundamental Structures of Computer Science II
30
CS202 - Fundamental Structures of Computer Science II
A 2-3 tree – Example 11/20/2018 CS202 - Fundamental Structures of Computer Science II
31
C++ class to describe a 2-3 tree node
class TreeNode { private: TreeItemType smallItem, largeItem; TreeNode *leftChildPtr, *midChildPtr, *rightChildPtr; // friend class-can access private class members friend class TwoThreeTree; }; // end TreeNode When a node contains only one data item, we can place it in smallItem and use leftChildPtr and midChildPtr to point to the node’s children. We place NULL in rightChildPtr. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
32
CS202 - Fundamental Structures of Computer Science II
Traversing a 2-3 Tree We can traverse a 2-3 tree in a sorted search-key order by performing an inorder traversal. Leaf Node: Visit that node. 2-node : Visit left subtree, Visit search key in the 2-node, Visit right subtree. 3-node: Visit smaller search key in 3-node, Visit middle subtree, Visit larger search key in 3-node, 11/20/2018 CS202 - Fundamental Structures of Computer Science II
33
CS202 - Fundamental Structures of Computer Science II
Searching a 2-3 Tree Searching a 2-3 tree is similar to searching a binary search tree. When considering a 3-node, we have to compare two values stored in that 3-node with the searched key, and we may search one of three subtrees of this node as a result of these comparisons. We can search the 2-3 tree and the shortest binary search tree with approximately the same efficiency. A binary search tree with N nodes cannot be shorter than log2(N+1) A 2-3 tree with N nodes cannot be taller than log2(N+1) A node in a 2-3 has at most two items. Searching a 2-3 tree is O(log N) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
34
A balanced binary search tree; (b) a 2-3 tree with the same elements
11/20/2018 CS202 - Fundamental Structures of Computer Science II
35
Inserting into a 2-3 tree While we insert items into a 2-3 tree,
its shape is maintained. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
36
After inserting 39 Insertion into a 2-node leaf is simple 11/20/2018
CS202 - Fundamental Structures of Computer Science II
37
(a), (b) The steps for inserting 38; (c) the resulting tree
Insertion into a 3-node causes it to divide. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
38
After inserting 37 Insertion into a 2-node leaf is simple 11/20/2018
CS202 - Fundamental Structures of Computer Science II
39
CS202 - Fundamental Structures of Computer Science II
inserting 36 11/20/2018 CS202 - Fundamental Structures of Computer Science II
40
CS202 - Fundamental Structures of Computer Science II
inserting 36 (cont.) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
41
The tree after the insertion of 35, 34, and 33
11/20/2018 CS202 - Fundamental Structures of Computer Science II
42
Insertion Algorithm - Splitting a leaf in a 2-3 tree
11/20/2018 CS202 - Fundamental Structures of Computer Science II
43
Splitting an internal node in a 2-3 tree
11/20/2018 CS202 - Fundamental Structures of Computer Science II
44
Splitting the root of a 2-3 tree
11/20/2018 CS202 - Fundamental Structures of Computer Science II
45
CS202 - Fundamental Structures of Computer Science II
Deleting from a 2-3 tree Deletion strategy is the inverse of insertion strategy. We merge the nodes when they become empty. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
46
CS202 - Fundamental Structures of Computer Science II
Delete 70 – Steps 11/20/2018 CS202 - Fundamental Structures of Computer Science II
47
CS202 - Fundamental Structures of Computer Science II
Delete 70 – Steps (cont.) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
48
Delete 70 – Steps (cont.) – Resulting Tree
11/20/2018 CS202 - Fundamental Structures of Computer Science II
49
CS202 - Fundamental Structures of Computer Science II
Delete 100 11/20/2018 CS202 - Fundamental Structures of Computer Science II
50
CS202 - Fundamental Structures of Computer Science II
Delete 100 (cont.) 11/20/2018 CS202 - Fundamental Structures of Computer Science II
51
CS202 - Fundamental Structures of Computer Science II
The steps for deleting 80 11/20/2018 CS202 - Fundamental Structures of Computer Science II
52
The steps for deleting 80 (cont.)
11/20/2018 CS202 - Fundamental Structures of Computer Science II
53
The steps for deleting 80 (cont.)
11/20/2018 CS202 - Fundamental Structures of Computer Science II
54
CS202 - Fundamental Structures of Computer Science II
Deletion Algorithm To delete an item X from a 2-3 tree: First locate the node n contains X. If n is not a leaf, we find X’s inorder successor and swap it with X. As a result of the swap, the deletion always begin at a leaf. If the leaf contains an item in addition to X, we simply delete X from that leaf, and we are done. But, if the leaf only contains only X, deleting X would leave the leaf without a data item. In this case, we must perform some additional work to complete the deletion. Depending on the empty node and its siblings, we perform certain operations: Delete empty root Merge nodes Redistribute values These operations can be repeated until the root starting from the leaf. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
55
Deletion-- (a) Redistributing values; (b) merging a leaf
11/20/2018 CS202 - Fundamental Structures of Computer Science II
56
CS202 - Fundamental Structures of Computer Science II
Deletion -- (c) redistributing values and children; (d) merging internal nodes; 11/20/2018 CS202 - Fundamental Structures of Computer Science II
57
Deletion -- (e) deleting the root
11/20/2018 CS202 - Fundamental Structures of Computer Science II
58
CS202 - Fundamental Structures of Computer Science II
2-3 Tree Analysis We can use 2-3 in the implementation of ADT table. A 2-3 tree is always balanced. Insertion and deletion operations are O(log N) in a 2-3 tree implementation of a table. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
59
CS202 - Fundamental Structures of Computer Science II
2-3-4 Tree A tree is like a 2-3 tree, but it allows 4-nodes, which are nodes that have four children and three data items. There is a close relation between trees and red-black trees. 2-3-4 trees are also known as 2-4 trees in other books. 2-3-4 trees and 2-4 trees are members of M-way search tree families. In M-way search tree, a node have maximum M children. Although a tree has more efficient insertion and deletion operations than a 2-3 tree, a tree has greater storage requirenments. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
60
CS202 - Fundamental Structures of Computer Science II
A Tree Example 11/20/2018 CS202 - Fundamental Structures of Computer Science II
61
CS202 - Fundamental Structures of Computer Science II
A Tree T is a tree of height h if T is empty (a tree of height 0), or T is of the form r TL TR where r is a node that contains one data item and TL and TR are both trees, each of height h-1.The search key in r must be greater than each key in TL and smaller than each key in TR, or TL TM TR where r is a node that contains two data items and TL , TM and TR are trees, each of height h-1.The smaller search key in r must be greater than each key in TL and smaller than each key in TM, the larger search key in r must be greater than each key in TM and smaller than each key in TR. TL TML TMR TR where r is a node that contains three data items and TL , TML , TMR , and TR are trees, each of height h-1. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
62
CS202 - Fundamental Structures of Computer Science II
Nodes in a Tree 3-Node 2-Node 4-Node 11/20/2018 CS202 - Fundamental Structures of Computer Science II
63
CS202 - Fundamental Structures of Computer Science II
A node in a tree – C++ class TreeNode { private: TreeItemType smallItem, middleItem, largeItem; TreeNode *leftChildPtr, *lMidChildPtr, *rMidChildPtr, *rightChildPtr; friend class TwoThreeFourTree; }; // end TreeNode 11/20/2018 CS202 - Fundamental Structures of Computer Science II
64
CS202 - Fundamental Structures of Computer Science II
Operations of a Tree Searching and traversing algorithms for a tree are similar to the 2-3 algorithms. For a tree, we can also use similar insertion and deletion algorithms that are used for 2-3 trees. But we can also use a little bit different insertion and deletion algorithms for trees. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
65
CS202 - Fundamental Structures of Computer Science II
2-3-4 Tree Insertion Splits a 4-nodes by moving one of its items up to its parent node. For a 2-3 tree, the insertion algorithm traces a path from the root to a leaf and then backs up from the leaf as it splits nodes. To avoid this return path after reaching a leaf, the insertion algorithm for a tree splits 4-nodes as soon as it encounters them on the way down the tree from the root to a leaf. As a result, when a 4-node is split and an item is moved up to node’s parent, the parent cannot possibly be a 4-node and so can accommodate another item. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
66
2-3-4 Tree Insertion - Example
Inserting 20. Split 4-nodes as they are encountered 11/20/2018 CS202 - Fundamental Structures of Computer Science II
67
CS202 - Fundamental Structures of Computer Science II
After inserting 50 and 40 11/20/2018 CS202 - Fundamental Structures of Computer Science II
68
The steps for inserting 70
11/20/2018 CS202 - Fundamental Structures of Computer Science II
69
CS202 - Fundamental Structures of Computer Science II
After inserting 80 and 15 11/20/2018 CS202 - Fundamental Structures of Computer Science II
70
The steps for inserting 90
11/20/2018 CS202 - Fundamental Structures of Computer Science II
71
The steps for inserting 100
11/20/2018 CS202 - Fundamental Structures of Computer Science II
72
Splitting 4-nodes during insertion
We split each 4-node as son as we encounter it during our search from the root to a leaf that will accommodate the new item to be inserted. The 4-node which will be split will: be the root, or have a 2-node parent, or have a 3-node parent. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
73
Splitting a 4-node root during insertion
11/20/2018 CS202 - Fundamental Structures of Computer Science II
74
Splitting a 4-node whose parent is a 2-node during insertion
11/20/2018 CS202 - Fundamental Structures of Computer Science II
75
Splitting a 4-node whose parent is a 3-node during insertion
11/20/2018 CS202 - Fundamental Structures of Computer Science II
76
CS202 - Fundamental Structures of Computer Science II
Deleting from a tree For a 2-3 tree, the deletion algorithm traces a path from the root to a leaf and then backs up from the leaf as empty nodes appear to restructure the tree. To avoid this return path after reaching a leaf, the deletion algorithm for a tree transforms each 2-node into either 3-node or 4-node as soon as it encounters them on the way down the tree from the root to a leaf. If an adjacent sibling is 3-node or 4-node, transfer an item from that sibling to our 2-node. If there is adjacent sibling is 2-node, merge them. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
77
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees A tree requires more storage than a binary search tree. A special binary search tree, red-black-tree, can be used to represent a tree, so that we can retain advantages of a tree without a storage overhead. 3-node and 4-nodes in a tree is represented by a binary tree. To distinguish the original 2-nodes from 2-nodes are generated from 3-nodes and 4-nodes, we use red and black pointers. All original pointers in a tree are black pointers, use red pointers for child pointers to link 2-nodes that result from the split of 3-nodes and 4-nodes. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
78
Red-Black Trees Properties
The children of red node (pointed by a red pointer) are black nodes (pointed by black pointer) All external nodes (leaves and nodes with a single child) have the same black pointers on the path from the root to that external node. 11/20/2018 CS202 - Fundamental Structures of Computer Science II
79
Red-black representation of a 4-node
11/20/2018 CS202 - Fundamental Structures of Computer Science II
80
Red-black representation of a 3-node
11/20/2018 CS202 - Fundamental Structures of Computer Science II
81
A 2-3-4 Tree and Its Corresponding Red-Black Tree
11/20/2018 CS202 - Fundamental Structures of Computer Science II
82
A Node in a Red-Black Tree
enum Color {RED, BLACK}; class TreeNode { private: TreeItemType Item; TreeNode *leftChildPtr, *rightChildPtr; Color leftColor, rightColor; friend class RedBlackTree; }; // end TreeNode 11/20/2018 CS202 - Fundamental Structures of Computer Science II
83
Splitting a red-black representation of a 4-node that is the root
11/20/2018 CS202 - Fundamental Structures of Computer Science II
84
CS202 - Fundamental Structures of Computer Science II
Splitting a red-black representation of a 4-node whose parent is a 2-node 11/20/2018 CS202 - Fundamental Structures of Computer Science II
85
CS202 - Fundamental Structures of Computer Science II
Splitting a red-black representation of a 4-node whose parent is a 3-node 11/20/2018 CS202 - Fundamental Structures of Computer Science II
86
CS202 - Fundamental Structures of Computer Science II
Splitting a red-black representation of a 4-node whose parent is a 3-node 11/20/2018 CS202 - Fundamental Structures of Computer Science II
87
CS202 - Fundamental Structures of Computer Science II
Splitting a red-black representation of a 4-node whose parent is a 3-node 11/20/2018 CS202 - Fundamental Structures of Computer Science II
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.