4
4
#include < iomanip>
5
5
#include < iostream>
6
6
#include < memory>
7
+ #include < queue>
8
+ #include < stack>
7
9
8
10
int sub (int a, int b) { return a - b; }
9
11
@@ -15,48 +17,58 @@ void inc(int a, int &b, const int &c) {
15
17
return ;
16
18
}
17
19
18
- template <typename T> struct array {
19
- int m_length = 0 ;
20
- T *data;
21
- array (int length = 0 ) : m_length(length), data(new T[length]) {}
22
- ~array () { delete[] data; }
23
- T &operator [](std::size_t index) { return data[index ]; }
24
-
25
- class Iterator {
26
- public:
27
- Iterator (T *ptr) : m_ptr(ptr) {}
28
-
29
- Iterator &operator ++() {
30
- ++m_ptr;
31
- return *this ;
32
- }
20
+ template <typename T> struct Node {
21
+ Node *l_child = nullptr ;
22
+ Node *r_child = nullptr ;
23
+ T val;
24
+ };
25
+ // dfs
26
+ template <typename T> void recursive_dfs (Node<T> *node) {
27
+ // pre order
28
+ std::cout << node->val << std::endl;
29
+ if (node->l_child != nullptr )
30
+ recursive_dfs (node->l_child );
31
+ // mid order
32
+ // std::cout << node->val << std::endl;
33
+
34
+ if (node->r_child != nullptr )
35
+ recursive_dfs (node->r_child );
36
+ // post order
37
+ // std::cout << node->val << std::endl;
38
+ }
33
39
34
- // Iterator & ++operator() {
35
- // m_ptr++;
36
- // return *this;
37
- // }
40
+ template <typename T> void bfs (Node<T> *node) {
41
+ std::queue<Node<T> *> q;
42
+ q.push (node);
43
+ while (!q.empty ()) {
44
+ auto node = q.front ();
45
+ q.pop ();
38
46
39
- Iterator &operator --() {
40
- --m_ptr;
41
- return *this ;
42
- }
47
+ std::cout << node->val << std::endl;
48
+ if (node->l_child != nullptr )
49
+ q.push (node->l_child );
43
50
44
- // bool operator!=(const Iterator &rhs) const { return m_ptr != rhs.m_ptr; }
51
+ if (node->r_child != nullptr )
52
+ q.push (node->r_child );
53
+ }
54
+ }
45
55
46
- bool operator !=(const Iterator &other) const {
47
- return m_ptr != other.m_ptr ;
48
- }
56
+ template <typename T> void dfs (Node<T> *node) {
57
+ std::stack<Node<T> *> stack;
58
+ stack.push (node);
59
+ while (!stack.empty ()) {
60
+ auto node = stack.top ();
61
+ stack.pop ();
49
62
50
- T & operator *() { return *m_ptr; }
63
+ std::cout << node-> val << std::endl;
51
64
52
- private:
53
- T *m_ptr;
54
- };
65
+ if (node->r_child != nullptr )
66
+ stack.push (node->r_child );
55
67
56
- public:
57
- Iterator begin () { return Iterator (data); }
58
- Iterator end () { return Iterator (data + m_length); }
59
- };
68
+ if (node-> l_child != nullptr )
69
+ stack. push (node-> l_child );
70
+ }
71
+ }
60
72
61
73
int main () {
62
74
// double number = 3.1914;
@@ -101,19 +113,61 @@ int main() {
101
113
// f();
102
114
// std::cout << "after call: " << a << ' ' << b << ' ' << c << '\n';
103
115
104
- std::vector<int > foo (3 );
105
- array<int > my_arr (4 );
116
+ /*
106
117
107
- my_arr[0 ] = 3 ;
108
- my_arr[1 ] = 7 ;
109
- my_arr[2 ] = 5 ;
110
- my_arr[3 ] = 6 ;
111
118
112
- // std::sort(my_arr.begin(), my_arr.end());
119
+ a
120
+ / \
121
+ b c
122
+ / \ / \
123
+ d e f g
124
+ / \ /
125
+ h i j
113
126
114
- // std::cout << *(my_arr++) << std::endl;
115
- for (array<int >::Iterator it = my_arr.begin (); it != my_arr.end (); ++it) {
127
+ */
116
128
117
- std::cout << *it << std::endl;
118
- }
119
- }
129
+ Node<std::string> *a = new Node<std::string>;
130
+ Node<std::string> *b = new Node<std::string>;
131
+ Node<std::string> *c = new Node<std::string>;
132
+ Node<std::string> *d = new Node<std::string>;
133
+ Node<std::string> *e = new Node<std::string>;
134
+ Node<std::string> *f = new Node<std::string>;
135
+ Node<std::string> *g = new Node<std::string>;
136
+ Node<std::string> *h = new Node<std::string>;
137
+ Node<std::string> *i = new Node<std::string>;
138
+ Node<std::string> *j = new Node<std::string>;
139
+
140
+ a->val = " a" ;
141
+ a->l_child = b;
142
+ a->r_child = c;
143
+
144
+ b->val = " b" ;
145
+ b->l_child = d;
146
+ b->r_child = e;
147
+
148
+ c->val = " c" ;
149
+ c->l_child = f;
150
+ c->r_child = g;
151
+
152
+ d->val = " d" ;
153
+ d->l_child = h;
154
+ d->r_child = i;
155
+
156
+ e->val = " e" ;
157
+ e->l_child = j;
158
+
159
+ h->val = " h" ;
160
+ i->val = " i" ;
161
+ f->val = " f" ;
162
+ g->val = " g" ;
163
+ j->val = " j" ;
164
+ std::cout << " recursive_dfs" << std::endl;
165
+ recursive_dfs (a);
166
+
167
+ std::cout << " dfs" << std::endl;
168
+ dfs (a);
169
+
170
+ std::cout << " bfs" << std::endl;
171
+
172
+ bfs (a);
173
+ }
0 commit comments