-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralTree.cpp
More file actions
76 lines (67 loc) · 1.25 KB
/
GeneralTree.cpp
File metadata and controls
76 lines (67 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "GeneralTree.h"
#include<string>
using namespace std;
Node::Node(string value, Node* parent)
{
this->Value = value;
this->Parent = parent;
}
Node::Node(string value)
{
this->Value = value;
this->Parent = nullptr;
}
void GeneralTree::AddChild(string value,string parenttext)
{
Node* parent = Find(parenttext);
Node* child = Find(value);
if (child != nullptr)
return;
if (parent == nullptr)
{
AddParent(parenttext);
parent = Find(parenttext);
}
parent->Childs.push_back(new Node(value,parent));
}
void GeneralTree::AddParent(string value)
{
Node* parent = Find(value);
if (parent == nullptr)
{
parent = new Node(value);
Nodes.push_back(parent);
}
}
Node* GeneralTree::Find(std::string value)
{
for (auto n : Nodes)
{
if (n->Value == value)
return n;
Node* child = SearchChilds(value,n);
if (child != nullptr)
return child;
}
return nullptr;
}
Node* GeneralTree::SearchChilds(string value,Node* node)
{
if (node->Childs.empty())
return nullptr;
for (auto child : node->Childs)
{
if (child->Value == value)
return child;
auto ChildSearch = SearchChilds(value, child);
if (ChildSearch != nullptr)
return ChildSearch;
}
return nullptr;
}
GeneralTree::GeneralTree()
{
}
GeneralTree::~GeneralTree()
{
}