-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_binary_tree.py
51 lines (43 loc) · 1.09 KB
/
new_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
__author__ = 'Gaurav'
class BSTnode:
def __init__(self, content):
self.data = content
self.l_child = None
self.r_child = None
def binary_insert(root, node):
if root is None:
root = node
else:
if root.data > node.data:
if root.l_child == None:
root.l_child = node
else:
binary_insert(root.l_child, node)
else:
if root.r_child == None:
root.r_child = node
else:
binary_insert(root.r_child, node)
def in_order_print(root):
if not root:
return
in_order_print(root.l_child)
print root.data
in_order_print(root.r_child)
def pre_order_print(root):
if not root:
return
print root.data
pre_order_print(root.l_child)
pre_order_print(root.r_child)
def main():
r = BSTnode(3)
binary_insert(r, BSTnode(7))
binary_insert(r, BSTnode(1))
binary_insert(r, BSTnode(5))
print "in order:"
in_order_print(r)
print "pre order"
pre_order_print(r)
if __name__ == '__main__':
main()