cs2133 Lec13
cs2133 Lec13
Lecture No. 13
3 9 18
7 16 20
5
2
1
11-Dec-18
dummy
14
4 15
3 9 18
7 16 20
5
3
Inorder traversal
void fastInorder(TreeNode* p)
{
while((p=nexInorder(p)) != dummy)
cout << p->getInfo();
}
Start the inorder traversal by calling
fastInorder(dummy).
2
11-Dec-18
Trace or nextInorder
p dummy
14
4 15
3 9 18
7 16 20
5
5
Trace or nextInorder
dummy p
14
4 15
3 9 18
7 16 20
5
6
3
11-Dec-18
Trace or nextInorder
dummy
p 14
4 15
3 9 18
7 16 20
5
7
Trace or nextInorder
dummy
14
p 4 15
3 9 18
7 16 20
5
8
4
11-Dec-18
Trace or nextInorder
dummy
14
4 15
p 3 9 18
7 16 20
5
9
Trace or nextInorder
dummy
14
4 p 15
3 9 18
7 16 20
5
10
5
11-Dec-18
Trace or nextInorder
dummy
14
4 15
3 9 18
7 16 20
p 5
11
Trace or nextInorder
dummy
14
4 15
3 9 18
7 p 16 20
5
And so on. 12
6
11-Dec-18
13
we had.
14
7
11-Dec-18
B C
D E F G
H I J
15
16
8
11-Dec-18
B C
D E F G
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
17
For any array element at position i, the left child is at 2i, the right
child is at (2i +1) and the parent is at i 2.
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
18
9
11-Dec-18
For any array element at position i, the left child is at 2i, the right
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
19
For any array element at position i, the left child is at 2i, the right
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
20
10
11-Dec-18
For any array element at position i, the left child is at 2i, the right
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
21
For any array element at position i, the left child is at 2i, the right
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
22
11
11-Dec-18
1 A
2 3
B C
4 5 6 7
D E F G
8 9 10
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Level-order numbers array index 23
1 A
2 3
B C
4 5 6 7
D E F G
8 9 10
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
24
12
11-Dec-18
1 A
2 3
B C
4 5 6 7
D E F G
8 9 10
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
26
13
11-Dec-18
Heap
The heap order property: in a (min) heap, for every node X, the
key in the parent is smaller than (or equal to) the key in X.
Or, the parent node has key smaller than or equal to both of its
children nodes.
27
Heap
13
21 16
24 31 19 68
65 26 32 This is a min
heap
28
14
11-Dec-18
Heap
13
21 19
6 31 16 68
65 26 32
29
Heap
30
15
11-Dec-18
1
13
2 21 3 16
4 24 5 31 6 19 7 68
8 9 26 10 32
65 This is an existing heap
13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9 10 11 12
31
13 14
1
insert(14) 13
2 21 3 16
4 24 5 31 6 19 7 68
8 9 26 10 32 11
65 14
13 21 16 24 31 19 68 65 26 32 14
0 1 2 3 4 5 6 7 8 9 10 11 12
32
13 14
16
11-Dec-18
1
insert(14) 13
2 21 3 16
4 24 5 31 6 19 7 68
8 9 26 10 32 11
65
13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9 10 11 12
33
13 14
1
insert(14) 13
2 21 3 16
4 24 5 6 19 7 68
8 9 26 10 32 11 31
65
13 21 16 24 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12
34
13 14
17
11-Dec-18
1
insert(14) 13
2 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
13 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12
35
13 14
1
insert(14) 13
2 14 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12
36
13 14
18
11-Dec-18
1
insert(14) with 13
exchange
2 21 3 16
4 24 5 31 6 19 7 68
8 9 26 10 32 11
65 14
13 21 16 24 31 19 68 65 26 32 14
0 1 2 3 4 5 6 7 8 9 10 11 12
37
13 14
1
insert(14) with 13
exchange
2 21 3 16
4 24 5 14 6 19 7 68
8 9 26 10 32 11 31
65
13 21 16 24 14 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12
38
13 14
19
11-Dec-18
1
insert(14) with 13
exchange
2 14 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
13 14 16 24 21 19 68 65 26 32 31
0 1 2 3 4 5 6 7 8 9 10 11 12
39
13 14
1
insert(15) with 13
exchange
2 14 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31 12
65 15
13 14 16 24 21 19 68 65 26 32 31 15
0 1 2 3 4 5 6 7 8 9 10 11 12
40
13 14
20
11-Dec-18
1
insert(15) with 13
exchange
2 14 3 16
4 24 5 6 7 68
21 15
8 9 26 10 32 11 31 12 19
65
13 14 16 24 21 15 68 65 26 32 31 19
0 1 2 3 4 5 6 7 8 9 10 11 12
41
13 14
1
insert(15) with 13
exchange
2 14 3 15
4 24 5 6 16 7 68
21
8 9 26 10 32 11 31 12 19
65
13 14 15 24 21 16 68 65 26 32 31 19
0 1 2 3 4 5 6 7 8 9 10 11 12
42
13 14
21
11-Dec-18
1
insert(15) with 13
exchange
2 14 3 15
4 24 5 6 16 7 68
21
8 9 26 10 32 11 31 12 19
65
13 14 15 24 21 16 68 65 26 32 31 19
0 1 2 3 4 5 6 7 8 9 10 11 12
43
13 14
DeleteMin
2 14 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
44
22
11-Dec-18
DeleteMin
deleteMin()
1
2 14 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
45
DeleteMin
deleteMin()
1
14
2 3 16
4 24 5 6 19 7 68
21
8 9 26 10 32 11 31
65
46
23
11-Dec-18
DeleteMin
deleteMin()
1
14
2 21 3 16
4 24 5 6 19 7 68
8 9 26 10 32 11 31
65
47
DeleteMin
deleteMin()
1
14
2 21 3 16
4 24 5 6 19 7 68
31
8 9 26 10 32 11
65
48
24
11-Dec-18
DeleteMin
2 21 3 16
4 24 5 6 19 7 68
31
8 9 26 10 32
65
49
BuildHeap
Each call to insert will either take unit time (leaf node) or log2N (if
50
25
11-Dec-18
BuildHeap
51
BuildHeap
52
26
11-Dec-18
BuildHeap
65 31 32 26 21 19 68 13 24 15 14 16 5 70 12
0 1 2 3 4 5 6 7 8 9 10 11 12
53
13 14 15
BuildHeap
2 31 3 32
4 26 5 6 19 7 68
21
8 9 24 10 15 11 14 12 16 13 5 14 70 15 12
13
65 31 32 26 21 19 68 13 24 15 14 16 5 70 12
0 1 2 3 4 5 6 7 8 9 10 11 12
54
13 14 15
27
11-Dec-18
BuildHeap
percolateDown(i);
55
BuildHeap
1
i = 15/2 = 7 65 Why I=n/2?
2 31 3 32
4 26 5 6 19 7 68 i
21
8 9 24 10 15 11 14 12 16 13 5 14 70 15 12
13
i
65 31 32 26 21 19 68 13 24 15 14 16 5 70 12
0 1 2 3 4 5 6 7 8 9 10 11 12
56
13 14 15
28
11-Dec-18
BuildHeap
1
i = 15/2 = 7 65
2 31 3 32
4 26 5 6 19 7 12 i
21
8 9 24 10 15 11 14 12 16 13 5 14 70 15 68
13
i
65 31 32 26 21 19 12 13 24 15 14 16 5 70 68
0 1 2 3 4 5 6 7 8 9 10 11 12
57
13 14 15
BuildHeap
1
i=6 65
2 31 3 32
4 26 5 6 19 i 7 12
21
8 9 24 10 15 11 14 12 16 13 5 14 70 15 68
13
i
65 31 32 26 21 19 12 13 24 15 14 16 5 70 68
0 1 2 3 4 5 6 7 8 9 10 11 12
58
13 14 15
29
11-Dec-18
BuildHeap
1
i=5 65
2 31 3 32
4 26 5 65 7 12
21 i
8 9 24 10 15 11 14 12 16 13 19 14 70 15 68
13
i
65 31 32 26 21 5 12 13 24 15 14 16 19 70 68
0 1 2 3 4 5 6 7 8 9 10 11 12
59
13 14 15
30