Saturday, June 9, 2018

Trees in java

Simple Trees in Java:

class node1
{
   int data;
    node1 left;
    node1 right;

    node1(int data){
        this.data = data;
        this .left = null;
        this.right = null;
    }

    void inorder(){
        if(left != null) {
            left.inorder();
        }
            System.out.println(this.data);
        if(right != null)
            right.inorder();

    }
}

public class Tree {

    void inorder(node1 root) {
        if (root!= null) {

            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }

    public static void main(String[] args)
    {
        node1 root1 = new node1(5);
        root1.left = new node1(3);
        root1.right = new node1(7);
        root1.left.left = new node1(2);
        root1.left.right = new node1(4);
        root1.right.left = new node1(6);
        root1.right.right = new node1(8);

        Tree tree = new Tree();
        tree.inorder(root1);

        root1.inorder();

    }
}


For addition of add_item function for creating binary tree:
Note to use in lower class if root has to be passed otherwise in the upper class of only item has 
to be passed as root should be referenced with object.



class node1
{
   int data;
    node1 left;
    node1 right;

    node1(int data){
        this.data = data;
        this .left = null;
        this.right = null;
    }

  void add_node(int item)
  {
      if(item < this.data )
      {
          if(this.left == null)
          {
              this.left = new node1(item);
          }

          else{
              this.left.add_node(item);
          }
      }
      else{
          if(this.right == null)
          {
              this.right = new node1(item);
          }
          else          {
              this.right.add_node(item);
          }
      }
  }


}

public class Tree {

    void inorder(node1 root) {
        if (root!= null) {

            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }

    void add_node(node1 root,int item)
    {
        if(root == null)
            root = new node1(item);
        else{
            if(item < root.data )
                if(root.left == null)
                    root.left = new node1(item);
                else                {
                    add_node(root.left,item);
                }
            else{
                if(root.right == null)
                    root.right = new node1(item);
                else {
                    add_node(root.right, item);
                }
            }
        }
    }

    public static void main(String[] args)
    {
        node1 root1 = new node1(5);
//        root1.add_node(3);//        root1.add_node(2);//        root1.add_node(1);//        root1.add_node(6);//        root1.add_node(8);//        root1.add_node(7);




        Tree tree = new Tree();


        tree.add_node(root1,3);
        tree.add_node(root1,2);
        tree.add_node(root1,1);
        tree.add_node(root1,6);
        tree.add_node(root1,8);
        tree.add_node(root1,7);
        tree.add_node(root1,9);
        tree.inorder(root1);


    }
}

No comments:

Post a Comment