1
Data Structures
 Mohammad Nazir
2
Complete Binary Tree
3
Complete Binary Tree
 A complete binary tree is a tree that is
completely filled, with the possible
exception of the bottom level.
 The bottom level is filled from left to right.
 Different definition of complete binary tree
than an earlier definition we had.
 The earlier definition could be called a
perfect binary tree.
4
Complete Binary Tree
A
CB
E
I
D
H J
GF
5
Complete Binary Tree
 Recall that such a tree of height h has
between 2h to 2h+1 –1 nodes.
 Because the tree is so regular, it can be
stored in an array; no pointers are
necessary.
6
Complete Binary Tree
A
CB
E
I
D
H J
GF
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
7
Complete Binary Tree
 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 floor(i /2).
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
8
Complete Binary Tree
 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
1 2 3 4 5 6 7 8 9 10 11 12 13 140
9
Complete Binary Tree
 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
1 2 3 4 5 6 7 8 9 10 11 12 13 140
10
Complete Binary Tree
 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
1 2 3 4 5 6 7 8 9 10 11 12 13 140
11
Complete Binary Tree
 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
1 2 3 4 5 6 7 8 9 10 11 12 13 140
12
Complete Binary Tree
Level-order numbers  array index
A
CB
E
I
D
H J
GF
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
4 5 6 7
8 9 10
13
Complete Binary Tree
A
CB
E
I
D
H J
GF
1
2 3
4 5 6 7
8 9 10
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
14
Complete Binary Tree
 Question: why don’t we store all binary trees in arrays?
Why use pointers?
A
CB
E
I
D
H J
GF
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
4 5 6 7
8 9 10
15
Complete Binary Tree
 Question: why don’t we store all binary trees in arrays?
Why use pointers?
A
CB
E
I
D
H J
GF
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
4 5 6 7
8 9 10
16
Complete Binary Tree
 Question: why don’t we store all binary trees in arrays?
Why use pointers?
A
CB
E
I
D
H J
GF
A B C D E F G H I J
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
4 5 6 7
8 9 10
17
The Heap ADT
18
Heap
 A heap is a complete binary tree that
conforms to the heap order.
 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.
19
Heap
13
1621
31
26
24
65 32
6819
This is a min
heap
20
Heap
Not a heap: heap property violated
13
1921
31
26
6
65 32
6816


21
Heap
 Analogously, we can define a max-heap,
where the parent has a key larger than the
its two children.
 Thus the largest key would be in the root.
22
Inserting into a Heap
13
1621
31
26
24
65 32
6819
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
This is an existing heap
23
Inserting into a Heap
insert(14) 13
1621
31
26
24
65 32
6819
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
1411
24
Inserting into a Heap
insert(14) 13
1621
31
26
24
65 32
6819
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10 11
25
Inserting into a Heap
insert(14) 13
1621
3126
24
65 32
6819
13 21 16 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10 11
26
Inserting into a Heap
insert(14) 13
16
21
3126
24
65 32
6819
13 2116 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10 11
27
Inserting into a Heap
insert(14) 13
16
21
3126
24
65 32
6819
13 2116 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10 11
14
14
28
Inserting into a Heap
insert(14) with
exchange
13
1621
31
26
24
65 32
6819
13 21 16 24 31 19 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
1411
29
Inserting into a Heap
insert(14) with
exchange
13
1621
3126
24
65 32
6819
13 21 16 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
30
Inserting into a Heap
insert(14) with
exchange
13
16
21
3126
24
65 32
6819
13 2116 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
31
Inserting into a Heap
insert(15) with
exchange
13
16
21
3126
24
65 32
6819
13 2116 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
32
Inserting into a Heap
insert(15) with
exchange
13
16
21
3126
24
65 32
68
19
13 2116 24 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
33
Inserting into a Heap
insert(15) with
exchange
13
1621
3126
24
65 32
68
19
13 21 1624 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
34
Inserting into a Heap
insert(15) with
exchange
13
1621
3126
24
65 32
68
19
13 21 1624 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
35
DeleteMin
 Finding the minimum is easy; it is at the top of the
heap.
 Deleting it (or removing it) causes a hole which needs
to be filled.
13
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10
14
11
36
DeleteMin
deleteMin()
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10
14
11
37
DeleteMin
deleteMin()
14
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10 11
38
DeleteMin
deleteMin()
14
16
3126
24
65 32
6819
1
2 3
7654
8 9 10
21
11
39
DeleteMin
deleteMin()
14
16
31
26
24
65 32
6819
1
2 3
7654
8 9 10
21
11
40
DeleteMin
deleteMin(): heap size is reduced by 1.
14
16
31
26
24
65 32
6819
1
2 3
7654
8 9 10
21

