Tuesday, January 24, 2017

java tree example

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package treetest;

public class TreeTest {

    public static void main(String[] args) {
        Node treeRootNode = new Node(null);

        treeRootNode.setId("1");

        // add child to root node
        Node childNode = addChild(treeRootNode, "2");//742
        // add child to the child node created above
        addChild(childNode, "4");//69c
        addChild(childNode, "5");//69c
//
//        // add child to root node
        Node child2 = addChild(treeRootNode, "3");//742
//        // add child to the child node created above
//        addChild(child2, "child-2-1");//922
//        addChild(child2, "child-2-2");//922
//        addChild(child2, "child-2-2");//922

        printTree(treeRootNode, " ");

        deleteChild(treeRootNode, "3");
//
//        deleteChild(treeRootNode, "1");

//        deleteChild(treeRootNode, "1");
        printTree(treeRootNode, " ");

    }

    static Node addChild(Node parent, String id) {
        Node node = new Node(parent);
        node.setId(id);
        node.getParent().getChildren().add(node);

//        parent.getChildren().add(node);
        System.out.println("---parent.getChildren().get(0)=" + parent.getChildren().get(0).getId() + " Parent=" + parent.getChildren().get(0).getParent().getId());
        return node;
    }

    static void deleteChild(Node parent, String id) {

//        if(parent.getParent().getId().equalsIgnoreCase(""))
//        {
//            System.out.println("---empty tree=" );
//            parent.getChildren().clear();
//        }
//      
        for (int i = 0; i < parent.getChildren().size(); i++) {
            System.out.println("i=" + i + " " + parent.getChildren().get(i).getId());

            if (parent.getId().equalsIgnoreCase(id)) {
                System.out.println("--- tree=");
                parent.getChildren().clear();
                System.out.println("---empty tree=" + parent.getId());
//                parent.getParent().setId("");
                break;
            }

            if (id.equalsIgnoreCase(parent.getChildren().get(i).getId())) {

                System.out.println("----deleted =" + parent.getChildren().get(i).getId() + " parent=" + parent.getId());

//                parent.getChildren().remove(parent.getChildren().get(i).getId());
                parent.getChildren().remove(i);

                break;
            }
            deleteChild(parent.getChildren().get(i), id);
        }

//        parent.getChildren().add(node);
//        System.out.println("---parent.getChildren().get(0)="+parent.getChildren().get(0).getId()+ " Parent="+parent.getChildren().get(0).getParent().getId());
    }

    private static void printTree(Node node, String appender) {

//        for (Node each : node.getChildren()) {
//            printTree(each, appender + " ** ");
//        }
        System.out.println(appender + node.getId());
        for (int i = 0; i < node.getChildren().size(); i++) {
//            System.out.println("i="+i+" "+node.getId());
            printTree(node.getChildren().get(i), appender + " ** ");
        }
    }
}









//////////////////////////////////////////////////////////////


package treetest;
import java.util.ArrayList;
import java.util.List;

public class Node {

    private String id;
    private  List<Node> children = new ArrayList<>();
    private Node parent;

    public Node(Node parent) {
       
        this.parent = parent;
       
        System.out.println("--cons() parent="+this.parent);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<Node> getChildren() {
//        children.clear();
       
        return children;
    }

    public Node getParent() {
        return parent;
    }

}




output:
root
  ** child#1
  **  ** child-1-1
  **  ** child-1-2
  ** child#2
  **  ** child-2-1
  **  ** child-2-2