二叉树图形演示程序
来源:互联网 发布:单片机调试助手 编辑:程序博客网 时间:2024/05/16 06:46
用了两天半时间写的这个程序,晒晒,望大家指正,嘿嘿!包括二叉树的创建,各种遍历,查找,删除。
程序说明:运行前需在tc目录下新建一个tree.txt文本文档,先序输入一个树,例如 abcdef..g..h..i..j..k.lmno..p..q..r..
#include "Conio.h"
#include "graphics.h"
#include "stdio.h"
#include "dos.h"
#define closegr closegraph
typedef struct node{
char data;
int x;
int y;
int m;
struct node *lchild;
struct node *rchild;
struct node *parent;
}TreeNode;
TreeNode *p[100];
TreeNode *root;
TreeNode *droot;
float x=310;
int y=30;
float x0=150;
int y0=80;
int i=0;
int count=10;
int j=0;
int m=0;
char str[100];
char *strstr=str;
TreeNode *CreateTree(char **,int); /*建树*/
void PreOrder(TreeNode *); /*先序遍历*/
void Parent(TreeNode *); /*连接父节点*/
void InOrder(TreeNode *); /*中序遍历*/
void PosOrder(TreeNode *); /*后序遍历*/
TreeNode *Find(TreeNode *,char); /*查找*/
int Delete(TreeNode *,char); /*删除一个*/
void DeleteAll(TreeNode *); /*删除全部节点*/
void PosAddress(TreeNode *); /*按后序遍历,将各个节点的地址存入指针数组*/
void PosLine(); /*按后序遍历,将各个节点连接起来*/
void DrawCircle(int color,int x,int y); /*画圆*/
void DrawText(int color,int font,int size,int x,int y,char *c); /*输出文字*/
void DrawLine(int color,int x1,int y1,int x2,int y2); /*画线*/
void DeleteLine(); /*删除线段,即用背景色覆盖原线*/
char menu();
void initgr(void)
{
int gd = DETECT, gm = 0;
initgraph(&gd, &gm, "");
}
void DrawCircle(int color,int x,int y)
{
setfillstyle(1,color);
setcolor(color);
pieslice(x,y,0,360,10);
}
void DrawText(int color,int font,int size,int x,int y,char *c)
{
setcolor(color);
settextstyle(font,0,size);
outtextxy(x,y,c);
}
void DrawLine(int color,int x1,int y1,int x2,int y2)
{
setcolor(color);
line(x1,y1,x2,y2);
}
void DeleteLine(TreeNode *rootp,int color)
{
if(rootp->parent==0 && rootp->lchild!=0)
DrawLine(color,rootp->x,rootp->y,rootp->lchild->x,rootp->lchild->y);
if(rootp->parent==0 && rootp->rchild!=0)
DrawLine(color,rootp->x,rootp->y,rootp->rchild->x,rootp->rchild->y);
if(rootp->parent!=0)
DrawLine(color,rootp->x,rootp->y,rootp->parent->x,rootp->parent->y);
if(rootp->parent!=0 && rootp->lchild!=0)
DrawLine(color,rootp->lchild->x,rootp->lchild->y,rootp->x,rootp->y);
if(rootp->parent!=0 && rootp->rchild!=0)
DrawLine(color,rootp->rchild->x,rootp->rchild->y,rootp->x,rootp->y);
}
void Pout(TreeNode *rootp)
{
char data[5]={0};
if(!rootp) return;
data[0]=rootp->data;
DrawCircle(2,rootp->x,rootp->y);
DrawText(4,0,1,rootp->x,rootp->y,data);
if(rootp->parent!=0)
DrawLine(6,rootp->x,rootp->y,rootp->parent->x,rootp->parent->y);
Pout(rootp->lchild);
Pout(rootp->rchild);
}
TreeNode *CreateTree(char **p,int flag)
{
TreeNode *rootp;
char cur[5]={0};
cur[0]=*(*p);
if(cur[0]=='.') return 0;
rootp=(TreeNode *)malloc(sizeof(TreeNode));
if(!rootp) return 0;
rootp->data=cur[0];
if(flag==0) /*flag=0:根节点 flag=1:左节点 flag=2:右节点*/
{
root=rootp;
rootp->parent=0;
rootp->x=x;
rootp->y=y;
DrawCircle(2,x,y);
DrawText(4,0,1,x,y,cur);
getch();
}
else if(flag==1)
{
rootp->m=1; /*m=1:左节点*/
DrawLine(6,x,y,x-x0,y+y0);
getch();
x-=x0;
y+=y0;
rootp->x=x;
rootp->y=y;
DrawCircle(2,x,y);
DrawText(4,0,1,x,y,cur);
getch();
x0=x0/2;
}
else if(flag==2)
{
rootp->m=2;
DrawLine(6,x,y,x+x0,y+y0);
getch();
x+=x0;
y+=y0;
rootp->x=x;
rootp->y=y;
DrawCircle(2,x,y);
DrawText(4,0,1,x,y,cur);
getch();
x0=x0/2;
}
(*p)++;
rootp->lchild=CreateTree(p,1);
if(rootp->lchild==0) (*p)++;
rootp->rchild=CreateTree(p,2);
if(rootp->rchild==0)
{
if(flag==1) { x0=x0*2; x+=x0; y-=y0; }
else if(flag==2) { x0=x0*2;x-=x0; y-=y0; }
(*p)++;
}
else
{
if(rootp->m==1) { x0=x0*2; x+=x0; y-=y0; }
else if(rootp->m==2) { x0=x0*2; x-=x0; y-=y0; }
}
Parent(rootp); /*树创建完毕后,就让各个孩子知道自己的父节点,以便Delete()和LianXian()用*/
return rootp;
}
void PreOrder(TreeNode *rootp)
{
char data[5]={0};
if(!rootp) return;
data[0]=rootp->data;
DrawText(90,3,2,count,450,data);
count+=20;
DrawCircle(14,rootp->x,rootp->y);
DrawText(2,0,1,rootp->x,rootp->y,data);
getch();
PreOrder(rootp->lchild);
PreOrder(rootp->rchild);
}
void Parent(TreeNode *rootp)
{
if(!rootp) return;
if(rootp->lchild!=0)
rootp->lchild->parent=rootp;
if(rootp->rchild!=0)
rootp->rchild->parent=rootp;
Parent(rootp->lchild);
Parent(rootp->rchild);
}
void InOrder(TreeNode *rootp)
{
char data[5]={0};
if(!rootp) return;
InOrder(rootp->lchild);
data[0]=rootp->data;
DrawText(90,3,2,count,450,data);
count+=20;
DrawCircle(20,rootp->x,rootp->y);
DrawText(3,0,1,rootp->x,rootp->y,data);
getch();
InOrder(rootp->rchild);
}
void PosOrder(TreeNode *rootp)
{
char data[5]={0};
if(!rootp) return;
PosOrder(rootp->lchild);
PosOrder(rootp->rchild);
DrawCircle(1,rootp->x,rootp->y);
data[0]=rootp->data;
DrawText(90,3,2,count,450,data);
count+=20;
DrawText(4,0,1,rootp->x,rootp->y,data);
getch();
}
TreeNode *Find(TreeNode *rootp,char c)
{
char data[5]={0};
int b;
if(!rootp) return 0;
getch();
if(rootp->data==c)
{
data[0]=rootp->data;
getch();
DrawCircle(4,rootp->x,rootp->y);
DrawText(2,0,1,rootp->x,rootp->y,data);
getch();
DrawText(4,3,3,50,450,"Find it success! Press any key to continue!/n");
getch();
/*exit(rootp);*/
main();
}
else
{
data[0]=rootp->data;
DrawCircle(14,rootp->x,rootp->y);
DrawText(2,0,1,rootp->x,rootp->y,data);
Find(rootp->lchild,c);
Find(rootp->rchild,c);
}
}
int Delete(TreeNode *rootp,char c)
{
char data[5]={0};
if(!rootp) return 0;
getch();
if(rootp->data==c)
{
if(rootp==root) root=0;
data[0]=rootp->data;
DrawCircle(4,rootp->x,rootp->y);
DrawText(2,0,1,rootp->x,rootp->y,data);
getch();
i=0;
DeleteAll(rootp);
if(rootp->m==1) rootp->parent->lchild=0;
if(rootp->m==2) rootp->parent->rchild=0;
free(rootp);
rootp=0;
getch();
setcolor(4);
settextstyle(3,0,3);
outtextxy(50,450,"Delete success!");
outtextxy(260,450,"Press any key to continue!");
getch();
main();
}
else
{
data[0]=rootp->data;
DrawCircle(14,rootp->x,rootp->y);
setcolor(2);
outtextxy(rootp->x,rootp->y,data);
Delete(rootp->lchild,c);
Delete(rootp->rchild,c);
}
}
void DeleteAll(TreeNode *rootp)
{
char data[5]={0};
if(!rootp) return;
if(i==0) droot=rootp;
i++;
data[0]=rootp->data;
DrawCircle(0,rootp->x,rootp->y);
DeleteLine(rootp,0);
DeleteAll(rootp->lchild);
DeleteAll(rootp->rchild);
}
void PosAddress(TreeNode *rootp) /*按后序遍历的顺序,将各节点的地址存入指针数组中*/
{
if(!rootp) return;
PosAddress(rootp->lchild);
PosAddress(rootp->rchild);
setfillstyle(1,12);
setcolor(12);
pieslice(rootp->x,rootp->y,0,360,3);
p[j]=rootp;
j++;
}
void PosLine()
{
i=0;
while(p[i+1]!=0)
{
getch();
DrawLine(2,p[i]->x,p[i]->y,p[i+1]->x,p[i+1]->y);
i++;
}
}
char menu()
{
char choose;
system("cls");
DrawText(156,3,4,135,10,"Please make a choice/n");
setcolor(2);
arc(270,70,240,30,40);
arc(300,100,80,190,40);
arc(210,170,240,30,40);
arc(240,200,80,190,40);
arc(140,270,240,30,40);
arc(170,300,80,190,40);
arc(260,270,240,30,40);
arc(290,300,80,190,40);
arc(330,170,240,30,40);
arc(360,200,80,190,40);
arc(380,270,240,30,40);
arc(410,300,80,190,40);
arc(320,370,240,30,40);
arc(350,400,80,190,40);
arc(430,370,240,30,40);
arc(460,400,80,190,40);
DrawText(2,4,4,280,70,"1");
DrawText(4,3,2,320,70,"Create");
DrawText(2,4,4,220,170,"2");
DrawText(4,3,2,110,170,"PreOrder");
DrawText(2,4,4,150,270,"3");
DrawText(4,3,2,115,310,"InOrder");
DrawText(2,4,4,270,270,"4");
DrawText(4,3,2,230,310,"PosOrder");
DrawText(2,4,4,340,170,"5");
DrawText(4,3,2,380,170,"Find");
DrawText(2,4,4,390,270,"6");
DrawText(4,3,2,431,270,"DeleteOne");
DrawText(2,4,4,330,370,"7");
DrawText(4,3,2,300,410,"DeleteAll");
DrawText(2,4,4,440,370,"8");
DrawText(4,3,2,430,410,"Exit");
setcolor(6);
line(280,110,238,160); /*12*/
line(210,210,170,260); /*23*/
line(210,210,260,270); /*24*/
line(280,110,330,170); /*15*/
line(330,210,380,270); /*56*/
line(380,310,350,360); /*67*/
line(380,310,430,370);
choose=getch();
return choose;
}
main()
{
TreeNode *rootp;
char fu,ch,en;
int r=0,d;
FILE *fp;
clrscr();
initgr();
if(m==0)
{
DrawText(2,3,5,30,150,"Welcome to my Binary Tree!/n");
DrawText(156,1,4,120,250,"Press any key to into it!");
DrawText(3,3,1,400,400,"author: Xiao MengMeng");
DrawText(3,3,1,410,430,"date: 2010.8.3--8.5");
getch();
}
m++;
if((fp=fopen("tree.txt","r"))==NULL)
{
system("cls");
DrawText(4,3,4,150,150,"Error on open tree.txt!");
DrawText(4,3,4,190,200,"Press any to exit!");
getch();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
str[r]=ch;
ch=fgetc(fp);
printf("%c",ch);
r++;
}
while(1)
{
fu=menu();
switch(fu)
{
case '1':
system("cls");
DrawText(11,3,4,30,10,"Create");
rootp=CreateTree(&strstr,0);
if(rootp!=0)
{
DrawText(4,3,3,40,450,"Create success! Press any key to continue!/n");
getch();
continue;
}
getch();
break;
case '2':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"PreOrder");
Pout(root); getch();
PreOrder(root);
count=10;
break;
case '3':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"InOrder");
Pout(root); getch();
InOrder(root);
count=10;
break;
case '4':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"PosOrder");
Pout(root); getch();
PosOrder(root);
count=10;
break;
case '5':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"Find");
Pout(root);
getch();
printf("/n/n/n/n You want to find? :");
scanf("%c",&fu);
rootp=Find(root,fu);
if(!rootp)
{
DrawText(4,3,3,90,450,"No find... Press any key to continue!");
getch();
}
break;
case '6':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"Delete One");
Pout(root);
getch();
printf("/n/n/n/n You want to delete? :");
scanf("%c",&fu);
r=Delete(root,fu);
if(r!=1)
{
DrawText(4,3,3,90,450,"No find... Press any key to continue!");
getch();
}
break;
case '7':
system("cls");
if(root==0)
{
DrawText(10,3,4,150,150,"The tree is empty!");
DrawText(10,3,4,110,200,"Press any key to continue...");
getch();
continue;
}
DrawText(11,3,4,30,10,"Delete All");
Pout(root);
getch();
DeleteAll(root);
droot=0;
free(droot);
getch();
PosAddress(root);
PosLine();
root=0;
DrawText(4,3,3,110,450,"Success! Press any key to continue!");
getch();
exit(0);
break;
case '8':exit(0);
default:break;
}
}
fclose(fp);
closegraph();
}
- 二叉树图形演示程序
- 图形化二叉树演示软件
- 图形基类演示程序完成。
- 二叉树演示C代码
- 二叉树的图形显示
- 二叉树图形显示(VC)
- 图形化打印二叉树
- 基于二叉堆的ASTAR算法演示程序
- 树与二叉树遍历演示
- 动画演示平衡二叉树旋转
- 动画演示平衡二叉树旋转
- 查找二叉树的实现与演示
- 需求分析图形演示
- 演示程序
- MFC 输出图形正向二叉树
- 第十四周-图形构造平衡二叉树
- python 按图形打印二叉树
- 八皇后VC图形演示
- 【从CSLA中学C#】3 序列化的使用
- http://acm.hdu.edu.cn/showproblem.php?pid=1124 N!末尾0的个数
- 安装完Framework后如何不重启系统?
- 虚拟机
- HP推出融合基础设施(Converged Infrastructure)
- 二叉树图形演示程序
- 在ubuntu9.04上编译安装meshlab v123a
- pku 1166 The Clocks
- 信贷业务培训心得
- C# 遍历枚举
- C# 遍历枚举
- Debian(lenny)SVN设置目录权限
- 今天我没有合并
- Windows Mobile SDK例子浅析