Skip to content

Commit fcde5d2

Browse files
authored
Update main.cpp
1 parent 5b6c831 commit fcde5d2

File tree

1 file changed

+101
-47
lines changed

1 file changed

+101
-47
lines changed

src/main.cpp

Lines changed: 101 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <iomanip>
55
#include <iostream>
66
#include <memory>
7+
#include <queue>
8+
#include <stack>
79

810
int sub(int a, int b) { return a - b; }
911

@@ -15,48 +17,58 @@ void inc(int a, int &b, const int &c) {
1517
return;
1618
}
1719

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+
}
3339

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();
3846

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);
4350

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+
}
4555

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();
4962

50-
T &operator*() { return *m_ptr; }
63+
std::cout << node->val << std::endl;
5164

52-
private:
53-
T *m_ptr;
54-
};
65+
if (node->r_child != nullptr)
66+
stack.push(node->r_child);
5567

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+
}
6072

6173
int main() {
6274
// double number = 3.1914;
@@ -101,19 +113,61 @@ int main() {
101113
// f();
102114
// std::cout << "after call: " << a << ' ' << b << ' ' << c << '\n';
103115

104-
std::vector<int> foo(3);
105-
array<int> my_arr(4);
116+
/*
106117
107-
my_arr[0] = 3;
108-
my_arr[1] = 7;
109-
my_arr[2] = 5;
110-
my_arr[3] = 6;
111118
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
113126
114-
// std::cout << *(my_arr++) << std::endl;
115-
for (array<int>::Iterator it = my_arr.begin(); it != my_arr.end(); ++it) {
127+
*/
116128

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

Comments
 (0)