Complete binary tree and heap

  • 1.
  • 2.
  • 3.
    3 Complete Binary Tree A complete binary tree is a tree that is completely filled, with the possible exception of the bottom level.  The bottom level is filled from left to right.  Different definition of complete binary tree than an earlier definition we had.  The earlier definition could be called a perfect binary tree.
  • 4.
  • 5.
    5 Complete Binary Tree Recall that such a tree of height h has between 2h to 2h+1 –1 nodes.  Because the tree is so regular, it can be stored in an array; no pointers are necessary.
  • 6.
    6 Complete Binary Tree A CB E I D HJ GF A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 7.
    7 Complete Binary Tree 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 floor(i /2). A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 8.
    8 Complete Binary Tree 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 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 9.
    9 Complete Binary Tree 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 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 10.
    10 Complete Binary Tree 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 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 11.
    11 Complete Binary Tree 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 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 12.
    12 Complete Binary Tree Level-ordernumbers  array index A CB E I D H J GF A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10
  • 13.
    13 Complete Binary Tree A CB E I D HJ GF 1 2 3 4 5 6 7 8 9 10 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140
  • 14.
    14 Complete Binary Tree Question: why don’t we store all binary trees in arrays? Why use pointers? A CB E I D H J GF A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10
  • 15.
    15 Complete Binary Tree Question: why don’t we store all binary trees in arrays? Why use pointers? A CB E I D H J GF A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10
  • 16.
    16 Complete Binary Tree Question: why don’t we store all binary trees in arrays? Why use pointers? A CB E I D H J GF A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10
  • 17.
  • 18.
    18 Heap  A heapis a complete binary tree that conforms to the heap order.  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.
  • 19.
  • 20.
    20 Heap Not a heap:heap property violated 13 1921 31 26 6 65 32 6816  
  • 21.
    21 Heap  Analogously, wecan define a max-heap, where the parent has a key larger than the its two children.  Thus the largest key would be in the root.
  • 22.
    22 Inserting into aHeap 13 1621 31 26 24 65 32 6819 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 This is an existing heap
  • 23.
    23 Inserting into aHeap insert(14) 13 1621 31 26 24 65 32 6819 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 1411
  • 24.
    24 Inserting into aHeap insert(14) 13 1621 31 26 24 65 32 6819 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 11
  • 25.
    25 Inserting into aHeap insert(14) 13 1621 3126 24 65 32 6819 13 21 16 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 11
  • 26.
    26 Inserting into aHeap insert(14) 13 16 21 3126 24 65 32 6819 13 2116 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 11
  • 27.
    27 Inserting into aHeap insert(14) 13 16 21 3126 24 65 32 6819 13 2116 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 11 14 14
  • 28.
    28 Inserting into aHeap insert(14) with exchange 13 1621 31 26 24 65 32 6819 13 21 16 24 31 19 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 1411
  • 29.
    29 Inserting into aHeap insert(14) with exchange 13 1621 3126 24 65 32 6819 13 21 16 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11
  • 30.
    30 Inserting into aHeap insert(14) with exchange 13 16 21 3126 24 65 32 6819 13 2116 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11
  • 31.
    31 Inserting into aHeap insert(15) with exchange 13 16 21 3126 24 65 32 6819 13 2116 24 3119 68 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11 15 15 12
  • 32.
    32 Inserting into aHeap insert(15) with exchange 13 16 21 3126 24 65 32 68 19 13 2116 24 31 1968 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11 15 15 12
  • 33.
    33 Inserting into aHeap insert(15) with exchange 13 1621 3126 24 65 32 68 19 13 21 1624 31 1968 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11 15 15 12
  • 34.
    34 Inserting into aHeap insert(15) with exchange 13 1621 3126 24 65 32 68 19 13 21 1624 31 1968 65 26 32 1 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 7654 8 9 10 14 14 11 15 15 12
  • 35.
    35 DeleteMin  Finding theminimum is easy; it is at the top of the heap.  Deleting it (or removing it) causes a hole which needs to be filled. 13 16 21 3126 24 65 32 6819 1 2 3 7654 8 9 10 14 11
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
    40 DeleteMin deleteMin(): heap sizeis reduced by 1. 14 16 31 26 24 65 32 6819 1 2 3 7654 8 9 10 21