麻烦编出来的结果是一个完整的cpp,可以直接运行,求高手指点~

要求
先用
struct treeNode
{
char data;
treeNode *lchild,*rchild;
}; //定义一个节点 
然后用类
class binaryTree
{
private:  
treeNode *T;
public:
binaryTree(); //构造函数
int createTree();//创建树
int preTravel();//先序遍历树
int inTravel();//中序遍历树
};
void main()
{
……
}
这个空指针用#输入表示
大概就这样吧
麻烦下大家帮我完成下
不要用模板template
请用类实现,并且只要创建二叉树,先序遍历,中序遍历,后序遍历中的2种遍历即可,如果有现成的程序可以直接摘抄过来,但是只需要完成这几种功能的函数皆可,其余的不要

aluckdog
浏览 162回答 3
3回答

梦里花落0921

说明:输入时按前序遍历方式依次输入各节点值,默认的结束符为0。即当一个节点为叶子节点时,把它的左子节点和右子节点都输为0,当然你可以自己修改为加别的值。例如某棵树的形状如下:&nbsp;A&nbsp;/ \&nbsp;B C&nbsp;/ \ \&nbsp;D E F&nbsp;则按如下输入:ABD00E00C0F00。&nbsp;程序运行后结果如下:&nbsp;前序遍历结果:&nbsp;ABDECF&nbsp;中序遍历结果:&nbsp;DBEACF&nbsp;程序源文件:&nbsp;#include <stdio.h>&nbsp;#include <stdlib.h>&nbsp;struct treeNode&nbsp;{&nbsp;char data;&nbsp;struct treeNode *lchild;&nbsp;struct treeNode *rchild;&nbsp;}; //定义一个节点&nbsp;class binaryTree&nbsp;{&nbsp;private:&nbsp;struct treeNode *T;&nbsp;public:&nbsp;binaryTree() {T = NULL;}; //构造函数&nbsp;int createTree(); //创建树&nbsp;int preTravel(); //先序遍历树&nbsp;int inTravel(); //中序遍历树&nbsp;};&nbsp;struct treeNode * createBT(struct treeNode *bt, int k)&nbsp;{&nbsp;char b;&nbsp;struct treeNode *p, *t;&nbsp;b = getchar();&nbsp;if (b != '0')&nbsp;{&nbsp;p = (struct treeNode *)malloc(sizeof(struct treeNode));&nbsp;p->data = b;&nbsp;p->lchild = NULL;&nbsp;p->rchild = NULL;&nbsp;if (k == 0) t = p;&nbsp;if (k == 1) bt->lchild = p;&nbsp;if (k == 2) bt->rchild = p;&nbsp;createBT(p, 1);&nbsp;createBT(p, 2);&nbsp;}&nbsp;return t;&nbsp;}&nbsp;void preTravelBT(struct treeNode *bt)&nbsp;{&nbsp;if (bt != NULL)&nbsp;{&nbsp;putchar(bt->data);&nbsp;preTravelBT(bt->lchild);&nbsp;preTravelBT(bt->rchild);&nbsp;}&nbsp;}&nbsp;void inTravelBT(struct treeNode *bt)&nbsp;{&nbsp;if (bt != NULL)&nbsp;{&nbsp;inTravelBT(bt->lchild);&nbsp;putchar(bt->data);&nbsp;inTravelBT(bt->rchild);&nbsp;}&nbsp;}&nbsp;int binaryTree::createTree()&nbsp;{&nbsp;printf("请输入各节点数据:\n");&nbsp;T = createBT(T, 0);&nbsp;return 0;&nbsp;}&nbsp;int binaryTree::preTravel()&nbsp;{&nbsp;printf("前序遍历结果:\n");&nbsp;preTravelBT(T);&nbsp;printf("\n");&nbsp;return 0;&nbsp;}&nbsp;int binaryTree::inTravel()&nbsp;{&nbsp;printf("中序遍历结果:\n");&nbsp;inTravelBT(T);&nbsp;printf("\n");&nbsp;return 0;&nbsp;}&nbsp;void main()&nbsp;{&nbsp;binaryTree MyTree;&nbsp;MyTree.createTree();&nbsp;MyTree.preTravel();&nbsp;MyTree.inTravel();&nbsp;}

神不在的星期二

