C++ program to print all the elements of binary search tree














































C++ program to print all the elements of binary search tree



Description:
This Program is to print all the elements present in the binary search tree.Let's learn it by the help of example:

Input : Tree elements are : 1 2 3 5 6 4 7 8 9 10
Output : All elements in binary search tree are : 1 2 3 5 6 4 7 8 9 10

Program :

//C++ program to print all the elements of binary search 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 search tree. Node* Tree(Node* root, int data) { // Create node using data entered by user. Node *temp = newNode(data); Node *temp1 = new Node; temp1 = root; // If root is null then the node created. if(root == NULL) root = temp; else { // Search the position for the new node to be inserted. while(temp1 != NULL) { if(temp1->data < data ) { if(temp1->right == NULL) { // If current node is NULL then the value will be inserted here and break. temp1->right = temp; break; } // Shift pointer to the left. temp1 = temp1->right; } else if(temp1->data > data) { if(temp1->left == NULL) { temp1->left = temp; break; } // Shift pointer to the left. temp1 = temp1->left; } } } return root; } //function to display all the element present in the binary search tree void display(struct Node* root) { if (root != NULL) { display(root->left); cout<<root->data<<" "; display(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 search tree. for(int i = 0; i < size; i++) root = Tree(root, arr[i]); cout<<"\nEntered Element are : "; display(root); return 0; }

More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 7886 19
C++ program to find an element into binary tree 869 16
C++ std::is_void 646 15
C++ program to find the closest element in binary search tree 1120 19
C++ program to replace every element with the least greater element on its right 649 12
C++ program to delete an element into binary tree 878 24
C++ program to find maximum element between two nodes of binary search tree 760 20
C++ std::remove_copy_if with std::vectors 642 11
C++ program to print duplicate elements from the binary search tree 3262 15
C++ program to find depth of the deepest odd level node in binary tree 643 23
C++ program to remove duplicate elements from the binary search tree 1913 20
C++ std::rotate_copy with std::vector 626 14
C++ std::copy_n with std::vector 718 22
C++ std::copy_if with std::vector 1738 18
C++ program to print all the elements of binary search tree 8834 22
C++ std::reverse_copy with std::list 673 18
C++ program to print all the elements of binary tree 1611 18
C++ program to print all full nodes in a binary tree 630 25
C++ program to find sink odd nodes in binary tree 649 15
C++ std::is_copy_assignable 678 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 720 19
C++ std::is_copy_constructible 697 27
C++ program to delete an element into binary search tree 3735 18
C++ std::reverse_copy with std::vector 559 18
C++ std::rotate with std::vector 931 15
C++ program to check for symmetric binary tree using recursion 664 25
C++ program to maximum sum from a tree with adjacent levels not allowed 640 15
C++ std::copy_n with std::list 689 21
C++ program to check if two trees are identical using recursion 611 15
C++ std::copy_n 1009 21
C++ std::copy_if with std::list 1237 19
C++ program to print the nodes at odd levels of a tree 631 13
C++ program to find lowest common ancestor in a binary tree 765 29
C++ program to find depth of the deepest odd level leaf node 547 13
C++ std::remove_copy_if with std::list 753 20
C++ program to add all greater values to every node in a given binary search tree 728 15

Comments