Question: Given the root pointer to a binary tree, write a program to insert an element in it.

    Input: Sample Tree (Pointer to node 1 is given), Insert 7
    tree_insert_element_1
    Output: After insertion
    tree_insert_element_2

    We are given a binary tree with random allocated values and the pointer to the root node is given. Since the given tree is a binary tree, we can insert the element wherever we want. The only property of binary tree is that every node can have a maximum of two children. So, to keep the tree leveled we can perform a level order traversal on the binary tree and insert the element wherever we find the node whose right child or left child is NULL.

    In the above example, while we are doing a level order traversal as soon as we come across node 3, we see that its left child is NULL. So we insert the new node to the left of node 3.

    Here is an implementation of the above approach.

    #include<stdio.h>
    #include<malloc.h>
    
    struct binaryTreeNode{
    	int data;
    	struct binaryTreeNode * left;
    	struct binaryTreeNode * right;
    };
    
    // A function to insert a number in a binary tree
    // Parameters - the root node and the number to insert
    struct binaryTreeNode * insertInBinaryTree(struct binaryTreeNode * root, int num)
    {
    	struct binaryTreeNode * temp = NULL;
    	struct queue * Q = NULL;
    	
    	// Create a new node to insert
    	struct binaryTreeNode * newNode = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
    	
    	// Assign the values to this new node
    	newNode -> data = num;
    	newNode -> left = NULL;
    	newNode -> right = NULL;
    	
    	if(root == NULL)
    	{
    		// If the root is NULL then the newNode is inserted at root
    		root = newNode;
    		return root;
    	}
    	
    	// else proceed wiht the level order traversal
    	Q = enQueue(Q, root);
    	
    	while(!isQueueEmpty(Q))
    	{
    		temp = Q -> front -> data;
    		
    		Q = deQueue(Q);
    		
    		// Now we have a node of the tree in temp
    		// If its left is empty we insert the node else we enqueue its
    		if(temp -> left != NULL)
    		{
    			Q = enQueue(Q, temp -> left);
    		}
    		else
    		{
    			// Assign the new node to its left
    			temp -> left = newNode;
    			
    			// Delete the queue and return
    			free(Q);
    			return root;
    		}
    		
    		// Do the same for the right sub-tree of the traversed node
    		if(temp -> right != NULL)
    		{
    			Q = enQueue(Q, temp -> right);
    		}
    		else
    		{
    			// Assign the node to its right
    			temp -> right = newNode;
    			
    			// Delete the queue and return
    			free(Q);
    			return root;
    		}
    	}
    	
    	free(Q);
    	return root;
    }
    

    Time Complexity:- O(n)
    Space Complexity:- O(n)

    Ideone link for the running program:- http://ideone.com/CFeFKF

    1 comment
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find if an element is present in it. Input: Sample Tree (Pointer to node 1 is given), Search – 3 Output: Present We discussed how to find if an element exists in the binary tree in this post. Find if an element is present in a binary tree That involved recursion. If recursion seems difficult to you, we can even solve this problem with the help of level order traversal of binary tree. The property of level order traversal is that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find if an element is present in it. Input: Sample Tree (Pointer to node 1 is given), Search – 3 Output: Present One simple way of solving this problem is to find the element in the left sub-tree, in the right sub-tree, and in the root data. This approach can be easily implemented with recursion. This approach works because each node of the tree has 3 parts -> the root, the left and the right. What we do is we see …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the maximum element present in it. Input: Sample Tree (Pointer to node 1 is given) Output: Maximum = 9 We discussed how to find the maximum element in a binary tree using recursion in this post. Find maximum element in a binary tree That involved recursion. If recursion seems difficult to you, we can even solve this problem with the help of level order traversal of binary tree. The property of level order traversal is that it passes through each …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Trees

    Find the maximum element in a binary tree

    by nikoo28
    3 minutes read

    Question: Given the root pointer to a binary tree, find the maximum element present in it. Input: Sample Tree (Pointer to node 1 is given) Output: Maximum = 9 One simple way of solving this problem is to find the maximum element in the left sub-tree, find maximum in the right sub-tree, compare it with the root data and select the one that gives the maximum value. This approach can be easily implemented with recursion. This approach works because each node of the tree has 3 parts -> the root, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    Level order Traversal in a Binary Tree

    by nikoo28
    3 minutes read

    We discussed the basic tree traversals in this post – Basic tree traversals We learned about the pre-order traversal, in-order traversal and post-order traversal. These are standard traversals that are related with root, left sub-tree and the right sub-tree. Level order traversal follows an entirely different approach and is not related with the left sub-tree and the right sub-tree. It does not hold the same property for all the nodes and hence recursive technique cannot be applied to level order traversal. Let us try to understand with the help of …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    PostOrder Traversal in a binary tree

    by nikoo28
    3 minutes read

    We discussed about the basic tree traversals in this post – Binary Tree Traversals Lets take our own sample tree for understanding post-order traversal. In Post-Order traversal, the root is visited after both sub-trees are completed. Post-order traversal is defined as follows:- Traverse the left sub-tree in post-order. (Step 1) Traverse the right sub-tree in post-order. (Step 2) Visit the root The post-order traversal can then be defined in this way – The nodes of the tree will therefore be in order – 4, 5, 2, 6, 7, 3, 1 …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More