Documente Academic
Documente Profesional
Documente Cultură
Dr. Nguyen Ho
Man Rang
Chapter 8
AVL Trees, B-Trees AVL Tree Concepts
AVL Balance
Multiway Trees
B-Trees
8.1
AVL Trees, B-Trees
Outcomes
Dr. Nguyen Ho
Man Rang
AVL Balance
structures using pseudocode.
AVL Tree
• L.O.3.3 - List necessary methods supplied for tree Operations
B-Trees
• L.O.3.4 - Identify the importance of “blanced” feature
in tree structures and give examples to demonstate it.
• L.O.3.5 - Identiy cases in which AVL tree and B-tree
are unblanced, and demonstrate methods to resolve all
the cases step-by-step using figures.
8.2
AVL Trees, B-Trees
Outcomes
Dr. Nguyen Ho
Man Rang
• L.O.3.6 - Implement binary tree and AVL tree using
C/C++.
• L.O.3.7 - Use binary tree and AVL tree to solve
problems in real-life, especially related to searching
techniques. AVL Tree Concepts
AVL Tree
experiment (program) to evaluate methods supplied for Operations
tree structures. Multiway Trees
AVL Balance
AVL Tree
Operations
3 AVL Tree Operations
Multiway Trees
B-Trees
4 Multiway Trees
5 B-Trees
8.4
AVL Trees, B-Trees
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
Multiway Trees
B-Trees
8.5
AVL Trees, B-Trees
AVL Tree
Dr. Nguyen Ho
Man Rang
Definition
• the left and right subtrees are again AVL Multiway Trees
B-Trees
trees.
Discovered by G.M.Adel’son-Vel’skii and E.M.Landis in 1962.
8.6
AVL Trees, B-Trees
AVL Tree
Dr. Nguyen Ho
Man Rang
AVL Tree
or equals to the key of each node in its Operations
Multiway Trees
right subtree. B-Trees
Balance factor
AVL Tree Concepts
• left_higher (LH): HL = HR + 1 AVL Balance
Multiway Trees
• right_higher (RH): HR = HL + 1 B-Trees
8.8
AVL Trees, B-Trees
AVL Trees
Dr. Nguyen Ho
Man Rang
8 8 8 8
5 5 10 5 10
AVL Tree Concepts
9 3 6 12 AVL Balance
AVL Tree
Operations
8
Multiway Trees
B-Trees
5 10
3 6 9
1 4 5 7
8.9
AVL Trees, B-Trees
Non-AVL Trees
Dr. Nguyen Ho
Man Rang
8 8
5 10
AVL Tree Concepts
3 9 12 AVL Balance
AVL Tree
Operations
8 8
Multiway Trees
B-Trees
5 10 5 10
12 3 12
15 1 15
8.10
AVL Trees, B-Trees
Why AVL Trees?
Dr. Nguyen Ho
Man Rang
AVL Balance
inefficient. AVL Tree
Operations
Multiway Trees
• It is possible that after a number of insert B-Trees
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
Multiway Trees
B-Trees
8.12
AVL Trees, B-Trees
Balancing Trees
Dr. Nguyen Ho
• When we insert a node into a tree or delete Man Rang
AVL Tree
• left of left: a subtree of a tree that is Operations
Multiway Trees
left high has also become left high; B-Trees
• right of right: a subtree of a tree that is
right high has also become right high;
• right of left: a subtree of a tree that is
left high has become right high;
• left of right: a subtree of a tree that is
right high has become left high; 8.13
AVL Trees, B-Trees
Unbalanced tree cases
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.14
AVL Trees, B-Trees
Unbalanced tree cases
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.15
AVL Trees, B-Trees
Rotate Right
Dr. Nguyen Ho
Man Rang
Algorithm rotateRight(ref root
<pointer>)
Exchanges pointers to rotate the tree right.
AVL Tree Concepts
Pre: root is pointer to tree to be rotated AVL Balance
Multiway Trees
root->left = tempPtr->right
tempPtr->right = root
Return tempPtr
End rotateRight
8.16
AVL Trees, B-Trees
Rotate Right
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
AVL Tree
Operations
B-Trees
root->right = tempPtr->left
tempPtr->left = root
Return tempPtr
End rotateLeft
8.18
AVL Trees, B-Trees
Balancing Trees - Case 1: Left of Left
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.19
AVL Trees, B-Trees
Balancing Trees - Case 1: Left of Left
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.20
AVL Trees, B-Trees
Balancing Trees - Case 2: Right of Right
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.21
AVL Trees, B-Trees
Balancing Trees - Case 2: Right of Right
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.22
AVL Trees, B-Trees
Balancing Trees - Case 3: Right of Left
Dr. Nguyen Ho
Man Rang
AVL Balance
Multiway Trees
B-Trees
8.23
AVL Trees, B-Trees
Balancing Trees - Case 3: Right of Left
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.24
AVL Trees, B-Trees
Balancing Trees - Case 4: Left of Right
Dr. Nguyen Ho
Man Rang
AVL Balance
Multiway Trees
B-Trees
8.25
AVL Trees, B-Trees
Balancing Trees - Case 4: Left of Right
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.26
AVL Trees, B-Trees
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
Multiway Trees
B-Trees
8.27
AVL Trees, B-Trees
AVL Tree Structure
Dr. Nguyen Ho
Man Rang
node avlTree
d a t a <dataType> r o o t <p o i n t e r >
l e f t <p o i n t e r > end a v l T r e e
r i g h t <p o i n t e r >
AVL Tree Concepts
b a l a n c e <b a l a n c e _ f a c t o r >
AVL Balance
end node
AVL Tree
Operations
B-Trees
dataType
k e y <keyType>
f i e l d 1 <... >
f i e l d 2 <... >
...
f i e l d n <... >
end dataType
Note: Array is not suitable for AVL Tree. 8.28
AVL Trees, B-Trees
AVL Tree Operations
Dr. Nguyen Ho
Man Rang
• Search and retrieval are the same for any AVL Tree Concepts
AVL Balance
binary tree. AVL Tree
Operations
• AVL Insert Multiway Trees
8.29
AVL Trees, B-Trees
AVL Insert
Dr. Nguyen Ho
• Insert can make an out of balance condition. Man Rang
AVL Balance
AVL Tree
Operations
• Otherwise, some inserts can make an automatic Multiway Trees
balancing. B-Trees
8.30
AVL Trees, B-Trees
AVL Insert Algorithm
Algorithm AVLInsert(ref root <pointer>, Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Pre: root is a pointer to first node in AVL Operations
Multiway Trees
tree/subtree B-Trees
// Insert at root
if root null then AVL Tree Concepts
AVL Balance
root = newPtr AVL Tree
Operations
taller = true Multiway Trees
end
8.32
AVL Trees, B-Trees
AVL Insert Algorithm
if newPtr->data.key < root->data.key Dr. Nguyen Ho
Man Rang
then
root->left = AVLInsert(root->left,
newPtr, taller)
// Left subtree is taller AVL Tree Concepts
AVL Tree
if root is LH then Operations
Multiway Trees
root = leftBalance(root, taller) B-Trees
AVL Tree
taller = false Operations
else if root is EH then Multiway Trees
root->balance = RH B-Trees
else
root = rightBalance(root, taller)
end
end
end
return root
End AVLInsert
8.34
AVL Trees, B-Trees
AVL Left Balance Algorithm
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
[sub]tree B-Trees
taller is true
Post: root has been updated (if necessary)
taller has been updated
8.35
AVL Trees, B-Trees
AVL Left Balance Algorithm
Dr. Nguyen Ho
Man Rang
leftTree = root->left
AVL Tree Concepts
// Case 1: Left of left. Single rotation right. AVL Balance
root->balance = EH B-Trees
leftTree->balance = EH
taller = false
8.36
AVL Trees, B-Trees
AVL Left Balance Algorithm
// Case 2: Right of Left. Double rotation required. Dr. Nguyen Ho
Man Rang
else
rightTree = leftTree->right
if rightTree->balance = LH then
root->balance = RH
leftTree->balance = EH AVL Tree Concepts
else if rightTree->balance = EH then AVL Balance
leftTree->balance = EH AVL Tree
else Operations
leftTree->balance = LH B-Trees
end
rightTree->balance = EH
root->left = rotateLeft(leftTree)
root = rotateRight(root)
taller = false
end
return root
End leftBalance 8.37
AVL Trees, B-Trees
AVL Right Balance Algorithm
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
[sub]tree B-Trees
taller is true
Post: root has been updated (if necessary)
taller has been updated
8.38
AVL Trees, B-Trees
AVL Right Balance Algorithm
Dr. Nguyen Ho
Man Rang
rightTree = root->right
AVL Tree
if rightTree is RH then Operations
Multiway Trees
root = rotateLeft(root) B-Trees
root->balance = EH
rightTree->balance = EH
taller = false
8.39
AVL Trees, B-Trees
AVL Right Balance Algorithm
// Case 2: Left of Right. Double rotation required. Dr. Nguyen Ho
Man Rang
else
leftTree = rightTree->left
if leftTree->balance = RH then
root->balance = LH
rightTree->balance = EH AVL Tree Concepts
else if leftTree->balance = EH then AVL Balance
rightTree->balance = EH AVL Tree
Operations
else
Multiway Trees
root->balance = EH
B-Trees
rightTree->balance = RH
end
leftTree->balance = EH
root->right = rotateRight(rightTree)
root = rotateLeft(root)
taller = false
end
return root
8.40
AVL Trees, B-Trees
AVL Delete Algorithm
Dr. Nguyen Ho
The AVL delete follows the basic logic of the binary search Man Rang
tree delete with the addition of the logic to balance the tree.
As with the insert logic, the balancing occurs as we back out
of the tree.
Multiway Trees
rebalances if necessary.
B-Trees
shorter = false
success = false
return null
end
if deleteKey < root->data.key then AVL Tree Concepts
8.43
AVL Trees, B-Trees
AVL Delete Algorithm
Dr. Nguyen Ho
else Man Rang
// ... // Delete node has two subtrees
else
exchPtr = root->left
while exchPtr->right not null do
exchPtr = exchPtr->right AVL Tree Concepts
end AVL Balance
root->data = exchPtr->data AVL Tree
Operations
root->left = AVLDelete(root->left,
Multiway Trees
exchPtr->data.key, shorter, success) B-Trees
if shorter then
root = deleteRightBalance(root,
shorter)
end
end
end
Return root
End AVLDelete 8.44
AVL Trees, B-Trees
Delete Right Balance
Dr. Nguyen Ho
Man Rang
Algorithm deleteRightBalance(ref root <pointer>,
ref shorter <boolean>)
The (sub)tree is shorter after a deletion on the left
branch. Adjust the balance factors and if necessary
balance the tree by rotating left. AVL Tree Concepts
AVL Balance
Pre: tree is shorter AVL Tree
Post: balance factors updated and balance restored Operations
Multiway Trees
root updated
B-Trees
shorter updated
if root LH then
root->balance = EH
else if root EH then
root->balance = RH
shorter = false
8.45
AVL Trees, B-Trees
Delete Right Balance
Dr. Nguyen Ho
else Man Rang
rightTree = root->right
if rightTree LH then
leftTree = rightTree->left
if leftTree LH then
rightTree->balance = RH AVL Tree Concepts
// ...
else
if rightTree not EH then
root->balance = EH AVL Tree Concepts
AVL Balance
Pre: tree is shorter AVL Tree
Post: balance factors updated and balance restored Operations
Multiway Trees
root updated
B-Trees
shorter updated
if root RH then
root->balance = EH
else if root EH then
root->balance = LH
shorter = false
8.48
AVL Trees, B-Trees
Delete Left Balance
Dr. Nguyen Ho
Man Rang
else
leftTree = root->left
if leftTree RH then
rightTree = leftTree->right
if rightTree RH then
leftTree->balance = LH AVL Tree Concepts
AVL Balance
root->balance = EH
else if rightTree EH then AVL Tree
Operations
root->balance = RH Multiway Trees
leftTree->balance = EH B-Trees
else
root->balance = RH
leftTree->balance = EH
end
rightTree->balance = EH
root->left = rotateLeft(leftTree)
root = rotateRight(root)
8.49
AVL Trees, B-Trees
Delete Left Balance
Dr. Nguyen Ho
Man Rang
else
// ...
else
if leftTree not EH then
root->balance = EH AVL Tree Concepts
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
Multiway Trees
B-Trees
8.51
AVL Trees, B-Trees
Multiway Trees
Dr. Nguyen Ho
Man Rang
AVL Balance
Tree whose outdegree is not restricted to 2 AVL Tree
Operations
while retaining the general properties of binary Multiway Trees
8.52
AVL Trees, B-Trees
M-Way Search Trees
Dr. Nguyen Ho
Man Rang
AVL Balance
• < the key of the right data entry. AVL Tree
Operations
Multiway Trees
B-Trees
8.53
AVL Trees, B-Trees
M-Way Search Trees
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.54
AVL Trees, B-Trees
M-Way Node Structure
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.55
AVL Trees, B-Trees
Dr. Nguyen Ho
Man Rang
AVL Tree
Operations
Multiway Trees
B-Trees
8.56
AVL Trees, B-Trees
B-Trees
Dr. Nguyen Ho
Man Rang
B-Trees
8.57
AVL Trees, B-Trees
B-Trees
Dr. Nguyen Ho
Man Rang
AVL Tree
subtrees. Operations
Multiway Trees
• All other nodes have at least dm/2e − 1 B-Trees
entries.
• All leaf nodes are at the same level.
8.58
AVL Trees, B-Trees
B-Trees
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
Hình: m = 5
8.59
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang
B-Trees
8.60
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.61
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.62
AVL Trees, B-Trees
B-Tree Insertion
Algorithm BTreeInsert(ref root <pointer>, val data Dr. Nguyen Ho
Man Rang
<record>)
Inserts data into B-tree. Equal keys placed on right
branch.
Pre: root is a pointer to the B-tree. May be null.
Post: data inserted AVL Tree Concepts
walker = 0 B-Trees
else
walker = nodePtr−>numEntries
while target < nodePtr−>entries[walker].data.key
do
walker = walker - 1
end
end
return walker 8.66
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Algorithm splitNode(val node <pointer>, val Man Rang
AVL Tree
node. Operations
Post: upEntry now contains entry to be inserted Multiway Trees
fromNdx = minEntries + 2
end
toNdx = 1
rightPtr->numEntries = node->numEntries –
fromNdx + 1 AVL Tree Concepts
toNdx = toNdx + 1
end
node->numEntries =
node->numEntries−rightPtr->numEntries
if entryNdx <= minEntries then
insertEntry(node, entryNdx, upEntry)
else
8.68
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang
else
insertEntry(rightPtr, entryNdx−minEntries,
upEntry)
node->numEntries = node->numEntries− 1
rightPtr->numEntries = rightPtr->numEntries AVL Tree Concepts
+1 AVL Balance
8.69
AVL Trees, B-Trees
B-Tree Insertion
Algorithm insertEntry(val node <pointer>, val Dr. Nguyen Ho
Man Rang
entryNdx <index>, val newEntry <entry>)
Inserts one entry into a node by shifting nodes to
make room.
Pre: node is pointer to node to contain data.
entryNdx is index to location for new data. AVL Tree Concepts
Multiway Trees
shifter = node->numEntries + 1 B-Trees
while shifter > entryNdx + 1 do
node->entries[shifter] = node->entries[shifter -
1]
shifter = shifter - 1
end
node->entries[shifter] = newEntry
node->numEntries = node->numEntries + 1
return 8.70
AVL Trees, B-Trees
B-Tree Deletion
Dr. Nguyen Ho
Man Rang
AVL Balance
• If the data to be deleted are not in a leaf AVL Tree
Operations
node, then replace that entry by the largest Multiway Trees
8.71
AVL Trees, B-Trees
B-Tree Deletion
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.72
AVL Trees, B-Trees
B-Tree Deletion
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.73
AVL Trees, B-Trees
Reflow
Dr. Nguyen Ho
Man Rang
AVL Tree
Multiway Trees
8.74
AVL Trees, B-Trees
Balance
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.75
AVL Trees, B-Trees
Balance
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.76
AVL Trees, B-Trees
Combine
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.77
AVL Trees, B-Trees
B-Tree Traversal
Dr. Nguyen Ho
Man Rang
AVL Balance
AVL Tree
Operations
Multiway Trees
B-Trees
8.78
AVL Trees, B-Trees
B-Tree Traversal
Algorithm BTreeTraversal (val root <pointer>) Dr. Nguyen Ho
Man Rang
Processes tree using inorder traversal.
Pre: root is pointer to B-Tree.
Post: Every entry has been processed in order.
scanCount = 0
ptr = root−>firstPtr AVL Tree Concepts
if found – – B-Trees
AVL Balance
• B+Tree: AVL Tree
Operations
• Each data entry must be represented at Multiway Trees
8.82