[Header file] C ++ realizes binary tree

zhaozj2021-02-16  62

#ifndef BITREE_H # define BITREE_H # include using namespace std; template class bitnode {public: bitnode (); // Constructor bitnode (const bitnode &); // copy constructor const elemtype date () const ; // Read data const bitnode * lchild () const; // Return left pointer const bitnode * rchild () const; // Return to right pointer void get_date (const ele mType); // Enter data void get_lchild (const bitnode *) ; // Enter the left pointer void get_rchild (const bitnode *); // Enter the right pointer bitnode operator = (const bitnode &); // assignment, only declared private: elemType _date; // node data bitnode * _lchild, * _ rchild; / / Left and right kids pointer}; // bifurcous node

// bitnode class function Implement Template BitNode :: bitnode () {get_date (0); get_rchild (0); get_lchild (0);}

Template Const BitNode * BitNode :: lchild () const {return_lchild;}

Template Const BitNode * BitNode :: rChild () const {return_rchild;}

Template Const ElemType BitNode :: Date () const {return_date;}

Template Void BitNode :: get_date (const ele mtype de)

{_DATE = de;} template void bitnode :: get_lchild (const bitnode * pev) {_lchild = (bitnode *) pev;}

Template Void BitNode :: get_rchild (const bitnode * pEV) {_rchild = (bitnode *) pev;}

// Tree class Template Class Bitree {public: Bitree (); // Configuration Function Bitree (const bitnode * copy (const bitnode *); void print () const; // Print tree data Const Bool Empty () const; // Test the tree whether the tree is empty const Int node () const; // Return node bit Bitree & Operator = (const bitree &); // assignment private: int node_nu; // Number bitNode * PV; // Root pointer void add_node (); // increase node const bitnode * init (); // Initialize Void fvisit (const bitnode *) const; // Single traversal Void mvisit (const bitnode *) const; // Sub-sequence traversal Void Hvisit (const bitnode *) const; // sequence traversal};

// Binary tree code implementation // public function set template Bitree :: bitree () {pv = null; node_nu = 0; pv = (bitNode *) init ();

Template Bitree :: Bitree (const bitree & tre) {ix (tree.empty ()) pv = (bitnode *) copy (tree.pv);

Template VOID BITREE :: Print () const {cout << "Please select the print order: preface [x]? 中序 [z]? Sequence [h] ?:"; char CH; CIN >> CH; IF (EMPTY ()! = 0) {Switch (CH) {CASE 'X': FVISIT (PV); Break; Case 'Z': MVISIT (PV); Break; Case 'h': hvisit (PV); Break; default: cerr << "error operation. Unable to output!" << endl;}} else CERR << "tree is empty" << Endl;}

Template Const Bool Bitree :: Empty () const {= == 0) Return False; Else Return True;

template const int BiTree :: node () const {return node_nu;} template const bitnode * BiTree :: copy (const bitnode * ptr) { BitNode * pev; if (ptr == 0) Return 0; if (Ptr-> Date () == 0) PEV = 0; Else {IF (! (pev = new bitnode ) Return 0 ;

Add_node (); pev-> get_date; pev-> get_lchild (copy (ptr-> lchild ())); pev-> get_rchild (copy (ptr-> rchild ());

} Return PEV;

Template Bitree & Bitree :: Operator = (const bitree & tree) {if.empty ()) PV = Copy (Tree.pv);

Return * this;}

// Private function collection

Template * bitree :: init () {bitnode * pev; elemType de; coudute << "Please enter the" << node () 1 << " Data: "; CIN >> DE; if (de == 0) PEV = NULL; ELSE {IF (! (PEV = New BitNode ) Return 0;

Add_node (); pev-> get_date (de); pev-> get_lchild (init ()); pev-> get_rchild (init ());}

Return PEV;

Template Void Bitree :: add_node () { node_nu;}

Template Void Bitree :: fvisit (const bitnode * pev) const {if (pev! = null) cout << pev-> Date () << Endl; if (PEV-> LCHILD ()! = Null) Fvisit (pev-> lchild ()); if (pev-> rchild ()! = Null) fvisit (pev-> rchild ());} template Void Bitree : : Mvisit (const bitnode * pev) const {if (pev-> lchild ()! = Null) mvisit (pev-> lchild ()); if (pev! = Null) cout << pev-> Date << Endl; if (pev-> rchild ()! = null) mvisit (pev-> rchild ());}

Template Void Bitree :: Hvisit (Const Bitnode * PEV) const {ife (pev-> lchild ()! = null) hvisit (pev-> lchild ()); if (PEV- > rchild ()! = NULL) HVISIT (pev-> rchild ()); if (pev! = null) cout << pev-> date () << endl;}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-16069.html

New Post(0)