-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.py
More file actions
73 lines (54 loc) · 1.65 KB
/
BinaryTree.py
File metadata and controls
73 lines (54 loc) · 1.65 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class BinaryTree:
def __init__(self,value):
self.value=value
self.left_child =None
self.right_child=None
def insert_left(self,value):
if self.left_child==None:
self.left_child=BinaryTree(value)
else:
new_node=BinaryTree(value)
new_node.left_child=self.left_child
self.left_child=new_node
def insert_right(self,value):
if self.right_child==None:
self.right_child=BinaryTree(value)
else:
new_node=BinaryTree(value)
new_node.right_child=self.right_child
self.right_child=new_node
def pre_order(self):
print(self.value)
if self.left_child:
self.left_child.pre_order()
if self.right_child:
self.right_child.pre_order()
def in_order(self):
if self.left_child:
self.left_child.in_order()
print(self.value)
if self.right_child:
self.right_child.in_order()
def post_order(self):
if self.left_child:
self.left_child.post_order()
if self.right_child:
self.right_child.post_order()
print(self.value)
# a_node=BinaryTree('a')
# a_node.insert_left('b')
# a_node.insert_right('c')
# b_node=a_node.left_child
# b_node.insert_right('d')
# c_node=a_node.right_child
# c_node.insert_left('e')
# c_node.insert_right('f')
# d_node=b_node.right_child
# e_node=c_node.left_child
# f_node=c_node.right_child
# print(a_node.value)
# print(b_node.value)
# print(c_node.value)
# print(d_node.value)
# print(e_node.value)
# print(f_node.value)