多叉树 (递归遍历,尾插入)

学习<<数据结构与算法分析>>,给以后留个记录

tree.h

#ifndef TREE_H
#define TREE_H

#include <string>
#include <iostream>

using namespace std;

struct TreeNode
{
    string element;
    TreeNode *firstChild = nullptr;
    TreeNode *nextSibling = nullptr;
    TreeNode(string str) : element(str){}
    TreeNode(){}
};

class tree
{
public:
    tree();
    /// 手动添加一些节点,用于实验
    bool addNode();
    /// 尾插法
    void insertNode(string father, string self);
    /// 返回根节点
    TreeNode* getRootNode() const;
    /// 深度优先遍历
    void travel(TreeNode *root);

private:
    TreeNode* find(TreeNode *root, string str);

private:
    TreeNode *root;
};

#endif // TREE_H

tree.cpp

#include "tree.h"

tree::tree()
{
    root = new TreeNode;
    addNode();
}

///
/// \brief tree::addNode
///      A
///      |
/// B    C   D
///     /|\
///    E F G
bool tree::addNode()
{
    root->element = string("A");
    root->firstChild = new TreeNode("B");
    root->firstChild->nextSibling = new TreeNode("C");
    root->firstChild->nextSibling->nextSibling = new TreeNode("D");
    root->firstChild->nextSibling->firstChild = new TreeNode("E");
    root->firstChild->nextSibling->firstChild->nextSibling = new TreeNode("F");
    root->firstChild->nextSibling->firstChild->nextSibling->nextSibling = new TreeNode("G");

}

void tree::insertNode(string father, string self)
{
    auto temp = find(root, father);
    if(temp->firstChild == nullptr)     //父节点还没有子节点
    {
        temp->firstChild = new TreeNode;
        temp->firstChild->element = self;
    }
    else
    {
        auto i = temp->firstChild;
        for(; i->nextSibling != nullptr; i = i->nextSibling){}
        i->nextSibling = new TreeNode;
        i->nextSibling->element = self;
    }

}

TreeNode *tree::getRootNode() const
{
    return root;
}

void tree::travel(TreeNode *root)
{
    cout << root->element << " ";
    for(TreeNode *i = root->firstChild; i != nullptr; i = i->nextSibling)
    {
        travel(i);
    }
}

TreeNode *tree::find(TreeNode *root, string str)
{
    if(root->element == str)
    {
        return root;
    }
    else
    {
        if(root->firstChild != nullptr)
        {
            for(TreeNode * i = root->firstChild; i != nullptr; i = i->nextSibling)
            {
                auto temp = find(i, str);
                if(temp != nullptr)
                    return temp;
            }
        }
    }
    return nullptr;
}

main.cpp

#include <iostream>
#include "tree.h"

using namespace std;

int main(int argc, char *argv[])
{
    cout << "Hello World!" << endl;
    tree a;

    a.travel(a.getRootNode());
//    auto aaa = a.find(a.getRootNode(), "F");
//    cout << aaa->element << endl;
    a.insertNode("G", "H");
    a.travel(a.getRootNode());

    return 0;
}

总结:
看过一些其他的实现方法,和本文的主要差别是对节点结构体的设计上的.
一般的设计用到std::list,如下:

struct Node
{
    Object element;
    std::list<Node *> child;
};

这样做的话会使代码节点,毕竟STL中分装了大量的方法可以使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值