How do you insert items into a binary tree in java so that they are in order? I want to use random values and sort them from smallest to largest then insert them into a binary tree in this order:
          1
     2         3
   4   5     6   7
 8  9
How do you insert items into a binary tree in java so that they are in order? I want to use random values and sort them from smallest to largest then insert them into a binary tree in this order:
          1
     2         3
   4   5     6   7
 8  9
 
    
    When you say 'in order' you need to clarify, do you mean in sorted order or do you mean insertion order, etc.
There are lots of resources available on inserting into binary trees or the difference between to types of binary trees, or how to print a binary tree diagram, that I suspect this is a duplicate.
What is different about your example? Having '1' as the root node means you must not have a rebalancing tree since both '2' and '3' are larger than the value for your root node. Your insert rule seems inconsistent since if '1' is the first node inserted then all other values will cascade to the right branch of the tree unless you use a different rule for the root then at the other levels which would be a pain to code.
 
    
     
    
    Something like this?:
public class BinaryTree {
    private List<Integer> list = new ArrayList<Integer>();
    public class BinaryTreeNode {
        private int p;
        public BinaryTreeNode(int p) {
            this.p = p;
        }
        private BinaryTreeNode getChild(int childP){
            BinaryTreeNode result= null;
            if (childP < list.size()){
                result = new BinaryTreeNode(childP);
            }
            return result;
        }
        public BinaryTreeNode getLeft(){
            return getChild(p*2+1);
        }
        public BinaryTreeNode getRight(){
            return getChild(p*2+2);
        }
        public int getValue(){
            return list.get(p);
        }
    }
    public void add(int item){
        list.add(item);
    }
    public BinaryTreeNode getRoot(){
        BinaryTreeNode result = null;
        if (!list.isEmpty()){
            result = new BinaryTreeNode(0);
        }
        return result;
    }
}
 
    
    In Naftalin, Walder Java Collections and Generics I've faced with this implementation that I love best:
public interface TreeVisitor<E, R> {
    public R visit(E leaf);
    public R visit(E value, Tree<E> left, Tree<E> right);
}
public abstract class Tree<E> {
    public abstract <R> R accept(TreeVisitor<E, R> visitor);
    public static <E> Tree<E> leaf(final E leaf) {
        return new Tree<E>() {
            @Override
            public <R> R accept(TreeVisitor<E, R> visitor) {
                return visitor.visit(leaf);
            }
        };
    }
    public static <E> Tree<E> branch(final E value, final Tree<E> left, final Tree<E> right){
        return new Tree<E>(){
            @Override
            public <R> R accept(TreeVisitor<E, R> visitor) {
                return visitor.visit(value, left, right);
            }
        };
    }
}
Now you can add any operation you want and create your tree as follows:
Tree<Integer> t = Tree.branch(1, 
            Tree.branch(2, 
                Tree.branch(4, Tree.leaf(8), Tree.leaf(9)), Tree.leaf(5)),
                Tree.branch(3, Tree.leaf(6), Tree.leaf(7));
I found the answer that I needed from this one.
Create a Complete Binary Tree using Linked Lists w/o comparing node values
Some of the other things I was pointed to, either weren't quite what I wanted, or didn't work past like 8 or so values.
 
    
    