【数据结构周周练】013 利用栈和非递归算法求二叉树的高

47 篇文章 162 订阅
45 篇文章 26 订阅

一、前言

二叉树的高是树比较重要的一个概念,指的是树中结点的最大层数本次算法通过非递归算法来求得树的高度,借用栈来实现树中结点的存储。

学英语真的很重要,所以文中的注释还有输出以后会尽量用英语写,文中出现的英语语法或者单词使用错误,还希望各位英语大神能不吝赐教。

二、题目

将下图用二叉树存入,并求树的高度。其中圆角矩形内为结点数据,旁边数字为结点编号,编号为0的结点为根节点,箭头指向的结点为箭尾的孩子结点。

 

 三、代码

#define MAXQUEUESIZE 10

#include<iostream>
#include<malloc.h>

using namespace std;

typedef struct BiTNode {
	int data;
	int number;
	struct BiTNode *lChild, *rChild, *parent;
}BiTNode, *BiTree;

typedef BiTree SElemType;

typedef struct LNode{
	SElemType data;
	int high;
	struct LNode *next;
}LNode,*LinkStack;

int number = 0;
int yon = 0;
int yesOrNo[] = { 1,0,1,0,0,1,1,1,0,0,1,0,0,1,0,0 };
int numData[] = { 1,2,4,3,5,7,8,6 };
BiTree treeParent = NULL;

int InitStack(LinkStack &S) {
	S = (LinkStack)malloc(sizeof(LNode));
	if (!S) {
		cout << "空间分配失败(Allocate space failure)" << endl;
		exit(OVERFLOW);
	}
	S->high = 0;
	S->next = NULL;
	return 1;
}

int Push(LinkStack &S, SElemType e) {
	LinkStack p = (LinkStack)malloc(sizeof(LNode));
	if (!p)
	{
		cout << "结点分配失败(Allocate node failure)" << endl;
		exit(OVERFLOW);
	}
	S->data = e;
	p->next = S;
	p->high = S->high;
	S = p;
	S->high += 1;
	return 1;
}

int Pop(LinkStack &S, SElemType &e) {
	LinkStack p = S->next;
	if (!p)
	{
		cout << "栈空(The stack is null)" << endl;
		exit(OVERFLOW);
	}
	e = p->data;
	S->next = p->next;
	free(p);
	S->high -= 1;
	return 1;
}

// Operation of the tree
int OperationBiTree(BiTree &T) {
	T = (BiTree)malloc(sizeof(BiTNode));
	if (!T)
	{
		cout << "空间分配失败" << endl;
		exit(OVERFLOW);
	}
	T->number = number;
	number++;
	T->data = numData[T->number];
	T->lChild = NULL;
	T->rChild = NULL;
	T->parent = treeParent;
	return 1;
}

//establish the tree, utilize recursion
void EstablishBiTree(BiTree &T) {
	OperationBiTree(T);
	treeParent = T;
	if (yesOrNo[yon++])
		EstablishBiTree(T->lChild);
	treeParent = T;
	if (yesOrNo[yon++])
		EstablishBiTree(T->rChild);
}


//Seek high of the tree
int BiTreeHigh(BiTree T) {
	BiTree p = T;
	LinkStack S;
	int high = 0;
	InitStack(S);
	while (p||S->next)
	{
		if (p) {
			Push(S, p);
			p = p->lChild;
			if (high<S->high)
				high = S->high;
		}
		else
		{
			Pop(S, p);
			p = p->rChild;
		}
	}
	return ++high;
}

void VisitBiTree(BiTree T) {
	cout << "The number of present node is :" << T->number << "; ";
	cout << "data is :" << T->data << "; ";
	if (!T->parent)
		cout << " this node is the root of the tree.\n";
	if (!T->lChild && !T->rChild)
		cout << " this node is the leaf of the tree.\n";
	cout << endl;
	
}

//Visit tree use the preorder technique. 
void PreOrderBiTree(BiTree T) {
	if (T)
	{
		VisitBiTree(T);
		PreOrderBiTree(T->lChild);
		PreOrderBiTree(T->rChild);
	}
}

void main() {
	BiTree T;
	EstablishBiTree(T);
	PreOrderBiTree(T);
	cout << "The high of tree which we establish is " << BiTreeHigh(T) << endl;
}

四、实现效果

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值