-4

how to do insert random integer between 1 and 10000 to the tree?? I just using scanner.in in code below

 private BinaryTree insert(BinaryTree node, int value)
 {
     if (node == null)
         node = new BinaryTree(value);
     else
     {
         if (value <= node.getValue())
             node.left_child = insert(node.left_child, value);
         else
             node.right_child = insert(node.right_child, value);
     }
     return node;
 }

public static void main(String[] args)

    do    
    {
        //tree operations
        System.out.println("\nTree Operations\n");
        System.out.println("1. insert ");


        int choice = scan.nextInt();            
        switch (choice)
        {
        case 1 : 
            System.out.println("get integer element to insert");
            bst.insert( scan.nextInt() );                     
            break;                                    
        default : 
            System.out.println("Wrong Entry \n ");
            break;   
        }

2 Answers2

0

I am assuming that your BinaryTree class is as follows:

public BinaryTree{

private int value = null;
private BinaryTree left_child=null;
private BinaryTree right_child=null;
//Also insert getters and setters here

BinaryTree(value){
this.value = value;
}

 private BinaryTree insert(BinaryTree node, int value)
 {
     if (node == null)
         node = new BinaryTree(value);
     else
     {
         if (value <= node.getValue())
             node.left_child = insert(node.left_child, value);
         else
             node.right_child = insert(node.right_child, value);
     }
     return node;
 }
}

In your main method, you should have:

BinaryTree node = new BinaryTree("RootValue");
do{
    //tree operations
    System.out.println("\nTree Operations\n");
    System.out.println("1. insert ");


    int choice = scan.nextInt();            
    switch (choice)
    {
    case 1 : 
        System.out.println("get integer element to insert");
        node = bst.insert( node,scan.nextInt() );                     
        break;                                    
    default : 
        System.out.println("Wrong Entry \n ");
        break;   
    }

I haven't tested this, but I think the primary issue with your code is that your insert method takes two parameters, but you're only passing one in your code. Lemme know if this works.

Chetan Jadhav CD
  • 1,116
  • 8
  • 14
-2

This is just for insert, you need another process to make it balanced. I would suggest, Take all the integers in an array,Sort the array, and then construct a binary tree in one pass,

private BinaryTree constructBalanced(int a[], int low, int high) {

  if (low > high) return null;
  else {

    int mid = (low + high) / 2;
    BinaryTree root = new BinaryTree(a[mid]);
    root.left = constructBalanced(int a[], low, mid - 1);
    root.right = constructBalanced(int a[], int mid + 1, high);
    return root;
  }
}
Biffen
  • 6,249
  • 6
  • 28
  • 36
Rohit Kasat
  • 300
  • 3
  • 13