Bitree:
1 //chain-structured two-fork tree2#include <iostream>3 using namespacestd;4 #defineMAXSIZE 1005typedefCharElemtype;6 7typedefstructBitnode8 {9 elemtype data;TenBitnode *Lchild; OneBitnode *Rchild; A}*Bitree; - - //Constructing an empty binary tree T the voidInitbitree (Bitree *T) - { -*t =NULL; - } + - //destroy the binary tree + voidDestroybitree (Bitree *T) A { at if(*T) - { - if((*t)lchild) -Destroybitree (& (*t),lchild); - if((*t)rchild) -Destroybitree (& (*t),rchild); in Delete(*T); -*t =NULL; to } + } - the //generate two fork tree * voidCreatebitree (Bitree *T) $ {Panax Notoginseng Elemtype ch; - if((Ch=getchar ()) = ='#') the*t =NULL; + Else A { the*t =NewBitnode; +(*t)->data =ch; -Createbitree (& (*t),lchild); $Createbitree (& (*t),rchild); $ } - } - the //determines whether the empty - BOOLEmptybitree (bitree T)Wuyi { the if(T) - { Wucout<<"Non-empty"<<Endl; - return false; About } $ Else - { -cout<<"Empty"<<Endl; - return true; A } + } the - #defineClearbitree Destroybitree $ the //Find T depth the intDepthbitree (Bitree *T) the { the inti; - intJ; in if(! (*T)) the return 0; the if((*t)lchild) About { thei = Depthbitree (& (*t)lchild); the //cout<< "L" << I <<endl; the } + Else -i =0; the if((*t)rchild)Bayi { thej = Depthbitree (& (*t)rchild); the //cout<< "R" << J <<endl; - } - Else thej =0; the return(i>j? i+1: j+1); the } the - //returns the root of a binary tree the elemtype Root (bitree T) the { the if(Emptybitree (T))94 return 0; the Else the return(T)data; the }98 About //returns the value of the node referred to by P . - elemtype Value (Bitree p)101 {102 returnP->data;103 }104 the //copy value to p point node106 voidAssign (Bitree p, elemtype value)107 {108P->data =value;109 } the 111 //Pre-order recursive traversal T the voidPreordertraverse (bitree T)113 { the if(t==NULL) the return; theCout<<t->data;117Preordertraverse (t->lchild);118Preordertraverse (t->rchild);119 } - 121 //middle order recursive traversal T122 voidInordertraverse (bitree T)123 {124 if(t==NULL) the return; 126Preordertraverse (t->lchild);127Cout<<t->data; -Preordertraverse (t->rchild);129 } the 33s //Post- recursion recursive traversal T the voidPosordertraverse (bitree T)133 {134 if(t==NULL)135 return; 136Preordertraverse (t->lchild);137Preordertraverse (t->rchild);138Cout<<t->data;139 } $ 141 intMain ()142 {143 Bitree T;144 Elemtype ch;145 146Initbitree (&T);147Createbitree (&T);148 Emptybitree (T);149 intD = Depthbitree (&T); Maxcout<<"two fork Tree depth:"<<d<<Endl;151CH =Root (T); thecout<<"two fork Roots:"<<ch<<Endl;153 154cout<<"Pre-order traversal:";155 Preordertraverse (T);156cout<<"\ n Sequence traversal:";157 Inordertraverse (T);158cout<<"\ nthe post-traversal:";159 Posordertraverse (T); the 161Clearbitree (&T);162cout<<"\ nthe two fork tree is:";163 Emptybitree (T);164cout<<"two fork Tree depth:"<<depthbitree (&t) <<Endl;165 inti =Root (T);166 if(!i)167cout<<"the tree is empty and has no roots. "<<Endl;168 169 return 0; the}
C + + chain binary tree