-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTree2.py
42 lines (34 loc) · 1.03 KB
/
SearchTree2.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
from BinaryTreePrinter import BinaryTreePrinter
class Node:
def __init__(self, val):
self.left: Node = None
self.right: Node = None
self.val = val
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, val):
if self.root is None:
self.root = Node(val)
else:
self.__insert(self.root, val)
def __insert(self, node: Node, val):
if node.val == val:
return
elif val < node.val:
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 __str__(self):
tree_print = BinaryTreePrinter()
return tree_print.get_tree_string(self.root)
bst = BinarySearchTree()
for i in [1,2,5,3,4,6]:
bst.insert(i)
print(bst)