简体   繁体   中英

Binary Tree implementation for adding nodes level by level

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 .

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.

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.

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM