void Crt BinTree( ifstream& in BinTreenode s&T)i TreeData X, if(!in. eofoi In>>; /读入根结点的值 if(x!=Revalue )t T= new BinTreenode;∥建立根结点 if(T-NULL)( cerr<<存储分配错!endl; exit(1) T->data=x:
void Crt_BinTree ( ifstream& in, BinTreeNode *& T ) { TreeData x; if ( !in.eof ( ) ) { in >> x; //读入根结点的值 if ( x != RefValue ) { T = new BinTreeNode; //建立根结点 if ( T == NULL ) { cerr << “存储分配错!” << endl; exit (1); } T->data = x;
Crt BinTree( in, T->left child ) Crt BinTree( in, T->rightChild ) else t=NULL:/封闭叶结点 建立二叉树的主程序 void create Bin Tree( filename Bin Treenode *& Tree)( 建立一棵二叉树 Tree fin是输入流对象
Crt_BinTree ( in, T->leftChild ); Crt_BinTree ( in, T->rightChild ); } else T = NULL; //封闭叶结点 } } 建立二叉树的主程序 void createBinTree ( filename, BinTreeNode *& Tree ) { //建立一棵二叉树Tree。fin是输入流对象
TreeData item ifstream fin(filename, ios: in ios: nocreate ) /打开文件 if(!fin )t cerr<“文件未发现!<<endl;exit(1); Crt BinTree( ifstream& fin, Tree )i fin close() /关闭文件
TreeData item; ifstream fin (filename, ios::in | ios::nocreate ); //打开文件 if ( !fin ) { cerr << “文件未发现!” << endl; exit (1); } Crt_BinTree ( ifstream& fin, Tree ); fin.close ( ); //关闭文件 }
删除二叉树的递归算法 void destroy( BinTreeNode T)i if(T!=NULL)( destroy(T->leftchild ) destroy(T->right Child delete t:
删除二叉树的递归算法 void destroy ( BinTreeNode *T ) { if ( T != NULL ) { destroy ( T->leftChild ); destroy ( T->rightChild ); delete T; } }
计算二叉树结点个数的递归算法 int Count( BinTreeNode T)i if(T=- NULL)return 0 else return 1 Count(T->left Child Count(T->right Child
int Count ( BinTreeNode *T ) { if ( T == NULL ) return 0; else return 1 + Count ( T->leftChild ) + Count ( T->rightChild ); } 计算二叉树结点个数的递归算法