二叉树的建立
来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/30 14:47:50
二叉树的建立
#define NULL 0
#include "stdio.h"
#include "stdlib.h"
//二叉链表结点定义
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
};
// 先序建立二叉树
struct tree *create(struct tree *BT,int k)
{
struct tree *p;
int x;
p=(struct tree *)malloc(sizeof(struct tree));
printf("输入结点的整数值(0表示空) :");
scanf("%d",&x);
if(x!=0)
{
if(!(p=(struct tree *)malloc(sizeof(struct tree))))
exit(0);
//生成主根或子树根
p->data=x;
p->lchild=NULL;
p->rchild=NULL;
if(k==0)
BT=p;
if(k==1)
BT->lchild=p;
if(k==2)
BT->rchild=p;
create(p,1);//建立左子树
create(p,2);//建立右子树
}
return(BT);
}
// 先序遍历
int visit(struct tree *BT)
{
if(BT!=NULL)
{
printf("%d ",BT->data);
visit(BT->lchild);
visit(BT->rchild);
}
return 0;
}
void main()
{
struct tree *p;
p=(struct tree *)malloc(sizeof(struct tree));
p=create(p,0);
visit(p);
printf("\n");
}
#include "stdio.h"
#include "stdlib.h"
//二叉链表结点定义
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
};
// 先序建立二叉树
struct tree *create(struct tree *BT,int k)
{
struct tree *p;
int x;
p=(struct tree *)malloc(sizeof(struct tree));
printf("输入结点的整数值(0表示空) :");
scanf("%d",&x);
if(x!=0)
{
if(!(p=(struct tree *)malloc(sizeof(struct tree))))
exit(0);
//生成主根或子树根
p->data=x;
p->lchild=NULL;
p->rchild=NULL;
if(k==0)
BT=p;
if(k==1)
BT->lchild=p;
if(k==2)
BT->rchild=p;
create(p,1);//建立左子树
create(p,2);//建立右子树
}
return(BT);
}
// 先序遍历
int visit(struct tree *BT)
{
if(BT!=NULL)
{
printf("%d ",BT->data);
visit(BT->lchild);
visit(BT->rchild);
}
return 0;
}
void main()
{
struct tree *p;
p=(struct tree *)malloc(sizeof(struct tree));
p=create(p,0);
visit(p);
printf("\n");
}
二叉树的后续序列为DCEGBFHKJIA,中序序列为DCBGEAHFIJK,试建立这颗二叉树,画出该二叉树的先序线索二叉
完全二叉树的定义,
按先序次序建立以下二叉树,然后按先序的顺序输出结点的值、层次、左右孩子结点;用C语言编写,初学数据结
46,88,45,39,70,58,101,10,66,34.建立一个二叉排序树,画出该二叉树,并求在相等概率下查找的平
数据结构试验(用C语言)建立一棵二叉树,并用递归或者非递归的算法分别用先序.中序和后序遍历、谢谢
数据结构中 二叉树建立结点为什么用 双重指针?详细解释下双重指针
关于二叉树结点算法的问题
求二叉树的结点个数算法
求二叉树的最远路径问题
二叉树的深度有关问题?
二叉树的结点指针值是什么?
为什么由二叉树的中序序列及前序序列唯一确定二叉树?