为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C C++
吃鸡游戏 2021-12-03 11:07:56
#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(){.............}
查看完整描述

3 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

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

查看完整回答
反对 回复 2021-12-07
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

int main(){
BiNode* root;
BiTree* tree = new BiTree(root);

tree->preOrder(root);
tree->inOrder(root);
tree->postOrder(root);

delete tree;
}



查看完整回答
反对 回复 2021-12-07
?
LEATH

TA贡献1936条经验 获得超6个赞

如下主函数调用实现:
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;
}



查看完整回答
反对 回复 2021-12-07
  • 3 回答
  • 0 关注
  • 315 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信