建立一棵二叉树,要求先建立一个空树InitTree()和销毁树DestroyTree()函数?

建立一棵二叉树,要求先建立一个空树InitTree()和销毁树DestroyTree()函数,


HUX布斯
浏览 1391回答 2
2回答

慕神8447489

#include<iostream>using namespace std;typedef char ElemType;struct BTreeNode{ ElemType data;BTreeNode *leftChild;BTreeNode *rightChild;};void InitTree(BTreeNode* T){T=NULL;}void DestroyTree(BTreeNode* T){if(T!=NULL) {DestroyTree(T->leftChild);DestroyTree(T->rightChild);delete T;}}void CreateBiTree(BTreeNode* &T){ ElemType mark;cin>>mark;if(mark=='$') T=NULL;else {T=new BTreeNode;T->data=mark;CreateBiTree(T->leftChild);CreateBiTree(T->rightChild);}}void InOrder(BTreeNode* T ){if(T!=NULL){InOrder(T->leftChild);cout<<T->data;InOrder(T->rightChild);}}void main(){BTreeNode *T=new BTreeNode;InitTree(T);cout<<"按先序序列输入:"<<endl;cout<<"例如输入ABC$$DE$G$$F$$$"<<endl;CreateBiTree(T);cout<<"按中序序列输出:"<<endl;InOrder(T );cout<<endl;DestroyTree(T);}
打开App,查看更多内容
随时随地看视频慕课网APP