Você está na página 1de 12

Balanced Search Trees

CSC 3102

B.B. Karki, LSU

Why Balancing Search Trees


n Binary search tree (BST): F Its nodes contain the elements of a set of orderable items, one element per node, so that all elements in the left subtree are smaller than the element in the subtrees root and all the elements in the right tree greater than it. n With BST, the time of efficiency of searching, insertion, and deletion are

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

B.B. Karki, LSU

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

B.B. Karki, LSU

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

Not AVL tree


0

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

Balancing AVL Trees


n Whenever we detect that a tree has become unbalanced, we must rebalance it. F Transform tree to a balanced one by a rotation F Rotation is a local transformation of the subtree rooted at an out-of-balance node. n Four types of rotations (corresponding to the four cases of unbalanced trees) F Singe right (R) rotation: Left of left F Single left (L) Rotation: Right of right F Double left-right (LR) rotation: Right of left F Double right-left (RL) rotation: Left of right
2 -2 2 -2

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

n Rotates the out-of-balance

3
1 0

2
0

node to the right


n Single R-rotation in its

2
0

more general form.


F A new node is inserted

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

B.B. Karki, LSU

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

B.B. Karki, LSU

L-Rotation and RL-Rotation


-2 0

1
-1

L
0

2
n Single L-rotation and double

1 3

RL-rotation are mirror images of R-rotation and LR-rotation, respectively.


-2

1
1

RL
0

3
0

2
CSC 3102
8

B.B. Karki, LSU

Construction of an AVL Tree


n Construct an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive

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

B.B. Karki, LSU

Efficiency of AVL Tree


n The trees height is bounded both above and below by logarithmic functions

log 2 n h 1.4405log 2 (n + 2) -1.3277


n The inequalities imply that the operations of searching and insertion (also

deletion) are Q(log n) in the worst case.

n Exact formula for the height of an AVL tree (derived from extensive

experiments): 1.01log2 n + 0.1


n Drawbacks: F Involve frequent rotations F Need to maintain balances for the trees nodes (extra storage) F Difficult deletion operation.

CSC 3102

10

B.B. Karki, LSU

AVL Abstract Data Type


n AVL data structure: Implementation

requires two types of structures:


F Head structure (AVL_TREE) F Data node (NODE) 4 Balance factors are LH = left

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

NODE key data leftSubTree rightSubTree bal end NODE

<keyType> <dataType> <node ponter> <node pointer> <LH, EH, RH>

CSC 3102

11

B.B. Karki, LSU

AVL Basic Algorithm: Create AVL Tree


n The function AVL_Create allocates dynamic memory for an

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

to null, and stores the address of the compare function.

AVL_TREE *AVL_Create (int (*compare) (void *argu1, void *argu2)

CSC 3102

12

B.B. Karki, LSU

AVL Basic Algorithm: AVL Tree Insert


n All inserts take place at a leaf node. n First find the appropriate leaf node
Algorithm AVLInsert (root, newPtr, taller) If (root null) taller = true root = newPtr else if (newPtr->key < root->key) AVLInsert(root->left, newPtr, taller) if (taller) if (root LH) leftBalance(root, taller) elseif (root RH) taller = false adjust B else(newPtr->key < root->key) AVLInsert(root->right, newPtr, taller) if (taller) if (root LH) taller = false elseif (root RH) rightBalance(root, taller) adjust B else error (Duplicate Data) recycle (newPtr) taller = false return
13

with a recursive call, and then connect it to its parent.


F Set the taller flag whenever a tree

has potentially grown higher. n As we back out of the tree,

constantly check the balance of each node by testing the flag.


n When a node is out-of-balance,

balance it by calling either left balance or right balance, and then continue up the tree.

CSC 3102

B.B. Karki, LSU

AVL Left Balance Algorithm


n It is called when the root (the
Algorithm leftBalance (root, taller) leftTree = root->left If (leftTree LH) /* Case 1: Left of left */ rotateRight (root) adjust B taller = false Else /* Case 2. Right of left */ rightTree = leftTree->right adjust B rotateLeft (leftTree) rotateRight (root) taller false return

parent) is left heavy.


F The tree has grown taller,

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

the root and the appropriate subtree pointers.


F rotateRight(root) F rotateLeft(root)

Algorithm rotateRight (root) tempPtr = root->left root->left = tempPtr->right tempPtr-> = root root = tmpPtr return
14

CSC 3102

B.B. Karki, LSU

AVL Basic Algorithm: AVL Tree Delete


n

All deletes take place at a leaf node.


F First find the node to be deleted, and

determine if the delete node is a leaf or not n

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

B.B. Karki, LSU

Delete Right Balance Algorithm


n If the subtree is shorter after a

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

B.B. Karki, LSU

AVL Tree Data Processing


n Retrieve AVL Tree Node F Searches the tree for the node containing the required key by following the left-right structure of the tree F When it is located, the pointer to its data is returned to the calling function. If the node is not found, a null pointer is returned. F Two functions: 4 An application interface function (AVL_Retrieve) provides a pointer to the tree and a pointer to the key. 4 ADT recursive retrieve function (_retrieve) actually locates the data. n Traverse AVL Tree F Processes the tree using inorder (LNR) traversal. F Two functions: 4 An application interface function (AVL_Traverse) passes the address of the application-dependent function (process) that processes the data. It uses only one parameter, the address of the node to be processed. 4 ADT internal function (_traversal) actually traverses the tree.

CSC 3102

17

B.B. Karki, LSU

AVL Tree Utility Functions


n AVL Empty Tree
F Checks the tree count. If it is zero (empty), it returns true; otherwise, it

returns false.

n AVL Full Tree


F Returns true if there is no room (no enough memory) for another node and

false if there is room.

n AVL Count
F Returns the number of nodes (tree count) currently in the tree.

n Destroy AVL Tree


F Deletes all data in tree and recycles memory. F Because we need to traverse the tree to find all of the data and nodes that

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

B.B. Karki, LSU

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

the roots three subtrees based on the comparison.


n Repeat the above process recursively until the search key K is

found.

CSC 3102

20

B.B. Karki, LSU

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

B.B. Karki, LSU

Construction of a 2-3 Tree


n Construct a 2-3 tree for the list 9, 5, 8, 3, 2, 4 and 7 by successive

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

Efficiency of 2-3 Tree


n As for any search tree, the efficiency depends on the trees height, h. n Upper bound for the height:

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

B.B. Karki, LSU

12

Você também pode gostar