Sie sind auf Seite 1von 30

Data Structures & Algorithm Analysis

Week 9
Balance Search Trees
Objectives:

 At the end of this lesson, the student will be able to:


 perform a rotation to restore the balance of an AVL tree after an
addition.
 Create B-tree
AVL Trees
 A binary search tree
 Whenever it becomes unbalanced
 Rearranges its nodes to get a balanced binary search tree
 Balance of a binary search tree upset when
 Add a node
 Remove a node
 Thus during these operations, the AVL tree must rearrange
nodes to maintain balance
AVL Trees

Fig. 1 : After inserting (a) 60; (b) 50; and (c) 20 into
an initially empty binary search tree, the tree is not
balanced; d) a corresponding AVL tree rotates its
nodes to restore balance.
AVL Trees

Fig. 2 : (a) Adding 80 to tree in Fig. 1 does not


change the balance of the tree;
(b) a subsequent addition of 90 makes tree
unbalanced; (c) left rotation restores balance.
Single Rotations

Fig. 3 : Before and after an addition to an AVL subtree that


requires a right rotation to maintain its balance.
Single Rotations

Fig. 4 : Before and after an addition to an AVL subtree that


requires a left rotation to maintain balance.
Single Rotations

 Algorithm for right rotation


Algorithm rotateRight(nodeN)
nodeC = left child of nodeN
Set nodeN’s left child to nodeC’s right child
Set nodeC’s right child to nodeN
 Algorithm for left rotation

Algorithm rotateLeft(nodeN)
nodeC = right child of nodeN
Set nodeN’s right child to nodeC’s left child
Set nodeC’s left child to nodeN
Double Rotations

Fig. 5 : (a) Adding 70 to the tree in Fig. 2(c) destroys its


balance; to restore the balance, perform both
(b) a right rotation and (c) a left rotation.
Double Rotations

Fig. 6 : (a-b) Before and after an addition to an


AVL subtree that requires both a right rotation
and a left rotation to maintain its balance.
Double Rotations

Fig. 6 : (c-d) Before and after an addition to an


AVL subtree that requires both a right rotation
and a left rotation to maintain its balance.
Double Rotations
 Algorithm to perform the right-left double rotation
illustrated in Fig. 6

Algorithm rotateRightLeft(nodeN)
nodeC = right child of nodeN
Set nodeN’s right child to the
subtree produced by rotateRight(nodeC)
rotateLeft(nodeN)
Double Rotations

Fig. 7 : (a) The AVL tree in Fig. 5 after additions


that maintain its balance; (b) after an addition that
destroys the balance … continued →
Double Rotations

Fig. 7 (ctd.) : (c) after a left rotation;


(d) after a right rotation.
Double Rotations

Fig. 8 : Before and


after an addition to an
AVL subtree that
requires both a left
and right rotation to
maintain its balance.
Double Rotations
 Algorithm to perform the left-right double rotation
illustrated in Fig. 8

Algorithm rotateLeftRight(nodeN)
nodeC = left child of nodeN
Set nodeN’s left child to the
subtree produced by rotateLeft(nodeC)
rotateRight(nodeN)
Double Rotations
 An imbalance at node N of an AVL tree can be corrected by a
double rotation if
 The addition occurred in the left subtree of N's right child (left-
right rotation) or …
 The addition occurred in the right subtree of N's left child
(right-left rotation)
 A double rotation is accomplished by performing two single
rotations
 A rotation about N's grandchild
 A rotation about node N's new child
Double Rotations

Fig. 9 : The result of adding 60, 50, 20, 80, 90, 70,
55, 10, 40, and 35 to an initially empty
(a) AVL tree; (b) binary search tree.
Exercise: AVL Tree

 Show the steps of inserting the following nodes into an


initially empty AVL tree.
 2,1,4,5,9,3
 Q,U,E,S,T,I,O,N
 8,3,2,6,7
B-Trees
 B-tree of order m
 Balanced multiway search tree of order m
 Properties for maintaining balance
 Root has either no children or between 2 and m children
 Other interior nodes (nonleaves) have between m/2 and m
children
 All leaves are on the same level
 High order B-tree works well when tree maintained in
external (disk) storage.
B-Trees
The Split Operation : B-tree
B-Tree Insert : Example m=5
 If an internal nodes are equals to 5, split the node.
 Consider the sequence of inserting nodes as below :
a g f b k d h m j e s i r x c l n t u p

Insert “a” a

Insert “g” ag

Insert “f” afg


f

Insert “b” abfg

ab gk
Insert “k” abfgk
B-Tree Insert : Example m=5

Insert “d”
abd gk

Insert “h”
abd ghk

Inset “m”
abd ghkm
B-Tree Insert : Example m=5

Insert “j”
abd ghjkm

fj

abd gh km

fj
Insert “e”

abde gh km
B-Tree Insert : Example m=5
fj
Insert “s”

abde gh kms

fj
Insert “i”

abde ghi kms

fj
Insert “r”

abde ghi kmrs

fj
Insert “x”

abde ghi kmrsx


B-Tree Insert : Example m=5

fj

Insert “x”
abde ghi kmrsx

fjr

abde ghi km sx

Insert “c”
fjr

abcde ghi km sx
B-Tree Insert : Example m=5

cfjr

ab de ghi km sx

cfjr

Insert “l”

ab de ghI klm sx

cfjr

Insert “n”

ab de ghI klmn sx
B-Tree Insert : Example m=5
cfjr
Insert “t”

ab de ghI klmn stx

cfjr
Insert “u”

ab de ghI klmn stux

cfjr
Insert “p”

ab de ghI klmnp stux


B-Tree Insert : Example m=5

cfjmr

ab de ghI kl np stux

cf mr

ab de ghI kl np stux

Das könnte Ihnen auch gefallen