Skip to content

Commit 50e01ba

Browse files
authored
Add files via upload
1 parent 2ef381f commit 50e01ba

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

binary_tree.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class tree:
2+
def __init__(self, data):
3+
self.left = None
4+
self.data = data
5+
self.right = None
6+
def node(self, data):
7+
if self.data:
8+
if self.data>data:
9+
if self.left is None:
10+
self.left = tree(data)
11+
else:
12+
self.left.node(data)
13+
elif self.data<data:
14+
if self.right is None:
15+
self.right = tree(data)
16+
else:
17+
self.right.node(data)
18+
else:
19+
self.data = data
20+
21+
def show1(self):
22+
if self.data:
23+
print(self.data)
24+
if self.left:
25+
self.left.show1()
26+
if self.right:
27+
self.right.show1()
28+
else:
29+
return -1
30+
def show2(self):
31+
if self.data:
32+
if self.left:
33+
self.left.show2()
34+
if self.right:
35+
self.right.show2()
36+
print(self.data)
37+
else:
38+
return -1
39+
def show3(self):
40+
if self.data:
41+
if self.left:
42+
self.left.show3()
43+
print(self.data)
44+
if self.right:
45+
self.right.show3()
46+
else:
47+
return -1
48+
root = tree(10)
49+
root.node(50)
50+
root.node(30)
51+
root.node(80)
52+
root.node(5)
53+
root.node(2)
54+
root.node(7)
55+
print("Inorder")
56+
root.show3()
57+
print("Postorder")
58+
root.show2()
59+
print("Preorder")
60+
root.show1()

0 commit comments

Comments
 (0)