Auf Thema antworten

Hier meine main

[code]public class Main {


    public static void main(String[] args) {

      

        Scanner scanner = new Scanner(System.in);

        System.out.println("Provide Branching Factor");

        int b= scanner.nextInt();

        System.out.println("Provide height");

        int h= scanner.nextInt();

        System.out.println("Provide Approximation");

        int approx= scanner.nextInt();

      

        System.out.println("Values provided:"+b+" , "+ h + " , " + approx);

      

        Tree newTree = new Tree(b,h,approx);

      

      

    }


}[/code]


Die Node Class:

[code]

public class Node {

    int e;

    private ArrayList<Node> daughter;

    int height;


  

    public Node(int e){

        this.daughter = new ArrayList<Node>();

        this.e = e;

    }

  

    public void addDaughter(Node childNode){

        this.daughter.add(childNode);

    }


    @Override

    public String toString() {

        return "Node [e=" + e + " height: " + height + ", daughter= " + daughter + "]\n";

    }


    public void setHeight(int h){

        this.height=h;

    }

  

    public int getHeight(){

        return this.height;

    }

  

}[/code]


Und die Tree Klasse:

[code]public class Tree {

    public int b;

    public int h;

    public int approx;


    private Node root;

    public int t;

    private ArrayList<Node> tree;


    public Tree(int b, int h, int approx) {

        super();

        this.b = b;

        this.h = h;

        this.approx = approx;

        root = null;

        tree = new ArrayList<Node>();


        // Random T between -250 and +250

        Random randomGenerator = new Random();

        t = (randomGenerator.nextInt(500) - 250);


        for (int i = 0; i <= h; i++) {

            insert();

          

        }

        System.out.println(tree.toString());


    }


    public void insert() {

        if (root == null) {

            root = new Node(t);

            root.setHeight(h);

            tree.add(root);

  

        } else {

            insert(root);

        }


    }


    private void insert(Node parent) {

        for(int i=0;i<b;i++){

            int height=h-1;

            Node child = new Node(t+1);

            child.setHeight(height);

            parent.addDaughter(child);  

            tree.add(parent);

          

        }

    }


}[/code]


Alles etwas chaotisch.



Oben