2叉树排序

// erchashu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

typedef struct btree
{
	int data;
	struct btree *left;
	struct btree *right; 
}BTR,*PBTR;

typedef struct BTRSt
{
	PBTR ptree;
	struct BTRSt *link;
}Stack,*PStack;


PBTR Bitree=NULL;
/*
函数功能:实现非递归建立二叉树 
函数原型:void creat_btree(int *a,int size) 
函数参数:int *a :保存二叉树节点的数组首地址
int size:节点数目
函数返回值:void 
优点:建立的二叉树按中序遍历后:是从小到大有序的可以是一种排序算法 
*/
void creat_btree(int *a,int size)
{
	int i;
	PBTR pre,pre2;
	for(i=0;i<size;i++)
	{
		if(Bitree==NULL)
		{
			Bitree=(PBTR)malloc(sizeof(BTR));
			if(Bitree==NULL)
			{
				printf("Malloc fail/n");
				break;
			}
			else
			{
				Bitree->data=a[i];
				Bitree->left=NULL;
				Bitree->right=NULL;
				continue;
			}
		}
		else
		{
			pre = Bitree; 
		}

		while(1)
		{
			if(a[i]>pre->data)
			{
				if(pre->right==NULL)
				{
					pre2=(PBTR)malloc(sizeof(BTR));
					if(pre2==NULL)
					{
						printf("Malloc fail/n");
						break;
					}
					else
					{
						pre2->data=a[i];
						pre2->left=NULL;
						pre2->right=NULL;
						pre->right=pre2;
						break;
					}
				}
				else
				{
					pre=pre->right;  
				}
			}
			else
			{
				if(pre->left==NULL)
				{
					pre2=(PBTR)malloc(sizeof(BTR));
					if(pre2==NULL)
					{
						printf("Malloc fail/n");
						break;
					}
					else
					{
						pre2->data=a[i];
						pre2->left=NULL;
						pre2->right=NULL;
						pre->left=pre2;
						break;
					}
				}
				else
				{
					pre=pre->left;  
				}

			}    
		}
	}   
}
/*
函数功能:实现递归前序遍历二叉树 
函数原型:void preorder(PBTR head) 
函数参数:PBTR :保存二叉树根节点 
函数返回值:void 

*/
void preorder(PBTR p)
{
	if(p!=NULL)
	{
		printf("%4d",p->data);
		preorder(p->left);
		preorder(p->right);      
	}    
}


/*
函数功能:实现递归中序遍历二叉树 
函数原型:void midorder(PBTR head) 
函数参数:PBTR :保存二叉树根节点 
函数返回值:void 
*/
void midorder(PBTR p)
{
	if(p!=NULL)
	{
		midorder(p->left);
		printf("%4d",p->data);
		midorder(p->right);      
	}    
}

/*
函数功能:实现递归中序遍历二叉树 
函数原型:void postorder(PBTR head) 
函数参数:PBTR :保存二叉树根节点 
函数返回值:void 
*/
void postorder(PBTR p)
{
	if(p!=NULL)
	{
		postorder(p->left);
		postorder(p->right); 
		printf("%4d",p->data);     
	}    
}

/*
函数功能:递归求二叉树的深度 
函数原型:int btreedepth(PBTR head) 
函数参数:PBTR :保存二叉树根节点 
函数返回值:int :二叉树的深度 
*/

int btreedepth(PBTR head)
{
	int h,hl,hr;
	PBTR p;
	p=head;
	if(p==NULL)
	{
		h=0;
	}
	else
	{
		hl=btreedepth(p->left);
		hr=btreedepth(p->right);
		if(hl>hr)
		{
			h=hl+1;
		}
		else
		{
			h=hr+1;
		}
	}
	return h; 
}
/*主函数 main()作测试用*/
#define N 12
int main()
{   
	int h;
	int a[N]={1,45,89,13,24,56,39,78,79,69,20,44};
	printf("非递归建立建立二叉树....\n"); 
	creat_btree(a,N);
	//printf("前序遍历:      \n");
	//preorder(Bitree);
	printf("非递归建立建立二叉树后中序遍历就是按照由小到大排序\n"); 
	printf("中序遍历:      \n");
	midorder(Bitree);
	//printf("后续遍历:      \n");
	//postorder(Bitree);
	printf("二叉树的深度是:\n");
	printf("%4d/n",btreedepth(Bitree)); 

	getchar();
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值