N-ary tree - mirror


I've given a n-ary tree in .txt file, where first integer is parent and the next ones are child of it:
1
2
3
4
5
6
 
1 2 3 4 5
3 6 7
5 8
8 9 10 11 
0 (represents the end of the tree)

And, it could also be written like this (same tree):
1
2
3
4
5
5 8
3 6 7
8 9 10 11
1 2 3 4 5
0

My task is to output result as (First example):
1
2
3
4
5
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0

I'm really desperate to find a solution here and I my brains are slowly giving up. I've a function to read from .txt file and put all the values in the array. (I know that I shouldn't use EOF, but atm, that's not the problem we should focus on..)

How can I correctly make a tree and get the correct output?

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
 
const int MAX_CHILD = 100;

struct TreeNode {
 int val;
 TreeNode *children[MAX_CHILD];
 TreeNode(int x) : val(x) {
     for(int i = 0; i < MAX_CHILD; ++i) {
         children[i] = nullptr;
     }
 }
 };
 Node *newNode(int val)
{
TreeNode *temp = new TreeNode;
temp->val= val;
return temp;
}


TreeNode* invertTree(TreeNode* root) {
if(!root) {
    return root;
}


for(int i = 0; i < MAX_CHILD; ++i) {
    invertTree(children[i]);
}

// reverse the children nodes array
for(int left = 0, right = MAX_CHILD - 1; left < right; left++, right--) {
    swap(children[left], children[right]);
}

return root;
}

int main()
{
    ifstream file("Alise.in.txt");
    int linecount = 0;
    int tempArray[100];
    int *finalArray;
    // Bad practise - I know. I believe it doesn't hurt atm, as I've more srs problem
    while (!file.eof())
    {
        file >> tempArray[linecount];
        linecount++;
    }
    linecount = linecount - 1; // Skip the last integer on file '0'

    finalArray = new int[linecount];
    for (int i = 0; i < linecount; i++)
    {
        finalArray[i] = tempArray[i];
    }
    // Print the array.
    for (int i = 0; i < linecount; i++)
    {
        newNode(finalArray[i]); // Putting in values and making tree
    }
    delete[] finalArray;

return 0;
}
My task is to output result as (First example) :
1
2
3
4
5
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0

How is that a tree?
@Chidoro
1
2
3
4
5
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0


1 is root (have to check it tho - not sure how, cuz tree can be given with second example).
rest of the numbers are child of 1.
Repeat with each line and you get (left picture):
https://img.exs.lv/e/z/ezeliitis/image001.jpg
Last edited on
What are the original details of your assignment?
Task: Computer understands tree, if it's structure is: every line contains information about inside node (not a leaf); On every line - first integer is node and the next ones are childrens. Tree can be structured differently, for instance: .txt file
1
2
3
4
1 2 3 4 5
3 6 7
5 8
8 9 10 11


AS WELL AS:
1
2
3
4
5 8
3 6 7
8 9 10 11
1 2 3 4 5


It's mandatory to output tree's inside nodes in pre-order, first comes parent, then child information. Nodes in the tree can be up to 100. What I need to do? - output the tree in mirror style.


EXAMPLE 1: (Input - preorder sequence):

Input data from Example.in.txt file:
1
2
3
4
5
1 2 3 4 5
3 6 7
5 8
8 9 10 11
0


Output:
1
2
3
4
5
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0

EXAMPLE 2 (Input - random sequence/free/ ):

Input data from Example.in.txt file:
1
2
3
4
5
5 8
3 6 7
8 9 10 11
1 2 3 4 5
0

Output:
1
2
3
4
5
1 5 4 3 2
5 8
8 11 10 9
3 7 6
0
1
2
3
4
5
5 8
3 6 7
8 9 10 11
1 2 3 4 5
0


Should this be :
1
2
3
4
5
5 8
3 7 6
8 11 10 9
1 5 4 3 2
0


???
Yes. Any clues on how to solve that?
Bump!
Topic archived. No new replies allowed.