Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae9217e

Browse files
Binary Search Tree
1 parent 28eafd7 commit ae9217e

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ Data Structures and Algorithms Patterns implemented in Python.
3030
- [x] [Bucket Sort](Sorting-Algo/bucketsort.py)
3131
- [x] [Strings](Strings)
3232
- [x] [KMP (Knuth Morris Pratt) Pattern Searching](Strings/KMP.py)
33+
- [x] [Trees](Trees)
34+
- [x] [Binary Search Tree](Trees/binarysearchtree.py)
3335

‎Trees/binarysearchtree.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
Binary Search Tree
3+
A binary tree is a tree data structure in which each node can have a maximum of 2 children. It means that each node in a binary tree can have either one, or two or no children. Each node in a binary tree contains data and references to its children. Both the children are named as left child and the right child according to their position.
4+
5+
Time Complexity - O(logn)
6+
'''
7+
class BinaryTreeNode:
8+
def __init__(self, data):
9+
self.data = data
10+
self.leftChild = None
11+
self.rightChild = None
12+
13+
14+
def insert(root, newValue):
15+
# if binary search tree is empty, make a new node and declare it as root
16+
if root is None:
17+
root = BinaryTreeNode(newValue)
18+
return root
19+
# binary search tree is not empty, so we will insert it into the tree
20+
# if newValue is less than value of data in root, add it to left subtree and proceed recursively
21+
if newValue < root.data:
22+
root.leftChild = insert(root.leftChild, newValue)
23+
else:
24+
# if newValue is greater than value of data in root, add it to right subtree and proceed recursively
25+
root.rightChild = insert(root.rightChild, newValue)
26+
return root
27+
28+
29+
def search(root, value):
30+
# Condition 1
31+
if root == None:
32+
return False
33+
# Condition 2
34+
elif root.data == value:
35+
return True
36+
# Condition 3
37+
elif root.data < value:
38+
return search(root.rightChild, value)
39+
# Condition 4
40+
else:
41+
return search(root.leftChild, value)
42+
43+
44+
def findLargestElement(root):
45+
# check if binary search tree is empty
46+
if root == None:
47+
return False
48+
# check if current node is rightmost node
49+
elif root.rightChild == None:
50+
return root.data
51+
# check right subtree of current node
52+
else:
53+
return findLargestElement(root.rightChild)
54+
55+
56+
root = insert(None, 15)
57+
58+
59+
def findSmallestElement(root):
60+
# check if binary search tree is empty
61+
if root == None:
62+
return False
63+
# check if current node is leftmost node
64+
elif root.leftChild == None:
65+
return root.data
66+
# check right subtree of current node
67+
else:
68+
return findSmallestElement(root.leftChild)
69+
70+
71+
insert(root, 10)
72+
insert(root, 25)
73+
insert(root, 6)
74+
insert(root, 14)
75+
insert(root, 20)
76+
insert(root, 60)
77+
print(search(root, 14)) # True
78+
print(findLargestElement(root)) # 60
79+
print(findSmallestElement(root)) # 6

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /