Sessions 26 and 27 - Data Structures & Algorithm

Inorder traversal of tree template class nodetype

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: ructures & Algorithm 68 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 // begin preorder traversal of Tree template< class NODETYPE > void Tree< NODETYPE >::preOrderTraversal() const { preOrderHelper( rootPtr ); } // end function preOrderTraversal // utility function to perform preorder traversal of Tree template< class NODETYPE > void Tree< NODETYPE >::preOrderHelper( Preorder: TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { cout << ptr->data << ' '; // process node preOrderHelper( ptr->leftPtr ); // go to left subtree preOrderHelper( ptr->rightPtr ); // go to right subtree print, left, right } // end if } // end function preOrderHelper Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 69 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 // begin inorder traversal of Tree template< class NODETYPE > void Tree< NODETYPE >::inOrderTraversal() const { inOrderHelper( rootPtr ); } // end function inOrderTraversal // utility function to perform inorder traversal of Tree template< class NODETYPE > void Tree< NODETYPE >::inOrderHelper( In order: TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { inOrderHelper( ptr->leftPtr ); // go to left subtree cout << ptr->data << ' '; // process node inOrderHelper( ptr->rightPtr ); // go to right subtree left, print, right } // end if } // end function inOrderHelper Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 70 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 // begin postorder traversal of Tree template< class NODETYPE > void Tree< NODETYPE >::postOrderTraversal() const { postOrderHelper( rootPtr ); } // end function postOrderTraversal // utility function to perform postorder traversal of Tree template< class NODETYPE > Postorder: void Tree< NODETYPE >::postOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { postOrderHelper( ptr->leftPtr ); // go to left subtree postOrderHelper( ptr->rightPtr ); // go to right subtree cout << ptr->data << ' '; // process node left, right, print } // end if } // end function postOrderHelper #endif Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 71 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // Tree class test program. #include <iostream> using std::cout; using std::cin; using std::fixed; #include <iomanip> using std::setprecision; #include "tree.h" // Tree class definition int main() { Tree< int > intTree; // create Tree of int values int intValue; cout << "Enter 10 integer values:\n"; for( int i = 0; i < 10; i++ ) { cin >> intValue; intTree.insertNode( intValue ); } // end for Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Da...
View Full Document

Ask a homework question - tutors are online