Binary Insertion in Tree in C Easy

Insertion in a Binary Tree in level order

View Discussion

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Like Article

    Given a binary tree and a key, insert the key into the binary tree at the first position available in level order.

    The idea is to do an iterative level order traversal of the given tree using queue. If we find a node whose left child is empty, we make a new key as the left child of the node. Else if we find a node whose right child is empty, we make the new key as the right child. We keep traversing the tree until we find a node whose either left or right child is empty.

    C++

    #include <iostream>

    #include <queue>

    using namespace std;

    struct Node {

    int data;

    Node* left;

    Node* right;

    };

    Node* CreateNode( int data)

    {

    Node* newNode = new Node();

    if (!newNode) {

    cout << "Memory error\n" ;

    return NULL;

    }

    newNode->data = data;

    newNode->left = newNode->right = NULL;

    return newNode;

    }

    Node* InsertNode(Node* root, int data)

    {

    if (root == NULL) {

    root = CreateNode(data);

    return root;

    }

    queue<Node*> q;

    q.push(root);

    while (!q.empty()) {

    Node* temp = q.front();

    q.pop();

    if (temp->left != NULL)

    q.push(temp->left);

    else {

    temp->left = CreateNode(data);

    return root;

    }

    if (temp->right != NULL)

    q.push(temp->right);

    else {

    temp->right = CreateNode(data);

    return root;

    }

    }

    }

    void inorder(Node* temp)

    {

    if (temp == NULL)

    return ;

    inorder(temp->left);

    cout << temp->data << ' ' ;

    inorder(temp->right);

    }

    int main()

    {

    Node* root = CreateNode(10);

    root->left = CreateNode(11);

    root->left->left = CreateNode(7);

    root->right = CreateNode(9);

    root->right->left = CreateNode(15);

    root->right->right = CreateNode(8);

    cout << "Inorder traversal before insertion: " ;

    inorder(root);

    cout << endl;

    int key = 12;

    root = InsertNode(root, key);

    cout << "Inorder traversal after insertion: " ;

    inorder(root);

    cout << endl;

    return 0;

    }

    Java

    import java.util.LinkedList;

    import java.util.Queue;

    public class GFG {

    static class Node {

    int key;

    Node left, right;

    Node( int key)

    {

    this .key = key;

    left = null ;

    right = null ;

    }

    }

    static Node root;

    static Node temp = root;

    static void inorder(Node temp)

    {

    if (temp == null )

    return ;

    inorder(temp.left);

    System.out.print(temp.key + " " );

    inorder(temp.right);

    }

    static void insert(Node temp, int key)

    {

    if (temp == null ) {

    root = new Node(key);

    return ;

    }

    Queue<Node> q = new LinkedList<Node>();

    q.add(temp);

    while (!q.isEmpty()) {

    temp = q.peek();

    q.remove();

    if (temp.left == null ) {

    temp.left = new Node(key);

    break ;

    }

    else

    q.add(temp.left);

    if (temp.right == null ) {

    temp.right = new Node(key);

    break ;

    }

    else

    q.add(temp.right);

    }

    }

    public static void main(String args[])

    {

    root = new Node( 10 );

    root.left = new Node( 11 );

    root.left.left = new Node( 7 );

    root.right = new Node( 9 );

    root.right.left = new Node( 15 );

    root.right.right = new Node( 8 );

    System.out.print(

    "Inorder traversal before insertion:" );

    inorder(root);

    int key = 12 ;

    insert(root, key);

    System.out.print(

    "\nInorder traversal after insertion:" );

    inorder(root);

    }

    }

    Python3

    class newNode():

    def __init__( self , data):

    self .key = data

    self .left = None

    self .right = None

    def inorder(temp):

    if ( not temp):

    return

    inorder(temp.left)

    print (temp.key,end = " " )

    inorder(temp.right)

    def insert(temp,key):

    if not temp:

    root = newNode(key)

    return

    q = []

    q.append(temp)

    while ( len (q)):

    temp = q[ 0 ]

    q.pop( 0 )

    if ( not temp.left):

    temp.left = newNode(key)

    break

    else :

    q.append(temp.left)

    if ( not temp.right):

    temp.right = newNode(key)

    break

    else :

    q.append(temp.right)

    if __name__ = = '__main__' :

    root = newNode( 10 )

    root.left = newNode( 11 )

    root.left.left = newNode( 7 )

    root.right = newNode( 9 )

    root.right.left = newNode( 15 )

    root.right.right = newNode( 8 )

    print ( "Inorder traversal before insertion:" , end = " " )

    inorder(root)

    key = 12

    insert(root, key)

    print ()

    print ( "Inorder traversal after insertion:" , end = " " )

    inorder(root)

    C#

    using System;

    using System.Collections.Generic;

    class GFG {

    public class Node {

    public int key;

    public Node left, right;

    public Node( int key)

    {

    this .key = key;

    left = null ;

    right = null ;

    }

    }

    static Node root;

    static void inorder(Node temp)

    {

    if (temp == null )

    return ;

    inorder(temp.left);

    Console.Write(temp.key + " " );

    inorder(temp.right);

    }

    static void insert(Node temp, int key)

    {

    if (temp == null ) {

    root = new Node(key);

    return ;

    }

    Queue<Node> q = new Queue<Node>();

    q.Enqueue(temp);

    while (q.Count != 0) {

    temp = q.Peek();

    q.Dequeue();

    if (temp.left == null ) {

    temp.left = new Node(key);

    break ;

    }

    else

    q.Enqueue(temp.left);

    if (temp.right == null ) {

    temp.right = new Node(key);

    break ;

    }

    else

    q.Enqueue(temp.right);

    }

    }

    public static void Main(String[] args)

    {

    root = new Node(10);

    root.left = new Node(11);

    root.left.left = new Node(7);

    root.right = new Node(9);

    root.right.left = new Node(15);

    root.right.right = new Node(8);

    Console.Write(

    "Inorder traversal before insertion:" );

    inorder(root);

    int key = 12;

    insert(root, key);

    Console.Write(

    "\nInorder traversal after insertion:" );

    inorder(root);

    }

    }

    Javascript

    <script>

    class Node {

    constructor(val) {

    this .key = val;

    this .left = null ;

    this .right = null ;

    }

    }

    var temp;

    function inorder(temp) {

    if (temp == null )

    return ;

    inorder(temp.left);

    document.write(temp.key + " " );

    inorder(temp.right);

    }

    function insert(temp , key) {

    if (temp == null ) {

    root = new Node(key);

    return ;

    }

    var q = [];

    q.push(temp);

    while (q.length!=0) {

    temp = q.pop();

    if (temp.left == null ) {

    temp.left = new Node(key);

    break ;

    } else

    q.push(temp.left);

    if (temp.right == null ) {

    temp.right = new Node(key);

    break ;

    } else

    q.push(temp.right);

    }

    }

    var root = new Node(10);

    root.left = new Node(11);

    root.left.left = new Node(7);

    root.right = new Node(9);

    root.right.left = new Node(15);

    root.right.right = new Node(8);

    document.write( "Inorder traversal before insertion:" );

    inorder(root);

    var key = 12;

    insert(root, key);

    document.write( "<br\>Inorder traversal after insertion:" );

    inorder(root);

    </script>

    Output

    Inorder traversal before insertion: 7 11 10 15 9 8  Inorder traversal after insertion: 7 11 12 10 15 9 8            

    Time Complexity:O(V) where V is the number of nodes.
    Auxiliary Space: O(B), where B is the width of the tree and in the worst case we need to hold all vertices of a level in the queue.

    This article is contributed by Yash Singla. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


    hallfately80.blogspot.com

    Source: https://www.geeksforgeeks.org/insertion-in-a-binary-tree-in-level-order/

    0 Response to "Binary Insertion in Tree in C Easy"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel