1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!env python
"""
# ===============================================================================
#
# FILE: ch_2.py
#
# USAGE: ./ch_2.py
#
# DESCRIPTION: Perl Weekly Challenge #094
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-094/
# Binary Tree to Linked List
#
# AUTHOR: Lubos Kolouch
# CREATED: 01/06/2021 02:39:16 PM
#===============================================================================
"""
class Point:
""" Class to hold each tree node """
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class SumPath:
""" Class to walk the tree """
def __init__(self):
self.total_path = []
def walk_path(self, root):
""" Walk the tree and build the total path """
self.total_path.append(root.value)
if root.left:
self.walk_path(root.left)
if root.right:
self.walk_path(root.right)
return self.total_path
def test_case1():
""" Test 1 """
root = Point(1)
elem1 = Point(2)
pos = elem1
root.left = elem1
pos = root.left
elem1 = Point(3)
elem2 = Point(4)
pos.left = elem1
pos.right = elem2
sumpath = SumPath()
arr = sumpath.walk_path(root=root)
assert arr == [1, 2, 3, 4]
def test_case2():
""" Test 2 """
root = Point(1)
elem1 = Point(2)
elem2 = Point(3)
root.left = elem1
root.right = elem2
pos = root.left
elem1 = Point(4)
elem2 = Point(5)
pos.left = elem1
pos.right = elem2
pos = pos.right
elem1 = Point(6)
elem2 = Point(7)
pos.left = elem1
pos.right = elem2
sumpath = SumPath()
arr = sumpath.walk_path(root=root)
assert arr == [1, 2, 4, 5, 6, 7, 3]
return arr
def main():
""" Print the task from the web """
arr = test_case2()
res_str = ' -> '.join(list(map(str, arr)))
print(res_str)
if __name__ == "__main__":
main()
|