You are on page 1of 82

AVL Trees, B-Trees

Dr. Nguyen Ho
Man Rang

Chapter 8
AVL Trees, B-Trees AVL Tree Concepts

AVL Balance

Data Structures and Algorithms AVL Tree


Operations

Multiway Trees

B-Trees

Dr. Nguyen Ho Man Rang


Faculty of Computer Science and Engineering
University of Technology, VNU-HCM

8.1
AVL Trees, B-Trees
Outcomes
Dr. Nguyen Ho
Man Rang

• 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 AVL Tree Concepts

AVL Balance
structures using pseudocode.
AVL Tree
• L.O.3.3 - List necessary methods supplied for tree Operations

structures, and describe them using pseudocode. Multiway Trees

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

• L.O.3.8 - Analyze the complexity and develop AVL Balance

AVL Tree
experiment (program) to evaluate methods supplied for Operations
tree structures. Multiway Trees

• L.O.8.4 - Develop recursive implementations for B-Trees

methods supplied for the following structures: list, tree,


heap, searching, and graphs.
• L.O.1.2 - Analyze algorithms and use Big-O notation to
characterize the computational complexity of algorithms
composed by using the following control structures:
sequence, branching, and iteration (not recursion).
8.3
AVL Trees, B-Trees
Contents
Dr. Nguyen Ho
Man Rang

1 AVL Tree Concepts

2 AVL Balance AVL Tree Concepts

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 Concepts

AVL Tree Concepts AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.5
AVL Trees, B-Trees
AVL Tree
Dr. Nguyen Ho
Man Rang

Definition

AVL Tree is:


• A Binary Search Tree,
AVL Tree Concepts
• in which the heights of the left and right AVL Balance

subtrees of the root differ by at most 1, and AVL Tree


Operations

• 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.

AVL Tree is a Binary Search Tree that is balanced tree.

8.6
AVL Trees, B-Trees
AVL Tree
Dr. Nguyen Ho
Man Rang

A binary tree is an AVL Tree if


• Each node satisfies BST property: key of
the node is greater than the key of each AVL Tree Concepts

node in its left subtree and is smaller than AVL Balance

AVL Tree
or equals to the key of each node in its Operations

Multiway Trees
right subtree. B-Trees

• 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.
8.7
AVL Trees, B-Trees
AVL Tree
Dr. Nguyen Ho
Man Rang

Balance factor
AVL Tree Concepts
• left_higher (LH): HL = HR + 1 AVL Balance

• equal_height (EH): HL = HR AVL Tree


Operations

Multiway Trees
• right_higher (RH): HR = HL + 1 B-Trees

(HL , HR : the heights of left and right subtrees)

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

• When data elements are inserted in a BST


in sorted order: 1, 2, 3, ...
BST becomes a degenerate tree.
Search operation takes O(n), which is AVL Tree Concepts

AVL Balance
inefficient. AVL Tree
Operations

Multiway Trees
• It is possible that after a number of insert B-Trees

and delete operations, a binary tree may


become unbalanced and increase in height.

• AVL trees ensure that the complexity of


search is O(log2 n).
8.11
AVL Trees, B-Trees

Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance AVL Balance

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

a node from a tree, the resulting tree may


be unbalanced.
→ rebalance the tree.
AVL Tree Concepts

• Four unbalanced tree cases: AVL Balance

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 Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.14
AVL Trees, B-Trees
Unbalanced tree cases
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

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

Post: node rotated and root updated AVL Tree


Operations

Multiway Trees

tempPtr = root->left B-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 Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)


8.17
AVL Trees, B-Trees
Rotate Left
Dr. Nguyen Ho
Man Rang

Algorithm rotateLeft(ref root <pointer>)


Exchanges pointers to rotate the tree left.

Pre: root is pointer to tree to be rotated AVL Tree Concepts

Post: node rotated and root updated AVL Balance

AVL Tree
Operations

tempPtr = root->right Multiway Trees

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

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. AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.19
AVL Trees, B-Trees
Balancing Trees - Case 1: Left of Left
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.20
AVL Trees, B-Trees
Balancing Trees - Case 2: Right of Right
Dr. Nguyen Ho
Man Rang

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. AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.21
AVL Trees, B-Trees
Balancing Trees - Case 2: Right of Right
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.22
AVL Trees, B-Trees
Balancing Trees - Case 3: Right of Left
Dr. Nguyen Ho
Man Rang

Out of balance condition created by a right


high subtree of a left high tree
→ balance the tree by two steps:
1 rotating the left subtree to the left;
AVL Tree Concepts

