在節點類BinNode中添加插入節點函數node
///***********插入孩子節點*******************************
/// 將數據e做爲當前節點的左孩子或右孩子插入,並返回該節點指針
BinNodePosi(T) insertAsLC(T const&e)
{
return lc=new BinNode(e,this);
}
BinNodePosi(T) insertAsRC(T const&e)
{
return rc=new BinNode(e,this);
}
};
下面是節點類的源代碼
#ifndef BINNODE #define BINNODE #include <iostream> //*************************************************************************************** ///代碼5.2 , BinNode狀態與性質的判斷 ///1、 判斷該節點是什麼! /// 是不是根節點、是不是左子節點、是不是右子節點、是不是葉節點 #define isRoot(x) (!((x).parent)) #define isLChild(x) (!isRoot(x)&&(&(x)==(x).parent->lc)) //不是根節點,同時必須是父節點的左孩子 #define isRChild(x) (!isRoot(x)&&(&(x)==(x).parent->rc)) //不是根節點,同時必須是父節點的右孩子 ///2、判斷該節點有什麼 //判斷是否有孩子 #define hasLChild(x) ((x).lc!=NULL) //判斷節點x是否有左孩子 #define hasRChild(x) ( (x).rc ) //判斷節點x 是否有右孩子 #define hasChild(x) ( hasLChild(x)||hasRChild(x)) //判斷節點x是否有孩子(左、右至少有一個) //判斷是否爲葉節點 #define isLeaf(x) ( !hasChild(x) ) //判斷節點x是不是葉子節點 //**************************************************************************************** #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; this->lc=lc,this->rc=rc;//此處添加this指針,以便將成員變量lc、rc與形參lc和rc區分 height=h; npl=l; color=c; } ///***********插入孩子節點******************************* /// 將數據e做爲當前節點的左孩子或右孩子插入,並返回該節點指針 BinNodePosi(T) insertAsLC(T const&e) { return lc=new BinNode(e,this); } BinNodePosi(T) insertAsRC(T const&e) { return rc=new BinNode(e,this); } }; #endif // BINNODE
下面是用於測試的main()函數:ios
#include <iostream> #include <binnode.h> #include <string> using namespace std; //一、測試根節點函數isRoot void testRoot(BinNode<string> &node) { if(isRoot(node)) cout<<node.data<<" is root!"<<endl; else cout<<node.data<<" is not a root!"<<endl; } //二、測試左孩子函數isLChild void testLChild(BinNode<string> &node) { if(isLChild(node)) cout<<node.data<<" is "<<node.parent->data<<"'s left child!"<<endl; else cout<<node.data<<" is not left child!"<<endl; } //三、測試右孩子函數isRChild void testRChild(BinNode<string> &node) { if(isRChild(node)) cout<<node.data<<" is "<<node.parent->data<< "'s right child!"<<endl; else cout<<node.data<<" is not right child!"<<endl; } //四、測試是否有左孩子函數 void testHasLChild(BinNode<string> &node) { if(hasLChild(node)) cout<<node.data<<" has a left child: "<<endl; else cout<<node.data<<" has not left child!"<<endl; } //四、測試是否有右孩子函數 void testHasRChild(BinNode<string> &node) { if(hasRChild(node)) cout<<node.data<<" has a right child: "<<endl; else cout<<node.data<<" has not right child!"<<endl; } //五、測試是不是葉節點函數isLeaf() void testLeaf(BinNode<string> &node) { if(isLeaf(node)) cout<<node.data<<" is "<<node.parent->data<< "'s leaf!"<<endl; else cout<<node.data<<" is not leaf"<<endl; } int main() { BinNode<string>* n[4];//數組指針 //二、將n2設定爲n1的左節點,測試isLChild()函數 //建立n1與n2之間的父子關係 n[1]=new BinNode<string>("node1");//建立節點1 n[2]=n[1]->insertAsLC("node2"); testLChild(*n[2]); //三、將n3設定爲n1的右節點,測試isRChild()函數 //建立n一、n3之間的父子關係 n[3]=n[1]->insertAsRC("node3"); testRChild(*n[3]); //四、測試n1是否有左孩子 testHasLChild(*n[1]); //五、測試n1是否有右孩子 testHasRChild(*n[1]); return 0; }
下面是運行結果:數組