Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

java - implementing n-ary tree from the int massive

I have an array of int type, which the object does not contain sequential sequences of numbers, each node can have more than two, I did not find normal information on how to build such a tree, I did this: I get information about the name, weight, and count -ve descendants of the root Node. By the number of descendants, I created an array of type Node with the length of the number of descendants. What exactly should I put into this array - objects, or references to them like Node n1, n2 ...;

The problem is that the array can be of any length.

An example of a constructor for an object of the Node type that has a descendant constructor

public Node(String name, int weight, Node[] children) {
    this.name = name;
    this.weight = weight;
    this.children = children;
}

If Node has no children, then I made the same constructor only without the Children array. Here is the code that I have by this time (I filled the array with arbitrary data so that I have something to work with and see if what I write works, otherwise, apart from the order of information, it can be arbitrary) P.S. to search for each parameter for the root, I wrote 2 versions of the code, because the name can be written in list [startPoint], or in list [startPoint] + list [startPoint + 1], that is, 2 char characters

public class Main {

    public static void main(String[] args) {
        String korName;
        int korWeight;
        int temp = 0;

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter array's size");
        int size = scanner.nextInt();
        System.out.println("Enter starting point");
        int startPoint = scanner.nextInt();
        int[] list = new int[size];
        list[0] = 0;
        list[1] = 65;
        list[2] = 0;
        list[3] = 1;
        list[4] = 7;
        list[5] = 19;
        list[6] = 0;
        list[7] = 66;
        list[8] = 0;
        list[9] = 2;
        list[10] = 14;
        list[11] = 0;
        list[12] = 31253;
        list[13] = 31254;
        list[14] = 68;
        list[15] = 0;
        list[16] = 4;
        list[17] = 17;
        list[18] = 0;
        list[19] = 67;
        list[20] = 0;
        list[21] = 3;
        list[22] = 0;
        //--------------------------------------find root name-----------------------------------------
        if (list[startPoint + 1] != 0) {
            String a = String.valueOf(Character.toChars(list[startPoint]));
            String b = String.valueOf(Character.toChars(list[startPoint + 1]));
            korName = a + b;
        } else {
            korName = String.valueOf(Character.toChars(list[startPoint]));
        }
        //--------------------------------------find root weight---------------------------------------
        if (list[startPoint + 1] != 0) {
            korWeight = list[4];
        } else {
            korWeight = list[3];
        }
        //--------------------------------------find root children amount------------------------------
        if (list[startPoint+3] != 0 && list[startPoint+1] == 0){
            for (int i = 3; i < list.length; i++){
                if (list[startPoint+i] == 0){
                    break;
                }
                if (list[startPoint+i] != 0){
                    temp++;
                }
            }
            Node[]korChildren = new Node[temp];
        }
        if (list[startPoint+1] != 0 && list[startPoint+4] != 0){
            for (int i = 4; i < list.length; i++){
                if (list[startPoint+i] == 0){
                    break;
                }
                if (list[startPoint+i] != 0){
                    temp++;
                }
            }
            Node[]korChildren = new Node[temp];
        }

        System.out.println(temp);
    }

At this stage, I was stuck on the fact that I created an array of the correct size for the number of children, and I do not know how to fill the tree further. I would apreciate some hints, or corrections.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...