Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Lecture 28 Sohail Aslam.

Similar presentations


Presentation on theme: "Data Structures Lecture 28 Sohail Aslam."— Presentation transcript:

1 Data Structures Lecture 28 Sohail Aslam

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 15 4 9 7 18 3 5 16 20 p Start lecture 28

3 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 15 4 9 7 18 3 5 16 20 p? End of lecture 27.

4 Fix with Dummy Node dummy 14 4 15 3 9 18 7 16 20 5

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

6 Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

7 Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

8 Trace or nextInorder dummy p 14 4 15 3 9 18 7 16 20 5

9 Trace or nextInorder dummy 14 p 4 15 3 9 18 7 16 20 5

10 Trace or nextInorder dummy 14 4 15 p 3 9 18 7 16 20 5

11 Trace or nextInorder dummy 14 4 15 p 3 9 18 7 16 20 5

12 Trace or nextInorder dummy 14 4 15 3 9 18 7 16 20 p 5

13 Trace or nextInorder dummy 14 4 15 3 9 18 7 p 16 20 5 And so on.

14 Complete Binary Tree

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

16 Complete Binary Tree A B C D E F G H I J

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

18 Complete Binary Tree A B C D E F G H I J A B C D E F G H I J 1 2 3 4 5
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 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 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 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 1 2 3 4 5 6 7 8 9 10 11 12 13 14

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

24 Level-order numbers  array index
Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Level-order numbers  array index

25 Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D
1 2 3 4 5 6 7 8 9 10 11 12 13 14

26 Complete Binary Tree 1 A 2 3 B C 5 6 7 4 D E F G 8 9 10 H I J A B C D
End of lecture 28. Start of lecture 29. A B C D E F G H I J 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?


Download ppt "Data Structures Lecture 28 Sohail Aslam."

Similar presentations


Ads by Google