二叉树的遍历和存储 建树,由前中推后,由中后推前,根据节点建树,层次遍历,求叶子数,深度

一、根据节点建树
已知二叉树的一个按先序遍历输入的字符序列,如abc,de,g,f, (其中,表示空结点)。输出先序、中序、后序遍历,并计算叶子节点数和二叉树深度。

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

char str[51];
int num=0,cnt=0;
struct tree{
	char data;
	struct tree *l,*r;
};

typedef struct tree node;

node *create()
{
	node *root;
	char c=str[num++];
	 	if(c==','){
	 		return NULL;
		 }
		 else{
		 	root=(node *)malloc(sizeof(node));
		 	root->data=c;
		 	root->l=create();
		 	root->r=create();
		 	if(root->l==NULL&&root->r==NULL)
		 	{
		 		cnt++;							//计算叶子节点数
			 }
		 }
	return root; 
}
void preorder(node *root)			//先序遍历函数
{
	if(root){
		printf("%c",root->data);
		preorder(root->l);
		preorder(root->r);
	}
 } 
void midorder(node *root)			//中序遍历函数
{
	if(root){
		preorder(root->l);
		printf("%c",root->data);
		preorder(root->r);
	}
 } 
void endorder(node *root)			//后序遍历函数
{
	if(root){
		preorder(root->l);
		preorder(root->r);
		printf("%c",root->data);
	}
 } 
 void cengci (node *root){
	int x=0,y=0;
	node *p[51];
	p[y++]=root;		//第一个一定指向root,经常忘,不是指向root->data;
	while(x<y){
		if(p[x]){
			printf("%c",p[x]->data);
			p[y++]=p[x]->l;
			p[y++]=p[x]->r;
		}
		x++;
	}
 }
int height(node *root){			//计算树的深度的函数
 	 if(root)
 	 {
 	 	int hl=height(root->l);
 	 	int hr=height(root->r);
 	 	return hl>hr?(hl+1):(hr+1);
	  } 	
	  else{return 0;
	  }
 }
int main(){
	node *root;
	while(~scanf("%s",&str))
	{	
		num=0;		
		cnt=0;		//记得初始化
		root=create();		//建树
		preorder(root);		//先序遍历
		printf("\n");
		midorder(root);		//中序遍历
		printf("\n");
		endorder(root);		//后序遍历
		printf("\n");
		cengci(root);		//层次遍历
		printf("\n");
		printf("%d\n",cnt);	//输出叶子节点数
		printf("%d\n",height(root));//输出树的高度
	}
	return 0;	
}

二、根据前序遍历和中序遍历建树
测试实例
输入

ABDCEF
BDAECF

输出

DBEFCA
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct tree{
	char data;
	struct tree *l,*r;
};

typedef struct tree node;
node *create(char pre[],char mid[],int len)
{
	node *root;
	int i;
	root=(node*)malloc(sizeof(node));
	for(i=0;i<len;i++)
	{
		if(pre[0]==mid[i]){
			root->data=pre[0];
			root->l=create(pre+1,mid,i);				//先序遍历:根左右  中序遍历:左右根
			root->r=create(pre+1+i,mid+1+i,len-1-i);
			return root;
		}
	}
	return NULL;
}
endorder(node *root)
{
	if(root){
		preorder(root->l);
		preorder(root->r);
		printf("%c",root->data);
	}
	return ;
 } 

int main(){
	node *root;
	char pre[51],mid[51];
	while(~scanf("%s%s",&pre,&mid))
	{	
		root=create(pre,mid,strlen(pre));
		endorder(root);					//检查一下上边建树是否成功
		printf("\n");
	}
	return 0;	
}

三、根据中序和后序遍历建树
测试实例
输入

dbgeafc
dgebfca

输出

abdegcf
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

struct tree{
	char data;
	struct tree *l,*r;
};

typedef struct tree node;
node *create(char mid[],char end[],int len)
{
	node *root;
	int i;
	root=(node*)malloc(sizeof(node));
	for(i=0;i<len;i++)
	{
		if(mid[i]==end[len-1]){
			root->data=end[len-1];
			root->l=create(mid,end,i);				//中序遍历:左根右  后序遍历:左右根
			root->r=create(mid+1+i,end+i,len-1-i);
			return root;
		}
	}
	return NULL;
}
preorder(node *root)
{
	if(root){
		printf("%c",root->data);
		preorder(root->l);
		preorder(root->r);
	}
	return ;
 } 

int main(){
	node *root;
	char end[51],mid[51];
	while(~scanf("%s%s",&mid,&end))
	{	
		root=create(mid,end,strlen(mid));
		preorder(root);
		printf("\n");
	}
	return 0;	
}

遇到了一些问题
1、注意return 的返回值,关于return 的知识查博客中的笔记(快排)很重要***
2、注意每一个函数在调用其他函数的时候,其他函数必须在该函数的上方
不然这个细节问题会影响调用
3、对于给出的序列(前中,中后,带‘,’的序列)每个序列建树的时候有一些不同,细节性的问题要注意
4、二叉树 中的根节点指向问题要注意,都是细节,要仔细,不然不会成功运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值