Home » Program to Implement Binary Tree using the Linked List

Program to Implement Binary Tree using the Linked List

by Online Tutorials Library

Q. Program to implement Binary Tree using the linked list

Explanation

In this program, we need to create the binary tree by inserting nodes and displaying nodes in inorder fashion. A typical binary tree can be represented as follows:

Program to implement Binary Tree using the linked list

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:

Program to implement Binary Tree using the linked list

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.

Algorithm

  1. Define 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.
  2. When a node is created, data will pass to data attribute of the node and both left and right will be set to null.
  3. Define another class which has an attribute root.
    1. Root represents the root node of the tree and initialize it to null.
  4. insert() will add a new node to the tree:
    1. It checks whether the root is null, which means the tree is empty. It will add the new node as root.
    2. Else, it will add root to the queue.
    3. The variable node represents the current node.
    4. First, it checks whether a node has a left and right child. If yes, it will add both nodes to queue.
    5. If the left child is not present, it will add the new node as the left child.
    6. If the left is present, then it will add the new node as the right child.
  5. Inorder() will display nodes of the tree in inorder fashion.
    1. It traverses the entire tree then prints out left child followed by root then followed by the right child.

Solution

Python

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   

C

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   

JAVA

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  

C#

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   

PHP

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 TopicPrograms List

You may also like