Insertion in a Binary Tree (Level Order)

Insertion In A Binary Tree

Given a tree and a key, add a node in the first available node in the tree. After adding the node, print the level order traversal. In this article, Queue data structure is used to add a node. In this article, we will learn about the process of insertion in a binary tree (Level Order) in C++.

This approach ensures that the tree remains as compact as possible and follows the properties of a binary tree. Level order insertion is commonly used when the structure of the tree matters more than the ordering of elements.

Insertion in a Binary Tree in C++

Prerequisite Knowledge:

  1. A Binary Tree is a data structure with maximum of two children for each parent.
  2. Level Order Traversal is an example Of Breadth First Search algorithm.
  3. Level order is a traversal in which each node is visited in the level before we move to a lower level.
  4. Queues are used to find the level order traversal.
Insertion in a Binary Tree in C++

Algorithm:

  1. Create a queue q.
  2. If root is NULL, add node and return.
  3. Else continue until q is not empty.
  4. If a child does not exists, add the node there.
  5. Otherwise add the node to the leftmost node.

Code Implementation for Insertion in a Binary Tree in C++

This section demonstrates how to insert a new node into a binary tree using C++. It explains the logic step by step, showing how the tree structure is maintained while adding elements efficiently.

Run

#include<bits/stdc++.h>
using namespace std;

struct TreeNode
{
  int val;
  TreeNode *left;
  TreeNode *right;
    TreeNode (int x):val (x), left (NULL), right (NULL)
  { }
};

void insert (TreeNode * &root, int val)
{
  if (root == NULL)
    {
      root = new TreeNode (val);
      return;
    }
  if (val < root->val)
    {
      insert (root->left, val);
    }
  else
    {
      insert (root->right, val);
    }
}

void inorder_traversal (TreeNode * root)
{
  if (root == NULL)
    return;
  // Visit Left subtree
  inorder_traversal (root->left);
  // Print the data
  cout << root->val << " "; inorder_traversal (root->right);
  // Visit right subtree inorder_traversal (root->right);
}

int main ()
{
  TreeNode *root = NULL;
  insert (root, 50);
  insert (root, 20);
  insert (root, 70);
  insert (root, 10);
  insert (root, 30);
  insert (root, 60);
  insert (root, 80);
  cout << "The binary tree is : ";
  inorder_traversal (root);
  return 0;
}

Output:

The binary tree is : 10 20 30 50 60 70 80

Explanation:

  • The TreeNode structure defines a node of the Binary Search Tree (BST) with a value and pointers to left and right children.
  • The insert() function adds a new value to the BST by comparing it with the current node and recursively moving left or right.
  • If the root is NULL, a new node is created and assigned as the root of the tree.
  • The inorder_traversal() function visits the left subtree, prints the root value, and then visits the right subtree recursively.
  • In the main() function, multiple values are inserted into the BST, and inorder traversal prints the values in sorted order.

Time and Space Complexity:

OperationTime ComplexitySpace Complexity
InsertionO(h)O(h)
Inorder TraversalO(n)O(h)
Tree CreationO(n)O(n)

Conclusion:

Insertion in a binary tree using level order is a structured approach that ensures nodes are added in the first available position, keeping the tree complete and well-organized. By using a queue, the process efficiently traverses the tree level by level until the correct insertion point is found. This method is simple to implement and plays an important role in understanding how binary trees grow dynamically.

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription