二叉樹執行個體學習

來源:互聯網
上載者:User

標籤:names   代碼   style   div   父節點   gen   include   pen   temp   

第一次學習:

節點類代碼標頭檔:

#ifndef BINNODE_H#define BINNODE_H#include <iostream>//*****************************************//代碼5.2 , BinNode狀態與性質的判斷#define isRoot(x) (!((x).parent))#define isLChild(x) (!isRoot(x)&&(&(x)==(x).parent->lc))  //不是根節點,同時必須是父節點的左孩子//*******************************************#define BinNodePosi(T) BinNode<T>* //節點位置typedef enum{RB_RED,RB_BLACK} RBColor;//節點顏色template <typename T>class BinNode{public:    T data;//數值    int height;    int npl;//Null Path Length(左式堆,也可直接用height代替)    RBColor color;    BinNodePosi(T) parent;//父節點    BinNodePosi(T) lc;//左子節點    BinNodePosi(T) rc;//右子節點    //建構函式    BinNode():parent(NULL),lc(NULL),rc(NULL),height(0),npl(1),color(RB_RED){}    BinNode(T e,BinNodePosi(T) p=NULL,BinNodePosi(T) lc=NULL,BinNodePosi(T) rc=NULL,            int h=0,int l=1,RBColor c=RB_RED)    {        data=e;        parent=p;        lc=lc,rc=rc;        height=h;        npl=l;        color=c;    }};#endif // BINNODE_H

節點類測試程式:

#include <iostream>#include <binnode.h>#include <string>using namespace std;void judgeRoot(BinNode<string> &node){    if(isRoot(node))        cout<<node.data<<" is root!"<<endl;    else        cout<<node.data<<" is not a root!"<<endl;}void judgeLChild(BinNode<string> &node){    if(isLChild(node))        cout<<node.data<<" is left child!"<<endl;    else        cout<<node.data<<" is not left child!"<<endl;}int main(){    BinNode<string> n1("node1");    BinNode<string> n0("node0");    BinNode<string> n2("node2");    n1.lc=&n2;    n2.parent=&n1;//    judgeNode(n0);//測試根節點函數”isRoot()“    judgeLChild(n2);//測試左孩子函數“isLChild(node)” 

return 0; }

 

二叉樹執行個體學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.