Skip to content

Commit 4c973b4

Browse files
committed
resuming dsa
1 parent 4dab746 commit 4c973b4

8 files changed

+202
-1
lines changed

CheckBalanced.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Created by Elshad Karimov
2+
# Copyright © AppMillers. All rights reserved.
3+
4+
# Balanced Tree
5+
6+
def isBalancedHelper(root):
7+
if root is None:
8+
return 0
9+
leftHeight = isBalancedHelper(root.left)
10+
if leftHeight == -1:
11+
return -1
12+
rightHeight = isBalancedHelper(root.right)
13+
if rightHeight == -1:
14+
return -1
15+
if abs(leftHeight-rightHeight)>1:
16+
return -1
17+
18+
return max(leftHeight, rightHeight) + 1
19+
20+
def isBalanced(root):
21+
return isBalancedHelper(root) > -1
22+
23+
class Node():
24+
def __init__(self, value, left=None, right=None):
25+
self.value = value
26+
self.left = left
27+
self.right = right
28+
29+
N1 = Node("N1")
30+
N2 = Node("N2")
31+
N3 = Node("N3")
32+
N4 = Node("N4")
33+
N5 = Node("N5")
34+
N6 = Node("N6")
35+
N1.left = N2
36+
N1.right = N3
37+
N2.left = N4
38+
N2.right = N5
39+
N3.right = N6
40+
41+
print(isBalanced(N1))

DSA-Python.pdf

6 MB
Binary file not shown.
Binary file not shown.

MinCost2D.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Created by Elshad Karimov
2+
# Copyright © AppMillers. All rights reserved.
3+
4+
# Minimum Cost in 2D array
5+
6+
def findMinCost(twoDArray, row, col):
7+
if row == -1 or col == -1:
8+
return float('inf')
9+
elif row == 0 and col == 0:
10+
return twoDArray[0][0]
11+
else:
12+
op1 = findMinCost(twoDArray, row-1, col)
13+
op2 = findMinCost(twoDArray, row, col-1)
14+
return twoDArray[row][col] + min(op1,op2)
15+
16+
TwoDList = [[4,7,8,6,4],
17+
[6,7,3,9,2],
18+
[3,8,1,2,4],
19+
[7,1,7,3,7],
20+
[2,9,8,9,3]
21+
]
22+
23+
print(findMinCost(TwoDList, 4,4))

NumberofPaths.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Created by Elshad Karimov
2+
# Copyright © AppMillers. All rights reserved.
3+
4+
# Number of paths to reach the last cell with given cost in 2D array
5+
6+
def numberOfPaths(twoDArray, row, col, cost):
7+
if cost < 0:
8+
return 0
9+
elif row == 0 and col == 0:
10+
if twoDArray[0][0] - cost == 0:
11+
return 1
12+
else:
13+
return 0
14+
elif row == 0:
15+
return numberOfPaths(twoDArray, 0, col-1, cost - twoDArray[row][col] )
16+
elif col == 0:
17+
return numberOfPaths(twoDArray, row-1, 0, cost - twoDArray[row][col] )
18+
else:
19+
op1 = numberOfPaths(twoDArray, row -1, col, cost - twoDArray[row][col] )
20+
op2 = numberOfPaths(twoDArray, row, col-1, cost - twoDArray[row][col] )
21+
return op1 + op2
22+
23+
24+
TwoDList = [[4,7,1,6],
25+
[5,7,3,9],
26+
[3,2,1,2],
27+
[7,1,6,3]
28+
]
29+
30+
print(numberOfPaths(TwoDList, 3,3, 25))

Successor.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Created by Elshad Karimov
2+
# Copyright © AppMillers. All rights reserved.
3+
4+
# Validate BST
5+
6+
class Node:
7+
def __init__(self, key):
8+
self.data = key
9+
self.left = None
10+
self.right = None
11+
12+
def minValue(node):
13+
current = node
14+
while (current is not None):
15+
if current.left is None:
16+
break
17+
current = current.left
18+
return current
19+
20+
def inOrderSuccessor(root, n):
21+
if n.right is not None:
22+
return minValue(n.right)
23+
24+
p = n.parent
25+
while p is not None:
26+
if n != p.right:
27+
break
28+
n = p
29+
p = p.parent
30+
return p
31+
32+
def insert(node, data):
33+
if node is None:
34+
return Node(data)
35+
else:
36+
if data <= node.data:
37+
temp = insert(node.left, data)
38+
node.left = temp
39+
temp.parent = node
40+
else:
41+
temp = insert(node.right, data)
42+
node.right = temp
43+
temp.parent = node
44+
return node
45+
46+
root = None
47+
root = insert(root, 4)
48+
root = insert(root, 2)
49+
root = insert(root, 8)
50+
root = insert(root, 1)
51+
root = insert(root, 3)
52+
root = insert(root, 5)
53+
root = insert(root, 9)
54+
55+
temp = root.left #2
56+
57+
successor = inOrderSuccessor(root, temp)
58+
print(successor.data)
59+
60+
if successor is not None:
61+
print("Inorder successor of %d is %d" %(temp.data, successor.data))
62+
else:
63+
print("Inorder successor does not exist")
64+
65+
66+

ValidateBST.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Created by Elshad Karimov
2+
# Copyright © AppMillers. All rights reserved.
3+
4+
# Validate BST
5+
6+
class TreeNode:
7+
def __init__(self, value):
8+
self.val = value
9+
self.left = None
10+
self.right = None
11+
12+
def helper(node, minValue = float('-inf'), maxValue = float('inf')):
13+
if not node:
14+
return True
15+
val = node.val
16+
if val <= minValue or val >= maxValue:
17+
return False
18+
19+
if not helper(node.right, val, maxValue):
20+
return False
21+
22+
if not helper(node.left, minValue, val):
23+
return False
24+
25+
return True
26+
27+
def isValidBST(root):
28+
return helper(root)
29+
30+
root1 = TreeNode(2)
31+
root1.left = TreeNode(1)
32+
root1.right = TreeNode(4)
33+
34+
print(isValidBST(root1))
35+
36+
root2 = TreeNode(4)
37+
root2.left = TreeNode(1)
38+
root2.right = TreeNode(3)
39+
40+
print(isValidBST(root2))

python-in-built DSA/linked_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def add_after(self, data, value):
5454
new_node = Node(data)
5555
new_node.ref = temp.ref
5656
temp.ref = new_node
57-
57+
def add_before(self, data, value):
58+
pass
5859

5960
obj1 = LinkedList()
6061
obj1.add_starting(10)

0 commit comments

Comments
 (0)