数据结构C语言版(李云清)实验7 二叉树

该博客介绍了关于二叉树的六个实验:1.非递归前序遍历;2.层次遍历;3.获取前序遍历的末尾结点和后序遍历的首结点;4.查找指定值节点的层次;5.交换二叉树所有节点的左右子节点;6.递归构建二叉树的链表存储结构。

实验7 二叉树

1、编写算法函数void preorder1(bintree t)实现二叉树t的非递归前序遍历。

#include "bintree.h"
char *a="ABC##D#E##F##";  /*扩充二叉树序树t的前序序列*/

/*函数preorder1()的功能是非递归前序遍历二叉树t,请将函数补充完整并调试运行*/
void preorder1(bintree t)
{
    seqstack  s;
    init(&s);
    while ( !empty(&s) || t )
    {
        if (t)
        {
            printf("%c",t->data);
            push(&s,  t);
            t=t->lchild;
        }
        else
        {
            t=pop(&s);
            t=t->rchild;
        }
    }
}
int main()
{   bintree t;
    t=creatbintree();   /*建立二叉树t的存储结构*/
    printf("二叉树的前序序列为:\n");
    preorder1(t);       /*前序非递归遍历二叉树*/
    return 0;
}

2、编写算法函数void levelbintree(bintree t),实现二叉树的层次遍历。

#include "bintree.h"
char *a="ABC##D#E##F##";  			/*扩充二叉树序树t的前序序列*/
void levelbintree(bintree t)
{
    bintree queue[100];
    int f,r;
    f=0;r=1;
    if (t)
    {
        queue[0]=t;
        while (f<r)
        {
            t=queue[f++];
            printf("%c",t->data);
            if (t->lchild)
                    queue[r++]=t->lchild;
            if (t->rchild)
                    queue[r++]=t->rchild;
        }
    }


}
int main()
{   bintree t;
    t=creatbintree();   	/*建立二叉树t的存储结构*/
    printf("二叉树的层次序列为:\n");
    levelbintree(t);       /*层次遍历二叉树*/
    return 0;
}

3、编写函数bintree prelist(bintree t),bintree postfirst(bintree t),分别返回二叉树t在前序遍历下的最后一个结点地址和后序遍历下的第一个结点地址。


#include "bintree.h"
char *a="ABC##D##EF#G###";  /*扩充二叉树序树t的前序序列*/
bintree prelast(bintree t)      //递归实现
{
    if (!t)
            return t;       //空树
    else
        if (t->lchild==NULL &&t->rchild==NULL)      //树根是唯一的结点
            return t;
        else
            if (t->rchild)          //右子树非空
                return prelast(t->rchild);
                else
                              return prelast(t->lchild);
}
bintree prelast1(bintree t)     //非递归实现
{
    bintree p=t;
    if (p)
    {
        while (p->lchild || p->rchild )         //p为非叶子
            if (p->rchild)
                p=p->rchild;
            else
                p=p->lchild;
    }
    return p;
}
bintree postfirst(bintree t)
{   bintree p=t;
    if (p)
    {
        while (p->lchild || p->rchild )
            if (p->lchild)
                p=p->lchild;
            else
                p=p->rchild;
    }
    return p;

}

int main()
{   bintree t,p,q;
    t=creatbintree();   	/*建立二叉树t的存储结构*/
    p=prelast(t);
	q=postfirst(t);
	if (t!=NULL)
            {   printf("前序遍历最后一个结点为:%c\n",p->data);
			    printf("后序遍历第一个结点为:%c\n",q->data);
            }
	 else	printf("二叉树为空!");
    return 0;
}



4、假设二叉树采用链式方式存储,t为其根结点,编写一个函数int Depth(bintree t, char x),求值为x的结点在二叉树中的层次。

#include "bintree.h"
char *a="ABC##D##EF#G###";  		/*扩充二叉树序树t的前序序列*/

/*
 	函数Depth,功能:求结点x所在的层次
*/
int Depth(bintree t,char x)
{
    int m,n;
    if( !t ) return -1;				        /*未找到的分支标记为-1*/
	if( t->data == x ) return 1;	/*找到返回1*/

	m = Depth(t->lchild,x);			/*因为没有子树就返回-1,所以没必要考虑是否存在子树*/

	if( m != -1 ) return m+1;		/*找到则加1往上计数*/
	else
    {       n = Depth(t->rchild,x);
            if( n != -1 ) return n+1;
                    else return -1;			/*没找到继续返回-1*/

    }

}

int main()
{  bintree root;
   char x;
   int k=0;
   root=creatbintree();
   printf("请输入树中的1个结点值:\n");
   scanf("%c",&x);
   k=Depth(root,x);
   printf("%c结点的层次为%d\n",x,k);
}

5、 试编写一个函数,将一棵给定二叉树中所有结点的左、右子女互换。

#include "bintree.h"
char *a="ABC##D##EF#G###";  		/*扩充二叉树序树t的前序序列*/
/*请将本函数补充完整,并进行测试*/
void change(bintree t)
{
    bintree temp;				/*将t的左右结点交换*/
    if (t)
    {
                temp = t->lchild;
                t->lchild = t->rchild;
                t->rchild = temp;
                if( t->lchild ) change( t->lchild );	/*交换左子树*/
                if( t->rchild ) change( t->rchild );	/*交换右子树*/
    }
}
int main()
{  bintree root;
   root=creatbintree();
   change(root);
   preorder(root);
}

6、试编写一个递归函数bintree buildBintree(char *pre, char *mid, int length),根据二叉树的前序序列pre、中序序列mid和前序序列长度length,构造二叉树的二叉链表存储结构,函数返回二叉树的树根地址。

#include "bintree.h"
#include <string.h>
char *a="";

/*
  大概的原理:前序序列的第一个字符是树的根结点root(比如说是 A ),
  并且A后面的是左子树的前序序列,然后右子树的前序序列
  在中序序列中, A 左边的是左子树的中序序列, A 右边是右子树的中序序列
*/
bintree buildBintree(char *pre, char *mid,int length)
{
	if( length )
	{
		/*↓↓以下两行,创建树的根节点*/
		bintree root = (bintree)malloc(sizeof(binnode));
		root->data = pre[0];
		/*↓↓以下三行,将中序序列拆分成【左子树的中序序列】和【右子树的中序序列】*/
		int i;
		for(i=0;mid[i] != pre[0];i++) ;
		mid[i] = '\0';
		/*↓↓以下两行,递归建立左子树和右子树,同理,将前序序列拆分成【左子树的前序序列】和【右子树的前序序列】*/
		root->lchild = buildBintree(pre+1,mid,i);
		root->rchild = buildBintree(pre+i+1,mid+i+1,length-i-1);
		/*最后return根结点*/
		return root;
	}
	else return NULL;
}

int main()
{   bintree root;
    char pre[100],mid[100];
    puts("请输入前序序列:");
    gets(pre);
    puts("请输入中序序列:");
    gets(mid);
    root=buildBintree(pre,mid,strlen(pre));
    puts("后序序列是:");
    postorder(root);
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值