博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【数据结构周周练】013 利用栈和非递归算法求二叉树的高
阅读量:4075 次
发布时间:2019-05-25

本文共 2657 字,大约阅读时间需要 8 分钟。

一、前言

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

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

二、题目

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

 

 三、代码

#define MAXQUEUESIZE 10#include
#include
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 treeint 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 recursionvoid 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 treeint 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
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;}

四、实现效果

你可能感兴趣的文章
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>