求二叉树的高度(递归与非递归算法)

一、 非递归算法

1、算法思想

采用层次遍历的算法,设置遍历level记录当前结点所在的层数,设置变量last指向当前层的最后结点,每次层次遍历出队时与last指针比较,若两者相等,则层数加1,并让last指向下一层的最后一个节点,直到队列为空遍历完成。level的值即为二叉树的高度

2、实现流程

在这里插入图片描述

3、代码实现

int Height(BTree root)
{
	Queue myQueue;
	InitQueue(&myQueue);

	int last = 0, level = 0;
	if (root == NULL)
	{
		return 0;
	}
	BTree temp = root;
	PushQueue(&myQueue, root);//根节点入队
	while (!(QueueIsEmpty(&myQueue)))//队列不空时
	{
		temp = GetQueueFrontValue(&myQueue);//取队头元素
		//printf("%c ", temp->data);
		PopQueue(&myQueue);//出队
		if (temp->lchild != NULL)
		{
			PushQueue(&myQueue, temp->lchild);
		}
		if (temp->rchild != NULL)
		{
			PushQueue(&myQueue, temp->rchild);
		}
		if (last == myQueue.front)//处理该层最右结点
		{
			level++;//层数+1
			last = myQueue.tail;//指向下一层
		}
			
	}
	return level;
}

二、递归算法

int TreeHeight(BTree root)
{
	if (root == NULL)
		return 0;
	else
	{
		int LchildHeight = TreeHeight(root->lchild);
		int RchildHeight = TreeHeight(root->rchild);
		return LchildHeight > RchildHeight ? (LchildHeight + 1) : (RchildHeight + 1);
	}
}

三、完整代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <Windows.h>

typedef char DataType;

#define MAXSIZE 20

//二叉树结构体
typedef struct tree
{
	DataType data;
	struct tree* lchild, * rchild;
}Tree,*BTree;
//队列结构体
typedef struct Queue
{
	BTree data[MAXSIZE];
	int front,tail;
}Queue;
//队列初始化
void InitQueue(Queue* myQueue)
{
	for (int i = 0; i < MAXSIZE; i++)
	{
		myQueue->data[i] = 0;
	}
	myQueue->front = myQueue->tail = -1;
}
//入队
void PushQueue(Queue* myQueue, BTree data)
{
	if (myQueue->tail == MAXSIZE)
	{
		printf("队列已满不能入队!\n");
		exit(-1);
	}
	myQueue->tail = (myQueue->tail + 1) % MAXSIZE;
	myQueue->data[myQueue->tail] = data;
	
}
//判断队列是否为空
bool QueueIsEmpty(Queue* myQueue)
{
	if (myQueue->front == myQueue->tail)//队列为空
		return true;
	else
		return false;
}
//出队
void PopQueue(Queue* myQueue)
{
	if (QueueIsEmpty(myQueue))
	{
		printf("队列为空不能出队!\n");
		exit(-1);
	}
	myQueue->front = (myQueue->front + 1) % MAXSIZE;
	myQueue->data[myQueue->front] = NULL;
	
}
//取队头元素
BTree GetQueueFrontValue(Queue* myQueue)
{
	if (QueueIsEmpty(myQueue))
	{
		printf("队列为空不能取队头元素!\n");
		exit(-1);
	}
	return myQueue->data[myQueue->front + 1];
}
//创建二叉树
int flag = 0;
BTree CreateBTree()
{
	BTree root = NULL;
	char temp=0;
	DataType data;
	if (flag == 0)
	{
		printf("请输入一个根节点:");
		flag = 1;
	}
	scanf_s("%c", &data, 1);
	temp = getchar();
	if (data == '#')
		return NULL;
	else
	{
		(BTree)root = (BTree)malloc(sizeof(Tree));
		if (root == NULL)
		{
			printf("内存分配失败!\n");
			exit(-1);
		}
		root->data = data;
		printf("请输入%c的左孩子:", root->data);
		root->lchild = CreateBTree();
		printf("请输入%c的右孩子:", root->data);
		root->rchild = CreateBTree();
	}
	return root;
}
//求树高递归算法
int TreeHeight(BTree root)
{
	if (root == NULL)
		return 0;
	else
	{
		int LchildHeight = TreeHeight(root->lchild);
		int RchildHeight = TreeHeight(root->rchild);
		return LchildHeight > RchildHeight ? (LchildHeight + 1) : (RchildHeight + 1);
	}
}
//求树高非递归算法
int Height(BTree root)
{
	Queue myQueue;
	InitQueue(&myQueue);

	int last = 0, level = 0;
	if (root == NULL)
	{
		return 0;
	}
	BTree temp = root;
	PushQueue(&myQueue, root);//根节点入队
	while (!(QueueIsEmpty(&myQueue)))//队列不空时
	{
		temp = GetQueueFrontValue(&myQueue);//取队头元素
		//printf("%c ", temp->data);
		PopQueue(&myQueue);//出队
		if (temp->lchild != NULL)
		{
			PushQueue(&myQueue, temp->lchild);
		}
		if (temp->rchild != NULL)
		{
			PushQueue(&myQueue, temp->rchild);
		}
		if (last == myQueue.front)//处理该层最右结点
		{
			level++;//层数+1
			last = myQueue.tail;//指向下一层
		}
			
	}
	return level;
}

void test()
{
	BTree root;
	root = CreateBTree();
	int height = TreeHeight(root);
	printf("递归遍历求树高为:%d\n", height);
	int height1 = Height(root);
	printf("非递归求树高为:%d\n", height1);
}

int main(void)
{
	test();

	system("pause");
	return EXIT_SUCCESS;
}

四、运行截图

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值