汉扬编程 编程大纲 上机实验报告建立二叉树A(B(D

上机实验报告建立二叉树A(B(D

  给你一段我以前做的二叉树遍历的程序,你模仿着修改就好:
?
#include??
#include??
#include?
#define?MAX?100
typedef?struct?BiTNode
{?
????struct?BiTNode?*lchild;
?int?data;?
????struct?BiTNode?*rchild;?
}BiTNode,*BiTree;?
BiTree?CreateBiTree()
{?
????int?n;?
????BiTree?T;?
?printf(\”请输入节点数据域中的整数数据值,并以数字0表示节点为空:\”);?
????scanf(\”%d\”,&n);?
?if(n==0)
??T=NULL;?
?else
?{?
????????T=(BiTree)malloc(sizeof(BiTNode));?
????????if(!T)?
????????printf(\”分配出错!\”);?
????????T->data=n;?
????????T->lchild=CreateBiTree();????/*递归生成左子树*/
????????T->rchild=CreateBiTree();????/*递归生成右子树*/
????}?
?return?T;?
}?
void?levelTraverse(BiTree?T)??????/*按层遍历*/
{
?BiTree?Queue[MAX];??????????/*定义一个足够大的列队*/????
?BiTree?p;
?int?front=0,rear=0;???????/*表示队头指针和队尾指针*/
?p=(BiTree)malloc(sizeof(BiTNode));?
????if(!p)?
????????printf(\”分配出错!\”);?
?if(T)
?{
??p=T;??????????????????????/*根指针入队*/
??Queue[rear]=p;
?????rear=(rear 1)%MAX;?????????/*队尾指针加一对MAX取余,可实现循环队列,合理利用空间*/
??while(front!=rear)????????/*队不空*/
??{
???p=Queue[front];?????????/*出队,将值赋给p*/
???printf(\”%d?\”,p->data);
???front=(front 1) ;
???{
????if(p->lchild)???????????/*如果p有左子树,将左子树入队*/
????{
?????Queue[rear]=p->lchild;
?????rear=(rear 1)%MAX;
????}
???????else
?????printf(\”空?\”);??????/*如果p没有左子树,输出“空”字样*/
???}
???{
????if(p->rchild)???????????/*如果p有右子树,将右子树入队*/
????{
?????Queue[rear]=p->rchild;
?????rear=(rear 1)%MAX;
????}
????else
?????printf(\”空?\”);??/*如果p没有右子树,输出“空”字样*/
???}
??}
?}
}
void?PreOrderTraverse(BiTree?T)??/*前序遍历,T是指向二叉链表根结点的指针*/
{?
???if?(T)
???{?
??printf(\”%d?\”,T->data);????????/*访问根指针*/
??PreOrderTraverse(T->lchild);??/*递归访问左子树*/
??PreOrderTraverse(T->rchild);??/*递归访问右子树*/
???}
}
void?MidOrderTraverse(BiTree?T)??/*中序遍历,T是指向二叉链表根结点的指针*/
{??
???if?(T)
???{?
??MidOrderTraverse(T->lchild);??/*递归访问左子树*/
??printf(\”%d?\”,T->data);????????/*访问根指针*/
??MidOrderTraverse(T->rchild);??/*递归访问右子树*/
???}
}
void?PostOrderTraverse(BiTree?T)??/*后序遍历,T是指向二叉链表根结点的指针*/
{?
???if?(T)
???{?
??PostOrderTraverse(T->lchild);??/*递归访问左子树*/
??PostOrderTraverse(T->rchild);??/*递归访问右子树*/
??printf(\”%d?\”,T->data);?????????/*访问根指针*/
???}
}
void?main()
{?
?????BiTree?TreeHead;?
??char?judge;???????????/*作为遍历方式选取的选择用*/
??char?flag=\’Y\’;????????/*作为是否重新遍历的判断标志*/
?????TreeHead=CreateBiTree();???/*生成一棵二叉树,返回二叉树的根结点*/
??getchar();
??if(TreeHead)
??{
??printf(\”分层遍历的结果为:\”);???/*分层遍历二叉树,以便检查后面的各种遍历*/
??levelTraverse(TreeHead);
??printf(\”

\”);
??while(flag==\’Y\’)
??{
???printf(\”*************************************************

\”);
???printf(\”?Q:前序遍历???????Z:中序遍历????????H:后序遍历

\”);
???printf(\”*************************************************

\”);
???printf(\”请根据上面的大写英语字母提示输入所需要的遍历方式:\”);
???scanf(\”%c\”,&judge);
???printf(\”

\”);
???if(judge==\’Q\’)????????/*前序遍历*/
???{
????printf(\”前序遍历为:\”);?????
????PreOrderTraverse(TreeHead);
????printf(\”

\”);
???}
???else?if(judge==\’Z\’)????/*中序遍历*/
???{
????printf(\”中序遍历为:\”);?????
????MidOrderTraverse(TreeHead);
????printf(\”

\”);
???}
???else?if(judge==\’H\’)????/*后序遍历*/
???{
????printf(\”后序遍历为:\”);
????PostOrderTraverse(TreeHead);
????printf(\”

\”);
???}
???else?
????printf(\”输入错误,请按照提示输入相应的大写字母!

\”);???/*输入错误提示*/
???getchar();
???printf(\”需要继续吗?Y/N:\”);?????/*重复遍历提示*/
???scanf(\”%c\”,&flag);
???printf(\”

\”);
???getchar();
??}
??}
??else
???printf(\”二叉树为空,无法执行后续操作

\”);??/*空树提示*/
}。
  

本文来自网络,不代表汉扬编程立场,转载请注明出处:http://www.hyzlch.com/mianfei/6422.html

已知二叉树的前序和后序遍历,怎么求中序遍历啊?

用C实现输入二叉树的前序和中序遍历

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

返回顶部