I need to create a tree from an array after processing the elements by following algorithm:
1. let A[n] be the array
2. while lenght(A) > 1
3. for i = 0 to lenght(A)-2
4. new_A[i] = max(A[i], A[i+1]) + 1
5. end for
6. [minValue, minIndex] = someFunction(new_A) // someFunction returns the minimum value and index of the same
7. delete A[minIndex] and A[minIndex + 1] from A and insert minValue in that place // basically A[minIndex] and A[minIndex + 1] are being replaced by minValue
8. // This is how the length of A is being decreased by 1 in each iteration
9. end while
Example:
+--------------+----------------------+-----------------+---------------+
| iteration No | Array A | Array new_A | Remarks |
+--------------+----------------------+-----------------+---------------+
| 1 | 5-9-3-2-1-6-8-3 |10-10-4-3-7-9-9 | minValue = 3 |
| | | | minIndex = 3 |
+--------------+----------------------+-----------------+---------------+
| 2 | 5-9-3-3-6-8-3 |10-10-4-7-9-9 | minValue = 4 |
| | | | minIndex = 2 |
+--------------+----------------------+-----------------+---------------+
| 3 | 5-9-4-6-8-3 |10-10-7-9-9 | minValue = 7 |
| | | | minIndex = 2 |
+--------------+----------------------+-----------------+---------------+
| 4 | 5-9-7-8-3 |10-10-9-9 | minValue = 9 |
| | | | minIndex = 2 |
+--------------+----------------------+-----------------+---------------+
| 5 | 5-9-9-3 |10-10-10 | minValue = 10 |
| | | | minIndex = 0 |
+--------------+----------------------+-----------------+---------------+
| 6 | 10-9-3 |11-10 | minValue = 10 |
| | | | minIndex = 1 |
+--------------+----------------------+-----------------+---------------+
| 7 | 10-10 |11 | minValue = 11 |
| | | | minIndex = 0 |
+--------------+----------------------+-----------------+---------------+
| 8 | 11 |-- | -- |
+--------------+----------------------+-----------------+---------------+
Until this everything is okay. But I need to represent this in a tree. The resulting tree will be as follows:
iteration# 8 11
/ \
/ \
iteration# 7 / \------- 10
/ / \
iteration# 6 10 / \
/ \ / \
iteration# 5 | | 9 \
| | / \ \
iteration# 4 | | 7 \ \
| | / \ \ |
iteration# 3 | | 4 \ \ |
| | / \ \ \ |
iteration# 2 | | / 3 \ \ |
| | / / \ \ \ |
iteration# 1 5 9 3 2 1 6 8 3
The logic is to get the minValue
and make it a root and make the corresponding array elements (from which the minValue
came) children. This is how we can grow the tree. It can be somehow called a binary tree as each node has exactly 2 children. The problem I am facing is that if I take previous root as one of the children I might not get the exact answer. Because see in the iteration 5, the minValue
I am getting is not the contribution of previous root. So now my whole previously made tree might get lost. Is there anything I can do to get the whole tree structure? I am using JAVA.
EDIT: Is there any way to create a tree from the bottom (i.e. the leaf node) in JAVA. Like first create a parent with two children, then put this parent as a child of another node and gradually reach to the top. So taking into account above example can anyone help me write the code. Below is my code, I am just not able to create the tree from the leaf.
public class Main {
private static int GATE_COST = 1;
private static BinaryTree bt;
private static float num;
public static void main(String[] args) throws IOException {
File filePin = new File("C:/somePath/files/pin.txt");
BufferedReader buffer = new BufferedReader(new FileReader(filePin));
String line = buffer.readLine();
int lenData = Integer.parseInt(line.trim());
float[] data = new float[lenData];
for (int i = 0; i < lenData; i++) {
line = buffer.readLine();
data[i] = Float.parseFloat(line.trim());
}
bt = new BinaryTree();
num = sysnthesis(data);
System.out.println("\nArrival: " + num);
}
private static float sysnthesis(float[] data) {
while (data.length > 1) {
for (int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
float[] cost = getCost(data);
float minValue = cost[0];
int minIndex = 0;
for (int i = 0; i < cost.length; i++) {
if (cost[i] < minValue) {
minValue = cost[i];
minIndex = i;
}
}
createTree(data, minValue, minIndex); // I am not able to create its body
data = getNewData(data, minValue, minIndex);
System.out.print("\n");
}
System.out.print(data[0]);
return data[0];
}
private static float[] getCost(float[] data) {
float[] cost = new float[data.length - 1];
for (int i = 0; i < data.length - 1; i++) {
cost[i] = Math.max(data[i], data[i+1]) + GATE_COST;
}
return cost;
}
private static float[] getNewData(float[] data, float minValue, int minIndex) {
float[] newData = new float[data.length - 1];
int i = 0;
for (; i < minIndex; i++) {
newData[i] = data[i];
}
newData[i++] = minValue;
for (; i < data.length - 1; i++) {
newData[i] = data[i+1];
}
return newData;
}
private static void createTree(float[] data, float minValue, int minIndex) {
/**
My code goes here
*/
}
}
pin.txt contains something like this:
8
5.0
9.0
3.0
2.0
1.0
6.0
8.0
3.0
Thanks in advance