-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_ordered_list.py
More file actions
55 lines (43 loc) · 1.23 KB
/
Copy pathpre_ordered_list.py
File metadata and controls
55 lines (43 loc) · 1.23 KB
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
pre_ordered_list = []
class BinaryTree:
def __init__(self, root_data):
self.data = root_data
self.left_child = None
self.right_child = None
def preorder(self, root):
left = root.get_left_child()
right = root.get_right_child()
pre_ordered_list.append(root.data)
if left != None:
self.preorder(left)
if right != None:
self.preorder(right)
return
def get_right_child(self):
return self.right_child
def get_left_child(self):
return self.left_child
def set_root_val(self, obj):
self.data = obj
def get_root_val(self):
return self.data
print(pre_ordered_list)
test_tree = BinaryTree(1)
left1 = BinaryTree(2)
right1 = BinaryTree(3)
test_tree.right_child = right1
test_tree.left_child = left1
print(test_tree.get_root_val())
print(test_tree.get_left_child().get_root_val())
print(test_tree.get_right_child())
left2 = BinaryTree(4)
left1.left_child = left2
right2 = BinaryTree(5)
left1.right_child = right2
left3 = BinaryTree(6)
right1.left_child = left3
right3 = BinaryTree(7)
right1.right_child = right3
print(test_tree.get_left_child().get_root_val())
test_tree.preorder(test_tree)
print(pre_ordered_list)