Java program to implement Binary Tree using the Linked ListIn this program, we need to create the binary tree by inserting nodes and displaying nodes in in-order fashion. A typical binary tree can be represented as follows: In the binary tree, each node can have at most two children. Each node can have zero, one or two children. Each node in the binary tree contains the following information: Data that represents value stored in the node. Left that represents the pointer to the left child. Right that represents the pointer to the right child. AlgorithmDefine Node class which has three attributes namely: data left and right. Here, left represents the left child of the node and right represents the right child of the node.
a. insert() will add a new node to the tree:
a. Inorder() will display nodes of the tree in inorder fashion.
Program:Output: Binary tree after insertion 1 Binary tree after insertion 2 1 3 Binary tree after insertion 4 2 5 1 3 Binary tree after insertion 4 2 5 1 6 3 7 Next TopicJava Programs |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India