void PreOrder_Nonrecursive(Bitree T)//先序遍历二叉树的非递归算法&nbsp;{//思路为利用自己的堆栈模拟函数递归调用时栈区的变化。&nbsp;InitStack(S);//初始化堆栈。参照严蔚敏编《数据结构C语言版》实现堆栈的相关功能函数,这里会用到。&nbsp;Push(S,T); //根指针进栈&nbsp;while(!StackEmpty(S))&nbsp;{&nbsp;while(Gettop(S,p)&&p)//若栈顶元素不是空指针&nbsp;{&nbsp;visit(p->data);//访问&nbsp;push(S,p->lchild);//进入左子树&nbsp;} //向左走到尽头&nbsp;pop(S,p);将栈顶元素(实际上是空指针)丢弃&nbsp;if(!StackEmpty(S))&nbsp;{&nbsp;pop(S,p);&nbsp;push(S,p->rchild); //向右一步,从这个结点开始继续进行先序遍历&nbsp;}&nbsp;}//while&nbsp;}//PreOrder_Nonrecursive&nbsp;int NodeCount_BiTree(Bitree T)//求二叉树中结点的数目&nbsp;{&nbsp;if(!T) return 0; //递归结束条件:空树结点数为0&nbsp;else return (NodeCount(T->lchild)+NodeCount(T->rchild)+1);//二叉树中结点数=左子树的结点数+右子树的结点数+根结点自己&nbsp;}//NodeCount_BiTree&nbsp;int LeafCount_BiTree(Bitree T)//求二叉树中叶子结点的数目&nbsp;{&nbsp;if(!T) return 0; //空树没有叶子&nbsp;else if(!T->lchild&&!T->rchild) return 1; //叶子结点&nbsp;else return Leaf_Count(T->lchild)+Leaf_Count(T->rchild);//左子树的叶子数加上右子树的叶子数&nbsp;}//LeafCount_BiTree&nbsp;若已知二叉树的结点数为n,叶子的结点数为n0,那么n2=n0-1,n1=n-n2-n0=n-2n0+1。或者用以下算法&nbsp;int n1=0;//定义全局变量n1&nbsp;void Count_n1(Bitree T)&nbsp;{//先序遍历二叉树过程中统计度为1的结点数&nbsp;if(T)&nbsp;{&nbsp;if((T->lchild && !T->rchild) || (!T->lchild && T->rchild))//判断是否是度为1的结点,实际上是异或&nbsp;n1++;&nbsp;Count_n1(T->lchild);&nbsp;Count_n1(T->rchild);&nbsp;}&nbsp;}&nbsp;int Get_Depth(Bitree T)//求二叉树深度的递归算法&nbsp;{&nbsp;if(!T) return 0;//递归结束条件:空树深度为0&nbsp;else&nbsp;{&nbsp;m=Get_Depth(T->lchild);&nbsp;n=Get_Depth(T->rchild);&nbsp;return (m>n?m:n)+1;//二叉树深度为其左右子树较大深度+1&nbsp;}&nbsp;}//Get_Depth

撒科打诨

/** main.cpp** Created on: 2008-12-1* Author: Admin*/#include<iostream>using std::cout;using std::cin;struct treeNode{char data;treeNode *lchild,*rchild;}; //定义一个节点class binaryTree{private:treeNode *TreeRoot;void subCreate(treeNode *,int,const char *);void Free(treeNode *);void subpreTravel(treeNode *);void subinTravel(treeNode *);public:binaryTree(){TreeRoot=new treeNode;}//构造函数void createTree();//创建树void preTravel(){subpreTravel(TreeRoot);cout<<"\n";}//先序遍历树void inTravel(){subinTravel(TreeRoot);cout<<"\n";}//中序遍历树~binaryTree(){Free(TreeRoot);}};void binaryTree::subCreate(treeNode * T,int root,const char * strNode){static int longth=strlen(strNode);int R=2*(root+1);int L=2*(root+1)-1;if(root ==0)T->data=strNode[0];if(L<longth){T->lchild=new treeNode;T->lchild->data=strNode[L];subCreate(T->lchild,L,strNode);}elseT->lchild=NULL;if(R<longth){T->rchild=new treeNode;T->rchild->data=strNode[R];subCreate(T->rchild,R,strNode);}elseT->rchild=NULL;}void binaryTree::createTree(){subCreate(this->TreeRoot,0,"abcdefghijk");}void binaryTree::subpreTravel(treeNode * T){if(T==NULL)return;cout<<T->data;subpreTravel(T->lchild);subpreTravel(T->rchild);}void binaryTree::subinTravel(treeNode * T){if(T==NULL)return;subinTravel(T->lchild);cout<<T->data;subinTravel(T->rchild);}void binaryTree::Free(treeNode * T){if(T==NULL)return;if(T->lchild==NULL&&T->rchild==NULL)delete T;else{Free(T->lchild);Free(T->rchild);}}int main(){binaryTree BT;BT.createTree();BT.preTravel();BT.inTravel();return 0;}//用二叉堆的方式建树
打开App,查看更多内容
随时随地看视频慕课网APP