学习<<数据结构与算法分析>>,给以后留个记录
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中分装了大量的方法可以使用