二叉树实例学习(五)——更新节点及祖辈节点高度

编程入门 行业动态 更新时间:2024-10-08 00:28:14

二叉树实例学习(五)——更新<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点及祖辈节点高度"/>

二叉树实例学习(五)——更新节点及祖辈节点高度

在第(四)节获取节点高度函数getHeight()函数的基础上,增添并测试更新节点高度函数和更新祖辈节点高度函数:updateHight()、updateAboveHei()。在上节中,每插入一个新节点,根结点以及新节点的其它祖辈节点的高度不增加,如今这已成为过去。在插入节点函数insertAsLC()、insertAsRC()函数中,分别添加updateAboveHei(),则每次插入新的节点,都会更新祖辈的节点高度。

节点类的代码,如第四节,不再增加。

树的定义代码如下:

#ifndef BINTREE
#define BINTREE
#include<binnode.h>template<typename T>
class BinTree
{
public:int _size;BinNodePosi(T) _root;//根结点指针int getHight(BinNodePosi(T) x){int l_hight,r_hight;if(x==NULL)return -1;else if(!hasChild(*x)){return 0;}else{l_hight = getHight(x->lc)+1;r_hight = getHight(x->rc)+1;}return l_hight>r_hight?l_hight:r_hight;}int max(int a,int b){if(a>b)return a;elsereturn b;}///更新节点高度函数updateHeight()/// 以及更新祖辈节点高度函数updateAboveHeight()virtual int updateHeight(BinNodePosi(T) x)//更新节点x的高度
    {return x->height=1+max(getHight(x->lc),getHight(x->rc));}void updateAboveHeight(BinNode<T> *x)//跟新节点x及其祖先的高度
    {while(x){updateHeight(x);//先更新当前节点高度x=x->parent;//然后更新祖辈节点高度
        }}public:BinTree():_size(0),_root(NULL){}int size()const{return _size;}//获取树的规模,即共有多少个节点bool empty(){return !_root;}//判断是否为空树BinNodePosi(T) root()const{return _root;}//获取根结点指针BinNodePosi(T) insertAsRoot(T const&e){_size=1;return _root=new BinNode<T>(e);}BinNodePosi(T) insertAsLC(BinNodePosi(T) x,T const&e){_size++;x->insertAsLC(e);
//        x->height =getHight(x);//将该语句替换为updateAboveHeight(x);
        updateAboveHeight(x);return x->lc;}BinNodePosi(T) insertAsRC(BinNodePosi(T) x,T const&e){_size++;x->insertAsRC(e);updateAboveHeight(x);return x->rc;}
};#endif // BINTREE

在测试程序中的树形结构如图所示

测试代码与第四节一样:

int main()
{BinNode<string>* n[6];//数组指针
BinTree<string> bt;n[0]= bt.insertAsRoot("n0");n[1]= bt.insertAsLC(n[0],"n1");n[2]= bt.insertAsRC(n[0],"n2");n[3]= bt.insertAsLC(n[1],"n3");n[4]=bt.insertAsLC(n[2],"n4");n[5]=bt.insertAsLC(n[4],"n5");//测试根结点的高度cout<<bt.getHight(n[2])<<endl;
//    bt.updateHeight(bt._root);cout<<bt._root->height<<endl;return 0;
}

我们可以看到,随着新节点的插入,根结点的高度随之而增加。

转载于:.html

更多推荐

二叉树实例学习(五)——更新节点及祖辈节点高度

本文发布于:2024-02-06 06:08:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   祖辈   实例   高度   二叉树

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!