Binary Tree using Linked List

A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.Every node excluding a root in a tree is connected by a directed edge from exactly one other node. This node is called a parent.

On the other hand, each node can be connected to arbitrary number of nodes, called children. Nodes with no children are called leaves, or external nodes. Nodes which are not leaves are called internal nodes. Nodes with the same parent are called siblings.
In the linked list representation, we cannot directly access the children of the current node unless we traverse the list.

Important terms :

Binary Tree Structure

Types of traversal :

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.
    Root represents the root node of the tree and initialize it to null.
    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.
  4. Inorder() will display nodes of the tree in inorder fashion.
    It traverses the entire tree then prints out left child followed by root then followed by the right child.

Program using C