forked from its-Kumar/Python_Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.py
90 lines (76 loc) · 2.61 KB
/
binary_search_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
class Node:
def __init__(self, data=None) -> None:
"""Node to store data
Args:
data (Any, optional): Data to be added. Defaults to None.
"""
self.data = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self) -> None:
"""BinarySearch Tree Data Structure"""
self.root = None
def _insert_recursive(self, data, node):
if data["id"] < node.data["id"]:
if node.left is None:
node.left = Node(data)
else:
self._insert_recursive(data, node.left)
elif data["id"] > node.data["id"]:
if node.right is None:
node.right = Node(data)
else:
self._insert_recursive(data, node.right)
else:
return
def insert(self, data):
"""Insert data in `BinarySearchTree`
Args:
data (Any): Data to be added
Example:
>>> bst = BinarySearchTree()
>>> bst.insert({"id" : 1})
>>> bst.insert({"id" : 3})
>>> bst.insert({"id" : 2})
>>> bst.root.data
{'id': 1}
"""
if self.root is None:
self.root = Node(data)
else:
self._insert_recursive(data, self.root)
def _search_recursive(self, blog_post_id, node):
if node.left == None and node.right == None:
return False
if blog_post_id == node.data["id"]:
return node.data
if blog_post_id < node.data["id"] and node.left is not None:
if blog_post_id == node.left.data["id"]:
return node.left.data
return self._search_recursive(blog_post_id, node.left)
if blog_post_id > node.data["id"] and node.right is not None:
if blog_post_id == node.right.data["id"]:
return node.right.data
return self._search_recursive(blog_post_id, node.right)
return False
def search(self, blog_post_id):
"""Search data in `BinarySearchTree`
Args:
blog_post_id (int): id to search
Returns:
Any: searched data
Example:
>>> bst = BinarySearchTree()
>>> bst.insert({"id" : 1})
>>> bst.insert({"id" : 3})
>>> bst.insert({"id" : 2})
>>> bst.search(3)
{'id': 3}
>>> bst.search(4)
False
"""
blog_post_id = int(blog_post_id)
if self.root is None:
return False
return self._search_recursive(blog_post_id, self.root)