Skip to content

Adding the missing examples of Tree Traversal to C++ #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions contents/tree_traversal/code/c++/tree_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ void dfs_recursive(node const& n) {
}
}

void dfs_recursive_postorder(node const& n) {
for (auto const& child : n.children) {
dfs_recursive_postorder(child);
}
std::cout << n.value << '\n';
}


void dfs_recursive_inorder_btree(node const& n) {
switch (n.children_size) {
case 2:
dfs_recursive_inorder_btree(n.children[0]);
std::cout << n.value << '\n';
dfs_recursive_inorder_btree(n.children[1]);
break;
case 1:
dfs_recursive_inorder_btree(n.children[0]);
std::cout << n.value << '\n';
break;
case 0:
std::cout << n.value << '\n';
break;
default:
std::cout << "This is not a binary tree.\n";
break;
}
}

// Simple non-recursive scheme for DFS
void dfs_stack(node const& n) {
// this stack holds pointers into n's `children` vector,
Expand Down Expand Up @@ -76,7 +104,17 @@ node create_tree(size_t num_row, size_t num_child) {
int main() {
// Creating Tree in main
auto root = create_node(3, 3);
auto binary_root = create_node(3, 2);
std::cout << "DFS recursive:\n";
dfs_recursive(root);
std::cout << "DFS post order recursive:\n";
dfs_recursive_postorder(root);
std::cout << "DFS inorder binary tree:\n";
dfs_recursive_inorder_btree(binary_root);
std::cout << "DFS stack:\n";
dfs_stack(root);
std::cout << "BFS queue:\n";
bfs_queue(root);

return 0;
}
10 changes: 4 additions & 6 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ Now, in this case the first element searched through is still the root of the tr
{% sample lang="jl" %}
[import:18-26, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cpp" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:18-26, lang:"julia"](code/julia/Tree.jl)
[import:29-34 lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:47-58, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
Expand Down Expand Up @@ -109,8 +108,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
{% sample lang="jl" %}
[import:28-43, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cpp" %}
This has not been implemented in your chosen language, so here is the Julia code
[import:28-43, lang:"julia"](code/julia/Tree.jl)
[import:37-55 lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:60-79, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
Expand Down Expand Up @@ -153,7 +151,7 @@ In code, it looks like this:
{% sample lang="jl" %}
[import:45-56, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cpp" %}
[import:29-45, lang:"c_cpp"](code/c++/tree_example.cpp)
[import:58-73, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:81-94, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
Expand Down Expand Up @@ -189,7 +187,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
{% sample lang="jl" %}
[import:58-69, lang:"julia"](code/julia/Tree.jl)
{% sample lang="cpp" %}
[import:47-61, lang:"c_cpp"](code/c++/tree_example.cpp)
[import:76-89, lang:"c_cpp"](code/c++/tree_example.cpp)
{% sample lang="cs" %}
[import:96-109, lang:"csharp"](code/csharp/Tree.cs)
{% sample lang="c" %}
Expand Down