早八什么的,已经没什么好怕的了

树形结构

Question one

/*
编写算法函数void levelorder(tree t)实现树的层次遍历。
*/

#include "tree.h"

void levelorder(tree t)    /* t为指向树根结点的指针*/
{
	tree queue[MAXLEN];//队列思想
	int head = 0, end = 1;
	int i;
	queue[head] = t;//将根节点加入队列
	while(head < end)
	{
		for(i = 0; i < m; i++)	
			if(queue[head]->child[i])//判断其子节点是否含有节点可以判断,可以的话就逐层入队
				queue[end++]  =  queue[head]->child[i]; 
		printf("%c ",queue[head++]->data);
	}
}

 int main()
 {
   tree t;
   printf("please input the preorder sequence of the tree:\n");
   t=createtree();
   printf("\nthe levelorder is:");
   levelorder(t);
   return 0;
 }

Question two

/*
假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PreOrder1(tree root),实现树的前序遍历算法。
*/
#include "tree.h"
void  PreOrder1(tree root)
{
	tree stack[MAXLEN];//本质上就是将递归的行为转化为栈,虽然递归的本质就是栈罢了
	int i;
	int top = -1;
	while(root || top != -1)
	{
		if(root)
		{
			printf("%c ",root->data);
			for(i = m - 1; i > 0; i--)
			{
				if(root->child[i] != NULL)//遍历自己的所有子树
				{
					stack[++top] = root->child[i];
				}
			}
			root = root->child[0];//将栈更新为自己的第一颗子树
		}
		else
		{
			root = stack[top--];//如果不满足就将当前的结果出栈即可
		}
	}
}
int main ()
{
        tree root;
        printf("please input the preorder sequence of the tree:\n");
		root =createtree();
		printf("前序序列是:\n");
		PreOrder1(root);
		return 0;
}

Question three

/*
   假设树采用指针方式的孩子表示法表示,试编写一个非递归函数void PostOrder1(tree t),实现树的后序遍历算法。
*/

#include "tree.h"//6.3
 void PostOrder1(tree root)//感觉是题目设计的问题,为什么不直接用void而用int
{
	int i;
	tree stacktree[MAXLEN];//表示进行处理的栈
	int top = -1;
	tree fintree[MAXLEN];//表示已经处理完的栈
	int toped = -1;
	if(root)  stacktree[++top] = root;//将根节点入栈
	while(top != -1)
	{
		root = stacktree[top--];//取出栈顶的元素进行处理
		for(i = 0; i < m;i ++)
			if(root->child[i]) stacktree[++top] = root->child[i];//如果可以就入栈
		fintree[++toped] = root;//将已经处理完的根节点进入栈
	}
	while(toped != -1)	printf("%c ", fintree[toped--]->data);
}
int main ()
{
    tree root;
    printf("please input the preorder sequence of the tree:\n");
	root =createtree();
	printf("后序序列是:\n");
	PostOrder1(root);
	return 0;
}

Question four

/*
假设树采用指针方式的孩子表示法表示,试编写一个函数int equal(tree t1, tree t2),
判断两棵给定的树是否等价(两棵树等价当且仅当其根结点的值相等且其对应的子树均相互等价)。
*/
#include "tree.h"
#define TRUE  1
#define FALSE 0

int equal(tree t1,tree t2)
{
	int flag = TRUE;
	int i;
	if(t1 == NULL && t2 == NULL)	return TRUE;//如果两个根目前都为空,直接可以将目前结果的比较值变为0
	else
	{
		if((t1 == NULL && t2 != NULL) || (t1 != NULL && t2 == NULL))	return FALSE;//如果t1和t2不满足同时为空的消息,那么说明这次的比较为失败
		else
		{
			if(t1->data != t2->data)	return FALSE;//如果该节点所指向的内容不同则是其原因
			else
			{
				for(i = 0; i < m; i++)	flag = flag&&equal(t1->child[i], t2->child[i]);//将flag看成是所有的结果的整合
				return flag;
			}
		}
	}
}

int main ()
{
	tree t1,t2;
    printf("please input the preorder sequence of the tree:\n");
	t1=createtree();
	getchar();
	printf("please input the preorder sequence of the tree:\n");
	t2=createtree();
    if ( equal(t1,t2) == TRUE)
	{
		printf ("两树相等\n");
	}
	else
	{
		printf ("两树不相等\n");
	}

	return 0;
}

Question five

/*
假设树采用指针方式的孩子表示法存储结构,试编写一个函数tree Ct(char s[]),
根据输入的树的括号表示字符串s,生成树的存储结构。例如,若要建立教材图6.4所示的树,
应输入A(B(E,F),C,D(G(I,J,K),H))。(说明,tree.h中定义的常量m表示树的最
大度,请根据建树的需要自行修改m的值)

*/

#include "tree.h"
/*请将本函数补充完整,并进行测试*/
tree Ct(char s[MAXLEN])
{
	int length, i, j;
	int top = -1;
	length = strlen(s);//调度字符串的长度
	tree stack[MAXLEN],root = NULL,temp = NULL, n;//使用栈的思想
	int childtree[MAXLEN];//计算其第几个孩子
	for(i = 0; i < length; i ++)//遍历所有的长度
	{
		if(s[i] == ',')	continue;//如果处理的是,的话就可以直接跳过;
		else if(s[i] == '(')//如果这个结果是(话就将这个结果入栈,并表示目前的孩子节点为0
		{
			stack[++top] = temp;
			childtree[top] = 0;
		}
		else if(s[i] == ')')	top--;//如果是这个就出栈
		else if(top != -1)//如果不是最后一个的话就不是处理根节点
		{
			n = (tree)malloc(sizeof(node));//建立空间
			n->data = s[i];//将数据存入
			for(j = 0; j < m;j++)	n->child[j] = NULL;//将他的所有孩子节点都变初始化为空
			temp = n;//状态更新为n的状态
			stack[top]->child[childtree[top]++] = temp;//将这个节点所对应的节点状态读入;
		}
		else
		{
			root = (tree)malloc(sizeof(node));//处理根节点
			root->data = s[i];
			for(j = 0; j < m; j++)	root->child[j] = NULL;
			temp = root; 
		}
	}
	return root;
}

int main ()
{
    char s[MAXLEN];
	tree root = NULL;
	printf ("请用树的括号表示法输入一棵树:\n");
	scanf ("%s",s);
	root = Ct(s);
	preorder(root);  /*前序遍历树*/
	return 0;//A(B(E,F),C,D(G(I,J,K),H))
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

菜狗原来是我自己

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

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

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

打赏作者

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

抵扣说明:

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

余额充值