二叉树的类定义 template <class Type> class Binary Tree; template <class Type> Class Bin TreeNode i friend class Binarytree publica Bin TreeNode (: leftchild (null) 今, getchild(NUD{} Bin TreeNode( type item Bin TreeNode<Type> *left- NUlL, Bin TreeNode<Type> *right=NULL ) data(item), leftchild (left), rightchild (right) ype getData( const return data; 3
template <class Type> class BinaryTree; template <class Type> Class BinTreeNode { friend class BinaryTree; public: BinTreeNode ( ) : leftChild (NULL), rightChild (NULL) { } BinTreeNode ( Type item, BinTreeNode<Type> *left = NULL, BinTreeNode<Type> *right = NULL ) : data (item), leftChild (left), rightChild (right) { } Type & GetData ( ) const { return data;} 二叉树的类定义
Bin TreeNode<Type>*Getleft(const freturn leftchild; j Bin TreeNode<Type>*GetRight( const f return rightChild; 3 void SetData( const Type item i data=item; 3 void Setleft( Bin TreeNode <Type>*L i leftchild= l; j void Setright( Bin TreeNode <Type>*R) frightChild=r;) private Bin TreeNode<Type> *leftChild, *rightChild Type data;
BinTreeNode<Type> *GetLeft ( ) const { return leftChild;} BinTreeNode<Type> *GetRight ( ) const { return rightChild;} void SetData ( const Type & item ) { data = item; } void SetLeft ( BinTreeNode <Type> *L ) { leftChild = L; } void SetRight ( BinTreeNode <Type> *R ) { rightChild = R; } private: BinTreeNode<Type> *leftChild, *rightChild; Type data; };
template <class Type> class BinaryTree t public: Binary Tree(: root(NulL Binary Tree( Type value ): Revalue (value), root(Null virtual -(i destroy root);3 virtual int IsEmpty( f return root==NULL?1:0; virtual Bin TreeNode <Type>* parent( Bin TreeNode <Type>current) f return root==NULL root ==current? NULL: Parent( root, current
template <class Type> class BinaryTree { public: BinaryTree ( ) : root (NULL) { } BinaryTree ( Type value ) : RefValue (value), root (NULL) { } virtual ~BinaryTree ( ) { destroy ( root ); } virtual int IsEmpty ( ) { return root == NULL ? 1 : 0; } virtual BinTreeNode <Type> *Parent ( BinTreeNode <Type> *current ) { return root == NULL || root == current? NULL : Parent ( root, current ); }
virtual BinTreeNode <Type>*Leftchild( Bin TreeNode <Type> *current) f return root !=nULL current-→ leftchild:NUL;} virtual Bin TreeNode <Type>* Rightchild( Bin TreeNode <Type> *current) f return root!=NULL? currentrightChild: NULL; virtual int Insert( const Type item) virtual int Find( const Type &item)const Bin TreeNode<Type>*GetRoot( const t return root; 3 friend istream &operator >>( istream &in, Binary Tree<Type>&Tree
virtual BinTreeNode <Type> *LeftChild ( BinTreeNode <Type> *current ) { return root != NULL ? current→leftChild : NULL; } virtual BinTreeNode <Type> *RightChild ( BinTreeNode <Type> *current ) { return root != NULL ? current→rightChild : NULL; } virtual int Insert ( const Type & item); virtual int Find ( const Type &item ) const; BinTreeNode <Type> *GetRoot ( ) const { return root; } friend istream &operator >> ( istream &in, BinaryTree<Type> &Tree )
friend ostream &operator <<( ostream &out, Binary Tree <Type>&Tree p rivate BinTreeNode <Type> *root; Type revalue Bin TreeNode <Type>x parent( BinTreeNode <Type> *start Bin TreeNode <Type>*current ) int Insert( Bin TreeNode<Type>*¤t const Type &item void Traverse( Bin TreeNode<Type>*current ostream &out)const int Find( Bin TreeNode<Type>*current const Type &item )const
friend ostream &operator << ( ostream &out, BinaryTree <Type> &Tree ) private: BinTreeNode <Type> *root; Type RefValue; BinTreeNode <Type> *Parent ( BinTreeNode <Type> *start, BinTreeNode <Type> *current ); int Insert ( BinTreeNode<Type> * ¤t, const Type &item ) void Traverse ( BinTreeNode<Type> *current, ostream &out ) const int Find ( BinTreeNode<Type> *current, const Type &item ) const