Documente Academic
Documente Profesional
Documente Cultură
Lecture 5
Balanced BSTs
A B-Tree is a perfectly balanced tree in which each node can have up to (B-1)
entries and up to B children. Each internal node has one more child than it has
entries.
Balanced BSTs: 2-3-4 Tree
A 2-3-4 Tree is a B-Tree of order 4.
20 40 70
10 26 55 88 90
1 3 4 17 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
● Entry find(K k):
○ At each node, check for a key matching k.
○ If not found, move down to the appropriate child by comparing k against the keys in the node.
○ Continue until k is found, or at a leaf not containing k.
find(3)
20 40 70
10 26 55 88 90
1 3 4 17 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
● Entry find(K k):
○ At each node, check for a key matching k.
○ If not found, move down to the appropriate child by comparing k against the keys in the node.
○ Continue until k is found, or at a leaf not containing k.
find(3)
20 40 70
10 26 55 88 90
1 3 4 17 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
● insert(K k, V v): (inserting key value pair)
○ Walk down the tree like finding k.
○ When encountering a 3-key-node, kick the middle value up as shown: 20
○ This ensures there’s room when we reach a leaf (and room to kick up)
○ When reaching a leaf, add the key to it’s ordered position.
1 30 40 70
20 40
1 30 70
Balanced BSTs: 2-3-4 Tree
● insert(K k, V v): (inserting key value pair)
○ Walk down the tree like finding k.
○ When encountering a 3-key-node, kick the middle value up as shown:
○ This ensures there’s room when we reach a leaf (and room to kick up)
○ When reaching a leaf, add the key to it’s ordered position.
insert(8)
20 40 70
10 18 26 55 88 90
1 3 4 17 19 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
insert(8)
40
20 70
10 18 26 55 88 90
1 3 4 17 19 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
insert(8)
40
20 70
10 18 26 55 88 90
1 3 4 17 19 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
insert(8)
40
20 70
3 10 18 26 55 88 90
1 4 17 19 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
insert(8)
40
20 70
3 10 18 26 55 88 90
1 4 8 17 19 22 39 41 66 77 89 99
Balanced BSTs: 2-3-4 Tree
insert(8)
40
Newly created 3-node;
will be broken up next
insert through this path 20 70
3 10 18 26 55 88 90
1 4 8 17 19 22 39 41 66 77 89 99
Balanced BSTs: Runtime
● Between 2h and 4h leaves in a 2-3-4 tree.
● Thus n >= 2h and log(n) >= h, meaning h = O(log(n))
● Thus insert is in O(log(n))
Balanced BSTs: Red-Black
● B-Trees can be hard to code, especially the remove() operation (omitted)
● Red-Black trees are binary search trees that maintain the balanced-ness
guarantee of 2-3-4 trees.
● Maintain certain invariants through rotations:
○ A node is either red or black.
○ The root & leaves are black.
○ If a node is red, both of its children are black.
○ Every path from a given node to any of its leaf descendants must contain the same number of
black nodes.
● Convertible to 2-3-4 trees and vice versa
● Used in , !
Balanced BSTs: Red-Black conversion
20 40 70
10 18 26 55 88 90
40
20 70
10 26 55 88
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Retrieves and removes the head of this queue, or returns null if this queue is empty.
Min (Max) Heap
A min (max) heap is a tree with the following two
1
properties/invariants:
7 4 6 8
Min (Max) Heap
A min (max) heap is a tree with the following two
1
properties/invariants:
3 5
7 4 6 8
1 3 5 7 4 6 8
What do you think? Min (Max) Heap
Is there any relationship between two nodes , where
≠ , is not a descendant of and is not a 1
descendant of ? No.
7 4 6 8
1 3 5 7 4 6 8
What do you think? Min (Max) Heap
Is there any relationship between two nodes , where
≠ , is not a descendant of and is not a 1
descendant of ? No.
7
Binary Heap:
1. Place the element in the next open spot in the array (leftmost open spot in the
last row of the heap).
2. Swap elements upwards (by comparing the added element with parent
elements) until the heap invariants are satisfied.
7 7
5
Binary Heap:
1. Place the element in the next open spot in the array (leftmost open spot in the
last row of the heap).
2. Swap elements upwards (by comparing the added element with parent
elements) until the heap invariants are satisfied.
7 7 5
5 7
Binary Heap:
1. Place the element in the next open spot in the array (leftmost open spot in the
last row of the heap).
2. Swap elements upwards (by comparing the added element with parent
elements) until the heap invariants are satisfied.
7 7 5 5
5 7 7 3
Binary Heap:
1. Place the element in the next open spot in the array (leftmost open spot in the
last row of the heap).
2. Swap elements upwards (by comparing the added element with parent
elements) until the heap invariants are satisfied.
7 7 5 5 3
5 7 7 3 7 5
Binary Heap:
7 5
4
Binary Heap:
3 3
7 5 4 5
4 7
Binary Heap:
3
3
What happens if we call
7 5 and then ?
4 5
7 1
3 3
7 5 4 5
4 7
Binary Heap:
3
3 3
What happens if we call
7 5 and then ?
4 5 1 5
7 1 7 4
3 3
7 5 4 5
4 7
Binary Heap:
3
3 3
What happens if we call
7 5 and then ?
4 5 1 5
7 1 7 4
3 3
1
7 5 4 5
3 5
4 7
7 4
Binary Heap:
1. Swap the root with the last element in the array (rightmost filled spot in the
last row of the heap).
2. Swap elements downwards (by comparing the new root element with its
children) until the heap invariants are satisfied.
5 7
Binary Heap:
1. Swap the root with the last element in the array (rightmost filled spot in the
last row of the heap).
2. Swap elements downwards (by comparing the new root element with its
children) until the heap invariants are satisfied.
3 7
5 7 5 3
Binary Heap:
1. Swap the root with the last element in the array (rightmost filled spot in the
last row of the heap).
2. Swap elements downwards (by comparing the new root element with its
children) until the heap invariants are satisfied.
3 7 5
5 7 5 3 7
Binary Heap:
1. Swap the root with the last element in the array (rightmost filled spot in the
last row of the heap).
2. Swap elements downwards (by comparing the new root element with its
children) until the heap invariants are satisfied.
3 7 5 5
5 7 5 3 7 7
Binary Heap:
1. Swap the root with the last element in the array (rightmost filled spot in the
last row of the heap).
2. Swap elements downwards (by comparing the new root element with its
children) until the heap invariants are satisfied.
3 7 5 5 7
7
5 7 5 3 7 7 5
Binary Heap:
1
3 5
7 4
What happens if we
call twice?
Binary Heap:
1 1
3 5 3 5
7 4 7 4
What happens if we
call twice?
Binary Heap:
1 1 4
3 5 3 5 3 5
7 4 7 4 7 1
What happens if we
call twice?
Binary Heap:
1 1 4 3
3 5 3 5 3 5 4 5
7 4 7 4 7 1 7
What happens if we
call twice?
Binary Heap:
1 1 4 3
3 5 3 5 3 5 4 5
7 4 7 4 7 1 7
What happens if we 3
call twice?
4 5
7
Binary Heap:
1 1 4 3
3 5 3 5 3 5 4 5
7 4 7 4 7 1 7
What happens if we 3 7
call twice?
4 5 4 5
7 3
Binary Heap:
1 1 4 3
3 5 3 5 3 5 4 5
7 4 7 4 7 1 7
What happens if we 3 7 4
call twice?
4 5 4 5 7 5
7 3
Worst-case runtime of heap operations
Operation Description Runtime for binary heap Runtime for -ary heap
Operation Description Runtime for binary heap Runtime for -ary heap
Operation Description Runtime for binary heap Runtime for -ary heap
Operation Description Runtime for binary heap Runtime for -ary heap
Operation Description Runtime for binary heap Runtime for -ary heap
Operation Description Runtime for binary heap Runtime for -ary heap
4 6
8 1 3 5
Heapify 7 4 6 8 1 3 5
4 6
8 1 3 5
Heapify 7 4 3 8 1 6 5
7 7
4 6 4 3
8 1 3 5 8 1 6 5
Heapify 7 4 3 8 1 6 5
7 7
4 6 4 3
8 1 3 5 8 1 6 5
Heapify 7 4
1 3 8 1
4 6 5
7 7 7
4 6 4 3 1 3
8 1 3 5 8 1 6 5 8 4 6 5
Heapify 7 4
1 3 8 1
4 6 5
7 7 7
4 6 4 3 1 3
8 1 3 5 8 1 6 5 8 4 6 5
Heapify 7
1 4
7 3 8 7
1
4 6 5
7 7 7
4 6 4 3 1 3
8 1 3 5 8 1 6 5 8 4 6 5
7 3
8 4 6 5
Heapify 7
1 4 3 8 1
7 6 5
7 7 7
4 6 4 3 1 3
8 1 3 5 8 1 6 5 8 4 6 5
1 1
7 3 4 3
8 4 6 5 8 7 6 5
Heapify 7
1 4 3 8 1
7 6 5
7 7 7
4 6 4 3 1 3
8 1 3 5 8 1 6 5 8 4 6 5
1 1
7 3 4 3
8 4 6 5 8 7 6 5
Worst-Case Heapify Runtime Proof Legend
height of tree
level 1
... ...
... level h
Worst-Case Heapify Runtime Proof Legend
height of tree
level 1
... ...
... level h
Worst-Case Heapify Runtime Proof Legend
height of tree
level 1
... ...
... level h
Worst-Case Heapify Runtime Proof Legend
height of tree
level 1
... ...
level 1
... ...
level 1
... ...
How? WolframAlpha :3
How? WolframAlpha :3
Recall that .
ϴ
*Why do we care about the height of the nodes?
This is equal to the max # of swaps to move the
node into the correct position (at the bottom).
Project 2 Open Q&A