0

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: input to tree

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();
    }
}
Community
  • 1
  • 1
Mark
  • 253
  • 1
  • 5
  • 22
  • Do you any pseudocode or something, do you understand the problem? – Iharob Al Asimi Dec 29 '14 at 19:10
  • edited the question. I start with ftree(0,-1) – Mark Dec 29 '14 at 19:18
  • 1
    At the moment this question sounds more like a riddle, and it's hard to figure out what's the correlation between the numbers and the tree (root 2 3 1 3?), and understanding what you're trying to do. One way to tackle it would be to understand the code, but since you have a problem with it, it's a bit dangerous to assume that the code is *correct*... *clarify*. – Karoly Horvath Dec 29 '14 at 20:09
  • What does `2 0 3 0 1 0 3` mean exactly? Looks like each number defines the number of subnodes of a given node starting with the root, but please give an example such that there is more than one node on a given depth has children. – kkm inactive - support strike Dec 29 '14 at 22:35

1 Answers1

1

The easiest, that worked for me, was to first build the tree, and only then to fork the tree and make the process tree.

For the purpose of building a tree I made a structure which will represent the node:

typedef struct _node {
    int val; // how many children the node has
    struct _node **children; // all the children of the node
} node, *pNode;

and also a structure that will represent a queue:

typedef struct _queue {
    pNode el;
    struct _queueInt *next;
} queue, *pQueue;

then I went through the input and added new nodes to the queue (first the root node manually, then the others while the queue was not empty), simultaniosly building the tree structure.

I found that the ADT queue is very good for this particular matter.

Special thanks to a coleague of mine which helped me to get this done.

Mark
  • 253
  • 1
  • 5
  • 22