-
Notifications
You must be signed in to change notification settings - Fork 17
/
binarysearchtree.py
79 lines (66 loc) · 2.37 KB
/
binarysearchtree.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
'''
Binary Search Tree
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.
Time Complexity - O(logn)
'''
class BinaryTreeNode:
def __init__(self, data):
self.data = data
self.leftChild = None
self.rightChild = None
def insert(root, newValue):
# if binary search tree is empty, make a new node and declare it as root
if root is None:
root = BinaryTreeNode(newValue)
return root
# binary search tree is not empty, so we will insert it into the tree
# if newValue is less than value of data in root, add it to left subtree and proceed recursively
if newValue < root.data:
root.leftChild = insert(root.leftChild, newValue)
else:
# if newValue is greater than value of data in root, add it to right subtree and proceed recursively
root.rightChild = insert(root.rightChild, newValue)
return root
def search(root, value):
# Condition 1
if root == None:
return False
# Condition 2
elif root.data == value:
return True
# Condition 3
elif root.data < value:
return search(root.rightChild, value)
# Condition 4
else:
return search(root.leftChild, value)
def findLargestElement(root):
# check if binary search tree is empty
if root == None:
return False
# check if current node is rightmost node
elif root.rightChild == None:
return root.data
# check right subtree of current node
else:
return findLargestElement(root.rightChild)
root = insert(None, 15)
def findSmallestElement(root):
# check if binary search tree is empty
if root == None:
return False
# check if current node is leftmost node
elif root.leftChild == None:
return root.data
# check right subtree of current node
else:
return findSmallestElement(root.leftChild)
insert(root, 10)
insert(root, 25)
insert(root, 6)
insert(root, 14)
insert(root, 20)
insert(root, 60)
print(search(root, 14)) # True
print(findLargestElement(root)) # 60
print(findSmallestElement(root)) # 6