zoukankan      html  css  js  c++  java
  • 决策树学习过程中的额外收获---三叉树建立

    最近在写一个决策树的程序,苦于每个节点的孩子数目不确定建树困难,通过查询资料发现可以通过容器来写很方便。

    首先结构体代码如下:

    typedef struct Node{
        string Data;                    //数据存储项,用于保存相应的数据
        vector<Node*> Children;         //孩子节点指针存储,用于存储指向孩子节点的指针
    }Node;

    然后是建立三叉树的建树代码:

    void BuildTree(Node *root)
    {
        string str;
        cin>>str;
        cout<<str<<endl;
        if(str=="END")
        {   cout<<"leave"<<endl;
            Node *t=new Node;
            t->Data="END";
            root->Children.push_back(t);
            return;
        }
        else
        {   cout<<"brand"<<endl;
            Node *t=new Node;
            t->Data=str;
            root->Children.push_back(t);
            for(int i=0;i<3;i++)
            BuildTree(t);
        }
    }

    接着是遍历三叉树的遍历函数:

    void Show(Node *root)
    {
        if(root->Data=="END")
        {
            cout<<"Leave node"<<endl;
            return;
        }
        else
        {
            cout<<root->Data<<endl;
            for(vector<Node*>::iterator it=root->Children.begin();it!=root->Children.end();it++)
            {
                Node* temp=*it;
                Show(temp);
            }
        }
    }

    最后是主调用函数:

    int main()
    {
       Node *root;
       root=new Node;
       BuildTree(root);
       Show(root);
       return 0;
    }

    输出结果:

    态度决定高度,细节决定成败,
  • 相关阅读:
    高精度“+”算法
    漏洞扫描
    端口扫描
    使用sqlmap
    Kali实现靶机远程控制
    Docker下配置KeepAlive支持nginx高可用
    web攻防环境--一句话木马
    Docker容器技术--自定义网桥后的默认网卡名称
    小白大数据学习指南
    Nginx简单操作
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/3690872.html
Copyright © 2011-2022 走看看