有关二叉树的递归算法 C语言编程
(2008-12-22 09:07:14)
标签:
音乐二叉树ifn1n2it |
分类: 数据结构C语言版编程实例 |
本程序实现有关二叉树的递归算法,包括
1.求二叉树的层次(高度)
2.求二叉树的叶子个数
3.求二叉树的总结点个数
4.求二叉树的度为1的结点个数
5.求二叉树的度为2的结点个数
6.复制二叉树
7.交换二叉树的左右子树
8.利用先序序列建立二叉链表存储的二叉树
(二)基本要求
1.用二叉链表的形式存储二叉树
2.利用C的图形功能显示二叉树形态 为蓝底白树
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NULL 0
typedef int Status;
typedef char TElemType;
typedef struct BiTNode{
}BiTNode,*BiTree;
Status CreateBiTree(BiTree
*T)
{ char data;
}
Status DestroyBiTree(BiTree
*T)
{
}
void Paint(BiTree T,int x,int y,int
g)
{ char c[2];
}
int level(BiTree
T)
{
}
int leaf(BiTree
T)
{
}
int nodes(BiTree
T)
{
}
int node1(BiTree
T)
{
}
int node2(BiTree
T)
{
}
void Copy(BiTree T,BiTree
*T2)
{
}
void exchange(BiTree
*T)
{
}
main()
{
BiTree T,T2;
int gdriver,gmode;
int d,yezi,jiedian,dian1,dian2;
x=320;y=30;
gdriver=DETECT;
printf("input the BiTree's data(in PreOrder and
<=63ge):\n");
if(!CreateBiTree(&T))printf("kongjian OVERFLOW! can
not create!\n");
else printf("cunchu success!\n");
printf("press any key to contunue:\n");
getch();
printf("level of the tree is
:\n");
d=level(T);
printf("%d\n",d);
printf("\n press any key to continue:\n");
getch();
printf("leaf number of the tree is
:\n");
yezi=leaf(T);
printf("%d\n",yezi);
printf("\n press any key to continue:\n");
getch();
printf("nodes number of the tree is
:\n");
jiedian=nodes(T);
printf("%d\n",jiedian);
printf("\n press any key to continue:\n");
getch();
printf("du wei 1 d jiedian node1 of the tree is
:\n");
dian1=node1(T);
printf("%d\n",dian1);
printf("\n press any key to continue:\n");
getch();
printf("node2 of the tree is
:\n");
dian2=node2(T);
printf("%d\n",dian2);
printf("\n press any key to continue:\n");
getch();
printf("Now copy the
tree:\n");
Copy(T,&T2);
printf("\n press any key to continue:\n");
getch();
printf("now,print the BiTree like a
tree:\n");
printf("press any key to see the tree:\n");
getch();
initgraph(&gdriver,&gmode,"C:\\TC");
setbkcolor(BLUE);
cleardevice();
Paint(T,x,y,g);
getch();
closegraph();
printf("press any key to continue:\n");
getch();
printf("Now change the tree d right and
left:\n");
exchange(&T);
printf("\n press any key to continue:\n");
getch();
printf("now,print the BiTree like a tree:\n");
printf("press any key to see the
tree:\n");
getch();
initgraph(&gdriver,&gmode,"C:\\TC");
setbkcolor(BLUE);
cleardevice();
Paint(T,x,y,g);
getch();
closegraph();
printf("press any key to continue:\n");
getch();
if(DestroyBiTree(&T))printf("the BiTree
destroyed!\n");
else printf("NULL!can not destroy!\n");
printf("press any key to
continue:\n");
getch();
}

加载中…