Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CSC 3102
all in Q(log n)
F This is true only in the average case F In worst case, the efficiency is Q(n) F How to avoid the worst-case degeneracy for BST?
n Balancing a BST can lead to significant performance improvements. F Find a structure that preserves the good properties of the classical BST. F Given a tree with 1000 nodes, the worst case is only 10 for a nearly complete tree.
CSC 3102
Balancing Approaches
n Two approaches to balance the search trees: n Instance simplification:
Transform an unbalanced binary search tree to a balanced one. F AVL tree F Red-black tree F If insertion/deletion violates the balance requirement, the tree is restructured by rotation to restore the balance required.
n Representation change:
Allow more than one element in a node of a search tree. F 2-3 trees, 2-3-4 trees, and B-trees F They differ in the number of elements admissible in a single node but all are perfectly balanced.
CSC 3102
AVL Trees
n An AVL tree is a height-balanced binary search tree F The balance factor (B), which is defined as the difference between the heights of the nodes left and right subtrees, of every node is either 0 or +1 or -1. |B| = |HL - HR| <= 1 F AVL trees were invented in 1962 by G.M. Adelson-Velsky and E.M. Landis.
B=1
10 AVL tree
0 1
10
0
5
1 -1 0
20
1
5
-1
20
4
0
7
0
12
0
7
0
2
CSC 3102
8
4
8
B.B. Karki, LSU
3
1
1
-1 -1
3 1
0 Right of left
1
1
2
0
3
Left of right 0
1
CSC 3102
Left of left
Right of right
5
2
B.B. Karki, LSU
R-Rotation
2 0
3
1 0
2
0
2
0
into the left subtree of the left child of a tree whose root had the balance of +1 before the insertion.
1
0
r c T3 T1 T2 T1
c r
T1
T3
CSC 3102
LR-Rotation
n A combination of two rotations: F Perform L-rotation of the left subtree of root r followed by the R-rotation of the new tree rooted at r. n General form: F a new node is inserted into the right subtree of the left child of a tree whose root had the balance of +1 before the +2 insertion.
2 0
3
-1 0
2
0
1
0
1 2
0
g c r
c g T1 T2 T3 T4 T1
T2
T3 T4
CSC 3102
1
-1
L
0
2
n Single L-rotation and double
1 3
1
1
RL
0
3
0
2
CSC 3102
8
insertions. F As you trace the algorithms operations, if there are several nodes with +/-2 balance, the rotation is done for the tree rooted at the unbalanced node that is closest to the newly inserted leaf.
5 AVL tree 3 7
CSC 3102
n Exact formula for the height of an AVL tree (derived from extensive
CSC 3102
10
AVL_TREE count <integer> root <node pointer> compare <pointer> end AVL_TREE
high, EH = even high, RH = right high n AVL functions to build and maintain
AVL trees:
F Three AVL Basic algorithms F Two AVL tree data processing F Four AVL tree utility functions
CSC 3102
11
AVL tree head node and return its address (the tree pointer) to the caller.
n Allocates the structure and sets its count to zero, the root pointer
CSC 3102
12
balance it by calling either left balance or right balance, and then continue up the tree.
CSC 3102
the parent is LH, and we are on a left branch. F Decide the case (1 or 2) examining B of the left subtree. n To rotate a node, exchange
Algorithm rotateRight (root) tempPtr = root->left root->left = tempPtr->right tempPtr-> = root root = tmpPtr return
14
CSC 3102
If there is no left subtree, connect the right tree to the parent If there is no right subtree, connect the left subtree to the parent. If there are both left subtree and right subtree, find a node to take place the delete nodes place.
F Loop to find the largest node on the left
subtree and copy its data to the delete node. F Continue the delete at the deleted nodes left subtree, this time looking for the substitute data we copied to replace the deleted data. F If the tree is shorter, balance it.
CSC 3102
15
Algorithm AVLDelete(root, delKey, shorter) If (deleteKey < root->key) success = AVLDelete(root->left, .., ..) if (shorter) deleteRightBalance(root, shorter) return success elseif (deleteKey > root->key) success = AVLDelete(root->right, .., ..) if (shorter) deleteLeftBalance(root, shorter) return success else deleteNode = root If (no left subtree) root = root->right shorter = true recycle (deleteNode) return success else if (no right subtree) .. else exchPtr = root->left loop (exchPtr not null) exchPtr = exchPtr->right root->data = exchPtr-> data AVLDelete(root->left, exchPtr->data.key, ..) if (shorter) dltRightBalance (root, shorter) return success true return
deletion on the left branch, decide if we need to balance the tree or not. F When the root is LH or EH, no balance is needed.
n Balance is needed if we deleted on
the left and the tree was already RH. Now its doubly RH so rotate the right subtree to the left. F If the right subtree is LH, rotate twice, first to the right and then to the left and update Bs. F If the right subtree is not LH, rotate only once.
Algorithm dltRightBalance (root, shorter) If (root LH) root->B = EH elseif (root EH) root->B = RH shorter = false else rightTree = root->right if (rightTree LH) leftTree = rightTree->left leftTree->B = EH rotateRight (rightTree) rotateLeft (root) else if (rightTree EH) root->B = RH rightTree->B = LH shorter = false else root->B = EH rightTree->B = EH rotateLeft (root) return
16
CSC 3102
CSC 3102
17
returns false.
n AVL Count
F Returns the number of nodes (tree count) currently in the tree.
need to be deleted, the application interface function (AVL_Destroy) calls a recursive function (_destroyAVL) to do the physical deletion. F It returns null head pointer.
CSC 3102
18
2-3 Trees
n Balance a search tree by allowing more than one key
in the same node. n The simplest case is a 2-3 tree, in which non-root nodes can be of two kinds: 2-nodes and 3-nodes.
F A 2-node contains a single key K and has two children: 4 Left child serves as the root of a subtree whose keys
are less than K 4 Right child serves as the root of a subtree whose keys are greater than K. F A 3-node contains two ordered keys K1 and K2 (K1 < K2) and has three children 4 Leftmost child serves as the root of a subtree with keys less than K1 4 Middle child serves as the root of a subtree with keys between K1 and K2 4 Rightmost child serves as the root of a subtree with keys greater than K2 F A 2-3 tree is always height-balanced: F 2-3 tress were introduced by John Hopcroft in 1970.
CSC 3102
19
<K
>K
2-node
K1, K2
< K1
(K1,K2)
>K2
3-node
B.B. Karki, LSU
Searching
n Start with root. n If the root is a 2-node, act as if it were a binary search tree n If the root is a 3-node, need no more than two key comparisons F Stop if K is equal to one of the roots keys or continue in one of
found.
CSC 3102
20
10
Insertion
n Always insert a new key K at a leaf. n First find the appropriate leaf by performing a search for K. n If the leaf in a question is a 2-node, insert K there as either the first or the
second key, depending on whether K is smaller or larger than the nodes old key.
n If the leaf is a 3-node, i.e., when the leaf node is full, we have a condition
known as overflow. In such case, we split the leaf in two: F Smallest of the three keys (two old ones and the new key) is put in the first leaf F Largest key is put in the second leaf F Middle key is promoted to the old leafs parent.
CSC 3102
21
insertions. F Whenever the new key K goes to a 3-node leaf, split the node in two F Promotion of a middle key to its parent can cause the parents overflow.
5 2-3 tree 3 8
2
CSC 3102
22
9
B.B. Karki, LSU
11
A 2-3 tree of height h with the smallest number of keys is a full tree of 2-nodes n 1 +2 + . + 2h = 2h+1 - 1 so h log2 (n+1) -1
n Lower found for the height:
A 2-3 tree of height h with the largest number of keys is a full tree of 3-nodes, each two keys and three children: n 2.1 +2.3 + . + 2.3h = 3h+1 - 1 so h log3 (n+1) -1
n These bounds on h,
log3 (n+1) -1 h log2 (n+1) -1 imply that the time efficiencies of searching, insertion, and deletion are all in Q(log n) in both the worst and average case.
CSC 3102
23
12