AVL Balance

2 rotating the root to the right. AVL Tree


Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.23
AVL Trees, B-Trees
Balancing Trees - Case 3: Right of Left
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.24
AVL Trees, B-Trees
Balancing Trees - Case 4: Left of Right
Dr. Nguyen Ho
Man Rang

Out of balance condition created by a left high


subtree of a right high tree
→ balance the tree by two steps:
1 rotating the right subtree to the right;
AVL Tree Concepts

AVL Balance

2 rotating the root to the left. AVL Tree


Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.25
AVL Trees, B-Trees
Balancing Trees - Case 4: Left of Right
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

(Source: Data Structures - A Pseudocode Approach with C++)

8.26
AVL Trees, B-Trees

Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Tree Operations AVL Balance

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

// G e n e r a l dataTye : Multiway Trees

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

• AVL Delete B-Trees

8.29
AVL Trees, B-Trees
AVL Insert
Dr. Nguyen Ho
• Insert can make an out of balance condition. Man Rang

AVL Tree Concepts

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

val newPtr <pointer>, ref taller


<boolean>)
Using recursion, insert a node into an AVL
tree. AVL Tree Concepts

AVL Balance

AVL Tree
Pre: root is a pointer to first node in AVL Operations

Multiway Trees
tree/subtree B-Trees

newPtr is a pointer to new node to be


inserted
Post: taller is a Boolean: true
indicating the subtree height has
increased, false indicating same height
Return root returned recursively up the 8.31
AVL Trees, B-Trees
AVL Insert Algorithm
Dr. Nguyen Ho
Man Rang

// Insert at root
if root null then AVL Tree Concepts

AVL Balance
root = newPtr AVL Tree
Operations
taller = true Multiway Trees

return root B-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

if taller then AVL Balance

AVL Tree
if root is LH then Operations

Multiway Trees
root = leftBalance(root, taller) B-Trees

else if root is EH then


root->balance = LH
else
root->balance = EH
taller = false
end
8.33
AVL Trees, B-Trees
AVL Insert Algorithm
Dr. Nguyen Ho
else Man Rang

root->right = AVLInsert(root->right, newPtr,


taller)
// Right subtree is taller
if taller then
if root is LH then AVL Tree Concepts

root->balance = EH AVL Balance

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

Algorithm leftBalance(ref root <pointer>,


ref taller <boolean>)
This algorithm is entered when the left AVL Tree Concepts

subtree is higher than the right subtree. AVL Balance

AVL Tree
Operations

Pre: root is a pointer to the root of the Multiway Trees

