二叉树源程序.docx
《二叉树源程序.docx》由会员分享,可在线阅读,更多相关《二叉树源程序.docx(8页珍藏版)》请在冰豆网上搜索。
![二叉树源程序.docx](https://file1.bdocx.com/fileroot1/2023-1/14/c2b018ef-e54e-4bba-92d7-db5944d5ec29/c2b018ef-e54e-4bba-92d7-db5944d5ec291.gif)
二叉树源程序
二叉树源程序
程序存放在bltree.c文件中,以下为程序清单:
#include
#definenull0
#definemax100
intcounter=0;
intstack[max],top=0;
//****************************************//
//****************************************//
typedefstructbtreenode
{intdata;
structbtreenode*lchild;
structbtreenode*rchild;
}bnode;
//*****************************//
bnode*creat(intx,bnode*lbt,bnode*rbt)//建立一个只有根结点的二叉树
{bnode*p;
p=(bnode*)malloc(sizeof(bnode));
p->data=x;
p->lchild=lbt;
p->rchild=rbt;
return(p);
}
//************************************************//
bnode*inslchild(bnode*p,intx)//x作为左孩子插入到二叉树中
{bnode*q;
if(p==null)
printf("Illegalinsert.");
else
{q=(bnode*)malloc(sizeof(bnode));
q->data=x;
q->lchild=null;
q->rchild=null;
if(p->lchild!
=null)//若p有左孩子,则将原来的左孩子作为结点x的右孩子
q->rchild=p->lchild;
p->lchild=q;//x做为p的左孩子
}
}
//***************************************//
bnode*insrchild(bnode*p,intx)
{bnode*q;
if(p==null)
printf("Illegalinsert.");
else
{q=(bnode*)malloc(sizeof(bnode));
q->data=x;
q->lchild=null;
q->rchild=null;
if(p->rchild!
=null)
q->lchild=p->rchild;
p->rchild=q;
}
}
//**************************************//
voidprorder(bnode*p)//输出二叉树的结构
{if(p==null)
return;
printf("%d\t%u\t%d\t%u\t%u\n",++counter,p,p->data,p->lchild,p->rchild);
if(p->lchild!
=null)
prorder(p->lchild);
if(p->rchild!
=null)
prorder(p->rchild);
}
//***********************************//
voidprint(bnode*p)//嵌套括号表示二叉树,输出左子树前打印左括号,
{//输出右子树后打印右括号。
if(p!
=null)
{printf("%d",p->data);
if(p->lchild!
=null||p->rchild!
=null)
{printf("(");
print(p->lchild);
if(p->rchild!
=null)
printf(",");
print(p->rchild);
printf(")");
}
}
}
//************************************//
voidpreorder(bnode*p)//前序遍历
{printf("\n前序遍历:
");
while(p!
=null||top!
=0)
{if(p!
=null)
{printf("%d",p->data);//输出结点值
push(p);//将指针值压入栈中
p=p->lchild;//遍历左子树
}
else
{p=(bnode*)pop();
p=p->rchild;//遍历右子树
}
}
}
//***********************************//
voidinorder(bnode*p)//中序遍历
{printf("\n中序遍历:
");
while(p!
=null||top!
=0)
{while(p!
=null)
{push(p);//将根结点压入栈中
p=p->lchild;//遍历左子树
}
p=(bnode*)pop();
printf("%d",p->data);//输出当前结点值
p=p->rchild;//遍历右子树
}
}
//*********************************//
voidpostorder(bnode*p)//后序遍历
{unsignedsign;printf("\n后序遍历:
");//设置一个标志,记录结点从栈中弹出的次数
while(p!
=null||top!
=0)
{
if(p!
=null)
{push(p);//第1次遇到结点p时压入其指针值
push
(1);//置标志为1
p=p->lchild;//遍历结点p的左子树
}
else
while(top!
=0)
{sign=pop();
p=(bnode*)pop();
if(sign==1)//sign=1表示仅走过p的左子树
{push(p);//第2次压入结点p的指针值
push
(2);//设置标志为2
p=p->rchild;//遍历p的右子树
break;
}
else
if(sign==2)//sign=2表示p的左右子树都已走完
{printf("%d",p->data);//输出结点p的值
p=null;
}
}//while(top!
=0)
}//while(p!
=null||top!
=0)
}
//*****************************************//
voidtranslevel(bnode*p)//层次遍历
{structnode
{bnode*vec[max];
intfront,rear;
}q;
printf("\n层次遍历:
");
q.front=q.rear=0;
if(p!
=null)
printf("%d",p->data);
q.vec[q.rear]=p;//结点指针进入队列
q.rear=q.rear+1;
while(q.front{p=q.vec[q.front];//队头出队列
q.front=q.front+1;
if(p->lchild!
=null)//输出左孩子,并入队列
{printf("%d",p->lchild->data);
q.vec[q.rear]=p->lchild;
q.rear=q.rear+1;
}
if(p->rchild!
=null)
{printf("%d",p->rchild->data);
q.vec[q.rear]=p->rchild;
q.rear=q.rear+1;
}
}
}
//************************************//
push(s)
{top++;
stack[top]=s;
}
//***********************************//
pop()
{top--;
return(stack[top+1]);
}
//**********************************//
main()
{bnode*bt,*p,*q;
intx,y;
printf("数据输入说明:
先输入根,再按左孩子<双亲节点<右孩子输入其他节点,输入-1时结束输入\n");
printf("请输入根:
");
scanf("%d",&x);
p=creat(x,null,null);
bt=p;
printf("\n输入其他节点:
");
scanf("%d",&x);
while(x!
=-1)
{p=bt;
q=p;
while(x!
=p->data&&q!
=null)
{p=q;
if(xdata)
q=p->lchild;
else
q=p->rchild;
}
if(x==p->data)
printf("Thenode%dexistedalready!
\n",x);
else
if(xdata)
inslchild(p,x);
else
insrchild(p,x);
scanf("%d",&x);
}
p=bt;
printf("structureofthebinarytree:
\n");
printf("number\taddress\tdata\tlchild\trchild\n");
prorder(p);
printf("\n");
print(p);
printf("输出左子树前打印左括号,输出右子树后打印右括号。
\n");
printf("1前序遍历二叉树\n");
printf("2中序遍历二叉树\n");
printf("3后序遍历二叉树\n");
printf("4层次遍历二叉树\n");
printf("5结束程序的运行\n");
loop:
printf("\n请选择遍历方式:
");
scanf("%d",&y);
switch(y)
{
case1:
preorder(p);
printf("\n");
gotoloop;
case2:
inorder(p);
printf("\n");
gotoloop;
case3:
postorder(p);
printf("\n");
gotoloop;
case4:
translevel(p);
printf("\n");
gotoloop;
default:
printf("程序运行结束,按任意键退出!
\n");
}
}