0

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
Relindrani
  • 11
  • 7
  • This isn't entirely clear. What do the numbers mean here; are they the values, or do they represent the order that you're adding things in? – Oliver Charlesworth Jul 16 '15 at 19:31
  • If it's the latter, then this should be relatively easy. If it's the former, you'll need to move a bunch of stuff around on every insertion. – Oliver Charlesworth Jul 16 '15 at 19:31
  • @OliverCharlesworth it is clear. He wants a complete binary tree. – gtgaxiola Jul 16 '15 at 19:31
  • 2
    @alifirat It is NOT a Binary Search Tree just a Binary Tree (complete) – gtgaxiola Jul 16 '15 at 19:32
  • They are just the order that I want to add the values. – Relindrani Jul 16 '15 at 19:33
  • possible duplicate of [Does anyone know how to implement a Complete Binary Tree using recursion without comparing the value of the node?\[Solved\]](http://stackoverflow.com/questions/20890929/does-anyone-know-how-to-implement-a-complete-binary-tree-using-recursion-without) – gtgaxiola Jul 16 '15 at 19:33
  • http://stackoverflow.com/questions/16630823/binary-tree-insert-algorithm This actually uses the breadth first search you had mentioned – Adam Jul 16 '15 at 19:38
  • I edited my question to make it a little more clear. The first link seems to be kinda what I wanted but it only uses values from 0-10 it seems. – Relindrani Jul 16 '15 at 19:46
  • Modify the code posted by Jim Mischel from the "first link" by adding a counter of the inserted nodes to the tree and pass this, along with the actual value, in the insert method. – laune Jul 17 '15 at 05:51

4 Answers4

0

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.

Community
  • 1
  • 1
Kelly S. French
  • 11,669
  • 9
  • 56
  • 90
0

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;
    }
}
David Pérez Cabrera
  • 4,594
  • 2
  • 20
  • 35
0

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));
  • This seems like good implementation but I was looking for something more traditional, like what the other answer shows. – Relindrani Jul 16 '15 at 20:14
  • @Relindrani Note, that the implementation is completely thread-safe, because you have no varaible you could modify. I myself use it in our projects. –  Jul 17 '15 at 19:24
-1

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.

Community
  • 1
  • 1
Relindrani
  • 11
  • 7