Sessions 26 and 27 - Data Structures & Algorithm

Ifndef treeh define treeh include iostream using

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: a; } // end getData function TreeNode< NODETYPE > *leftPtr; // pointer to left subtree TreeNode< NODETYPE > *rightPtr; // pointer to right subtree }; // end class TreeNode #endif Binary trees have two pointers. } // end TreeNode constructor Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 64 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 // return copy of node's data NODETYPE getData() const { return data; } // end getData function private: TreeNode< NODETYPE > *leftPtr; // pointer to left subtree NODETYPE data; TreeNode< NODETYPE > *rightPtr; // pointer to right subtree }; // end class TreeNode #endif Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 65 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 16 17 18 19 20 21 22 23 24 25 // Template Tree class definition. #ifndef TREE_H #define TREE_H #include <iostream> using std::endl; #include <new> #include "treenode.h" template< class NODETYPE > class Tree { public: Tree(); void insertNode( const NODETYPE & ); void preOrderTraversal() const; void inOrderTraversal() const; void postOrderTraversal() const; private: TreeNode< NODETYPE > *rootPtr; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 66 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 // utility functions void insertNodeHelper( TreeNode< NODETYPE > **, const NODETYPE & ); void preOrderHelper( TreeNode< NODETYPE > * ) const; void inOrderHelper( TreeNode< NODETYPE > * ) const; void postOrderHelper( TreeNode< NODETYPE > * ) const; }; // end class Tree // constructor template< class NODETYPE > Tree< NODETYPE >::Tree() { rootPtr = 0; } // end Tree constructor // insert node in Tree template< class NODETYPE > void Tree< NODETYPE >::insertNode( const NODETYPE &value ) { insertNodeHelper( &rootPtr, value ); } // end function insertNode Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 67 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 65 66 67 68 69 70 71 72 73 74 75 76 // utility function called by insertNode; receives a pointer // to a pointer so that the function can modify pointer's value template< class NODETYPE > void Tree< NODETYPE >::insertNodeHelper( TreeNode< NODETYPE > **ptr, const NODETYPE &value ) { // subtree is empty; create new TreeNode containing value if ( *ptr == 0 ) *ptr = new TreeNode< NODETYPE >( value ); else // subtree is not empty // data to insert is less than data in current node if ( value < ( *ptr )->data ) insertNodeHelper( &( ( *ptr )->leftPtr ), value ); else Recursive function to insert a new node. If the current node is empty, insert the new node here. If new value greater than current node (ptr), insert into right subtree. If less, insert into left subtree. // data to insert is greater than data in current node if ( value > ( *ptr )->data ) insertNodeHelper( &( ( *ptr )->rightPtr ), value ); If neither case applies, node is a duplicate -- ignore. else // duplicate data value ignored cout << value << " dup" << endl; } // end function insertNodeHelper Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data St...
View Full Document

Ask a homework question - tutors are online