Skip to content

Commit 2000ab3

Browse files
lazypropAmarasstormoficejiegilletleios
authored
Tree Traversal: Output Standardization (#857)
* 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 <[email protected]> Co-authored-by: James Schloss <[email protected]> * 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 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Update contents/convolutions/code/csharp/1DConvolution.cs Co-authored-by: Trashtalk217 <[email protected]> * Add trailing new line Co-authored-by: Trashtalk217 <[email protected]> * 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 <[email protected]> Co-authored-by: James Schloss <[email protected]> Co-authored-by: Eric Berquist <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> Co-authored-by: stormofice <[email protected]> Co-authored-by: James Schloss <[email protected]> * 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 <[email protected]> Co-authored-by: stormofice <[email protected]> Co-authored-by: Jérémie Gillet <[email protected]> Co-authored-by: James Schloss <[email protected]> Co-authored-by: Eric Berquist <[email protected]> Co-authored-by: Trashtalk217 <[email protected]> Co-authored-by: PaddyKe <[email protected]> Co-authored-by: Dimitri Belopopsky <[email protected]> Co-authored-by: Ayman Lafaz <[email protected]> Co-authored-by: Nicholas Tindle <[email protected]> Co-authored-by: Mahdi <[email protected]> Co-authored-by: Henrik Christensen <[email protected]> Co-authored-by: Neverik <[email protected]>
1 parent cf56e67 commit 2000ab3

20 files changed

+442
-356
lines changed

Diff for: contents/tree_traversal/code/c++/tree_example.cpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct node {
1717
// Simple recursive scheme for DFS
1818
void dfs_recursive(node const& n) {
1919
// Here we are doing something...
20-
std::cout << n.value << '\n';
20+
std::cout << n.value << ' ';
2121
for (auto const& child : n.children) {
2222
dfs_recursive(child);
2323
}
@@ -27,23 +27,23 @@ void dfs_recursive_postorder(node const& n) {
2727
for (auto const& child : n.children) {
2828
dfs_recursive_postorder(child);
2929
}
30-
std::cout << n.value << '\n';
30+
std::cout << n.value << ' ';
3131
}
3232

3333

3434
void dfs_recursive_inorder_btree(node const& n) {
3535
switch (n.children.size()) {
3636
case 2:
3737
dfs_recursive_inorder_btree(n.children[0]);
38-
std::cout << n.value << '\n';
38+
std::cout << n.value << ' ';
3939
dfs_recursive_inorder_btree(n.children[1]);
4040
break;
4141
case 1:
4242
dfs_recursive_inorder_btree(n.children[0]);
43-
std::cout << n.value << '\n';
43+
std::cout << n.value << ' ';
4444
break;
4545
case 0:
46-
std::cout << n.value << '\n';
46+
std::cout << n.value << ' ';
4747
break;
4848
default:
4949
std::cout << "This is not a binary tree.\n";
@@ -61,7 +61,7 @@ void dfs_stack(node const& n) {
6161
while (stack.size() > 0) {
6262
auto const& temp = *stack.top();
6363
stack.pop();
64-
std::cout << temp.value << '\n';
64+
std::cout << temp.value << ' ';
6565

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

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

101101
int main() {
102102
// Creating Tree in main
103-
auto root = create_tree(3, 3);
103+
auto root = create_tree(2, 3);
104104
auto binary_root = create_tree(3, 2);
105-
std::cout << "DFS recursive:\n";
105+
std::cout << "[#]\nRecursive DFS:\n";
106106
dfs_recursive(root);
107-
std::cout << "DFS post order recursive:\n";
107+
std::cout << '\n';
108+
std::cout << "[#]\nRecursive Postorder DFS:\n";
108109
dfs_recursive_postorder(root);
109-
std::cout << "DFS inorder binary tree:\n";
110-
dfs_recursive_inorder_btree(binary_root);
111-
std::cout << "DFS stack:\n";
110+
std::cout << '\n';
111+
std::cout << "[#]\nStack-based DFS:\n";
112112
dfs_stack(root);
113-
std::cout << "BFS queue:\n";
113+
std::cout << '\n';
114+
std::cout << "[#]\nQueue-based BFS:\n";
114115
bfs_queue(root);
116+
std::cout << '\n';
117+
std::cout << "[#]\nRecursive Inorder DFS for Binary Tree:\n";
118+
dfs_recursive_inorder_btree(binary_root);
119+
std::cout << '\n';
115120

116121
return 0;
117122
}

Diff for: contents/tree_traversal/code/c/tree_traversal.c

+30-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void destroy_tree(struct node n) {
3535
}
3636

3737
void dfs_recursive(struct node n) {
38-
printf("%d\n", n.id);
38+
printf("%d ", n.id);
3939

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

52-
printf("%d\n", n.id);
52+
printf("%d ", n.id);
5353
}
5454

5555
void dfs_recursive_inorder_btree(struct node n) {
5656
switch (n.children_size) {
5757
case 2:
5858
dfs_recursive_inorder_btree(n.children[0]);
59-
printf("%d\n", n.id);
59+
printf("%d ", n.id);
6060
dfs_recursive_inorder_btree(n.children[1]);
6161
break;
6262
case 1:
6363
dfs_recursive_inorder_btree(n.children[0]);
64-
printf("%d\n", n.id);
64+
printf("%d ", n.id);
6565
break;
6666
case 0:
67-
printf("%d\n", n.id);
67+
printf("%d ", n.id);
6868
break;
6969
default:
7070
printf("This is not a binary tree.\n");
@@ -83,7 +83,7 @@ void dfs_stack(struct node n) {
8383
break;
8484
}
8585

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

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

115115
int main() {
116-
struct node root = create_tree(3, 3);
116+
struct node root = create_tree(2, 3);
117+
118+
printf("[#]\nRecursive DFS:\n");
119+
dfs_recursive(root);
120+
printf("\n");
121+
122+
printf("[#]\nRecursive Postorder DFS:\n");
123+
dfs_recursive_postorder(root);
124+
printf("\n");
125+
126+
printf("[#]\nStack-based DFS:\n");
127+
dfs_stack(root);
128+
printf("\n");
129+
130+
printf("[#]\nQueue-based BFS:\n");
117131
bfs_queue(root);
132+
printf("\n");
133+
118134
destroy_tree(root);
135+
struct node root_binary = create_tree(3, 2);
119136

137+
printf("[#]\nRecursive Inorder DFS for Binary Tree:\n");
138+
dfs_recursive_inorder_btree(root_binary);
139+
printf("\n");
140+
141+
destroy_tree(root_binary);
120142
return 0;
121143
}

Diff for: contents/tree_traversal/code/clisp/tree-traversal.lisp

+12-7
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,41 @@
5858
(defun make-tree (num-rows num-child)
5959
"Creates a simple tree, where every node has 'num-child' children and is 'num-rows' deep."
6060
;; A tree with 0 rows can't be created.
61-
(if (eql num-rows 1)
61+
(if (eql num-rows 0)
6262
(make-node
63-
:data 1
63+
:data 0
6464
:children nil)
6565
(make-node
6666
:data num-rows
6767
:children (loop repeat num-child collect (make-tree (1- num-rows) num-child)))))
6868

6969
;; A tree for testing
70-
(defvar tree (make-tree 3 3))
70+
(defvar tree (make-tree 2 3))
7171

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

7575
;; Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
76+
(format t "[#]~%Recursive DFS:~%")
7677
(dfs-recursive tree)
7778
(format t "~%")
7879

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

83-
;; Should print: 1 2 1 3 1 2 1
84-
(dfs-recursive-inorder-btree binary-tree)
85-
(format t "~%")
86-
8785
;; Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
86+
(format t "[#]~%Stack-based DFS:~%")
8887
(dfs-stack tree)
8988
(format t "~%")
9089

9190
;; Should print: 3 2 2 2 1 1 1 1 1 1 1 1 1
91+
(format t "[#]~%Queue-based BFS:~%")
9292
(bfs-queue tree)
9393
(format t "~%")
94+
95+
;; Should print: 1 2 1 3 1 2 1
96+
(format t "[#]~%Recursive Inorder DFS for Binary Tree:~%")
97+
(dfs-recursive-inorder-btree binary-tree)
98+
(format t "~%")

Diff for: contents/tree_traversal/code/coconut/tree_traversal.coco

+8-13
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,37 @@ def bfs_queue(node is Node):
5050
def create_tree(num_rows, num_child):
5151
"""Creates a simple tree, where every node has
5252
'num_child' children and is 'num_rows' deep."""
53-
if num_rows == 1:
54-
return Node(1, ())
53+
if num_rows == 0:
54+
return Node(0, ())
5555
else:
5656
return Node(num_rows, tuple(create_tree(num_rows-1, num_child)
5757
for _ in range(num_child)))
5858

5959

6060
if __name__ =='__main__':
6161
# A ternary tree for testing
62-
tree = create_tree(3, 3)
62+
tree = create_tree(2, 3)
6363

64-
# Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
65-
print("Recursive DFS:")
64+
print("[#]\nRecursive DFS:")
6665
dfs_recursive(tree)
6766
print()
6867

69-
# Should print: 1 1 1 2 1 1 1 2 1 1 1 2 3
70-
print("Recursive Postorder DFS:")
68+
print("[#]\nRecursive Postorder DFS:")
7169
dfs_recursive_postorder(tree)
7270
print()
7371

74-
# Should print: 3 2 1 1 1 2 1 1 1 2 1 1 1
75-
print("Stack (DFS):")
72+
print("[#]\nStack-based DFS:")
7673
dfs_stack(tree)
7774
print()
7875

79-
# Should print: 3 2 2 2 1 1 1 1 1 1 1 1 1
80-
print("Queue (BFS):")
76+
print("[#]\nQueue-based BFS:")
8177
bfs_queue(tree)
8278
print()
8379

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

87-
# Should print: 1 2 1 3 1 2 1
88-
print("Recursive Inorder Binary Tree:")
83+
print("[#]\nRecursive Inorder DFS for Binary Tree:")
8984
dfs_recursive_inorder_btree(binary_tree)
9085
print()
9186

Diff for: contents/tree_traversal/code/crystal/tree-traversal.cr

+12-38
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ class Node
55
end
66

77
def dfs_recursive(node)
8-
print node.id
8+
print "#{node.id} "
99
node.children.each{ |child| dfs_recursive child }
1010
end
1111

1212
def dfs_recursive_postorder(node)
1313
node.children.each{ |child| dfs_recursive_postorder child }
14-
print node.id
14+
print "#{node.id} "
1515
end
1616

1717
def dfs_recursive_inorder_btree(node)
1818
case node.children.size
1919
when 2
2020
dfs_recursive_inorder_btree node.children[0]
21-
print node.id
21+
print "#{node.id} "
2222
dfs_recursive_inorder_btree node.children[1]
2323
when 1
2424
dfs_recursive_inorder_btree node.children[0]
25-
print node.id
25+
print "#{node.id} "
2626
when 0
27-
print node.id
27+
print "#{node.id} "
2828
else
2929
print "Not a binary tree!"
3030
end
@@ -35,7 +35,7 @@ def dfs_stack(node)
3535

3636
until stack.empty?
3737
temp = stack.pop
38-
print temp.id
38+
print "#{temp.id} "
3939
temp.children.each{ |child| stack.push child }
4040
end
4141
end
@@ -45,7 +45,7 @@ def bfs_queue(node)
4545

4646
until queue.empty?
4747
temp = queue.shift
48-
print temp.id
48+
print "#{temp.id} "
4949
temp.children.each{ |child| queue.push child }
5050
end
5151
end
@@ -60,54 +60,28 @@ def create_tree(levels, num_childs)
6060
Node.new(levels, children)
6161
end
6262

63-
def print_tree(node, depth = [] of String)
64-
puts "(#{node.id})"
65-
depth.push " "
66-
len = node.children.size - 1
67-
68-
(0 .. len).each do |i|
69-
depth.each{|c| print c}
70-
unless i == len
71-
print ""
72-
depth.push ""
73-
print_tree node.children[i], depth
74-
depth.pop
75-
else
76-
print ""
77-
depth.push " "
78-
print_tree node.children[i], depth
79-
depth.pop
80-
end
81-
end
82-
depth.pop
83-
end
84-
8563
def main
86-
puts "Creating Tree"
8764
root = create_tree levels: 2, num_childs: 3
88-
print_tree root
8965

90-
puts "Using recursive DFS:"
66+
puts "[#]\nRecursive DFS:"
9167
dfs_recursive root
9268
puts
9369

94-
puts "Using recursive DFS with post-order traversal:"
70+
puts "[#]\nRecursive Postorder DFS:"
9571
dfs_recursive_postorder root
9672
puts
9773

98-
puts "Using stack-based DFS:"
74+
puts "[#]\nStack-based DFS:"
9975
dfs_stack root
10076
puts
10177

102-
puts "Using queue-based BFS:"
78+
puts "[#]\nQueue-based BFS:"
10379
bfs_queue root
10480
puts
10581

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

110-
puts "Using In-order DFS:"
84+
puts "[#]\nRecursive Inorder DFS for Binary Tree:"
11185
dfs_recursive_inorder_btree root_bin
11286
puts
11387
end

0 commit comments

Comments
 (0)