0

I have been trying to write a program for creating a binary tree .I wish add nodes level by level .When i google the topic all i get is binary search tree . here is the code

public class BinaryTree
{
    public static void main(String[] args)
        throws NullPointerException
    {
        Tree myTree = new Tree();
        myTree.add(2,new Node(4));
        myTree.add(4,new Node(5));
        myTree.add(2,new Node(6));
        //myTree.add(2,new Node(7));
    }
}
class Node
{
    int data;
    boolean visited;
    Node left,right;

    Node(int data)
    {
        left=null;
        right=null;
        visited=false;
        this.data=data;
    }
}
class Tree
{
    Node root;
    int level,cLevel;
    Tree()
    {
        root=null;
        level=0;
        cLevel=level-1;
    }

    protected void add(int data,Node node)
    {
        System.out.println("node.data k: "+node.data);
        Node t;
        if(root==null)
        {
            Node n=new Node(data);
            root=n;
            root.visited=true;
            System.out.println("root visited"+root.data+""+root.visited);
            level++;
            cLevel++;
            return;
        }
        while(root!=null)
        {

        }
    }
}

I want to add new nodes level by level ,new level should not be created until a level isnt comleted ,all i get by googling is binary search tree .what should i do ,i have tried to use depth first and breath first approach which didnt prrove helpfull .

2
  • Why do both add and the Node constructor take data parameters? Commented Mar 16, 2014 at 18:56
  • that was for some thing i was trying to test ,it does not have anything to do with the insert algorithm . Commented Mar 16, 2014 at 18:58

3 Answers 3

1

You could achieve this by maintaining a queue of nodes which do not yet have two children. Every time you add a new node, stick it on the end of this queue, and also make it a child of the node at the front of this queue. Once the node at the front has two children, remove it from the queue. This way you'll build it up one level at a time, left to right, and only move on to the next level once the current one is finished.

Sign up to request clarification or add additional context in comments.

3 Comments

Every time you add a new node, stick it on the end of this queue, and also make it a child of the node at the front of this queue I do not understand what you mean by this
What I mean is, when you want to add a new node to the tree, you make it a child of the first node in the queue (which should be the node highest up the tree which doesn't have two children yet) to make sure child nodes are added in the right order. You then add the new node on to the end of the queue so that it'll only have children added once all the levels before it have filled up. This means when nodes are added to the tree they'll fill up each depth level from left to right because that's the order things are being added to the queue. Hope this clears things up a bit :-)
yep , but i think there is no need to make the new node child of the head of the queue , anyways i have implemented it with the idea you gave ,I am also posting the solution to the answer so that it helps others as well ,although your answer is enough for it!
1

You could also try "Limited Depth-first search"
A recursive implementation in Java using part of your code could be:

class Tree
{
    Node root;
    int level,cLevel;
    Tree()
    {
        root=null;
        level=0;
        cLevel=level-1;
    }

    protected void add(int data)
    {
        System.out.println("data k: "+ data);
        Node t;
        if(root==null)
        {
            root=new Node(data);
            level++;
        } else {
            cLevel = 0;
            boolean added = add(data, root);
            //Couldn't add to current level, add new level
            if (!added){
                level++;
                cLevel = 0;
                add(data, root);
            }

        }
    }

    private boolean add(int data, Node node)
    {   
        cLevel++;
        boolean added;
        //Depth limited
        if (cLevel<=level){
            added = true;
            //Try to add to current node
            if (node.left == null)
                node.left = new Node(data);
            else if (node.right == null)
                node.right = new Node(data);
            else if (!add(data, node.left)) //Recursively trying to add to children 
                added = add(data, node.right);
        } else {
            added=false;
        }
        cLevel--;
        return added;
    }

}

Hope it helps.

Comments

1

Done with it.Thanks Animatinator .Although i have tested it with hardcode as i do not have time right now ,i have a paper early morning of Compiler Construction .

protected void add(int data,Tree mytree)
{
    if(root==null)
    {
        root=new Node(data);
        myList.addLast(root);
        root.count++;
        return;
    }
    Node node=mytree.myList.getFirst();
    if(root!=null)
    {
        if(node.left==null)
        {
            node.count++;
            node.left=new Node(data);
            mytree.myList.add(node.left);
            return;
        }
        else
        {
            node.count++;
            node.right=new Node(data);
            mytree.myList.add(node.right);
        }
        if(node.left!=null & node.right!=null)
        {
            mytree.myList.removeFirst();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.