Skip to content

Commit 63ac50b

Browse files
avkaran-singhsinghpratyush
authored andcommitted
Fix #547: Add Binary Search Tree [Python] (#569)
* Create BinarySearchTree.py * Update BinarySearchTree.py * Update BinarySearchTree.py * Update BinarySearchTree.py * Update BinarySearchTree.py * Update BinarySearchTree.py * Update README.md * Update BinarySearchTree.py
1 parent 3fed19c commit 63ac50b

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
5454
| Data Structure | C | CPP | Java | Python | Golang | JavaScript | C# |
5555
|:--------------|:----------------:|:----------------:|:----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|
5656
| [AVL Tree](http://www.geeksforgeeks.org/avl-tree-set-1-insertion)|[:white_check_mark:](avl_tree/avl_tree.c)| |[:white_check_mark:](avl_tree/AvlTree.java) | | | | |
57-
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | | [:white_check_mark:](binary_search_tree/BinarySearchTree.java) | | [:white_check_mark:](binary_search_tree/binary_search_tree.go) | | |
57+
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | | [:white_check_mark:](binary_search_tree/BinarySearchTree.java) | [:white_check_mark:](binary_search_tree/BinarySearchTree.py) | [:white_check_mark:](binary_search_tree/binary_search_tree.go) | | |
5858
| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | [:white_check_mark:](linked_list/LinkedList.cs) |
5959
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | | | [:white_check_mark:](queue/queue.py) | | | |
6060
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | [:white_check_mark:](stack/Stack.cs) |
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
class Node(object):
2+
# Node for Binary search Tree and set the Node
3+
def __init__(self, data):
4+
self.data = data
5+
self.left = None
6+
self.right = None
7+
8+
# insert data in tree
9+
10+
11+
def insert(node, data):
12+
if node is None:
13+
# create a new node
14+
node = Node(data)
15+
return node
16+
else:
17+
# go to the left child
18+
if data <= node.data:
19+
node.left = insert(node.left, data)
20+
# got to the right child
21+
else:
22+
node.right = insert(node.right, data)
23+
return node
24+
25+
# seraching data
26+
27+
28+
def search(node, data):
29+
# if data not present
30+
if node is None:
31+
return None
32+
# go to the left if data is lesser from root
33+
if data < node.data:
34+
node = search(node.left, data)
35+
# go to the right if data is greater from root
36+
elif data > node.data:
37+
node = search(node.right, data)
38+
# data found
39+
elif data == node.data:
40+
return node
41+
return node
42+
43+
# find the minimum
44+
45+
46+
def minright(node):
47+
if node.left is None:
48+
return node
49+
else:
50+
node = minright(node.left)
51+
return node
52+
53+
# delete a node
54+
55+
56+
def delete(root, data):
57+
if root is None:
58+
return root
59+
if data < root.data:
60+
root.left = delete(root.left, data)
61+
elif data > root.data:
62+
root.right = delete(root.right, data)
63+
# data node is found
64+
else:
65+
if root.left is None:
66+
return root.right
67+
elif root.right is None:
68+
return root.left
69+
# if left or right child is Not None find the the minimum node in right child
70+
temp = minright(root.right)
71+
root.data = temp.data
72+
# recursive delete the minimum node in right child
73+
root.right = delete(root.right, temp.data)
74+
return root
75+
76+
# print inorder
77+
78+
79+
def inorder(root):
80+
if root is not None:
81+
inorder(root.left)
82+
print(root.data)
83+
inorder(root.right)
84+
85+
# print preorder
86+
87+
88+
def preorder(root):
89+
if root is not None:
90+
print(root.data)
91+
preorder(root.left)
92+
preorder(root.right)
93+
94+
# print postorder
95+
96+
97+
def postorder(root):
98+
if root is not None:
99+
preorder(root.left)
100+
preorder(root.right)
101+
print(root.data)
102+
103+
# check the method
104+
105+
106+
def main():
107+
root = None
108+
root = insert(root, 5)
109+
root = insert(root, 11)
110+
root = insert(root, 3)
111+
root = insert(root, 1)
112+
root = insert(root, 50)
113+
root = insert(root, 45)
114+
root = insert(root, 30)
115+
root = insert(root, 35)
116+
print("****** InOrder ******")
117+
inorder(root)
118+
print("****** PreOrder ******")
119+
preorder(root)
120+
print("****** PostOrder ******")
121+
postorder(root)
122+
print("***** search Node ******")
123+
temp = search(root, 28)
124+
if temp is not None:
125+
print("node search==> ", temp.data)
126+
else:
127+
print("node is not present")
128+
print("***** delete Node ******")
129+
root = delete(root, 11)
130+
inorder(root)
131+
132+
133+
if __name__ == '__main__':
134+
main()

0 commit comments

Comments
 (0)