Skip to content

Commit 2826032

Browse files
Moved Tree traversal to a combined py2/py3 implementation
1 parent 3ef05bf commit 2826032

File tree

3 files changed

+19
-110
lines changed

3 files changed

+19
-110
lines changed

chapters/tree_traversal/code/python2/Tree_example.py renamed to chapters/tree_traversal/code/python/Tree_example.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import print_function
2+
3+
14
class Node:
25

36
def __init__(self):
47
self.data = None
58
self.children = []
69

7-
810
def create_tree(node, num_row, num_child):
911
node.data = num_row
1012

@@ -17,7 +19,7 @@ def create_tree(node, num_row, num_child):
1719

1820
def DFS_recursive(node):
1921
if len(node.children) > 0:
20-
print node.data
22+
print(node.data)
2123

2224
for child in node.children:
2325
DFS_recursive(child)
@@ -29,7 +31,7 @@ def DFS_stack(node):
2931
temp = None
3032

3133
while len(stack) > 0:
32-
print stack[-1].data
34+
print(stack[-1].data)
3335
temp = stack.pop()
3436

3537
for child in temp.children:
@@ -42,7 +44,7 @@ def BFS_queue(node):
4244
temp = None
4345

4446
while len(queue) > 0:
45-
print queue[0].data
47+
print(queue[0].data)
4648
temp = queue.pop(0)
4749

4850
for child in temp.children:
@@ -51,13 +53,13 @@ def BFS_queue(node):
5153
def main():
5254
tree = create_tree(Node(), 3, 3)
5355

54-
print "Recursive:"
56+
print("Recursive:")
5557
DFS_recursive(tree)
5658

57-
print "Stack:"
59+
print("Stack:")
5860
DFS_stack(tree)
5961

60-
print "Queue:"
62+
print("Queue:")
6163
BFS_queue(tree)
6264

6365
main()

chapters/tree_traversal/code/python3/Tree_example.py

Lines changed: 0 additions & 84 deletions
This file was deleted.

chapters/tree_traversal/tree_traversal.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ Trees are naturally recursive data structures, and because of this, we cannot ac
1414
{% sample lang="js" %}
1515
This has not been implemented in your chosen language, so here is the Julia code
1616
[import:3-7, lang:"julia"](code/julia/Tree.jl)
17-
{% sample lang="py2" %}
18-
[import:1-5, lang:"python"](code/python2/Tree_example.py)
19-
{% sample lang="py3" %}
20-
[import:5-10, lang:"python"](code/python3/Tree_example.py)
17+
{% sample lang="py" %}
18+
[import:4-8, lang:"python"](code/python/Tree_example.py)
2119
{% sample lang="scratch" %}
2220
This has not been implemented in your chosen language, so here is the Julia code
2321
[import:3-7, lang:"julia"](code/julia/Tree.jl)
@@ -41,7 +39,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
4139
{% sample lang="js" %}
4240
[import:15-23, lang:"javascript"](code/javascript/Tree_example.js)
4341
{% sample lang="py2" %}
44-
[import:8-16, lang:"python"](code/python2/Tree_example.py)
42+
[import:10-18, lang:"python"](code/python/Tree_example.py)
4543
{% sample lang="py3" %}
4644
This has not been implemented in your chosen language, so here is the Julia code
4745
[import:9-16, lang:"julia"](code/julia/Tree.jl)
@@ -156,10 +154,8 @@ In code, it looks like this:
156154
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
157155
{% sample lang="js" %}
158156
[import:25-40, lang:"javascript"](code/javascript/Tree_example.js)
159-
{% sample lang="py2" %}
160-
[import:25-36, lang:"python"](code/python2/Tree_example.py)
161-
{% sample lang="py3" %}
162-
[import:31-45, lang:"python"](code/python3/Tree_example.py)
157+
{% sample lang="py" %}
158+
[import:27-38, lang:"python"](code/python/Tree_example.py)
163159
{% sample lang="scratch" %}
164160
This has not been implemented in your chosen language, so here is the Julia code
165161
[import:45-56, lang:"julia"](code/julia/Tree.jl)
@@ -189,10 +185,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
189185
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
190186
{% sample lang="js" %}
191187
[import:42-57, lang:"javascript"](code/javascript/Tree_example.js)
192-
{% sample lang="py2" %}
193-
[import:38-49, lang:"python"](code/python2/Tree_example.py)
194-
{% sample lang="py3" %}
195-
[import:48-62, lang:"python"](code/python3/Tree_example.py)
188+
{% sample lang="py" %}
189+
[import:40-51, lang:"python"](code/python/Tree_example.py)
196190
{% sample lang="scratch" %}
197191
This has not been implemented in your chosen language, so here is the Julia code
198192
[import:58-69, lang:"julia"](code/julia/Tree.jl)
@@ -225,12 +219,9 @@ tree_traversal.c
225219
{% sample lang="js" %}
226220
### JavaScript
227221
[import, lang:"javascript"](code/javascript/Tree_example.js)
228-
{% sample lang="py2" %}
229-
### Python 2
230-
[import, lang:"python"](code/python2/Tree_example.py)
231-
{% sample lang="py3" %}
232-
### Python 3
233-
[import, lang:"python"](code/python3/Tree_example.py)
222+
{% sample lang="py" %}
223+
### Python
224+
[import, lang:"python"](code/python/Tree_example.py)
234225
{% sample lang="scratch" %}
235226
### Scratch
236227
![scratch tree](code/scratch/scratch_tree.png)

0 commit comments

Comments
 (0)