已经把二叉树建立并遍历,请问主函数代码该怎么写?我不会写了~跪求

#include<iostream>
using namespace std;
struct BiNode
{
char data;
BiNode *lchild,*rchild;
};
class BiTree
{
public:
BiTree(BiNode *root) //有参构造函数,初始化一颗二叉树。
{
this->root=Creat();
}
BiNode *getroot()
{
return root;
}
~BiTree() //析构函数,释放二叉树
{
Release(root);
}
void PreOrder(BiNode *root); //前序遍历
void InOrder(BiNode *root); //中序遍历
void PostOrder(BiNode *root); //后序遍历 
private:
BiNode *root;
BiNode *Creat() //有参函数调用
{
char ch;
cin>>ch;
BiNode *root;
if(ch=='#') root=NULL;
else{
root=new BiNode;
root->data=ch;
root->lchild=Creat();
root->rchild=Creat();
}
return root;
}
void Release(BiNode *root) //析构函数调用 
{
if(root!=NULL)
{
Release(root->lchild);
Release(root->rchild);
delete root;
}
}
};
void BiTree::PreOrder(BiNode *root) //qian
{
if(root==NULL) return;
else{
cout<<root->data;
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
void BiTree::InOrder(BiNode *root) //zhong
{
if(root==NULL) return;
else{
InOrder(root->lchild);
cout<<root->data;
InOrder(root->rchild);
}
}
void BiTree::PostOrder(BiNode *root) //hou
{
if(root==NULL) return;
else{
PostOrder(root->lchild);
PostOrder(root->rchild);
cout<<root->data;
}
}
二叉树建立并遍历。 主函数代码改怎么写? int main(){.............}

吃鸡游戏
浏览 329回答 3
3回答

暮色呼如

1.这个你要仔细看下Create()函数啊,根据这个函数的功能;2.这些代码其实都不需要参数啊,因为这是个类啊,最好再熟悉下类和成员的基础概念,比如:BiTree(){this->root=Creat();}就可以了,因为root是类的私有数据成员,不需要出现在参数里面了。最简单的主函数如下:int main(){BiTree theTree();theTree.PreOrder();theTree.Release();return 0;}

哈士奇WWW

int main(){BiNode* root;BiTree* tree = new BiTree(root);tree->preOrder(root);tree->inOrder(root);tree->postOrder(root);delete tree;}

LEATH

如下主函数调用实现:int main(){/*BiTree theTree();theTree.PreOrder();theTree.Release();*/BiNode * root = new BiNode;BiTree hao(root);hao.PreOrder(root);hao.InOrder(root);hao.PostOrder(root);return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP