二叉树实例学习(二)

获取二叉树高度的代码链接:https://blog.csdn.net/tcherry/article/details/25822417

测试是否有左孩子 hasLChild(x)、右孩子函数 hasRChild(x),以及是否右孩子函数 hasChild(x) 。

节点头文件:

#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 isRChild(x) (!isRoot(x)&&(&(x)==(x).parent->rc))  //不是根节点,同时必须是父节点的右孩子
///二、判断该节点有什么
//判断是否有孩子
#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;
    }
};

#endif // BINNODE_H

下面是测试实例代码:

#include <iostream>
#include <binnode.h>
#include <string>
using namespace std;
//1、测试根节点函数isRoot
void testRoot(BinNode<string> &node)
{
    if(isRoot(node))
        cout<<node.data<<" is root!"<<endl;
    else
        cout<<node.data<<" is not a root!"<<endl;
}
//2、测试左孩子函数isLChild
void testLChild(BinNode<string> &node)
{
    if(isLChild(node))
        cout<<node.data<<" is left child!"<<endl;
    else
        cout<<node.data<<" is not left child!"<<endl;
}
//3、测试右孩子函数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;
}
//4、测试是否有左孩子函数

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;
}

//5、测试是否是叶节点函数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> n1("node1");
    BinNode<string> n0("node0");
    BinNode<string> n2("node2");
    BinNode<string> n3("node3");
    //2、将n2设定为n1的左节点,测试isLChild()函数
    n1.lc=&n2;
    n2.parent=&n1;
    testLChild(n2);
    //3、将n3设定为n1的右节点,测试isRChild()函数
    n1.rc=&n3;
    n3.parent=&n1;//创建n1、n3之间的父子关系
    testRChild(n3);
    //4、测试n3是否有左孩子
    testHasLChild(n2);
    //5、测试叶节点函数isLeaf
    testLeaf(n3);
    return 0;
}

 下面是测试结果:

原文地址:https://www.cnblogs.com/phoenixdsg/p/9737361.html