Professional Documents
Culture Documents
Data Structure and Algorithms (CO2003) : Chapter 7 - AVL Tree
Data Structure and Algorithms (CO2003) : Chapter 7 - AVL Tree
Contact: vbthinh@hcmut.edu.vn
2. AVL Balance
4. Multiway Trees
5. B-Trees
Lecturer: Vuong Ba Thinh Contact: vbthinh@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
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
L.O.3.5 - Identiy cases in which AVL tree and B-tree are unblanced, and demonstrate
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 76
Outcomes
L.O.3.7 - Use binary tree and AVL tree to solve problems in real-life, especially related to
searching techniques.
L.O.3.8 - Analyze the complexity and develop experiment (program) to evaluate methods
L.O.8.4 - Develop recursive implementations for methods supplied for the following
L.O.1.2 - Analyze algorithms and use Big-O notation to characterize the computational
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 76
AVL Tree Concepts
AVL Tree
Denition
AVL Tree is:
in which the heights of the left and right subtrees of the root dier by at most 1, and
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 76
AVL Tree
Each node satises 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 satises balanced tree property: the dierence between the heights of the left
subtree and right subtree of the node does not exceed one.
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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
Lecturer: Vuong Ba Thinh Contact: vbthinh@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.
left of left: a subtree of a tree that is left high has also become left high;
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;
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 76
Rotate Right
tempPtr = root->right
root->right = tempPtr->left
tempPtr->left = root
Return tempPtr
End rotateLeft
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 76
Balancing Trees - Case 1: Left of Left
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 18 / 76
Balancing Trees - Case 2: Right of Right
Lecturer: Vuong Ba Thinh Contact: vbthinh@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
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 76
Balancing Trees - Case 3: Right of Left
Lecturer: Vuong Ba Thinh Contact: vbthinh@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
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 76
Balancing Trees - Case 4: Left of Right
Lecturer: Vuong Ba Thinh Contact: vbthinh@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 <d a t a T y p e > dataType
r o o t <p o i n t e r >
end avlTree
Search and retrieval are the same for any binary tree.
AVL Insert
AVL Delete
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 76
AVL Insert
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 76
AVL Insert Algorithm
Algorithm AVLInsert(ref root <pointer>, val newPtr <pointer>, ref taller <boolean>)
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 76
AVL Insert Algorithm
else
root->balance = EH
taller = false
end
end
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 76
AVL Insert Algorithm
else
root->right = AVLInsert(root->right, newPtr, taller)
taller = false
else if root is EH then
root->balance = RH
else
root = rightBalance(root, taller)
end
end
end
return root
End AVLInsert
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 76
AVL Left Balance Algorithm
This algorithm is entered when the left subtree is higher than the right subtree.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 76
AVL Left Balance Algorithm
leftTree = root->left
root->balance = EH
leftTree->balance = EH
taller = false
Lecturer: Vuong Ba Thinh Contact: vbthinh@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)
This algorithm is entered when the right subtree is higher than the left subtree.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 76
AVL Right Balance Algorithm
rightTree = root->right
root->balance = EH
rightTree->balance = EH
taller = false
Lecturer: Vuong Ba Thinh Contact: vbthinh@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)
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>,
This algorithm deletes a node from an AVL tree and rebalances if necessary.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 37 / 76
AVL Delete Algorithm
success = false
return null
end
if deleteKey < root->data.key then
root->left = AVLDelete(root->left, deleteKey, shorter, success)
if shorter then
root = deleteRightBalance(root, shorter)
end
else if deleteKey > root->data.key then
root->right = AVLDelete(root->right, deleteKey, shorter, success)
if shorter then
root = deleteLeftBalance(root, shorter)
end
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 38 / 76
AVL Delete Algorithm
success = true
shorter = true
recycle(deleteNode)
return newRoot
else if no left subtree then
newRoot = root->right
success = true
shorter = true
recycle(deleteNode)
return newRoot
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 39 / 76
AVL Delete Algorithm
else
// ... else
exchPtr = root->left
end
root->data = exchPtr->data
if shorter then
root = deleteRightBalance(root, shorter)
end
end
end
Return root
End AVLDelete
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 40 / 76
Delete Right Balance
The (sub)tree is shorter after a deletion on the left branch. Adjust the balance factors and
if root LH then
root->balance = EH
shorter = false
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 43 / 76
Delete Left Balance
The (sub)tree is shorter after a deletion on the right branch. Adjust the balance factors and
if root RH then
root->balance = EH
shorter = false
Lecturer: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 47 / 76
M-Way Search Trees
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 48 / 76
M-Way Search Trees
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 49 / 76
M-Way Node Structure
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 50 / 76
B-Trees
B-Trees
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 51 / 76
B-Trees
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 52 / 76
B-Trees
Figure 1: m = 5
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 53 / 76
B-Tree Insertion
If the leaf node is overow, then split it and insert its median entry into its parent.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 54 / 76
B-Tree Insertion
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 55 / 76
B-Tree Insertion
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 56 / 76
B-Tree Insertion
if taller then
// Tree has grown. Create new root.
allocate(newPtr)
newPtr−>entries[1] = upEntry
newPtr−>rstPtr = root
newPtr−>numEntries = 1
root = newPtr
end
return root
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 57 / 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 overow, inserts median key's data
into parent.
Lecturer: Vuong Ba Thinh Contact: vbthinh@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−>rstPtr
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: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 59 / 76
B-Tree Insertion
Search B-tree node for data entry containing key <= target.
else
walker = nodePtr−>numEntries
end
end
return walker
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 60 / 76
B-Tree Insertion
Algorithm splitNode(val node <pointer>, val entryNdx <index>, ref upEntry <entry>)
allocate (rightPtr)
else
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 61 / 76
B-Tree Insertion
else
fromNdx = minEntries + 2
end
toNdx = 1
fromNdx = fromNdx + 1
toNdx = toNdx + 1
end
node->numEntries = node->numEntries−rightPtr->numEntries
else
Lecturer: Vuong Ba Thinh Contact: vbthinh@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->rstPtr = node->entries[medianNdx].rightPtr
return
End splitNode
Lecturer: Vuong Ba Thinh Contact: vbthinh@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.
shifter = node->numEntries + 1
shifter = shifter - 1
end
node->entries[shifter] = newEntry
node->numEntries = node->numEntries + 1
return
End insertEntry
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 64 / 76
B-Tree Deletion
If the data to be deleted are not in a leaf node, then replace that entry by the largest
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 65 / 76
B-Tree Deletion
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 66 / 76
B-Tree Deletion
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 67 / 76
Reow
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 68 / 76
Balance
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 69 / 76
Balance
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 70 / 76
Combine
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 71 / 76
B-Tree Traversal
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 72 / 76
B-Tree Traversal
end
scanCount = scanCount + 1
ptr = root−>entries[scanCount].rightPtr
end
end
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 73 / 76
B-Tree Search
Algorithm BTreeSearch(val root <pointer>, val target <key>, ref node <pointer>, ref
entryNo <index>)
if not found
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 74 / 76
B-Tree Search
else
entryNo = root−>numEntries
end
if target = root−>entries[entryNo].data.key then
found = true
node = root
else
return BTreeSearch (root−>entries[entryNo].rightPtr, target, node, entryNo)
end
end
return found
End BTreeSearch
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 75 / 76
B-Tree Variations
B+Tree:
Each leaf node has one additional pointer to move to the next leaf node.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 76 / 76