Skip to content

Commit

Permalink
Tree Traversal: Output Standardization (#857)
Browse files Browse the repository at this point in the history
* pep8ify python

* rename Tree_example.py

* standardize python output

* standardize c output

* standardize c++ output

* standardize rust output

* standardize javascript output

* standardize julia output

* Standardized Coconut output

* fix coconut

* standardize go output

* standardize common lisp output

* Verlet Integration: Output standardization (#855)

* Standardize julia output

* Standardize kotlin output

This also fixes a previous bug, which caused the time and velocity
values to not get printed correctly.

* Standardized c output

* Standardized cpp output

* Standardized lisp output

* Standardized fortran output

I was not able to prevent the preceding whitespaces, but they can
just be trimmed.

* Standardized go output

* Standardized java output

* Standardize javascript output

* Standardize nim output

* Standardize python output

* Standardize ruby output

As the original implementation only returned the time and not
the velocity, the code needed to be adjusted a bit. Now it returns
the two values as an array which gets deconstructed and printed.

* Standardize rust output

* Standardize swift output

* Standardized haskell output

* Standardized haskell output (no quote marks)

* attempt at fix for asm

Co-authored-by: Jérémie Gillet <jie.gillet@gmail.com>
Co-authored-by: James Schloss <jrs.schloss@gmail.com>

* standardize php output

* standardize swift output

* removed outdated comments in Coconut implementation

* standardize haskell output

* Fix asm exit codes (#858)

* standardize crystal output

* Flood fill in Coconut (#836)

* Added Barnsley fern in Coconut (+ .editorconfig for Coconut) (#814)

* Added the Jarvis march in Coconut (#734)

* Fix minor typo

* Change folder structure of convolutions (#861)

This puts every chapters' code into its own directory, which is
useful for automatic validation.

* standardize csharp output

* standardize java output

* fix php line numbers and python filename

* fix coconut and python line numbers

* fix minor bug in C

* fix labels and minor bug in go

* minor label fix in python

* Huffman encoding in Coconut (#732)

* Update README.md

Changing readme link to reflect change in twitch url

* Add C# implemenation for 1D Convolutions (#846)

* Add C# implemenation for 1D Convolutions

* Fix off by one error and julia line numbers

* Fix off by one error for linear convolutions

* Fix trailing zero

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Update contents/convolutions/code/csharp/1DConvolution.cs

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* Add trailing new line

Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>

* IFS Common Lisp implementation (#722)

* first working version

* cleaned up the chaos-game function and added some helpfull comments

* changed the .md file and upped iterations

* Skeleton of the final solution

* Update contents/IFS/IFS.md

Co-authored-by: Eric Berquist <eric.berquist@gmail.com>

Co-authored-by: James Schloss <jrs.schloss@gmail.com>
Co-authored-by: Eric Berquist <eric.berquist@gmail.com>

* added MonteCarlo in PowerShell (#760)

* Add Rust implementation for The Barnsley Fern (#818)

* Add Rust implementation for The Barnsley Fern

* Change operators constants and add Cargo.toml

* Fix bug in matrix multiplication

Co-authored-by: James Schloss <jrs.schloss@gmail.com>

* changing barnsley.md to read the correct coconut file (#867)

* added approximate counting implementation in python (#866)

* added approximate counting implementation in python

* Update approximate_counting.py

* updating approximate_counting.py

* removed redundancies

* fixing linking issues in convolution chapter (#868)

* Add initial devcontainer (#862)

* small changes to convolutional theorem chapter and code to make things more reproduceable (#872)

* added convolutional theorem implementation in python (#869)

* added convolutional theorem implementation in python

* fixed chapter linking

* added comments to the code

* changed random distribution to sawtooth

* corrected previous commit

* fixed comments

Co-authored-by: James Schloss <jrs.schloss@gmail.com>

* Use `[#]\n` instead of `[#]` for human readable output (#873)

* Add approximate counting algorithm in C (#844)

* Add approximate counting algorithm in C

* Fix typo

Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com>

Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com>
Co-authored-by: James Schloss <jrs.schloss@gmail.com>

* added 1d convolution implementation in python (#874)

* added 1d convolution implementation in python

* fixed some mistakes in the code so it outputs correct results

* making the code look better

* spacing code properly for readability

* fixing 1d convolution markdown file (#879)

* Add racket setup to devcontainer (#875)

* Normalize inputs for scheme euclid algorithm (#878)

Change inputs to make it consistent with other examples.

* Add initial setup for Swift in devcontainer (#880)

* Add scheme setup to devcontainer (#876)

* Clean up Monte Carlo integration in Racket (#781)

* Clean up Monte Carlo integration in Racket

* Add blank lines in Monte Carlo integration in Clojure

* Change Racket lang include from lisp to racket

* Add C++ code for Flood Fill algorithm (#860)

* fixing chapter to use split-op code (#888)

* use [#]\n for readability

* Implemented stacks and queues in Java (#897)

* Java tree traversal: updated dfsRecursiveInOrderBinary (#899)

* Tree traversal in smalltalk (#453)

* fixed print statements (#901)

* Fixes in PATH that make dlang, emojicode and factor usable (#890)

* fixing huffman encoding for Julia and adding Test (#828)

* JavaScript tree traversal: updated dfsInorder (#902)

* julia: change printf to print

* pep8ify python

* rename Tree_example.py

* standardize python output

* standardize c output

* standardize c++ output

* standardize rust output

* standardize javascript output

Resolved conflict with master (by Amaras)

* standardize julia output

Resolved conflict with master (by Amaras)

* Standardized Coconut output

* fix coconut

* standardize go output

* standardize common lisp output

* standardize php output

* standardize swift output

* removed outdated comments in Coconut implementation

* standardize haskell output

* standardize crystal output

* standardize csharp output

* standardize java output

Resolved the conflict with fixing dfs

* fix php line numbers and python filename

* fix coconut and python line numbers

* fix minor bug in C

* fix labels and minor bug in go

* minor label fix in python

* use [#]\n for readability

* julia: change printf to print

* update Tree.java. remove MainClass.java

Co-authored-by: Sammy Plat <amaras@vivaldi.net>
Co-authored-by: stormofice <58337328+stormofice@users.noreply.github.com>
Co-authored-by: Jérémie Gillet <jie.gillet@gmail.com>
Co-authored-by: James Schloss <jrs.schloss@gmail.com>
Co-authored-by: Eric Berquist <eric.berquist@gmail.com>
Co-authored-by: Trashtalk217 <trashtalk217@gmail.com>
Co-authored-by: PaddyKe <34421580+PaddyKe@users.noreply.github.com>
Co-authored-by: Dimitri Belopopsky <ShadowMitia@users.noreply.github.com>
Co-authored-by: Ayman Lafaz <ayman.lafaz@um5r.ac.ma>
Co-authored-by: Nicholas Tindle <nicktindle@outlook.com>
Co-authored-by: Mahdi <24981501+mahdisarikhani@users.noreply.github.com>
Co-authored-by: Henrik Christensen <sensen1695@hotmail.com>
Co-authored-by: Neverik <neverliberty@gmail.com>
  • Loading branch information
14 people committed Oct 25, 2021
1 parent cf56e67 commit 2000ab3
Show file tree
Hide file tree
Showing 20 changed files with 442 additions and 356 deletions.
33 changes: 19 additions & 14 deletions contents/tree_traversal/code/c++/tree_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct node {
// Simple recursive scheme for DFS
void dfs_recursive(node const& n) {
// Here we are doing something...
std::cout << n.value << '\n';
std::cout << n.value << ' ';
for (auto const& child : n.children) {
dfs_recursive(child);
}
Expand All @@ -27,23 +27,23 @@ void dfs_recursive_postorder(node const& n) {
for (auto const& child : n.children) {
dfs_recursive_postorder(child);
}
std::cout << n.value << '\n';
std::cout << n.value << ' ';
}


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';
std::cout << n.value << ' ';
dfs_recursive_inorder_btree(n.children[1]);
break;
case 1:
dfs_recursive_inorder_btree(n.children[0]);
std::cout << n.value << '\n';
std::cout << n.value << ' ';
break;
case 0:
std::cout << n.value << '\n';
std::cout << n.value << ' ';
break;
default:
std::cout << "This is not a binary tree.\n";
Expand All @@ -61,7 +61,7 @@ void dfs_stack(node const& n) {
while (stack.size() > 0) {
auto const& temp = *stack.top();
stack.pop();
std::cout << temp.value << '\n';
std::cout << temp.value << ' ';

for (auto const& child : temp.children) {
stack.push(&child);
Expand All @@ -78,7 +78,7 @@ void bfs_queue(node const& n) {
auto const& temp = *queue.front();
queue.pop();

std::cout << temp.value << '\n';
std::cout << temp.value << ' ';
for (auto const& child : temp.children) {
queue.push(&child);
}
Expand All @@ -100,18 +100,23 @@ node create_tree(size_t num_row, size_t num_child) {

int main() {
// Creating Tree in main
auto root = create_tree(3, 3);
auto root = create_tree(2, 3);
auto binary_root = create_tree(3, 2);
std::cout << "DFS recursive:\n";
std::cout << "[#]\nRecursive DFS:\n";
dfs_recursive(root);
std::cout << "DFS post order recursive:\n";
std::cout << '\n';
std::cout << "[#]\nRecursive Postorder DFS:\n";
dfs_recursive_postorder(root);
std::cout << "DFS inorder binary tree:\n";
dfs_recursive_inorder_btree(binary_root);
std::cout << "DFS stack:\n";
std::cout << '\n';
std::cout << "[#]\nStack-based DFS:\n";
dfs_stack(root);
std::cout << "BFS queue:\n";
std::cout << '\n';
std::cout << "[#]\nQueue-based BFS:\n";
bfs_queue(root);
std::cout << '\n';
std::cout << "[#]\nRecursive Inorder DFS for Binary Tree:\n";
dfs_recursive_inorder_btree(binary_root);
std::cout << '\n';

return 0;
}
38 changes: 30 additions & 8 deletions contents/tree_traversal/code/c/tree_traversal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void destroy_tree(struct node n) {
}

void dfs_recursive(struct node n) {
printf("%d\n", n.id);
printf("%d ", n.id);

if (n.children) {
for (size_t i = 0; i < n.children_size; ++i) {
Expand All @@ -49,22 +49,22 @@ void dfs_recursive_postorder(struct node n) {
dfs_recursive_postorder(n.children[i]);
}

printf("%d\n", n.id);
printf("%d ", n.id);
}

void dfs_recursive_inorder_btree(struct node n) {
switch (n.children_size) {
case 2:
dfs_recursive_inorder_btree(n.children[0]);
printf("%d\n", n.id);
printf("%d ", n.id);
dfs_recursive_inorder_btree(n.children[1]);
break;
case 1:
dfs_recursive_inorder_btree(n.children[0]);
printf("%d\n", n.id);
printf("%d ", n.id);
break;
case 0:
printf("%d\n", n.id);
printf("%d ", n.id);
break;
default:
printf("This is not a binary tree.\n");
Expand All @@ -83,7 +83,7 @@ void dfs_stack(struct node n) {
break;
}

printf("%d\n", tmp->id);
printf("%d ", tmp->id);
for (size_t i = 0; i < tmp->children_size; ++i) {
stack_push(&stk, &tmp->children[i]);
}
Expand All @@ -103,7 +103,7 @@ void bfs_queue(struct node n) {
break;
}

printf("%d\n", tmp->id);
printf("%d ", tmp->id);
for (size_t i = 0; i < tmp->children_size; ++i) {
enqueue(&q, &tmp->children[i]);
}
Expand All @@ -113,9 +113,31 @@ void bfs_queue(struct node n) {
}

int main() {
struct node root = create_tree(3, 3);
struct node root = create_tree(2, 3);

printf("[#]\nRecursive DFS:\n");
dfs_recursive(root);
printf("\n");

printf("[#]\nRecursive Postorder DFS:\n");
dfs_recursive_postorder(root);
printf("\n");

printf("[#]\nStack-based DFS:\n");
dfs_stack(root);
printf("\n");

printf("[#]\nQueue-based BFS:\n");
bfs_queue(root);
printf("\n");

destroy_tree(root);
struct node root_binary = create_tree(3, 2);

printf("[#]\nRecursive Inorder DFS for Binary Tree:\n");
dfs_recursive_inorder_btree(root_binary);
printf("\n");

destroy_tree(root_binary);
return 0;
}
19 changes: 12 additions & 7 deletions contents/tree_traversal/code/clisp/tree-traversal.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,41 @@
(defun make-tree (num-rows num-child)
"Creates a simple tree, where every node has 'num-child' children and is 'num-rows' deep."
;; A tree with 0 rows can't be created.
(if (eql num-rows 1)
(if (eql num-rows 0)
(make-node
:data 1
:data 0
:children nil)
(make-node
:data num-rows
:children (loop repeat num-child collect (make-tree (1- num-rows) num-child)))))

;; A tree for testing
(defvar tree (make-tree 3 3))
(defvar tree (make-tree 2 3))

;; A binary tree for testing
(defvar binary-tree (make-tree 3 2))

;; Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
(format t "[#]~%Recursive DFS:~%")
(dfs-recursive tree)
(format t "~%")

;; Should print: 1 1 1 2 1 1 1 2 1 1 1 2 3
(format t "[#]~%Recursive Postorder DFS:~%")
(dfs-recursive-postorder tree)
(format t "~%")

;; Should print: 1 2 1 3 1 2 1
(dfs-recursive-inorder-btree binary-tree)
(format t "~%")

;; Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
(format t "[#]~%Stack-based DFS:~%")
(dfs-stack tree)
(format t "~%")

;; Should print: 3 2 2 2 1 1 1 1 1 1 1 1 1
(format t "[#]~%Queue-based BFS:~%")
(bfs-queue tree)
(format t "~%")

;; Should print: 1 2 1 3 1 2 1
(format t "[#]~%Recursive Inorder DFS for Binary Tree:~%")
(dfs-recursive-inorder-btree binary-tree)
(format t "~%")
21 changes: 8 additions & 13 deletions contents/tree_traversal/code/coconut/tree_traversal.coco
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,37 @@ def bfs_queue(node is Node):
def create_tree(num_rows, num_child):
"""Creates a simple tree, where every node has
'num_child' children and is 'num_rows' deep."""
if num_rows == 1:
return Node(1, ())
if num_rows == 0:
return Node(0, ())
else:
return Node(num_rows, tuple(create_tree(num_rows-1, num_child)
for _ in range(num_child)))


if __name__ =='__main__':
# A ternary tree for testing
tree = create_tree(3, 3)
tree = create_tree(2, 3)

# Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
print("Recursive DFS:")
print("[#]\nRecursive DFS:")
dfs_recursive(tree)
print()

# Should print: 1 1 1 2 1 1 1 2 1 1 1 2 3
print("Recursive Postorder DFS:")
print("[#]\nRecursive Postorder DFS:")
dfs_recursive_postorder(tree)
print()

# Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
print("Stack (DFS):")
print("[#]\nStack-based DFS:")
dfs_stack(tree)
print()

# Should print: 3 2 2 2 1 1 1 1 1 1 1 1 1
print("Queue (BFS):")
print("[#]\nQueue-based BFS:")
bfs_queue(tree)
print()

# And a binary tree for testing
binary_tree = create_tree(3, 2)

# Should print: 1 2 1 3 1 2 1
print("Recursive Inorder Binary Tree:")
print("[#]\nRecursive Inorder DFS for Binary Tree:")
dfs_recursive_inorder_btree(binary_tree)
print()

50 changes: 12 additions & 38 deletions contents/tree_traversal/code/crystal/tree-traversal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ class Node
end

def dfs_recursive(node)
print node.id
print "#{node.id} "
node.children.each{ |child| dfs_recursive child }
end

def dfs_recursive_postorder(node)
node.children.each{ |child| dfs_recursive_postorder child }
print node.id
print "#{node.id} "
end

def dfs_recursive_inorder_btree(node)
case node.children.size
when 2
dfs_recursive_inorder_btree node.children[0]
print node.id
print "#{node.id} "
dfs_recursive_inorder_btree node.children[1]
when 1
dfs_recursive_inorder_btree node.children[0]
print node.id
print "#{node.id} "
when 0
print node.id
print "#{node.id} "
else
print "Not a binary tree!"
end
Expand All @@ -35,7 +35,7 @@ def dfs_stack(node)

until stack.empty?
temp = stack.pop
print temp.id
print "#{temp.id} "
temp.children.each{ |child| stack.push child }
end
end
Expand All @@ -45,7 +45,7 @@ def bfs_queue(node)

until queue.empty?
temp = queue.shift
print temp.id
print "#{temp.id} "
temp.children.each{ |child| queue.push child }
end
end
Expand All @@ -60,54 +60,28 @@ def create_tree(levels, num_childs)
Node.new(levels, children)
end

def print_tree(node, depth = [] of String)
puts "(#{node.id})"
depth.push " "
len = node.children.size - 1

(0 .. len).each do |i|
depth.each{|c| print c}
unless i == len
print ""
depth.push ""
print_tree node.children[i], depth
depth.pop
else
print ""
depth.push " "
print_tree node.children[i], depth
depth.pop
end
end
depth.pop
end

def main
puts "Creating Tree"
root = create_tree levels: 2, num_childs: 3
print_tree root

puts "Using recursive DFS:"
puts "[#]\nRecursive DFS:"
dfs_recursive root
puts

puts "Using recursive DFS with post-order traversal:"
puts "[#]\nRecursive Postorder DFS:"
dfs_recursive_postorder root
puts

puts "Using stack-based DFS:"
puts "[#]\nStack-based DFS:"
dfs_stack root
puts

puts "Using queue-based BFS:"
puts "[#]\nQueue-based BFS:"
bfs_queue root
puts

puts "Creating binary tree to test in-order traversal"
root_bin = create_tree levels: 3, num_childs: 2
print_tree root_bin

puts "Using In-order DFS:"
puts "[#]\nRecursive Inorder DFS for Binary Tree:"
dfs_recursive_inorder_btree root_bin
puts
end
Expand Down
Loading

0 comments on commit 2000ab3

Please sign in to comment.