Take the 2-minute tour ×

Currently working on a program to stimulate a tree made from arrayList of strings consisting of random 0 and 1. Each node has a limit on how many values it can store, and the root is always filled with the first node.size values. And any values after will need to go to a Left or Right child (depending on if it's a value 0 or 1) 0 = left 1 = right. As the tree grows, each level evaluates at the index of the height of the tree.


public class Node {
    public ArrayList<String> elements;
    public Node leftC;
    public Node rightC;
    private String data;
    int size = 5; //size limit of the Arraylist in each node

    public Node(s){
        elements = new ArrayList<String>();

    public void addString(String s){


public void insert(String s){

    root = insert(root,s);
private Node insert(Node n, String s){

    if (n == null){
        n = new Node(s);
    } else if (!n.isFull()){
    } else {
        if (s.charAt(0) == '0'){
            n.leftC = insert(n.leftC,s);
        } else if (s.charAt(0) == '1'){
            n.rightC = insert(n.rightC,s);
    return (n);

Edit: Thank you for the input, I took the advice and implemented a recursive function to do this. The tree is sort of working now...It properly adds the first Node.size values to the arraylist and sends the rest to it's children based on 0 or 1.

Root node:
[1011001001, 1011101101, 1111011011, 1011011101, 0101111011]

Left Child Node [0111001000, 0111111111, 0010101010, 0100000000, 0011010000]
Right Child Node [1111011111, 1011010110, 1001101000, 1000001110, 1110000000]

However, this is where I run in to a new issue, that I'm completely stuck at.

If I go to evaluate these new node's L and R child... it seems that because it's continuing to evaluate the value for charAt index 0, it's only making a triangle shaped tree

right child @ right [1100000011, 1100000000, 1011100000, 1011011011, 1010101010]
left child @ right - none
right child @ left - none
left child @ left [0010010101, 0010101011, 0110000100, 0011000000, 0010110111]

My question is.. how would I go about making the charAt index to be evaluated be in relation to the height of the tree?

For example.. if my tree was done correctly the next layer of nodes after the first would be (evaluate charAt index 1 instead of 0)

right child @ right [1100000011, 1100000000]
left child @ right [1011100000]

right child @ left [0110000100, 0111001000]
left child @ left [0010010101, 0010101011, 0011000000,0010110111,0001110000]

So as we proceed and the tree fills up at each node, we'll evaluate it @ the 3rd index etc. I am not sure how to implement that.

share|improve this question
It is really tedious to even go through this question and the code. Please make your question from standards suggested from sscce.org –  bragboy Dec 10 '11 at 13:14
It doesn't appear to be your problem, but if you make size final then it will eliminate the possibility of accidentally modifying that value. –  Hot Licks Dec 10 '11 at 13:23
You shouldn't have all that logic in Tree.treeInsert. Other than the root Node creation it should all be in Node.addString. Depth can be communicated by an extra parameter to addString. –  Hot Licks Dec 10 '11 at 13:26

2 Answers 2

Your basic problem is that you don't set runner to the appropriate left or right node after selecting the left or right path.

In particular, this code:

            } else if (runner.leftC==null){
                runner.leftC = new Node();
                System.out.println("ADDED "+s+" to step 2 LEFT");

You're adding the string to the prior node, vs to the just-created one.

This error would be unlikely if you'd used a recursive method on Node.

share|improve this answer

The two that are out of place (010110010001 & 110100111111) are the only ones being added with a string of "step 2", so your problem must be in there.

From the looks of it, you're setting "runner" to "root":

Node runner;
runner = root;

And then you're adding the strings:

runner.leftC = new Node();

What's happening, is "runner" is still equal to "root" at this point, which is why your elements are getting added to the root.

share|improve this answer

Your Answer


