C++ program to insert an element into binary tree














































C++ program to insert an element into binary tree



Description:
This Program is to insert the number entered by the user in the binary tree .

For Example:

Input for tree is: 4,5,7,8,9
The entered element to insert into binary tree is : 6

Output will be :
Element Inserted
After inserting array is : 4,5,6,7,8,9

Program :

//C++ program to insert an element into binary tree #include <bits/stdc++.h> using namespace std; // tree node struct Node { int data; Node *left, *right; }; // returns a new tree Node Node* newNode(int data) { Node* temp = new Node(); temp->data = data; temp->left = temp->right = NULL; return temp; } // A function to create binary tree. Node* Tree(Node* temp, int data ) { // If the tree is empty, return a new node if (temp == NULL) return newNode(data); // Otherwise, recur down the tree if (data < temp->data) temp->left = Tree(temp->left, data); else temp->right = Tree(temp->right, data); //return the (unchanged) node pointer return temp; } //function to display all the element present in the binary tree void display(struct Node* root) { if (!root) return; display(root->left); cout<<root->data<<" "; display(root->right); } //function to insert element in binary tree void insert(struct Node* root , int value) { queue<struct Node*> q; q.push(root); // Do level order traversal until we find an empty place. while (!q.empty()) { struct Node* root = q.front(); q.pop(); if (!root->left) { root->left = newNode(value); break; } else q.push(root->left); if (!root->right) { root->right = newNode(value); break; } else q.push(root->right); } } int main() { char ch; int n, arr[20],size; Node *root = new Node; root = NULL; cout<<"Enter the size of array : "; cin>>size; cout<<"Enter the elements in array : "; for(int i=0;i<size;i++) { cin>>arr[i]; } // Construct the binary tree. for(int i = 0; i < size; i++) { root = Tree(root, arr[i]); } cout<<"\nEnter the Element to be insert : "; cin>>n; insert(root,n); cout<<"\nElement Inserted"<<endl; cout<<"\nAfter Inserting "<<endl; cout<<"Elements are: "; display(root); cout<<endl; return 0; }

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 6884 19
C++ program to find an element into binary tree 815 16
C++ std::is_void 615 15
C++ program to find the closest element in binary search tree 1015 19
C++ program to replace every element with the least greater element on its right 615 12
C++ program to delete an element into binary tree 824 24
C++ program to find maximum element between two nodes of binary search tree 740 20
C++ std::remove_copy_if with std::vectors 597 11
C++ program to print duplicate elements from the binary search tree 3097 15
C++ program to find depth of the deepest odd level node in binary tree 614 23
C++ program to remove duplicate elements from the binary search tree 1755 20
C++ std::rotate_copy with std::vector 584 14
C++ std::copy_n with std::vector 671 22
C++ std::copy_if with std::vector 1660 18
C++ program to print all the elements of binary search tree 7983 22
C++ std::reverse_copy with std::list 640 18
C++ program to print all the elements of binary tree 1251 18
C++ program to print all full nodes in a binary tree 609 25
C++ program to find sink odd nodes in binary tree 610 15
C++ std::is_copy_assignable 641 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 664 19
C++ std::is_copy_constructible 663 27
C++ program to delete an element into binary search tree 3197 18
C++ std::reverse_copy with std::vector 526 18
C++ std::rotate with std::vector 798 15
C++ program to check for symmetric binary tree using recursion 630 25
C++ program to maximum sum from a tree with adjacent levels not allowed 593 15
C++ std::copy_n with std::list 641 21
C++ program to check if two trees are identical using recursion 587 15
C++ std::copy_n 966 21
C++ std::copy_if with std::list 1131 19
C++ program to print the nodes at odd levels of a tree 601 13
C++ program to find lowest common ancestor in a binary tree 718 29
C++ program to find depth of the deepest odd level leaf node 529 13
C++ std::remove_copy_if with std::list 718 20
C++ program to add all greater values to every node in a given binary search tree 683 15

Comments