Professional Documents
Culture Documents
July 9, 2015
⃝
c GfH&JPK
#4: Heaps + Search trees
First heaps
• what is a heap?
• how to build a heap?
• use of heaps for priority queues and sorting
⃝
c GfH&JPK 1
#4: Heaps + Search trees
Trees in arrays
16
16 14 10 8 7 9 3 2 4 1
1 2 3 4 5 6 7 8 9 10
14 10
8 7 9 3
2 4 1
⃝
c GfH&JPK 2
#4: Heaps + Search trees
Heaps
Such a special binary tree is a heap if for the keys in the nodes:
• each node key exceeds that of all its children, or this is a maxheap
• the keys of all its children exceed the key of the node. this is a minheap
⃝
c GfH&JPK 3
#4: Heaps + Search trees
⃝
c GfH&JPK 4
#4: Heaps + Search trees
Heapify – Strategy
Consider E[i] and assume its left and right subtrees are heaps
Construct a heap from the left- and right subtrees and E[i], let the value
of E[i] “float down” in the structure such that the structure rooted at E[i]
is a heap
⃝
c GfH&JPK 5
#4: Heaps + Search trees
Heapify – Algorithm
⃝
c GfH&JPK 6
#4: Heaps + Search trees
Heapify – Example
1 4 1
Heapify(E,1) 16 Heapify(E,2)
2 3
Heap 2
16 10
3 Heap 4 10
4 5 6 7 4 5 6 7
7 9 3 14 7 9 3
14
9 9 10
8 10 2 1
2 8 1 8
Heap
16 Heapify(E,4) 16
2 3 2 3
14 10 14 10
4 5 6 7 4 5 6 7
4 7 9 3 8 7 9 3
9 10 9 10
2 8 1 2 4 1
⃝
c GfH&JPK 7
#4: Heaps + Search trees
⃝
c GfH&JPK 8
#4: Heaps + Search trees
1 4 1 4 Heapify(E,4)
Heapify(E,5)
2 3 3 2
1 1 3 3
4 5 16 6 7 4 5 16 6 7
2 9 10 2 9 10
8 9 10 8 9 10
14 8 7 14 8 7
4 1 3 2 16 9 10 14 8 7
10 elements
⃝
c GfH&JPK 9
#4: Heaps + Search trees
2 2 3
1 3 3 1 10
4 5 16 6 7 4 5 16 6 7
14 9 10 14 9 3
8 9 10 8 9 10
2 8 7 2 8 7
1 4 1 4 Heapify(E,1)
recursion
Heapify(E,5)
2 16 3 2 16 3
10 10
4 5 6 7 4 5 6 7
14 1 9 3 14 7 9 3
8 9 10 8 9 10
2 8 7 2 8 1
⃝
c GfH&JPK 10
#4: Heaps + Search trees
⃝
c GfH&JPK 11
#4: Heaps + Search trees
⃝
c GfH&JPK 12
#4: Heaps + Search trees
Heapsort – Algorithm
void heapSort(int E[ ])
{ int i;
buildHeap(E);
for (i = E.length; i > 2; i −−)
{ swap(E[i], E[1]);
E.heapsize = E.heapsize−1;
Heapify(E,1);
}
}
⃝
c GfH&JPK 13
#4: Heaps + Search trees
∑
n−1 ∫ n
W (n) = 2·⌊log i⌋ 6 2 · (log e) ln x dx = 2·n· log n + c1·n + c2
i=1 1
⃝
c GfH&JPK 14
#4: Heaps + Search trees
⃝
c GfH&JPK 15
#4: Heaps + Search trees
6
3
3 7
7
2 5 9
6 9
⃝
c GfH&JPK 16
#4: Heaps + Search trees
12 parent
key A of A and B
left right
left parent right
child of A child of A
6 225
B C
⃝
c GfH&JPK 17
#4: Heaps + Search trees
• the key at a node is greater than all keys in its left subtree
• the key at a node is less or equal than all keys in its right subtree
Thus,
⃝
c GfH&JPK 18
#4: Heaps + Search trees
Note that such search procedure does not work for heaps; Why?
⃝
c GfH&JPK 19
#4: Heaps + Search trees
15 15
10 < 15 10 > 5
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 14 17 31
6 lookup key 10 6
15 15
10 < 12
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 14 17 31
6 6
⃝
c GfH&JPK 20
#4: Heaps + Search trees
However, we do not stop if we find a node that has key k; in such a case
we try to insert k in the right subtree of that node.
⃝
c GfH&JPK 21
#4: Heaps + Search trees
⃝
c GfH&JPK 22
#4: Heaps + Search trees
bstIns(T, 17)
30 30
35 13 35
13
38 7 23 38
7 23
18 28 18 28
17
⃝
c GfH&JPK 23
#4: Heaps + Search trees
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 17 31
6 6
15 15
5 16 5
3 12 20 3 12 20
10 14 17 31 10 14 17 31
6 6
⃝
c GfH&JPK 24
#4: Heaps + Search trees
15 6 15
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 14 17 31
15
6 16
3 12 20
10 14 17 31
⃝
c GfH&JPK 25
#4: Heaps + Search trees
• If z has one child, we “splice out” z by linking its parent and its child
⃝
c GfH&JPK 26
#4: Heaps + Search trees
⃝
c GfH&JPK 27
#4: Heaps + Search trees
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 14 17 31
6 successor of 5 6
15 15
5 16 5 16 go right
and then
3 12 20 3 12 20
go left
10 14 17 31 10 14 17 31 as much
as possible
6 6
⃝
c GfH&JPK 28
#4: Heaps + Search trees
5 16 5 16
3 12 20 3 12 20
10 14 17 31 10 14 17 31
6 successor of 14 6
15 15
5 16 5 16
go up left
as far as
12 20
3 12 20 3 possible
10 14 17 31 10 14 17 31 until you
have to
6 6 go up right
⃝
c GfH&JPK 29
#4: Heaps + Search trees
⃝
c GfH&JPK 30
#4: Heaps + Search trees
Operation Time
bstSearch Θ(d)
bstSucc Θ(d)
bstMax Θ(d)
bstIns Θ(d)
bstDel Θ(d)
⃝
c GfH&JPK 31
#4: Heaps + Search trees
Rotations - Concept
leftRotate(1) 2
1
2 1
rightRotate(2)
A C
B C A B
⃝
c GfH&JPK 32
#4: Heaps + Search trees
Rotate-left – Example
15
15
5 leftRotate(5) 12 20
3 12 20 5 14 17 31
10 14 17 31
3 10 13
13
⃝
c GfH&JPK 33
#4: Heaps + Search trees
Rotate-left – Algorithm
void leftRotate (bsttree T, node x)
{ node y = x.right;
x.right = y.left;
(y.left).parent = x;
y.parent = x.parent;
if (x.parent == null )
T.root = y ;
else
if (x == (x.parent).left))
(x.parent).left = y ;
else (x.parent).right = y ;
y.left = x;
x.parent = y ;
}
this algorithm takes Θ(1) time in worst case
rightRotate is similar (symmetric) and is omitted here
⃝
c GfH&JPK 34