-
Notifications
You must be signed in to change notification settings - Fork 6
/
binary_tree.py
142 lines (129 loc) · 3.83 KB
/
binary_tree.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Node:
def __init__(self, val = None) -> None:
self.data = val
self.left = None
self.right = None
class BinaryTree:
def __init__(self) -> None:
self.root = None
def _createTree(self, node):
value = int(input())
if value != -999:
if not node:
node = Node(value)
else:
node.data = value
print(f'Enter value for left of {node.data} :')
self._createTree(node.left)
print(f'Enter value for right of {node.data} :')
self._createTree(node.right)
def _inorder(self, node):
if node is not None:
self._inorder(node.left)
print(node.data, end = ' ')
self._inorder(node.right)
def _printLevel(self, root, level, space = ' '):
if not root:
return
if level == 1:
print(root.data, end = space)
elif level > 1:
self._printLevel(root.left, level - 1, space)
self._printLevel(root.right, level - 1, space)
def _height(self, root):
if not root:
return 0
lh = self._height(root.left)
rh = self._height(root.right)
if lh > rh:
return lh + 1
return rh + 1
def createTree(self):
print('Enter value for root node :')
self._createTree(self.root)
def height(self):
return self._height(self.root)
def printLevel(self, level, space = ' '):
self._printLevel(self.root, level, space)
def levelOrder(self):
space = ' '
h = self.height()
for i in range(1, h + 1):
self._printLevel(self.root, i, space)
print('\n')
def inorder(self):
self._inorder(self.root)
def insert(self, val):
if not self.root:
self.root = Node(val)
return
l = []
l.append(self.root)
while(len(l)):
temp = l[0]
l.pop(0)
if not temp.left:
temp.left = Node(val)
break
else:
l.append(temp.left)
if not temp.right:
temp.right = Node(val)
break
else:
l.append(temp.right)
def __str__(self) -> str:
def storeLevel(root, level, space = ' '):
nonlocal final_str
if not root:
return
if level == 1:
final_str += str(root.data) + space
elif level > 1:
storeLevel(root.left, level - 1, space)
storeLevel(root.right, level - 1, space)
final_str = ''
h = self.height()
leaves = 2**h/2
half_leaves = leaves//2
space = ''
for i in range(1, h + 1):
if i == h:
space = '\t'
elif( i == 1):
space = '\t' * int((half_leaves - 1)) + ' '
final_str += space
else:
space = '\t' * int((half_leaves//i - 1)) + ' '
final_str += space
space = '\t' * int((half_leaves//(i - 1))) + ' '
storeLevel(self.root, i, space)
final_str += '\n\n'
return final_str
#
if __name__ == '__main__':
tr = BinaryTree()
# tr.createTree()
# tr.root = Node(1)
# tr.root.left = Node(2)
# tr.root.right = Node(3)
# tr.root.left.left = Node(4)
# tr.root.left.right = Node(5)
# tr.root.right.left = Node(6)
# tr.root.right.right = Node(7)
tr.insert(1)
tr.insert(2)
tr.insert(3)
tr.insert(4)
tr.insert(5)
tr.insert(6)
tr.insert(7)
tr.insert(8)
tr.insert(9)
tr.insert(10)
tr.insert(11)
tr.insert(12)
tr.insert(13)
tr.insert(14)
tr.insert(15)
print(tr)