Beginner Java Tree Questions?

Today I was told to create a tree data structure with the underlying class,

public class Node(){
private string lable;
private List<Node> children;
}

After starting to create a tree, I hit first.

It contains the Node inside the node. I am completely confused. Well, a tree can be very familiar to you. You might think something like this.

This is the first time for me when I try to create a tree in java. Honestly, I used only setters and getters inside a class in java. Using these methods, I cannot think of inserting new nodes after the first level.

I saw several examples on google and many on stackoverflow. But for a novice (in a tree) like me, they look incomplete. Perhaps they might think that the OP could continue this.

If someone would explain to me his concept and how to add more children, with some general example I would be grateful.

Update:

, .

Node node = new Node();
String label = "Bikes";
ArrayList<Node> children = new ArrayList<Node>();

    Node childNode = new Node();
    childNode.setLabel("Yamaha");
    children.add(childNode);

    childNode = new Node();
    childNode.setLabel("Suzuki");
    children.add(childNode);

    childNode = new Node();
    childNode.setLabel("Honda");
    children.add(childNode);

    node.setLabel(label);
    node.setChildren(children);

, , . , addChild()

,

    public void addChildren(Node node){
    children.add(node);
}

:

        ArrayList<Node> children = new ArrayList<Node>();

    node.setLabel(label);
    node.addChildren(node);

. node. , , .

+3
6

() :

:

public class Node { 
....

sumbit . (: downvote: P)

, .

node .

node , . , .

, :

import java.util.*;
// This is your existing code:
class Node { 
    private String label;
    private List<Node> children = new ArrayList<Node>();

    // Here how you would build one with that strucure
    public static void main( String ... args ) { 

        Node one = new Node();
        one.label = "1";

        Node two = new Node();
        two.label = "2";

        Node root = new Node();
        root.label = "plus";
        root.children.add( one );
        root.children.add( two );

        print( root );
    }

    // Here how you'll print the values
    static void print( Node node ) { 

         System.out.println( node.label );

         for( Node child : node.children ) { 
            // if child is a branch 
            if( child.children.size() > 0 ) { 
                // print the branch ( recursively ) 
                print( child );
             } else { 
                // is a leaf, just print the label.
                System.out.println( "-- ["+child.label+"]" );
             }
          }
     }
} 

root node ( "" ) ( "1" "2" ), , , ( ), .

, .

+3

: wikipedia.
. , Java .
, .

+2

, ? node node. , (), ..

.

+1

node. node 0 . , , , node.

+1

, , , . . .

, , . - . , - , .

recursiveFunction(Node n) {
  do work for current node

  for(Node child : children) {
    recursiveFunction(child);
  }
}

It may take a little thought to see what work should be done in the current node, and if there are some special cases based on its values, but the structure will look similar.

+1
source

In OscarRYZ answer In a recursive method, the print(Node)method print(Node)is called on its own:

if( child.children.size() > 0 ) {
              // print the branch ( recursively )
              print( child );
           }else {
              // is a leaf, just print the label.
              System.out.println( "-- ["+child.label+"]" );
           }

this method print(Node)will be called recursively while the node is a branch, and when it hits the leaf, it just prints the label of the leaf from

System.out.println( "-- ["+child.label+"]" );
 and go back to its branch.

0
source

Source: https://habr.com/ru/post/1793111/


All Articles