[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

if leftTree is LH then AVL Tree


Operations
root = rotateRight(root) Multiway Trees

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

root->balance = EH Multiway Trees

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

Algorithm rightBalance(ref root


<pointer>, ref taller <boolean>)
This algorithm is entered when the right AVL Tree Concepts

subtree is higher than the left subtree. AVL Balance

AVL Tree
Operations

Pre: root is a pointer to the root of the Multiway Trees

[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

// Case 1: Right of right. Single rotation AVL Tree Concepts

left. AVL Balance

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.

Algorithm AVLDelete(ref root <pointer>, val AVL Tree Concepts


deleteKey <key>, ref shorter <boolean>, ref AVL Balance
success <boolean>) AVL Tree
This algorithm deletes a node from an AVL tree and Operations

Multiway Trees
rebalances if necessary.
B-Trees

Pre: root is a pointer to the root of the [sub]tree


deleteKey is the key of node to be deleted
Post: node deleted if found, tree unchanged if not
found
shorter is true if subtree is shorter
success is true if deleted, false if not found
Return pointer to root of (potential) new subtree 8.41
AVL Trees, B-Trees
AVL Delete Algorithm
Dr. Nguyen Ho
if tree null then Man Rang

shorter = false
success = false
return null
end
if deleteKey < root->data.key then AVL Tree Concepts

root->left = AVLDelete(root->left, deleteKey, AVL Balance

shorter, success) AVL Tree


Operations
if shorter then Multiway Trees
root = deleteRightBalance(root, shorter) B-Trees
end
else if deleteKey > root->data.key then
root->right = AVLDelete(root->right,
deleteKey, shorter, success)
if shorter then
root = deleteLeftBalance(root, shorter)
end
8.42
AVL Trees, B-Trees
AVL Delete Algorithm
Dr. Nguyen Ho
Man Rang

// Delete node found – test for leaf node


else
deleteNode = root
if no right subtree then
newRoot = root->left AVL Tree Concepts

success = true AVL Balance

shorter = true AVL Tree


Operations
recycle(deleteNode) Multiway Trees
return newRoot B-Trees
else if no left subtree then
newRoot = root->right
success = true
shorter = true
recycle(deleteNode)
return newRoot

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

root->balance = EH AVL Balance

else if leftTree EH then AVL Tree


Operations
root->balance = LH Multiway Trees
rightTree->balance = EH B-Trees
else
root->balance = LH
rightTree->balance = EH
end
leftTree->balance = EH
root->right = rotateRight(rightTree)
root = rotateLeft(root)
8.46
AVL Trees, B-Trees
Delete Right Balance
Dr. Nguyen Ho
else Man Rang

// ...

else
if rightTree not EH then
root->balance = EH AVL Tree Concepts

rightTree->balance = EH AVL Balance

else AVL Tree


Operations
root->balance = RH Multiway Trees
rightTree->balance = LH B-Trees
shorter = false
end
root = rotateLeft(root)
end
end
return root
End deleteRightBalance
8.47
AVL Trees, B-Trees
Delete Left Balance
Dr. Nguyen Ho
Man Rang
Algorithm deleteLeftBalance(ref root <pointer>,
ref shorter <boolean>)
The (sub)tree is shorter after a deletion on the right
branch. Adjust the balance factors and if necessary
balance the tree by rotating right. 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

leftTree->balance = EH AVL Balance

else AVL Tree


Operations
root->balance = LH Multiway Trees
leftTree->balance = RH B-Trees
shorter = false
end
root = rotateRight(root)
end
end
return root
End deleteLeftBalance
8.50
AVL Trees, B-Trees

Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

Multiway Trees AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.51
AVL Trees, B-Trees
Multiway Trees
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance
Tree whose outdegree is not restricted to 2 AVL Tree
Operations
while retaining the general properties of binary Multiway Trees

search trees. B-Trees

8.52
AVL Trees, B-Trees
M-Way Search Trees
Dr. Nguyen Ho
Man Rang

• Each node has m - 1 data entries and m


subtree pointers.
• The key values in a subtree such that:
• ≥ the key of the left data entry AVL Tree Concepts

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 Tree Concepts

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 Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.55
AVL Trees, B-Trees

Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

B-Trees AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.56
AVL Trees, B-Trees
B-Trees
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts


• M-way trees are unbalanced. AVL Balance

• Bayer, R. & McCreight, E. (1970) created AVL Tree


Operations

B-Trees. Multiway Trees

B-Trees

8.57
AVL Trees, B-Trees
B-Trees
Dr. Nguyen Ho
Man Rang

A B-tree is an m-way tree with the following


additional properties (m ≥ 3): AVL Tree Concepts

• The root is either a leaf or has at least 2 AVL Balance

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 Tree Concepts

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

AVL Tree Concepts


• Insert the new entry into a leaf node. AVL Balance

• If the leaf node is overflow, then split it and AVL Tree


Operations

insert its median entry into its parent. Multiway Trees

B-Trees

8.60
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.61
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

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

Return pointer to B-tree root. AVL Balance

taller = insertNode(root, data, upEntry) AVL Tree


Operations
if taller then Multiway Trees
// Tree has grown. Create new root. B-Trees
allocate(newPtr)
newPtr−>entries[1] = upEntry
newPtr−>firstPtr = root
newPtr−>numEntries = 1
root = newPtr
end
return root
End BTreeInsert 8.63
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
Man Rang
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. AVL Tree Concepts
Pre: root is a pointer to tree or subtree. May be AVL Balance
null. AVL Tree
Operations
Post: data inserted
Multiway Trees
upEntry is overflow entry to be inserted into parent.
B-Trees
Return tree taller <boolean>.

if root null then


upEntry.data = data
upEntry.rightPtr = null
taller = true
8.64
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
else Man Rang
entryNdx = searchNode(root, data.key)
if entryNdx > 0 then
subTree = root−>entries[entryNdx].rightPtr
else
subTree = root−>firstPtr
end AVL Tree Concepts
taller = insertNode(subTree, data, upEntry) AVL Balance
if taller then AVL Tree
if node full then Operations

splitNode(root, entryNdx, upEntry) Multiway Trees


taller = true B-Trees
else
insertEntry(root, entryNdx, upEntry)
taller = false
root−>numEntries = root−>numEntries + 1
end
end
end
return taller
End insertNode 8.65
AVL Trees, B-Trees
B-Tree Insertion
Algorithm searchNode(val nodePtr <pointer>, val Dr. Nguyen Ho
Man Rang
target <key>)
Search B-tree node for data entry containing key <=
target.
Pre: nodePtr is pointer to non-null node.
target is key to be located. AVL Tree Concepts
Return index to entry with key <= target. AVL Balance
0 if key < first entry in node AVL Tree
Operations

if target < nodePtr−>entry[1].data.key then Multiway Trees

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

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. AVL Tree Concepts

upEntry contains entry being inserted into split AVL Balance

AVL Tree
node. Operations
Post: upEntry now contains entry to be inserted Multiway Trees

into parent. B-Trees

minEntries = minimum number of entries


allocate (rightPtr)
// Build right subtree node
if entryNdx <= minEntries then
fromNdx = minEntries + 1
else
8.67
AVL Trees, B-Trees
B-Tree Insertion
Dr. Nguyen Ho
else Man Rang

fromNdx = minEntries + 2
end
toNdx = 1
rightPtr->numEntries = node->numEntries –
fromNdx + 1 AVL Tree Concepts

while fromNdx <= node->numEntries do AVL Balance

rightPtr->entries[toNdx] = AVL Tree


Operations
node->entries[fromNdx] Multiway Trees

fromNdx = fromNdx + 1 B-Trees

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

end AVL Tree


Operations
// Build entry for parent Multiway Trees
medianNdx = minEntries + 1 B-Trees
upEntry.data = node->entries[medianNdx].data
upEntry.rightPtr = rightPtr
rightPtr->firstPtr =
node->entries[medianNdx].rightPtr
return
End splitNode

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

newEntry contains data to be inserted. AVL Balance

Post: data has been inserted in sequence. AVL Tree


Operations

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

• It must take place at a leaf node. AVL Tree Concepts

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

entry on its left subtree. B-Trees

8.71
AVL Trees, B-Trees
B-Tree Deletion
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.72
AVL Trees, B-Trees
B-Tree Deletion
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.73
AVL Trees, B-Trees
Reflow
Dr. Nguyen Ho
Man Rang

For each node to have sufficient number of AVL Tree Concepts

entries: AVL Balance

AVL Tree

• Balance: shift data among nodes. Operations

Multiway Trees

• Combine: join data from nodes. B-Trees

8.74
AVL Trees, B-Trees
Balance
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.75
AVL Trees, B-Trees
Balance
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.76
AVL Trees, B-Trees
Combine
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

AVL Balance

AVL Tree
Operations

Multiway Trees

B-Trees

8.77
AVL Trees, B-Trees
B-Tree Traversal
Dr. Nguyen Ho
Man Rang

AVL Tree Concepts

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

while scanCount <= root−>numEntries do AVL Balance

if ptr not null then AVL Tree


Operations
BTreeTraversal(ptr) Multiway Trees
end B-Trees
scanCount = scanCount + 1
if scanCount <= root−>numEntries then
process (root−>entries[scanCount].data)
ptr = root−>entries[scanCount].rightPtr
end
end
return
End BTreeTraversal 8.79
AVL Trees, B-Trees
B-Tree Search
Dr. Nguyen Ho
Man Rang
Algorithm BTreeSearch(val root <pointer>, val
target <key>, ref node <pointer>, ref entryNo
<index>)
Recursively searches a B-tree for the target key.
AVL Tree Concepts
Pre: root is pointer to a tree or subtree AVL Balance
target is the data to be located AVL Tree
Operations

Post: Multiway Trees

if found – – B-Trees

node is pointer to located node


entryNo is entry within node
if not found – –
node is null and entryNo is zero

Return found <boolean>


8.80
AVL Trees, B-Trees
B-Tree Search
Dr. Nguyen Ho
if target < first entry then Man Rang
return BTreeSearch (root−>firstPtr, target, node,
entryNo)
else
entryNo = root−>numEntries
while target < root−>entries[entryNo].data.key do
AVL Tree Concepts
entryNo = entryNo - 1
AVL Balance
end
AVL Tree
if target = root−>entries[entryNo].data.key then Operations

found = true Multiway Trees

node = root B-Trees


else
return BTreeSearch
(root−>entries[entryNo].rightPtr, target,
node, entryNo)
end
end
return found
End BTreeSearch 8.81
AVL Trees, B-Trees
B-Tree Variations
Dr. Nguyen Ho
Man Rang

• B*Tree: the minimum number of (used)


entries is two thirds. AVL Tree Concepts

AVL Balance
• B+Tree: AVL Tree
Operations
• Each data entry must be represented at Multiway Trees

the leaf level. B-Trees

• Each leaf node has one additional


pointer to move to the next leaf node.

8.82

You might also like