CS301-Lec28 handout

CS301-Lec28 handout - CS301 Data Structures Lecture No. 28...

This preview shows pages 1–4. Sign up to view the full content.

CS301 – Data Structures Lecture No. 28 ___________________________________________________________________ Data Structures Lecture No. 28 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6 6.3.1 Summary Inorder traversal in threaded trees Complete Binary Tree Inorder traversal in threaded trees Discussion on the inroder traversal of the threaded binary tree will continue in this lecture. We have introduced the threads in the tree and have written the nextInorder routine. It is sure that the provision of the root can help this routine perform the inorder routine properly. It will go to the left most node before following the threads to find the inorder successors. The code of the routine is given below: /* The inorder routine for threaded binary tree */ TreeNode* nextInorder(TreeNode* p){ if(p->RTH == thread) return(p->R); else { p = p->R; while(p->LTH == child) p = p->L; return p; } } When we apply this routine on the sample tree, it does not work properly because the pointer that points to the node goes in the wrong direction. How can we fix this problem? Let’s review the threaded binary tree again:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS301 – Data Structures Lecture No. 28 ___________________________________________________________________ In the above figure, we have a binary search tree. Threads are also seen in it. These threads points to the successor and predecessor. Our nextInoder routine, first of all checks that the right pointer of the node is thread. It means that it does not point to any tree node. In this case, we will return the right pointer of the node as it is pointing to the inorder successor of that node. Otherwise, we will go to some other part. Here we will change the value of pointer p to its right before running a while loops as long as the left pointer is the node. That means the left child is not a thread. We move to the left of the pointer p and keep on doing so till the time the left pointer becomes a thread. We will pass the root of the tree to the nextInorder routine. The pointer p is pointing to the node 14 i.e. the root node. As the right pointer of the node 14 is not a thread, so the pointer p will move to the node 15 as shown below: 14 15 4 9 7 18 3 5 16 20 p
CS301 – Data Structures Lecture No. 28 ___________________________________________________________________ Here we want the inorder traversal. It is obvious from the above figure that 15 is not the first value. The first value should be 3. This means that we have moved in the wrong direction. How this problem can be overcome? We may want to implement some logic that in case of the root node, it is better not to go towards the right side. Rather, the left side movement will be appropriate. If this is not the root node, do as usual. It may lend complexities to our code. Is there any other way to fix it? Here we will use a programming trick to fix it. We will make this routine as a private member function of the class so other classes

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 12

CS301-Lec28 handout - CS301 Data Structures Lecture No. 28...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online