Ch07 AVL
Ch07 AVL
Ch07 AVL
2. AVL Balance
4. Multiway Trees
5. B-Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 76
Outcomes
• L.O.3.1 - Depict the following concepts: binary tree, complete binary tree, balanced
binary tree, AVL tree, multi-way tree, etc.
• L.O.3.2 - Describe the strorage structure for tree structures using pseudocode.
• L.O.3.3 - List necessary methods supplied for tree structures, and describe them using
pseudocode.
• 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.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 76
Outcomes
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 76
AVL Tree Concepts
AVL Tree
Definition
AVL Tree is:
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 76
AVL Tree
• Each node satisfies BST property: key of the node is greater than the key of each node in
its left subtree and is smaller than or equals to the key of each node in its right subtree.
• Each node satisfies balanced tree property: the difference between the heights of the left
subtree and right subtree of the node does not exceed one.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 76
AVL Tree
Balance factor
• left_higher (LH): HL = HR + 1
• equal_height (EH): HL = HR
• right_higher (RH): HR = HL + 1
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 6 / 76
AVL Trees
8 8 8 8
5 5 10 5 10
9 3 6 12
5 10
3 6 9
1 4 5 7
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 7 / 76
Non-AVL Trees
8 8
5 10
3 9 12
8 8
5 10 5 10
12 3 12
15 1 15
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 76
Why AVL Trees?
• It is possible that after a number of insert and delete operations, a binary tree may
become unbalanced and inscrease in height.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 9 / 76
AVL Balance
Balancing Trees
• When we insert a node into a tree or delete a node from a tree, the resulting tree may be
unbalanced.
→ rebalance the tree.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 10 / 76
Unbalanced tree cases
tempPtr = root->left
root->left = tempPtr->right
tempPtr->right = root
Return tempPtr
End rotateRight
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 76
Rotate Right
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 76
Rotate Left
tempPtr = root->right
root->right = tempPtr->left
tempPtr->left = root
Return tempPtr
End rotateLeft
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 15 / 76
Balancing Trees - Case 1: Left of Left
Out of balance condition created by a left high subtree of a left high tree
→ balance the tree by rotating the out of balance node to the right.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 76
Balancing Trees - Case 1: Left of Left
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 76
Balancing Trees - Case 2: Right of Right
Out of balance condition created by a right high subtree of a right high tree
→ balance the tree by rotating the out of balance node to the left.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 18 / 76
Balancing Trees - Case 2: Right of Right
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 19 / 76
Balancing Trees - Case 3: Right of Left
Out of balance condition created by a right high subtree of a left high tree
→ balance the tree by two steps:
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 76
Balancing Trees - Case 3: Right of Left
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 76
Balancing Trees - Case 4: Left of Right
Out of balance condition created by a left high subtree of a right high tree
→ balance the tree by two steps:
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 76
Balancing Trees - Case 4: Left of Right
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 76
AVL Tree Operations
AVL Tree Structure
node // G e n e r a l dataTye :
d a t a <dataType> dataType
l e f t <p o i n t e r > k e y <keyType>
r i g h t <p o i n t e r > f i e l d 1 <... >
b a l a n c e <b a l a n c e _ f a c t o r > f i e l d 2 <... >
end node ...
f i e l d n <... >
avlTree end dataType
r o o t <p o i n t e r >
end a v l T r e e
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 76
AVL Tree Operations
• Search and retrieval are the same for any binary tree.
• AVL Insert
• AVL Delete
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 76
AVL Insert
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 76
AVL Insert Algorithm
Algorithm AVLInsert(ref root <pointer>, val newPtr <pointer>, ref taller <boolean>)
Using recursion, insert a node into an AVL tree.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 76
AVL Insert Algorithm
// Insert at root
if root null then
root = newPtr
taller = true
return root
end
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 76
AVL Insert Algorithm
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 76
AVL Insert Algorithm
else
root->right = AVLInsert(root->right, newPtr, taller)
// Right subtree is taller
if taller then
if root is LH then
root->balance = EH
taller = false
else if root is EH then
root->balance = RH
else
root = rightBalance(root, taller)
end
end
end
return root
End
Lecturer: Duc AVLInsert
Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 76
AVL Left Balance Algorithm
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 76
AVL Left Balance Algorithm
leftTree = root->left
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 76
AVL Left Balance Algorithm
else
rightTree = leftTree->right
if rightTree->balance = LH then
root->balance = RH
leftTree->balance = EH
else if rightTree->balance = EH then
leftTree->balance = EH
else
root->balance = EH
leftTree->balance = LH
end
rightTree->balance = EH
root->left = rotateLeft(leftTree)
root = rotateRight(root), taller = false
end
return root
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 76
AVL Right Balance Algorithm
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 76
AVL Right Balance Algorithm
rightTree = root->right
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 35 / 76
AVL Right Balance Algorithm
else
leftTree = rightTree->left
if leftTree->balance = RH then
root->balance = LH
rightTree->balance = EH
else if leftTree->balance = EH then
rightTree->balance = EH
else
root->balance = EH
rightTree->balance = RH
end
leftTree->balance = EH
root->right = rotateRight(rightTree)
root = rotateLeft(root), taller = false
end
return
Lecturer: Duc root PhD. Contact: nddung@hcmut.edu.vn
Dung Nguyen, Data Structure and Algorithms [CO2003] 36 / 76
AVL Delete Algorithm
The AVL delete follows the basic logic of the binary search 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.
Algorithm AVLDelete(ref root <pointer>, val deleteKey <key>, ref shorter <boolean>,
ref success <boolean>)
This algorithm deletes a node from an AVL tree and rebalances if necessary.
else
// ... else
exchPtr = root->left
while exchPtr->right not null do
exchPtr = exchPtr->right
end
root->data = exchPtr->data
root->left = AVLDelete(root->left, exchPtr->data.key, shorter, success)
if shorter then
root = deleteRightBalance(root, shorter)
end
end
end
Return root
End AVLDelete
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 40 / 76
Delete Right Balance
if root LH then
root->balance = EH
else if root EH then
root->balance = RH
shorter = false
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 41 / 76
Delete Right Balance
else
rightTree = root->right
if rightTree LH then
leftTree = rightTree->left
if leftTree LH then
rightTree->balance = RH
root->balance = EH
else if leftTree EH then
root->balance = LH
rightTree->balance = EH
else
root->balance = LH
rightTree->balance = EH
end
leftTree->balance = EH
root->right = rotateRight(rightTree)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 42 / 76
Delete Right Balance
else
// ...
else
if rightTree not EH then
root->balance = EH
rightTree->balance = EH
else
root->balance = RH
rightTree->balance = LH
shorter = false
end
root = rotateLeft(root)
end
end
return root
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 43 / 76
Delete Left Balance
if root RH then
root->balance = EH
else if root EH then
root->balance = LH
shorter = false
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 44 / 76
Delete Left Balance
else
leftTree = root->left
if leftTree RH then
rightTree = leftTree->right
if rightTree RH then
leftTree->balance = LH
root->balance = EH
else if rightTree EH then
root->balance = RH
leftTree->balance = EH
else
root->balance = RH
leftTree->balance = EH
end
rightTree->balance = EH
root->left = rotateLeft(leftTree)
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 45 / 76
Delete Left Balance
else
// ... else
if leftTree not EH then
root->balance = EH
leftTree->balance = EH
else
root->balance = LH
leftTree->balance = RH
shorter = false
end
root = rotateRight(root)
end
end
return root
End deleteLeftBalance
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 46 / 76
Multiway Trees
Multiway Trees
Tree whose outdegree is not restricted to 2 while retaining the general properties of binary
search trees.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 47 / 76
M-Way Search Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 48 / 76
M-Way Search Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 49 / 76
M-Way Node Structure
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 50 / 76
B-Trees
B-Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 51 / 76
B-Trees
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 52 / 76
B-Trees
Figure 1: m = 5
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 53 / 76
B-Tree Insertion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 54 / 76
B-Tree Insertion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 55 / 76
B-Tree Insertion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 56 / 76
B-Tree Insertion
Algorithm insertNode (ref root <pointer>, val data <record>, ref upEntry <entry>)
Recursively searches tree to locate leaf for data. If node overflow, inserts median key’s data
into parent.
Pre: root is a pointer to tree or subtree. May be null.
Post: data inserted
upEntry is overflow entry to be inserted into parent.
Return tree taller <boolean>.
if root null then
upEntry.data = data
upEntry.rightPtr = null
taller = true
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 58 / 76
B-Tree Insertion
else
entryNdx = searchNode(root, data.key)
if entryNdx > 0 then
subTree = root−>entries[entryNdx].rightPtr
else
subTree = root−>firstPtr
end
taller = insertNode(subTree, data, upEntry)
if taller then
if node full then
splitNode(root, entryNdx, upEntry), taller = true
else
insertEntry(root, entryNdx, upEntry), taller = false
root−>numEntries = root−>numEntries + 1
end
end
end
return taller
End insertNode
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 59 / 76
B-Tree Insertion
Algorithm splitNode(val node <pointer>, val entryNdx <index>, ref upEntry <entry>)
Node has overflowed. Split node. No duplicate keys allowed.
Pre: node is pointer to node that overflowed.
entryNdx contains index location of parent.
upEntry contains entry being inserted into split node.
Post: upEntry now contains entry to be inserted into parent.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 61 / 76
B-Tree Insertion
else
fromNdx = minEntries + 2
end
toNdx = 1
rightPtr->numEntries = node->numEntries – fromNdx + 1
while fromNdx <= node->numEntries do
rightPtr->entries[toNdx] = node->entries[fromNdx]
fromNdx = fromNdx + 1
toNdx = toNdx + 1
end
node->numEntries = node->numEntries−rightPtr->numEntries
if entryNdx <= minEntries then
insertEntry(node, entryNdx, upEntry)
else
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 62 / 76
B-Tree Insertion
else
insertEntry(rightPtr, entryNdx−minEntries, upEntry)
node->numEntries = node->numEntries− 1
rightPtr->numEntries = rightPtr->numEntries + 1
end
// Build entry for parent
medianNdx = minEntries + 1
upEntry.data = node->entries[medianNdx].data
upEntry.rightPtr = rightPtr
rightPtr->firstPtr = node->entries[medianNdx].rightPtr
return
End splitNode
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 63 / 76
B-Tree Insertion
Algorithm insertEntry(val node <pointer>, val 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.
newEntry contains data to be inserted.
Post: data has been inserted in sequence.
shifter = node->numEntries + 1
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
End
Lecturer: Duc insertEntry
Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 64 / 76
B-Tree Deletion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 65 / 76
B-Tree Deletion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 66 / 76
B-Tree Deletion
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 67 / 76
Reflow
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 68 / 76
Balance
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 69 / 76
Balance
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 70 / 76
Combine
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 71 / 76
B-Tree Traversal
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 72 / 76
B-Tree Traversal
Algorithm BTreeSearch(val root <pointer>, val target <key>, ref node <pointer>, ref
entryNo <index>)
Recursively searches a B-tree for the target key.
Pre: root is pointer to a tree or subtree
target is the data to be located
Post:
if found – –
node is pointer to located node
entryNo is entry within node
if not found – –
node is null and entryNo is zero
Return found <boolean>
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 74 / 76
B-Tree Search
• B+Tree:
• Each data entry must be represented at the leaf level.
• Each leaf node has one additional pointer to move to the next leaf node.
Lecturer: Duc Dung Nguyen, PhD. Contact: nddung@hcmut.edu.vn Data Structure and Algorithms [CO2003] 76 / 76