Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a Binary tree in Java [closed]

I am constructing a binary tree. Let me know if this is a right way to do it. If not please tell me how to?? I could not find a proper link where constructing a general binary tree has been coded. Everywhere BST is coded.

  3
 / \
1   4
   / \
  2   5

This is the binary tree which i want to make.I should be able to do all the tree traversals.Simple stuff.

public class Binarytreenode
{
    public Binarytreenode left;
    public Binarytreenode right;
    public int data;

    public Binarytreenode(int data)
    {
        this.data=data;
    }

    public void printNode()
    {
        System.out.println(data);
    }

    public static void main(String ar[])
    {
        Binarytreenode root = new Binarytreenode(3);
        Binarytreenode n1 = new Binarytreenode(1);
        Binarytreenode n2 = new Binarytreenode(4);
        Binarytreenode n3 = new Binarytreenode(2);
        Binarytreenode n4 = new Binarytreenode(5);

        root.left = n1;
        root.right = n2;
        root.right.left = n3;
        root.right.right = n4;
    }
}
like image 348
VIN Avatar asked Dec 22 '13 17:12

VIN


People also ask

How do you create a binary tree in Java?

We'll follow these rules starting from the root node: if the new node's value is lower than the current node's, go to the left child. if the new node's value is greater than the current node's, go to the right child. when the current node is null, we've reached a leaf node, we insert the new node in that position.

Is there a built in binary tree in Java?

In the above example, we have implemented the binary tree in Java. Unlike other data structures, Java doesn't provide a built-in class for trees. Here, we have created our own class of BinaryTree . To learn about the binary tree, visit Binary Tree Data Structure.

What is Btree Java?

A binary tree is a recursive data structure where each node can have 2 children at most. A common type of binary tree is a binary search tree, in which every node has a value that is greater than or equal to the node values in the left sub-tree, and less than or equal to the node values in the right sub-tree.

What happens when a binary tree is completed?

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.


2 Answers

I think this is what you are looking for:

public class Binarytree
{
    private static Node root;

    public Binarytree(int data)
    {
        root = new Node(data);
    }

    public void add(Node parent, Node child, String orientation)
    {
        if (orientation.equals("left"))
        {
           parent.setLeft(child);
        }
        else
        {
            parent.setRight(child);
        }
    }

    public static void main(String args[])
    {
        Node n1 = new Node(1);
        Node n2 = new Node(4);
        Node n3 = new Node(2);
        Node n4 = new Node(5);

        Binarytree tree = new Binarytree(3); //  3
        tree.add(root, n1, "left"); //         1/ \
        tree.add(root, n2, "right"); //            4
        tree.add(n2, n3, "left"); //             2/ \
        tree.add(n2, n4, "right"); //                5
    }
}

class Node {
    private int key;
    private Node left;
    private Node right;

    Node (int key) {
        this.key = key;
        right = null;
        left = null;
    }

    public void setKey(int key) {
        this.key = key;
    }

    public int getKey() {
        return key;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getLeft() {
        return left;
    }

    public void setRight(Node right ) {
        this.right = right;
    }

    public Node getRight() {
        return right;
    }

}
like image 176
user11057 Avatar answered Sep 29 '22 19:09

user11057


The idea behind a binary tree is that it is sorted. Any values larger than the current value are in the right node and every value smaller is in the left node. That means you shouldn't do the tree-building in your main program. Rather, every node should have an "insert" method which determines whether the new node should go to the left or the right of the current node. When you have a new node, you create that node, and then you call root.insert(newNode).

The insert-method would then work like this (because this is obviously a student assignment and you are supposed to learn from it, you only get pseudo-code from me, no complete solution):

When value is smaller than own value:
     When there already is a left-node:
         call left-node.insert(new-node)
     When there isn't a left-node yet:
         the left-node is now the new-node
When value is larger than own value:
     When there already is a right-node:
         call right-node.insert(new-node)
     When there isn't a right-node yet:
         the right-node is now the new-node
When value is equal to own value:
     Duplicate value. Either ignore the value or throw an exception.

Finding if an object already is in the tree works the same way:

When requested value is equal to the value of this node
     return this node
When the requested value is smaller
     When a left node exists
         call left.find(value)         
     When no left node exists
          value doesn't exist in this tree
When the requested value is larger
     When a right node exists
         call right.find(value)         
     When no right node exists
          value doesn't exist in this tree

In the case you don't actually want to learn how binary trees work and just use them, just use TreeSet which is an already working binary-tree implementation.

like image 25
Philipp Avatar answered Sep 29 '22 18:09

Philipp