-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_binary_search_tree.py
More file actions
41 lines (35 loc) · 943 Bytes
/
simple_binary_search_tree.py
File metadata and controls
41 lines (35 loc) · 943 Bytes
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
"""Simple Binary Search Tree"""
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
class Tree:
def __init__(self, val):
self.root = Node(val)
self.tree_list = []
def insert(self, node, val):
if val <= node.data:
if node.left is None:
node.left = Node(val)
else:
self.insert(node.left, val)
else:
if node.right is None:
node.right = Node(val)
else:
self.insert(node.right, val)
def printInorder(self, node):
if(node is not None):
self.printInorder(node.left)
print(node.data)
self.tree_list.append(node.data)
self.printInorder(node.right)
if __name__ == '__main__':
tree = Tree(1)
tree.insert(tree.root, 2)
tree.insert(tree.root, 3)
tree.insert(tree.root, 4)
tree.insert(tree.root, 5)
tree.printInorder(tree.root)
print (tree.tree_list)