I have read a similar question on this matter, but I don't need to build a BST.
From a given input, for example: 2 0 3 0 1 0 3
, which will build:
Explanation:
2 -> the root has 2 children
0 -> the left child of the root has no children
3 -> the rigth child of the root has 3 children
0 -> the leftmost child on the second level (whose grandparen is the root) has no children
1 -> the middle child on the second level has 1 child
0 -> the right most child on the second level has no children
3 -> the only node on the third level has 3 children
<empty> -> the nodes on the last level have no children
My problem is getting the index of a child of the node.
The idea I have is to get the value of the parent node index, then add to it the counter, the problem I have is what happens if the node doesn't have any children? All the others are shifted for 1 to the right.
Please give me some advise.
EDIT: here is the chunk of code I use to generate the tree: It has to build a tree of processes of the given sequence
void ftree(int index, int parentIndex) {
int i, tmp, pid, child;
if(input[index] == 0){
sleep(1);
} else if(index >= inputSize) {
sleep(1);
} else if(index < inputSize) {
for(i = 0; i < input[index]; i++) {
if(parentIndex == -1) {
child = 1;
} else {
child = index + input[parentIndex] + i; // here is the calculation of the index of the child
}
pid = fork();
if (pid == 0) {
ftree(child, index);
break;
}
}
}
if(getpid() == pidOrg) {
pid = fork();
if(pid == 0) {
execlp("pstree", "pstree", pidString, "-c", NULL);
} else {
wait();
}
} else {
wait();
}
}