CS301-Lec15 handout

# CS301-Lec15 handout - CS301 Data Structures Lecture No 15...

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

CS301 – Data Structures Lecture No. 15 ___________________________________________________________________ Data Structures Lecture No. 15 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.3, 4.3.5, 4.6 Summary Level-order Traversal of a Binary Tree Storing Other Types of Data in Binary Tree Binary Search Tree (BST) with Strings Deleting a Node From BST Level-order Traversal of a Binary Tree In the last lecture, we implemented the tree traversal in preorder, postorder and inorder using recursive and non-recursive methods. We left one thing to explore further that how can we print a binary tree level by level by employing recursive or non-recursive method. See the figure below: Page 1 of 13 14 4 9 7 3 5 15 18 16 20 17 Fig 15.1: Level-order Traversal

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

View Full Document
CS301 – Data Structures Lecture No. 15 ___________________________________________________________________ In the above figure, levels have been shown using arrows: At the first level, there is only one number 14 . At second level, the numbers are 4 and 15 . At third level, 3 , 9 and 18 numbers are present. At fourth level the numbers are 7 , 16 and 20 . While on fifth and final level of this tree, we have numbers 5 and 17 . This will also be the order of the elements in the output if the level-order traversal is done for this tree. Surprisingly, its implementation is simple using non-recursive method and by employing queu e instead of stack. A queue is a FIFO structure, which can make the level-order traversal easier. The code for the level-order traversal is given below: 1. void levelorder( TreeNode <int> * treeNode ) 2. { 3. Queue <TreeNode<int> *> q; 4. 5. if( treeNode == NULL ) return; 6. q.enqueue(treeNode); 7. while( !q.empty() ) 8. { 9. treeNode = q.dequeue(); 10. cout << *(treeNode->getInfo()) << " "; 11. if(treeNode->getLeft() != NULL ) 12. q.enqueue( treeNode->getLeft()); 13. if(treeNode->getRight() != NULL ) 14. q.enqueue( treeNode->getRight()); 15. } 16. cout << endl; 17. } The name of the method is levelorder and it is accepting a pointer of type TreeNode <int> . This method will start traversing tree from the node being pointed to by this pointer. The first line ( line 3) in this method is creating a queue q by using the Queue class factory interface. This queue will be containing the TreeNode<int> * type of objects. Which means the queue will be containing nodes of the tree and within each node the element is of type int . The line 5 is checking to see, if the treeNode pointer passed to the function is pointing to NULL. In case it is NULL, there is no node to traverse and the method will return immediately. Otherwise at line 6 , the very first node (the node pointed to by the treeNode pointer) is added in the queue q .
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 / 13

CS301-Lec15 handout - CS301 Data Structures Lecture No 15...

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

View Full Document
Ask a homework question - tutors are online