Skip to content

Commit 9040faf

Browse files
Gathrosjiegillet
authored andcommitted
Adding the missing examples of Tree Traversal to C++ (#341)
* Adding missing examples to tree_example.cpp
1 parent 7b92b39 commit 9040faf

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

contents/tree_traversal/code/c++/tree_example.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,34 @@ void dfs_recursive(node const& n) {
2626
}
2727
}
2828

29+
void dfs_recursive_postorder(node const& n) {
30+
for (auto const& child : n.children) {
31+
dfs_recursive_postorder(child);
32+
}
33+
std::cout << n.value << '\n';
34+
}
35+
36+
37+
void dfs_recursive_inorder_btree(node const& n) {
38+
switch (n.children_size) {
39+
case 2:
40+
dfs_recursive_inorder_btree(n.children[0]);
41+
std::cout << n.value << '\n';
42+
dfs_recursive_inorder_btree(n.children[1]);
43+
break;
44+
case 1:
45+
dfs_recursive_inorder_btree(n.children[0]);
46+
std::cout << n.value << '\n';
47+
break;
48+
case 0:
49+
std::cout << n.value << '\n';
50+
break;
51+
default:
52+
std::cout << "This is not a binary tree.\n";
53+
break;
54+
}
55+
}
56+
2957
// Simple non-recursive scheme for DFS
3058
void dfs_stack(node const& n) {
3159
// this stack holds pointers into n's `children` vector,
@@ -76,7 +104,17 @@ node create_tree(size_t num_row, size_t num_child) {
76104
int main() {
77105
// Creating Tree in main
78106
auto root = create_node(3, 3);
107+
auto binary_root = create_node(3, 2);
108+
std::cout << "DFS recursive:\n";
79109
dfs_recursive(root);
110+
std::cout << "DFS post order recursive:\n";
111+
dfs_recursive_postorder(root);
112+
std::cout << "DFS inorder binary tree:\n";
113+
dfs_recursive_inorder_btree(binary_root);
114+
std::cout << "DFS stack:\n";
80115
dfs_stack(root);
116+
std::cout << "BFS queue:\n";
81117
bfs_queue(root);
118+
119+
return 0;
82120
}

contents/tree_traversal/tree_traversal.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ Now, in this case the first element searched through is still the root of the tr
7575
{% sample lang="jl" %}
7676
[import:18-26, lang:"julia"](code/julia/Tree.jl)
7777
{% sample lang="cpp" %}
78-
This has not been implemented in your chosen language, so here is the Julia code
79-
[import:18-26, lang:"julia"](code/julia/Tree.jl)
78+
[import:29-34 lang:"c_cpp"](code/c++/tree_example.cpp)
8079
{% sample lang="cs" %}
8180
[import:47-58, lang:"csharp"](code/csharp/Tree.cs)
8281
{% sample lang="c" %}
@@ -109,8 +108,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
109108
{% sample lang="jl" %}
110109
[import:28-43, lang:"julia"](code/julia/Tree.jl)
111110
{% sample lang="cpp" %}
112-
This has not been implemented in your chosen language, so here is the Julia code
113-
[import:28-43, lang:"julia"](code/julia/Tree.jl)
111+
[import:37-55 lang:"c_cpp"](code/c++/tree_example.cpp)
114112
{% sample lang="cs" %}
115113
[import:60-79, lang:"csharp"](code/csharp/Tree.cs)
116114
{% sample lang="c" %}
@@ -153,7 +151,7 @@ In code, it looks like this:
153151
{% sample lang="jl" %}
154152
[import:45-56, lang:"julia"](code/julia/Tree.jl)
155153
{% sample lang="cpp" %}
156-
[import:29-45, lang:"c_cpp"](code/c++/tree_example.cpp)
154+
[import:58-73, lang:"c_cpp"](code/c++/tree_example.cpp)
157155
{% sample lang="cs" %}
158156
[import:81-94, lang:"csharp"](code/csharp/Tree.cs)
159157
{% sample lang="c" %}
@@ -189,7 +187,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
189187
{% sample lang="jl" %}
190188
[import:58-69, lang:"julia"](code/julia/Tree.jl)
191189
{% sample lang="cpp" %}
192-
[import:47-61, lang:"c_cpp"](code/c++/tree_example.cpp)
190+
[import:76-89, lang:"c_cpp"](code/c++/tree_example.cpp)
193191
{% sample lang="cs" %}
194192
[import:96-109, lang:"csharp"](code/csharp/Tree.cs)
195193
{% sample lang="c" %}

0 commit comments

Comments
 (0)