国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
球平衡二叉樹(shù)高函數(shù)int height(Node* root);

5.已知平衡二叉樹(shù)節(jié)點(diǎn)的定義為

Struct Node{double value_; Node* lc_; Node* rc_;int bf_;}

其中bf_為節(jié)點(diǎn)的平衡因子(左子高減去右子高)。完成下面算法,它求平衡二叉樹(shù)的高度。

int height(Node* root);

要求其復(fù)雜度為O(㏒n),其中n為二叉樹(shù)節(jié)點(diǎn)的個(gè)數(shù)。

/*****************************************************************************/

/*===================================================================*/

/*****************************************************************************/

#include<iostream>

using namespace std;

 

#define SIZE 6

struct Node{

       double value_;

       Node* lc_;

       Node* rc_;

       int bf_;

};

 

int height(Node* root)                        //題目中要求設(shè)計(jì)的函數(shù);

{

       int floor=0;

       while(root->lc_!=NULL||root->rc_!=NULL){

              floor++;

              if(root->bf_>=0){

                     root=root->lc_;

              }

              else{

                     root=root->rc_;

              }

       }

       return floor+1;

}

 

 

int main()

{

       int hight=0;

       Node tree[SIZE];

 

       for(int i=0;i<SIZE;i++){                   //構(gòu)造二叉樹(shù);

              tree[SIZE].value_=i;

       }

       tree[0].bf_=1;   tree[1].bf_=0;

       tree[2].bf_=1;   tree[3].bf_=0;

       tree[4].bf_=0;   tree[5].bf_=0;

       tree[0].lc_=&(tree[1]);

       tree[0].rc_=&(tree[2]);

       tree[1].lc_=&(tree[3]);

       tree[1].rc_=&(tree[4]);

       tree[2].lc_=&(tree[5]);

       tree[2].rc_=NULL;

       tree[3].lc_=NULL;

       tree[3].rc_=NULL;

       tree[4].lc_=NULL;

       tree[4].rc_=NULL;

       tree[5].lc_=NULL;

       tree[5].rc_=NULL;

 

    hight=height(tree);                         //題中函數(shù)的運(yùn)用; 

       cout<<"默認(rèn)二叉樹(shù)的高度為:"<<hight<<endl<<endl;

    system("pause");

       return 0;

}

 

 程序中構(gòu)造的二叉樹(shù)如下:

/*****************************************************************************/

/*===================================================================*/

/*****************************************************************************/

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
AVL樹(shù)及其實(shí)現(xiàn)
【查找結(jié)構(gòu)3】平衡二叉查找樹(shù) [AVL]
《算法導(dǎo)論》讀書(shū)筆記之第10章 基本數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)
LeetCode實(shí)戰(zhàn):二叉樹(shù)的最大深度
二叉樹(shù)的幾種存儲(chǔ)方式(一個(gè)數(shù)組,指針形式,兩個(gè)數(shù)組)
動(dòng)畫(huà):二叉樹(shù)有幾種存儲(chǔ)方式?(上)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服