DDD怒涛大王决策凯撒

本文档展示了多个二叉树遍历算法的实现,包括非递归前序遍历、层次遍历,以及寻找二叉树中特定节点的函数。此外,还提供了交换二叉树节点左右子树的函数以及根据前序和中序序列构造二叉树的函数。这些函数对于理解和操作二叉树结构非常有用。
摘要由CSDN通过智能技术生成

二叉树

Question one

/*

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

*/

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

/*函数preorder1()的功能是非递归前序遍历二叉树t,请将函数补充完整并调试运行*/
void preorder1(bintree t)
{
	seqstack s;
	s.top = 0;
	while(t || s.top != 0)//如果这个节点不为空并且这个栈不为空即可继续运行
	{
		if(t)
		{
			push(&s, t);
			printf("%c", t->data);//如果有左子节点就
			t = t->lchild;
		}
		else
		{
			t = pop(&s);
			t = t->rchild;
		}
	}
}
int main()
{   bintree t;
    t=creatbintree();   /*建立二叉树t的存储结构*/
    printf("二叉树的前序序列为:\n");
    preorder1(t);       /*前序非递归遍历二叉树*/
    return 0;
}

Question two

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

#include "bintree.h"
char *a="ABC##D#E##F##";  			/*扩充二叉树序树t的前序序列*/
void levelbintree(bintree t)
{
	bintree queue[N];
	int head = 0, end = 1;
	queue[head] = t;
	while(head < end)
	{
		if(queue[head]->lchild)	queue[end++] = queue[head]->lchild;
		if(queue[head]->rchild) queue[end++] = queue[head]->rchild;
		printf("%c",queue[head++]->data);
	}
	return;
	
}
int main()
{   bintree t;
    t=creatbintree();   	/*建立二叉树t的存储结构*/
    printf("二叉树的层次序列为:\n");
    levelbintree(t);       /*层次遍历二叉树*/
    return 0;
}

Question three

/*
编写函数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 == NULL)	return NULL;//判断空树
	else
	{
		if(t->lchild == NULL && t->rchild == NULL)//如果是当前唯一的根节点
			return t;
		else
		{
			if(t->rchild)	return prelast(t->rchild);//如果存在右子树,那么最终节点一定在右子树上
			else return prelast(t->lchild);//最终的结果一定存在于叶子节点上面
		}
	}
}

bintree prelast_2(bintree t)//非递归做法
{
	bintree s = t;
	if(s)
	{
		while(s->lchild || s->rchild)//只要它不是叶子节点
		{
			if(s->rchild)	s = s->rchild;
			else s = s->lchild;
		}
	}
	return s;
}

bintree postfirst(bintree t)
{
	bintree s = t;
	if(s)
	{
		while(s->lchild || s->rchild)//只要它不是叶子节点
		{
			if(s->lchild)	s = s->lchild;
			else s = s->rchild;
		}
	}
	return s;
}

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;
}

Question four

/*
假设二叉树采用链式方式存储,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;//相当于设定终止的判定
	
	m = Depth(t->lchild, x);//不断递归遍历
	
	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);
}

Question five

/*
   试编写一个函数,将一棵给定二叉树中所有结点的左、右子女互换。
*/
#include "bintree.h"
char *a="ABC##D##EF#G###";  		/*扩充二叉树序树t的前序序列*/
/*请将本函数补充完整,并进行测试*/
void change(bintree t)
{
	bintree temp;
	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);
}

Question six

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

#include "bintree.h"
#include <string.h>
char *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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜狗原来是我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值