0% found this document useful (0 votes)
17 views30 pages

cs2133 Lec13

Uploaded by

Kashif Alirana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views30 pages

cs2133 Lec13

Uploaded by

Kashif Alirana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

11-Dec-18

Lecture No. 13

Calling nextInorder with root


TreeNode* nextInorder(TreeNode* p){
if(p->RTH == thread) return(p->R);
else {
p = p->R;
while(p->LTH == child)
p = p->L;
return p; 14
}
}
4 15 p?

3 9 18

7 16 20

5
2

1
11-Dec-18

Fix with Dummy Node

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

Complete Binary Tree

13

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.

14

7
11-Dec-18

Complete Binary Tree

B C

D E F G

H I J

15

Complete Binary Tree

 Recall that such a tree of height h has between 2h to 2h+1 –1 nodes.

 The height of such a tree is thus log2N where N is the number of

nodes in the tree.

 Because the tree is so regular, it can be stored in an array; no

pointers are necessary.

16

8
11-Dec-18

Complete Binary Tree

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

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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

18

9
11-Dec-18

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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

19

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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

20

10
11-Dec-18

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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

21

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
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

22

11
11-Dec-18

Complete Binary Tree

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

Complete Binary Tree

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

Complete Binary Tree

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

 Question: why don’t we store all binary trees in arrays?


Why use pointers? 25

The Heap ADT

26

13
11-Dec-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.

27

Heap

13

21 16

24 31 19 68

65 26 32 This is a min
heap

28

14
11-Dec-18

Heap

Not a heap: heap property violated

13

21 19


6 31 16 68

65 26 32

29

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.

30

15
11-Dec-18

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

Inserting into a Heap

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

 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.
1
13

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

deleteMin(): heap size is reduced by 1.


1
14

2 21 3 16

4 24 5 6 19 7 68
31

8 9 26 10 32
65

49

BuildHeap

 Suppose we are given as input N keys (or items) and we want to

build a heap of the keys.

 Obviously, this can be done with N successive inserts.

 Each call to insert will either take unit time (leaf node) or log2N (if

new key percolates all the way up to the root).

50

25
11-Dec-18

BuildHeap

 The worst time for building a heap of N keys could be Nlog2N.

 It turns out that we can build a heap in linear time.

51

BuildHeap

 Suppose we have a method percolateDown(p) which moves down

the key in node p downwards.

 This is what was happening in deleteMin.

52

26
11-Dec-18

BuildHeap

Initial data (N=15)

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

Initial data (N=15) 1


65

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

 The general algorithm is to place the N keys in an array and

consider it to be an unordered binary tree.

 The following algorithm will build a heap out of N keys.

for( i = N/2; i > 0; i-- )

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

You might also like