C语言数据结构代码——二叉搜索树

Head.H

#define ARRSIZE 6

typedef struct _tnode
{
	int ele;
	struct _tnode *ltree;
	struct _tnode *rtree;
}TNode, *tnode;

void InsertTree(tnode T, int num);
tnode InitSearchTree(int *a, int len);

tnode FindAimVal(tnode T, int aimval);
tnode FindMinNode(tnode T);
tnode FindMaxNode(tnode T);

void PrintAimNode(tnode T, int level);
void PrintTreeStructure(tnode T);
int GetTreeDeepth(tnode T);

 

 

Function.C

void InsertTree(tnode T, int num)
{
	if (T->ltree != NULL && num <= T->ele)
	{
		InsertTree(T->ltree, num);
		return;
	}
	else if (T->rtree != NULL && num > T->ele)
	{
		InsertTree(T->rtree, num);
		return;
	}

	tnode pInsert = (tnode)malloc(sizeof(TNode));
	pInsert->ele = num;
	pInsert->ltree = NULL;
	pInsert->rtree = NULL;

	if (T->ltree == NULL && num <= T->ele)
	{
		T->ltree = pInsert;
	}
	else if (T->rtree == NULL && num > T->ele)
	{
		T->rtree = pInsert;
	}

	return;

}

tnode InitSearchTree(int *a, int len)
{
	tnode ST = (tnode)malloc(sizeof(TNode));
	ST->ele = a[0];
	ST->ltree = NULL;
	ST->rtree = NULL;

	for (int i = 1; i < len; i++)
	{
		InsertTree(ST, a[i]);
	}

	return ST;
}


tnode FindAimVal(tnode T, int aimval)
{
	if (T == NULL)
	{
		return NULL;
	}

	if (aimval < T->ele)   //如果小于根节点值则去左孩子查找
	{
		return FindAimVal(T->ltree, aimval);
	}
	else if (aimval > T->ele)//如果大于根节点值则去左孩子查找
	{
		return FindAimVal(T->rtree, aimval);
	}
	else
	{
		return T;
	}
}


tnode FindMinNode(tnode T)
{
	if (T == NULL)
	{
		return NULL;
	}

	if (T->ltree == NULL)
	{
		return T;
	}
	else
	{
		return FindMinNode(T->ltree);
	}
}


tnode FindMaxNode(tnode T)
{
	if (T == NULL)
	{
		return NULL;
	}

	if (T->rtree == NULL)
	{
		return T;
	}
	else
	{
		return FindMaxNode(T->rtree);
	}
}

//显示の措施

//打印树某一层全部节点
void PrintAimNode(tnode T, int level)//打印二叉树某一层的结点所有节点的值
{
	if (T == NULL)
	{
		return;
	}
	else
	{
		if (level == 1)
		{
			printf("%d ", T->ele);
		}
		else {
			PrintAimNode(T->ltree, level - 1);
			PrintAimNode(T->rtree, level - 1);
		}
	}
}


//打印二叉树整体结构
void PrintTreeStructure(tnode T)
{
	int i;
	int length = GetTreeDeepth(T);

	for (i = 1; i <= length; i++)
	{
		PrintAimNode(T, i);
		printf("\n");
	}
}


//测深度
int GetTreeDeepth(tnode T)
{
	int h1, h2;

	if (T == NULL)
	{
		return 0;
	}

	h1 = GetTreeDeepth(T->ltree);
	h2 = GetTreeDeepth(T->rtree);

	if (h1 > h2)
	{
		return(h1 + 1);
	}
	else
	{
		return(h2 + 1);
	}
}

 

 

Main.C

void main(void)
{
	int array[ARRSIZE] = { 2,2,6,4,11,7 }; //http://images2015.cnblogs.com/blog/785692/201607/785692-20160729165441888-1605268104.png

	tnode ST = InitSearchTree(array, ARRSIZE);

	PrintTreeStructure(ST);

	tnode minnode = FindMinNode(ST);
	printf("最小值:%d \n", minnode->ele);

	tnode maxnode = FindMaxNode(ST);
	printf("最大值:%d \n", maxnode->ele);


	tnode aimnode1 = FindAimVal(ST, 12);   //
	if (aimnode1)
	{
		printf("find the val: %d \n", aimnode1->ele);
	}
	else
	{
		printf("cannot find the val \n");
	}


	tnode aimnode2 = FindAimVal(ST, 11);    //
	if (aimnode2)
	{
		printf("find the val: %d \n", aimnode2->ele);
	}
	else
	{
		printf("cannot find the val: \n");
	}

	printf("\n");
	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值