zoukankan      html  css  js  c++  java
  • 二叉树实例学习

    第一次学习:

    节点类代码头文件:

    #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; }
  • 相关阅读:
    深入理解 IE haslayout
    electron的应用
    自动化批量录入Web系统
    Flask + Vue的一个示例
    如何从git仓库里下载单个文件夹
    Django项目设置首页
    简单更改Django Admin登录页面
    Flask web项目使用.flaskenv文件
    Flask 里url_for的使用
    使用Flask-migrate迁移数据库
  • 原文地址:https://www.cnblogs.com/phoenixdsg/p/9733449.html
Copyright © 2011-2022 走看看