-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimalbst.java
140 lines (102 loc) · 3.75 KB
/
Optimalbst.java
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
public class Optimalbst extends minHeap {
public static int[][] costOfBST = new int[len + 1][len + 1];
// Initialisation of cost matrix
public static void diffZero() {
// cost of j-i = 0
for (int k = 0; k < len; k++)
costOfBST[k][k] = frequency[k];
// System.out.println(Arrays.deepToString(costOfBST));
}
public static int sum(int rows, int columns) {
int totalFreq = 0;
for (int i = rows; i <= columns; i++) {
if (i >= frequency.length)
continue;
totalFreq += frequency[i];
}
return totalFreq;
}
public static void diffN() {
// j - i >= N (N > 0)
for (int i = 2; i <= len; i++) {
for (int row = 0; row <= len - i + 1; row++) {
int col = row + i - 1;
// Integer.MAX_VALUE: To initialise maximum value
costOfBST[row][col] = Integer.MAX_VALUE;
// Formula: cost[i][j] = min i<k<=j(cost[i,k-1]+c[k,j])
for (int r = row; r <= col; r++) {
// c = cost when keys[r] becomes root of this subtree
int cost = ((r > row) ? costOfBST[row][r - 1] : 0)
+ ((r < col) ? costOfBST[r + 1][col] : 0) + sum(row, col);
if (cost < costOfBST[row][col])
costOfBST[row][col] = cost;
// if (r > row) {
// cost += costOfBST[row][r - 1];
// }
// else {
// cost += 0;
//
// }
//
// if (r < col){
// cost += costOfBST[r + 1][col];
// }
// else {
// cost += 0
//
// cost += sum(row,col);
}
}
}
// System.out.println(costOfBST);
System.out.println("Cost of Optimal Binary Search Tree: " + costOfBST[0][len - 1]);
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
minHeap obj = new minHeap();
diffZero();
diffN();
obj.insertValues();
System.out.println("Min Heap: ");
for (int i = 1; i < obj.heap.length; i++) {
System.out.print(obj.heap[i] + " ");
}
long endTime = System.currentTimeMillis();
System.out.println("\nExecution Time: " + (endTime - startTime) + " ms");
}
}
class minHeap {
public static int[] nodes = {10, 20, 30};
public static int[] frequency = {34, 8, 50};
public static int len = nodes.length;
public static int[] array = {5, 3, 17, 10, 84, 19, 6, 22, 9};
// array: Original array --> to find heap
public int arrayLen = array.length;
public int[] heap = new int[arrayLen + 1];
public int currentLen = 0;
// Inserting each value in heap from array.
public void insertValues() {
if (arrayLen > 0) {
for (int value : array) {
currentLen++;
int currentValue = currentLen;
heap[currentValue] = value;
swapSmaller(currentValue);
}
}
}
public void swapSmaller(int position) {
int currentPosition = position;
int parentPosition = position / 2;
while (currentPosition > 0 && heap[parentPosition] > heap[currentPosition]) {
swapping(currentPosition, parentPosition);
currentPosition = parentPosition;
parentPosition = parentPosition / 2;
}
}
public void swapping(int first, int second) {
int temporary = heap[first];
heap[first] = heap[second];
heap[second] = temporary;
}
}