Test environment: Windows 7 vs2010
The main implementation of the two-tree initialization recursive and non-recursive traversal, hierarchical traversal, get the number of leaf nodes, get the height of the tree and the mirror tree, part of the Code also refer to the relevant information on the Internet.
SOURCE program:
BinaryTreeNode.h
#ifndef _binary_node#define _binary_node#include<iostream>using namespace Std;template<class ItemType> struct Binarytreenode{itemtype item; Binarytreenode<itemtype> *leftptr; Binarytreenode<itemtype> *rightptr;friend ostream &operator<< (ostream &out,binarytreenode & BTN) {out<< "" <<btn.item;return out;} Friend IStream &operator>> (IStream &input,binarytreenode &btn) {Input>>btn.item;return input ;}}; #endif
BinaryTree.h
#ifndef _binary_tree#define _binary_tree#include "BinaryTreeNode.h" Template<class itemtype>class binarytree{ Private:binarytreenode<itemtype> *rootptr;void creatbinarytree (binarytreenode<itemtype> **rootPtr); Public:binarytree (binarytreenode<itemtype> *rootptr=nullptr); ~binarytree (void); void Initbinarytree (); Binarytreenode<itemtype> *gettreeroot () const;void mirroruserecursion (binarytreenode<itemtype> * ROOTPTR)///Mirror tree void Preorderuserecursion (binarytreenode<itemtype> *rootptr) const;//recursive first-order traversal void Inorderuserecursion (binarytreenode<itemtype> *rootptr) const;//recursive middle sequence traversal void postorderuserecursion ( binarytreenode<itemtype> *rootptr) const;//recursive post-order traversal void Preorderusestack () const;//non-recursive ordinal traversal void inorderusestack ( const;//non-recursive middle sequence traversal void postorderusestack () const;//non-recursive post-order traversal void Levelorder () const;//hierarchy traversal int getleafnum (Binarytreenode <ItemType> *rootptr) const;//gets the number of leaf nodes of the tree int getheight (binarytreenode<itemtype> *rootptr);//Get tree height void Clear ();//Clear Tree}; #enDif
BinaryTree.cpp
#include "BinaryTree.h" #define tree_eof-1#include<stack> #include <deque>template<class itemtype> Binarytree<itemtype>::binarytree (binarytreenode<itemtype> *rootptr=nullptr) {this->rootPtr= Rootptr;} Template<class itemtype>binarytree<itemtype>::~binarytree (void) {this->clear ();} Template<class itemtype>void Binarytree<itemtype>::creatbinarytree (BinaryTreeNode<ItemType> * * ROOTPTR) {ItemType data;cin>>data;if (data!=tree_eof) {*rootptr=new binarytreenode<itemtype>;(*rootPtr) ->item=data;cout<< "Please enter" <<data<< "Zuozi:" <<endl;creatbinarytree (& *rootptr) LEFTPTR);cout<< "Please enter" <<data<< "right Subtree:" <<endl;creatbinarytree (& (*ROOTPTR)->rightptr) ;} Else{*rootptr=nullptr;}} Template<class itemtype>void binarytree<itemtype>::initbinarytree () {cout<< "Start creating two fork trees \ n" << Endl;creatbinarytree (&this->rootptr);} Template<class itemtype>void Binarytree<itemType>::inorderuserecursion (binarytreenode<itemtype> *rootptr) const {if (ROOTPTR==NULLPTR) {return;} Inorderuserecursion (rootptr->leftptr); cout<<*rootptr;inorderuserecursion (ROOTPTR->RIGHTPTR);} Template<class itemtype>void binarytree<itemtype>::p reorderuserecursion (BinaryTreeNode<ItemType > *rootptr) const{if (rootptr==nullptr) {return;} Cout<<*rootptr;preorderuserecursion (rootptr->leftptr);p reorderuserecursion (ROOTPTR->RIGHTPTR);} Template<class itemtype>void binarytree<itemtype>::p ostorderuserecursion (BinaryTreeNode<ItemType > *rootptr) const{if (rootptr==nullptr) {return;} Postorderuserecursion (rootptr->leftptr);p ostorderuserecursion (rootptr->rightptr); cout<<*rootPtr;} Template<class itemtype>void binarytree<itemtype>::inorderusestack () const//middle sequence traversal {stack< Binarytreenode<itemtype>*> Tempstack; Binarytreenode<itemtype> *ptree=rootptr;while (ptree!=nullptr| |! Tempstack.empty ()) {while (ptree!=nuLLPTR) {Tempstack.push (pTree);p tree=ptree->leftptr;} if (!tempstack.empty ()) {ptree=tempstack.top (); Tempstack.pop (); cout<<*ptree;ptree=ptree->rightptr;// Out of the stack pointing to the right subtree}}}template<class itemtype>binarytreenode<itemtype> * Binarytree<itemtype>::gettreeroot ( ) Const{return This->rootptr;} Template<class itemtype>void binarytree<itemtype>::p reorderusestack () const//First Order traversal {stack< Binarytreenode<itemtype>*> Tempstack; Binarytreenode<itemtype> *ptree=rootptr;while (ptree!=nullptr| |! Tempstack.empty ()) {while (ptree!=nullptr) {Cout<<*ptree;tempstack.push (pTree);p tree=ptree->leftptr;} if (!tempstack.empty ()) {ptree=tempstack.top (); Tempstack.pop ();p tree=ptree->rightptr;//out of the stack pointing to the right subtree}}}template< Class Itemtype>void Binarytree<itemtype>::p ostorderusestack () const//sequence traversal {stack<binarytreenode< Itemtype>*> Tempstack; Binarytreenode<itemtype> *ptree=rootptr; Binarytreenode<itemtype> *pretree=nullptr;while (ptree!=nullptr| |! TeMpstack.empty ()) {while (ptree!=nullptr) {Tempstack.push (pTree);p tree=ptree->leftptr;} Ptree=tempstack.top ();//If the right subtree of the current node is empty, or its right subtree is already accessed if (ptree->rightptr==nullptr| | Ptree->rightptr==pretree) {cout<<*ptree;pretree=ptree;tempstack.pop ();p tree=nullptr;} else{ptree=ptree->rightptr;}}} Template<class itemtype>void binarytree<itemtype>::clear () {stack<binarytreenode<itemtype>* > tempstack; Binarytreenode<itemtype> *ptree=rootptr; Binarytreenode<itemtype> *pretree=nullptr;while (rootptr!=nullptr| |! Tempstack.empty ()) {while (rootptr->leftptr!=nullptr) {Tempstack.push (rootptr->leftptr);} Ptree=tempstack.top (); if (Ptree->rightptr==nullptr&&ptree->rightptr==pretree) {tempStack.pop (); Delete ptree;pretree=ptree;ptree=nullptr;} Else{ptree=ptree->rightptr;}} This->rootptr=nullptr;} Template<class itemtype>void Binarytree<itemtype>::levelorder () const{deque<binarytreenode< Itemtype>*> TEMPDQ; Binarytreenode<itemtype> * NOTEPTR=THIS->ROOTPTR;IF (NOTEPTR!=NULLPTR) {tempdq.push_back (noteptr);} while (!tempdq.empty ()) {Noteptr=tempdq.front (); cout<<*noteptr;if (noteptr->leftptr!=nullptr) { Tempdq.push_back (noteptr->leftptr);} if (noteptr->rightptr!=nullptr) {tempdq.push_back (noteptr->rightptr);} Tempdq.pop_front ();}} Template<class itemtype>int binarytree<itemtype>::getheight (binarytreenode<itemtype> *rootPtr) { if (rootptr==nullptr) {return 0;} int Lh=getheight (rootptr->leftptr); int rh=getheight (ROOTPTR->RIGHTPTR); return Max (LH,RH) +1;} Template<class itemtype>void binarytree<itemtype>::mirroruserecursion (BinaryTreeNode<ItemType> *ROOTPTR) {if (rootptr==nullptr) {return;} if (rootptr->leftptr==nullptr&&rootptr->rightptr==nullptr) {return;} Binarytreenode<itemtype> *tempptr=rootptr->leftptr;rootptr->leftptr=rootptr->rightptr;rootptr- >rightptr=tempptr;mirroruserecursion (rootptr->leftptr); mirroruserecursion (ROOTPTR->RIGHTPTR);} Re+Plate<class itemtype>int binarytree<itemtype>::getleafnum (binarytreenode<itemtype> *rootPtr) Const{if (rootptr==nullptr) {return 0;} if (rootptr->leftptr==nullptr&&rootptr->rightptr==nullptr) {return 1;} Return Getleafnum (rootptr->leftptr) +getleafnum (rootptr->rightptr);}
Test procedure: Main.cpp
#include "BinaryTree.cpp" using namespace Std;int Main () {binarytree<int> Btree;btree.initbinarytree ();cout< < tree height: <<btree.getheight (btree.gettreeroot ()) <<endl;cout<< tree leaf Number of nodes: << Btree.getleafnum (Btree.gettreeroot ()) <<endl;cout<< "\ n Recursive first order traversal:"; Btree.preorderuserecursion ( Btree.gettreeroot ());cout<< "\ n non-recursive first-order traversal:"; Btree.preorderusestack ();cout<< "\ n recursive sequential traversal:"; Btree.inorderuserecursion (Btree.gettreeroot ());cout<< "\ n non-recursive in-sequence traversal:"; Btree.inorderusestack ();cout<< "\ n Recursive post-order traversal: "; Btree.postorderuserecursion (Btree.gettreeroot ());cout<<" \ n non-recursive first sequence traversal: "; Btree.postorderusestack (); cout<< "\ n sequence traversal:"; btree. Levelorder (); Btree.mirroruserecursion (Btree.gettreeroot ());cout<< "\ n Mirror sequence traversal:"; btree. Levelorder (); System ("pause"); return 0;}
Copyright NOTICE: Welcome to reprint, if there are deficiencies, please treatise.
C + + implements two cross-tree related operations