-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS.DFS...
More file actions
91 lines (84 loc) · 1.62 KB
/
Copy pathBFS.DFS...
File metadata and controls
91 lines (84 loc) · 1.62 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <stack>
#include<queue>
#include <vector>
template<typename T>
class Node {
private:
T data;
Node* l;
Node* r;
public:
Node(T data_ = {}, Node* l_ = nullptr; Node* r_ = nullptr) {
data = data_;
l = l_;
r = r_;
}
~Node() {
data = {};
l = nullptr;
r = nullptr;
}
};
template<typename T>
class TTree {
private:
Node<T>* head;
public:
TTree(T data) {
head = new Node(data);
}
~TTree() {
delete[] head;
}
Node<T>* search(T data) {
Node* cur = head;
while (cur) {
if (cur->data = data)
return cur;
if (cur->data > data) {
cur = cur->l;
}
else
cur = cur->r;
}
return nullptr;
}
void add(Node* nd) {
Node* cur = head;
if (cur == nullptr) {
head = nd;
return;
}
while (1) {
if (cur->data > nd->data)
if (cur->l)
cur = cur->l;
else
break;
else
if (cur->r)
cur = cur->r;
else
break;
}
if (cur->data > nd->data)
cur->l = nd;
else
cur->r = nd;
}
};
int main()
{
Node<int> nd2(2);
Node<int> nd7(7);
Node<int> nd6(6);
Node<int> nd1(-1);
Node<int> nd4(4);
TTree<int> tree(5);
tree.add(&nd2);
tree.add(&nd7);
tree.add(&nd6);
tree.add(&nd1);
tree.add(&nd4);
}