程序菜鸟2014 2014-11-03 17:23
浏览 1089

数据结构C语言版二叉树的问题。

strong text #include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "conio.h"
#define stacksize 100
#define DataType char
//便于后期修改。可以直接去修改char 类型来达到快速的修改,在程序长的情况下。
typedef struct node//二叉树的结构体定义
{
DataType data;
struct node *l;
struct node *r;
}node,*bitree;
typedef struct Node //线索二叉树的结构体定义
{
DataType data;
int ltag;
int rtag;
struct Node *l;
struct Node *r;
}BiTNode,*tree;

typedef struct //盏的结构体定义
{
DataType zhan[stacksize];
int top;
}wode;

void initstack(wode s) //初始化盏
{
s->top=-1;
}
void CreateBiTree(bitree *bt) //二叉树的建立二叉链表方法 //创建的二叉树是不可见的,没成功?
{

char ch;
ch = getchar();
if(ch=='.') bt=NULL;
else
{
*bt=(bitree)malloc(sizeof(bitree)); //生成一个新结点
(*bt)->data=ch;
CreateBiTree(&(
(bt))->l); //生成左子树
CreateBiTree(&(*(bt))->r); //生成右子树
}
printf("建立完成"); return;
}
void Visit(char ch)//访问节点
{
printf("%c ",ch);
}
void preorder(bitree root)//以先序的遍历序列来输出二叉树
{
if(root!=NULL)
{
printf("先序输出二叉树");
printf("%c",root->data);
preorder(root->l);
preorder(root->r);
}
}
void inorder(bitree root)//中序遍历二叉树
{
if(root!=NULL)
{
printf("中序遍历二叉树序列");
inorder(root->l);
Visit(root->data);
inorder(root->r);
}
}
void postorder(bitree root)//后序遍历二叉树
{
if(root!=NULL)
{
printf("后序遍历二叉树序列");
postorder(root->l);
postorder(root->r);
Visit(root->data);
}
}
void printfpreorder(bitree root)//二叉树先序输出叶子节点
{
printf("二叉树先序输出叶子节点");
if(root!=NULL)
{
if(root->l==NULL&&root->r==NULL) //判断条件左右子树为空
printf("%c",root->data);
printfpreorder(root->l);
printfpreorder(root->r);
}
}
void leaf(bitree root) //后序遍历二叉树的叶子节点输出
{
printf("后序遍历二叉树的叶子节点输出");
int n;
if(root!=NULL)
{
leaf(root->l);
leaf(root->r);
if(root->l==NULL&&root->r==NULL) //判断条件
n++;
}
}
int posttreedepth(bitree root)//后序遍历二叉树的深度递归算法
{
printf("后序遍历二叉树的深递归算法");
int hl,hr,max;
if(root!=NULL)
{
hl=posttreedepth(root->l);
hr=posttreedepth(root->r);
max=hl>hr?hl:hr;
return(max+1);
}
else return(0);
}
int pretreedepth(bitree root,int h) //先序遍历二叉树的深度的递归算法
{
printf("先序遍历二叉树深度的递归算法");
int depth;
if(root!=NULL)
{
if(h>depth) depth=h;
pretreedepth(root->l,h++);
pretreedepth(root->r,h++);
}
return(depth);
}
void Inorder(bitree root)//中序遍历的非递归算法
{

printf("中序遍历的非递归算法");
int top=0;
bitree p;
bitree s[stacksize];
int m;
m=stacksize-1;
p=root;
do{
while(p!=NULL)
{
if(top>m) return;
top=top+1;
s[top]=p;
p=p->l;
}
if(top!=0)
{
p=s[top];
top=top-1;
Visit(p->data);
p=p->r;
}
}
while(p!=NULL||top!=0);
}
void inthred(tree root) //二叉树的中序线索化
{
printf("二叉树的中序线索化");
tree pre;
if(root!=NULL)
{
inthred(root->l);
if(root->l==NULL)
{
root->ltag=1;root->l=pre;
}
if(pre!=NULL&&pre->r==NULL)
{
pre->r=root;
pre->rtag=1;
}
pre=root;
inthred(root->r);
}
}
BiTNode *inpre(tree p)//中序线索化后找节点的前驱
{
printf("中序线索化后找节点的前驱");
tree pre,q;
if(p->ltag==1)
pre=p->l;
else {
for(q=p->l;q->rtag==0;q=q->r);
pre=q;
}
return(pre);
}
BiTNode *innext(tree p)//在中序线索二叉树里找节点的后继
{
printf("在中序线索二叉树中找节点的后继");
tree next,q;
if(p->ltag==1)
next=p->l;
else {
for(q=p->r;q->ltag==0;q=q->l);
next=q;
}
return(next);
}
BiTNode *infirst(tree root)//在中序线索二叉树中找第一个节点
{
printf("在中序线索二叉树中找第一个节点");
BiTNode *p=root;
if(!p)
return(NULL);
while(p->ltag==0)
p=p->l;
return p;
}
void tinorder(tree root)//遍历中序线索二叉树
{ printf("遍历中序线索二叉树");
BiTNode *p;
p=infirst(p);
while (p)
{
printf("%c ",p->data);
p=innext(p);
}
}
void main()
{
int m;
int h=0;
bitree bt;
tree root;
CreateBiTree(&bt);
for(;;)
{
printf("请输入数子来选择你想要的功能\n");
printf("数字为----1-------时 建立二叉树\n");
printf("数字为----2-------时 先序遍历输出二叉树\n");
printf("数字为----3------时 中序遍历二叉树\n");
printf("数字为-----4------时 后序遍历二叉树\n");
printf("数字为------5------时 二叉树先序输出叶子节点\n");
printf("数字为------6-------时 后序遍历二叉树输出节点\n");
printf("数字为-------7------时 后序遍历二叉树的深度\n");
printf("数字为-------8--------时 先序遍历二叉树的深度\n");
printf("数字为------9------时 中序遍历的非递归算法\n");
printf("数字为-----10时 中序遍历线索化\n");
printf("数字为-------11------时 中序遍历线索化后找节点的前驱\n");
printf("数字为------12------时 中序线索化后找后继\n");
printf("数字为-------13------时 中序线索二叉树找第一个节点\n");
printf("数字为------14-----时 遍历中序线索二叉树\n");
printf("\n\n请输入 (1--14) 的数字");
scanf_s("%d",&m);
if(m>=2&&m<=14)
{
switch (m)
{

case 2:(bt); break; //先序遍历输出二叉树
case 3:inorder(bt); break; //中序遍历二叉树
case 4:postorder(bt); break;
case 5:printfpreorder(bt); break; //二叉树先序输出叶子节点
case 6:leaf(bt); break; //后序遍历二叉树输出节点
case 7:posttreedepth(bt); break; //后序遍历二叉树的深度
case 8:pretreedepth(bt,h); break; //先序遍历二叉树的深度
case 9:Inorder(bt); break; //中序遍历的非递归算法
case 10:inthred(root); break; //中序遍历的线索化
case 11:BiTNode *inpre(tree p); break; //中序线索化后找节点的前驱
case 12: *innext(root); break; //中序线索二叉树找后继
case 13:BiTNode *infirst(tree root); break; //中序线索二叉树找第一个节点
case 14:tinorder(root); break; //遍历中序线索二叉树
case 0: exit(0); break;
}

printf("\n\n操作完毕,请再次选择!");

}
else

printf("\n\n选择错误,请再次选择!");

}
}
这串代码在vs2012 上有错误,但我是一个新手不太明白,求大神解答,谢谢了。

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥100 有人会搭建GPT-J-6B框架吗?有偿
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名