How to implement binary tree in java

0

 


Binary tree : It is most important data structure . Binary tree is non linear data structure in which objects are organized in a hierarchical structure. Like a real life tree this tree has a root , it can have maximum two branches (children) . 


As we can see in the image we have 1 on top which is called the root of the tree also that is the level 0 of the tree. Then we have level 1 , in this level we have 2 and 3 which are the children of the root. Then we reach level 2 , in this level we have 4 ,5 and 6 which are the children of the 2 and 3. 

Lets see how to write a program to implement the binary tree in java.


Java Code :



class Node{

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

class HindiCodingCommunity{

public static void main(String args[])
{
Node root = new Node(1);
root.left = new Node(2);
root.right= new Node(3);

root.left.left= new Node(4);
root.right.left= new Node(5);
root.right.right= new Node(6);
}

}

If you want to write for us then please contact us at hindicodingcommunity@gmail.com


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !