Skip to content

Commit 7f7872c

Browse files
committed
new questions
0 parents  commit 7f7872c

35 files changed

+1187
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
3+
def largest(arr):
4+
l = -sys.maxsize
5+
for i in arr:
6+
if i > l:
7+
l = i
8+
print(l)
9+
10+
arr = [1,2,3,4,5,6,7,8,2,4,5,7,34,56,23,1234,134,57,67,345]
11+
12+
largest(arr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
3+
def three_largest(arr):
4+
first = second = third = -sys.maxsize
5+
for i in arr:
6+
if i > first:
7+
third = second
8+
second = first
9+
first = i
10+
elif i > second:
11+
third = second
12+
second = i
13+
elif i > third:
14+
third = i
15+
print(first, second, third)
16+
17+
18+
arr = [10,45,3,7,4,6,8,9,4,6,4,23,45,56,47,25,34,67,634]
19+
three_largest(arr)
20+
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import collections
2+
3+
class Node():
4+
5+
def __init__(self, val):
6+
self.val = val
7+
self.left = None
8+
self.right = None
9+
10+
11+
def mean(arr):
12+
m = 0
13+
for x in arr:
14+
m += x
15+
return m / len(arr)
16+
17+
18+
def bfs(root):
19+
if not root:
20+
return
21+
queue = collections.deque([root])
22+
result = []
23+
while queue:
24+
next_queue = collections.deque()
25+
for node in queue:
26+
if node.left:
27+
next_queue.append(node.left)
28+
if node.right:
29+
next_queue.append(node.right)
30+
result.append(mean(queue))
31+
queue = next_queue
32+
return result
33+

data_structures/bst/bfs.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import collections
2+
3+
class Node():
4+
5+
def __init__(self, val):
6+
self.val = val
7+
self.left = None
8+
self.right = None
9+
10+
11+
def bfs(root):
12+
if not root:
13+
return
14+
15+
queue = collections.deque([root])
16+
17+
while queue:
18+
temp = queue.popleft()
19+
print(temp.val)
20+
if temp.right:
21+
queue.append(temp.right)
22+
if temp.left:
23+
queue.append(temp.left)
24+
25+
26+
root = Node(3)
27+
root.right = Node(4)
28+
root.left = Node(2)
29+
30+
root.left.left = Node(1)
31+
root.left.right = Node(2.5)
32+
33+
root.right.left = Node(3.5)
34+
root.right.right = Node(5)
35+
36+
bfs(root)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Node():
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
class BST():
10+
11+
def __init__(self):
12+
self.root = Node(None)
13+
14+
15+
def insert(self, new_data):
16+
if self.root is None:
17+
self.root = Node(new_data)
18+
else:
19+
if self.root.val < new_data:
20+
21+
self.insert(self.root.right, new_data)
22+
else:
23+
self.insert(self.root.left, new_data)
24+
25+
26+
def inorder(self):
27+
if self.root:
28+
self.inorder(self.root.left)
29+
print(self.root.val)
30+
self.inorder(self.root.right)
31+
32+
33+
if __name__ == '__main__':
34+
tree = BST()
35+
tree.insert(5)
36+
tree.insert(4)
37+
tree.insert(7)
38+
tree.inorder()

data_structures/bst/bt_to_bst.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Node:
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
def store_inorder(root, inorder):
10+
if root is None:
11+
return
12+
13+
store_inorder(root.left, inorder)
14+
inorder.append(root.data)
15+
store_inorder(root.right, inorder)
16+
17+
18+
def count_nodes(root):
19+
if root is None:
20+
return 0
21+
return count_nodes(root.left) + count_nodes(root.right) + 1
22+
23+
24+
def array_to_bst(arr, root):
25+
if root is None:
26+
return
27+
array_to_bst(arr, root.left)
28+
root.data = arr[0]
29+
arr.pop(0)
30+
array_to_bst(arr, root.right)
31+
32+
33+
def bt_to_bst(root):
34+
if root is None:
35+
return
36+
37+
n = count_nodes(root)
38+
arr = []
39+
store_inorder(root, arr)
40+
arr.sort()
41+
array_to_bst(arr, root)
42+

data_structures/bst/ceil.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Node():
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
def ceil(root, key):
10+
if not root:
11+
return -1
12+
if root.val == key:
13+
return root.val
14+
if root.val < key:
15+
return ceil(root.right, key)
16+
val = ceil(root.left, key)
17+
return val if val >= key else root.key
18+
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Node():
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
def check(root1, root2):
10+
if root1 == None:
11+
return True
12+
if root2 == None:
13+
return False
14+
if are_identical(root1, root2):
15+
return True
16+
return (check(root1.left, root2) or check(root1.right, root2))
17+
18+
19+
def are_identical(root1, root2):
20+
if root1 == None and root2 == None:
21+
return True
22+
if root1 == None or root2 == None:
23+
return False
24+
return (root1.val == root2.val and are_identical(root1.left, root2.left) and are_identical(root1.right, root2.right))
25+
26+
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
MAX = sys.maxsize
4+
MIN = -sys.maxsize
5+
6+
class Node:
7+
8+
def __init__(self, val):
9+
self.val = val
10+
self.left = None
11+
self.right = None
12+
13+
14+
def check_BST(root, min, max):
15+
if root is None:
16+
return True
17+
18+
if root.val < min or root.val > max:
19+
return False
20+
21+
return (check_BST(root.left, min, root.val - 1) and check_BST(root.right, root.val + 1, max))
22+
23+
root = Node(5)
24+
root.left = Node(4)
25+
root.right = Node(7)
26+
27+
print(check_BST(root, MIN, MAX))
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
3+
class Node():
4+
5+
def __init__(self, val):
6+
self.val = val
7+
self.left = None
8+
self.right = None
9+
10+
11+
def closest_element(root, k):
12+
if not root:
13+
return None
14+
curr = root
15+
min_diff = sys.maxsize
16+
element = None
17+
while curr:
18+
if curr.val == k:
19+
return curr.val
20+
if abs(curr.val - k) < min_diff:
21+
min_diff = abs(curr.val - k)
22+
element = curr.val
23+
24+
if curr.val > k:
25+
curr = curr.left
26+
else:
27+
curr = curr.right
28+
return element
29+
30+
31+
root = Node(5)
32+
root.left = Node(3)
33+
root.right = Node(10)
34+
35+
root.left.left = Node(2)
36+
root.left.right = Node(4)
37+
38+
root.right.right = Node(20)
39+
root.right.left = Node(8)
40+
41+
print(closest_element(root, 18))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Node():
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
def increasing_bst(root):
10+
def inorder(node):
11+
if node:
12+
yield from inorder(node.left)
13+
yield node.val
14+
yield from inorder(node.right)
15+
ans = curr = Node(None)
16+
for v in inorder(root):
17+
curr.right = Node(v)
18+
curr = curr.right
19+
return ans.right

data_structures/bst/deletion.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Node:
2+
3+
def __init__(self, val):
4+
self.val = val
5+
self.left = None
6+
self.right = None
7+
8+
9+
def inorder(root):
10+
if not root:
11+
return None
12+
stack = []
13+
while True:
14+
if root:
15+
stack.append(root)
16+
root = root.left
17+
else:
18+
if not stack:
19+
break
20+
root = stack.pop()
21+
print(root.val, end=" ")
22+
root = root.right()
23+
24+
25+
def min_value_node(root):
26+
curr = root
27+
while curr.left:
28+
curr = curr.left
29+
return curr
30+
31+
32+
def delete(root, val):
33+
if not root:
34+
return root
35+
36+
if val < root.val:
37+
root.left = delete(root.left, val)
38+
39+
elif val > root.val:
40+
root.right = delete(root.right, val)
41+
42+
else:
43+
# Root with one child or no child
44+
if root.left is None:
45+
temp = root.right
46+
root = None
47+
return temp
48+
49+
elif root.right is None:
50+
temp = root.left
51+
root = None
52+
return temp
53+
54+
temp = min_value_node(root.right)
55+
root.val = temp.val
56+
root.right = delete(root.right, temp.val)
57+
58+
return root
59+
60+
61+
62+

0 commit comments

Comments
 (0)