c语言-链表二叉树-创建-遍历-求高度等复试常见问题

二叉树创建

二叉树的细节就是c==‘0’ 要让T=NULL部分,我们整体就递归建立就可以了,二叉树输入由于是char类型,所以在提前创建二叉树的时候首先就要想好如何输入二叉树字符串,比如输入12000在按下回车键才可以结束程序,并正确的运行。否则程序无法正常结束。所以提前构建好二叉树模型转化成字符串的形式。

typedef struct tree{
	char data;
	struct tree *lchild,*rchild;
}Tree,*BiTree;
void create(BiTree &T)
{
	char c;
	scanf("%c",&c);
	if(c=='0')
		T=NULL;
	else
	{
		T=(BiTree)malloc(sizeof(Tree));
		T->data=c;
		create(T->lchild);
		create(T->rchild);
	}
 } 

三大遍历

 void inorder(BiTree T)
 {
 	if(T)
 	{
 		inorder(T->lchild);
		printf("%c",T->data);
		inorder(T->rchild); 
	 }
 }
 void pre(BiTree T)
 {
 	if(T)
 	{
 		printf("%c",T->data);
 		pre(T->lchild);
		pre(T->rchild); 
	 }
 }	
 
  void hou(BiTree T)
 {
 	if(T)
 	{
 		hou(T->lchild);
		hou(T->rchild); 
		printf("%c",T->data);
	 }
 }

案例

#include<stdio.h>
#include<malloc.h>
typedef struct tree{
	char data;
	struct tree *lchild,*rchild;
}Tree,*BiTree;
void create(BiTree &T)
{
	char c;
	scanf("%c",&c);
	if(c=='0')
		T=NULL;
	else
	{
		T=(BiTree)malloc(sizeof(Tree));
		T->data=c;
		create(T->lchild);
		create(T->rchild);
	}
 } 
 void inorder(BiTree T)
 {
 	if(T)
 	{
 		inorder(T->lchild);
		printf("%c",T->data);
		inorder(T->rchild); 
	 }
 }
 void pre(BiTree T)
 {
 	if(T)
 	{
 		printf("%c",T->data);
 		pre(T->lchild);
		pre(T->rchild); 
	 }
 }	
 
  void hou(BiTree T)
 {
 	if(T)
 	{
 		hou(T->lchild);
		hou(T->rchild); 
		printf("%c",T->data);
	 }
 }
 int main()
 {
 	BiTree T;
 	create(T);
 	inorder(T);
 }

这部分代码直接复制粘贴到这个例子案例就可以了。此外二叉排序树的建立,是提前输好值得。如ACB回车就可以了。

二叉搜索树的建立

 int create_bst(BiTree &T,char c)
 {
 	if(T==NULL)
 	{
 		T=(BiTree)malloc(sizeof(Tree));
 		T->data=c;
 		T->lchild=T->rchild=NULL;
	 }
	else if(T->data==c)
	{
		return 0;
	}
	else if(T->data>c)
	{
		return create_bst(T->lchild,c);
	}
	else
	{
		return create_bst(T->rchild,c);
	}
 }
 
 void bst_tree(BiTree &T,char str[],int n)
 {
 	T=NULL;
 	int i=0;
 	while(i<n)
 	{
 		create_bst(T,str[i]);
 		i++;
	}
 }
 
int main()
{
	BiTree T;
	char str[20];
	gets(str);
	int n=0;
	while(str[n]!='\0')
	{
		n++;
	}
	bst_tree(T,str,n);
	inorder(T);
}

判断是否是平衡二叉树

int depthmax(BiTree T)
{
	if(T==NULL)
		return 0;
	int left=depthmax(T->lchild);
	int right=depthmax(T->rchild);
	return left>right?left+1:right+1;
}

bool isbalance(BiTree T,int &i)
{
	if(T==NULL)
		return true; 
	if(abs(depthmax(T->lchild)-depthmax(T->rchild))>1)
		i=1;
		return false;
	return isbalance(T->lchild,i)&&isbalance(T->rchild,i);
}

