You are on page 1of 26

Data Structures

Lecture 28
Sohail Aslam

1
Calling nextInorder with root

TreeNode* nextInorder(TreeNode* p){


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

3 9 18

7 16 20

2
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

3
Fix with Dummy Node

dummy

14

4 15

3 9 18

7 16 20

4
Inorder traversal

void fastInorder(TreeNode* p)
{
while((p=nexInorder(p)) != dummy)
cout << p->getInfo();
}
 Start the inorder traversal by calling

fastInorder(dummy).

5
Trace or nextInorder

p dummy

14

4 15

3 9 18

7 16 20

6
Trace or nextInorder

dummy p

14

4 15

3 9 18

7 16 20

7
Trace or nextInorder

dummy

p 14

4 15

3 9 18

7 16 20

8
Trace or nextInorder

dummy

14

p 4 15

3 9 18

7 16 20

9
Trace or nextInorder

dummy

14

4 15

p 3 9 18

7 16 20

10
Trace or nextInorder

dummy

14

4 p 15

3 9 18

7 16 20

11
Trace or nextInorder

dummy

14

4 15

3 9 18

7 16 20

p 5

12
Trace or nextInorder

dummy

14

4 15

3 9 18

7 p 16 20

5
And so on.
13
Complete Binary Tree

14
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.

15
Complete Binary Tree

B C

D E F G

H I J

16
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.

17
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

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
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
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

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
Level-order numbers  array index
24
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

25
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?
26

You might also like