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

    输出结果:

    态度决定高度,细节决定成败,
  • 相关阅读:
    vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: guest_enable
    Vsftpd服务传输文件(转)
    搭建FTP服务
    sed命令
    创建服务类脚本
    jvm 方法区
    B+与B-树
    适配器模式
    java 垃圾回收总结(可达性分析 引用分类
    HBase常见问题答疑解惑【持续更新中】
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/3690872.html
Copyright © 2011-2022 走看看