求高度

void high(BiTree T,int i,int &h)
{
	if(T==NULL)
	{
		return ;
	 } 
	i++;
	if(i>=h)
		h=i;
	high(T->lchild,i,h);
	high(T->rchild,i,h);
}

层次遍历,求最小公共父节点

#include<stdio.h>
#include<malloc.h>
typedef struct tree{
	char data;
	struct tree *lchild,*rchild;
}Tree,*BiTree;
typedef struct queue{
	int front,rear;
	BiTree num[20];
}Queue;
void init(Queue &Q)
{
	Q.front=Q.rear=0;
}
void EnQueue(Queue &Q,BiTree T)
{
	Q.num[Q.rear++]=T;
}
BiTree DeQueue(Queue &Q)
{
	return Q.num[Q.front++];
}
int Empty(Queue Q)
{
	return Q.front==Q.rear;
}
void level(BiTree T)
{
	Queue Q;
	init(Q);
	BiTree temp;
	EnQueue(Q,T);
	while(!Empty(Q))
	{
		temp=DeQueue(Q);
		printf("%c",temp->data);
		if(temp->lchild!=NULL)
			EnQueue(Q,temp->lchild);
		if(temp->rchild!=NULL)
			EnQueue(Q,temp->rchild);
		
	}
}
void create(BiTree &T)
{
	char c;
	scanf("%c",&c);
	if(c=='0')
		T=NULL;
	else
	{
		T=(BiTree)malloc(sizeof(Tree));
		T->data=c;
		create(T->lchild);
		create(T->rchild);
	}
}
void inorder(BiTree T)
{
	if(T)
	{
		inorder(T->lchild);
		printf("%c",T->data);
		inorder(T->rchild);
	}
}
int  bst_insert(BiTree &T,char c)
{
	if(T==NULL)
	{
		T=(BiTree)malloc(sizeof(Tree));
		T->data=c;
		T->lchild=T->rchild=NULL;
	}
	else if(T->data==c)
		return 0;
	else if(c>T->data)
		return bst_insert(T->rchild,c);
	else
		return bst_insert(T->lchild,c);
}
void bst_create(BiTree &T,char str[],int n)
{
	T=NULL;
	int i=0;
	while(i<n)
	{
		bst_insert(T,str[i]);
		i++;
	}
 } 
 void out_str(BiTree T)
 {
 	if(T==NULL)
 		return ;
 	printf("%c",T->data);
 	if(T->lchild&&T->rchild)
 	{
 		printf("(");
 		out_str(T->lchild);
 		printf(")");
 	//	out_str(T->rchild);
 		
	 }
	else if(T->lchild)
	{
		printf("(");
		out_str(T->lchild);
	}
	else if(T->rchild)
	{
		out_str(T->rchild);
	}
	else if(T->lchild==NULL&&T->rchild==NULL)
	{
		printf(")");
	}

 }
 int main()
 {
 	BiTree T;
 	create(T);
 	out_str(T);
 }
// void create_str(BiTree T,char str[],int n)
// {
// 	if(T)
// 	{
// 		if(T->lchild==NULL&&T->rchild==NULL)
// 		{
// 			str[n++]='(';
// 			str[n++]=T->data;
// 			str[n++]=')';
//		}
//		else if(T->lchild!=NULL&&T->rchild==NULL)
//		{
//			str[n++]='(';
//			str[n++]=T->data;
//		}
//		else if(T->rchild!=NULL&&T->lchild==NULL)
//		{
//			str[n++]=T->data;
//			str[n++]=')';
//		}
//		else if()
//	 }
// }

// int main()
// {
// 	BiTree T;
// 	create(T);
// 	level(T);
// }
//int main()
//{
//	BiTree T;
//	char str[20];
//	gets(str);
//	int n=0;
//	while(str[n]!='\0')
//		n++;
//	bst_create(T,str,n);
//	inorder(T);
//}
//int main()
//{
//	BiTree T;
//	create(T);
//	inorder(T);
//}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值