一、 非递归算法
1、算法思想
采用层次遍历的算法,设置遍历level记录当前结点所在的层数,设置变量last指向当前层的最后结点,每次层次遍历出队时与last指针比较,若两者相等,则层数加1,并让last指向下一层的最后一个节点,直到队列为空遍历完成。level的值即为二叉树的高度
2、实现流程
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e7c2d6aed28a54f17517f0108f54f647.png)
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);
PopQueue(&myQueue);
if (temp->lchild != NULL)
{
PushQueue(&myQueue, temp->lchild);
}
if (temp->rchild != NULL)
{
PushQueue(&myQueue, temp->rchild);
}
if (last == myQueue.front)
{
level++;
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);
PopQueue(&myQueue);
if (temp->lchild != NULL)
{
PushQueue(&myQueue, temp->lchild);
}
if (temp->rchild != NULL)
{
PushQueue(&myQueue, temp->rchild);
}
if (last == myQueue.front)
{
level++;
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;
}
四、运行截图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0285480ab015b5f992a62f731c765eb4.png)