树——递归

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

/*
struct BiNode
{
	int data;
	struct BiNode  *lchild, *rchild;
};
//重新重命名类型
typedef struct BiNode BiNode;
//也是重命名类型 是一个指针的数据类型
typedef struct BiNode *BiTree;
*/


//二叉链表
typedef struct BiNode
{
	int data;
	struct BiNode  *lchild, *rchild;
}BiNode, *BiTree;


//中序遍历
void inOrder(BiNode *root)
{
	if (root != NULL)
	{
		inOrder(root->lchild);
		printf("%d ", root->data);
		inOrder(root->rchild);
	}
	return ;
}
void preOrder(BiNode *root)
{
	if (root != NULL)
	{
		printf("%d ", root->data);
		preOrder(root->lchild);	
		preOrder(root->rchild);
	}
}

void postOrder(BiNode *root)
{
	if (root != NULL)
	{
		postOrder(root->lchild);	
		postOrder(root->rchild);
		printf("%d ", root->data);
	}
}
//引申1
//遍历的本质。。。。。。。
==//全局变量==
int g_num;
void countLeafNum(BiNode *root)
{
	if (root != NULL)
	{
		
		countLeafNum(root->lchild); //递归遍历左子树,求叶子节点数

		if (root->lchild == NULL && root->rchild==NULL)
		{
			g_num ++;
		}
	
		countLeafNum(root->rchild);//递归遍历右子树,求叶子节点数
	}
}
==//不用到全局变量==
void countLeafNum2(BiNode *root, int *num)
{
	if (root != NULL)
	{

		countLeafNum2(root->lchild, num); //递归遍历左子树,求叶子节点数

		if (root->lchild == NULL && root->rchild==NULL)
		{
			(*num) ++;
		}

		countLeafNum2(root->rchild, num);//递归遍历右子树,求叶子节点数
	}
}
//求树的深度
int GetDepth(BiNode *root)
{
	int depthleft = 0, depthright=0; 
	int depthVal = 0;
	int tmp = 0;

	if (root == NULL)
	{
		depthVal = 0;
		return depthVal;
	}
	//求左子树高度
	depthleft = GetDepth(root->lchild);
	//求右子树高度
	depthright = GetDepth(root->rchild);

	 tmp = ((depthleft>depthright)? depthleft : depthright);
	depthVal = 1 + tmp; 

	return depthVal;
}
//求树的拷贝
BiNode *copyTree(BiNode *T)
{
	BiNode *newLptr = NULL, *newRptr = NULL;
	BiNode *newNode = NULL;
	if (T->lchild != NULL)
	{
		newLptr =copyTree(T->lchild);
	}
	else
	{
		newLptr = NULL;
	}

	if (T->rchild != NULL)
	{
		newRptr = copyTree(T->rchild);
	}
	else
	{
		newRptr = NULL; 
	}
	newNode = (BiNode *)malloc(sizeof(BiNode));
	if (newNode == NULL)
	{
		return NULL;
	}
	newNode->lchild = newLptr;
	newNode->rchild = newRptr;
	newNode->data = T->data;

	return newNode;
}
void main()
{
	BiNode t1, t2, t3, t4, t5;
	memset(&t1, 0, sizeof(BiNode));
	memset(&t2, 0, sizeof(BiNode));
	memset(&t3, 0, sizeof(BiNode));
	memset(&t4, 0, sizeof(BiNode));
	memset(&t5, 0, sizeof(BiNode));
	t1.data = 1;
	t2.data = 2;
	t3.data = 3;
	t4.data = 4;
	t5.data = 5;
	t1.lchild = &t2;
	t1.rchild = &t3;
	t2.lchild = &t4;
	t3.lchild = &t5;
	//t3的左孩子为t5
	inOrder(&t1);
	 printf("\n");
	 preOrder(&t1);
	 printf("\n");
	 postOrder(&t1);
	 g_num = 0;
	 countLeafNum(&t1);
	 printf("g_num: %d \n", g_num);

	 {
		int num = 0;
		countLeafNum2(&t1, &num);
		printf("num: %d \n", num);
	 }

	printf("树的高度为:%d \n", GetDepth(&t1));

	{
		BiNode *root = NULL;
		root =	copyTree(&t1);
		printf("新copy树\n中序遍历:");
		inOrder(root);
	}


	
	system("pause");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值