C++ program to print duplicate elements from the binary search tree














































C++ program to print duplicate elements from the binary search tree



Description:
This Program is to find the duplicate element from the binary search tree.

For example:
Input tree : 4,4,5,6,8,7

Output :
Duplicate element is there for : 4 No duplicate element of : 5 No duplicate element of : 6 No duplicate element of : 7 No duplicate element of : 8

Program :
//C++ program to print duplicate elements from the binary search tree #include <bits/stdc++.h> using namespace std; // tree nod struct Node { int data; int key ,count; Node *left, *right; }; // returns a new tree Node Node* newNode(int data) { struct Node *temp = (struct Node *)malloc(sizeof(struct Node)); temp->key = data; temp->left = temp->right = NULL; temp->count = 1; return temp; } // A function to create binary search tree. Node* Tree(Node* temp, int key ) { // If the tree is empty, return a new node if (temp == NULL) return newNode(key); if (key == temp->key) { (temp->count)++; return temp; } // Otherwise, recur down the tree if (key < temp->key) temp->left = Tree(temp->left, key); else temp->right = Tree(temp->right, key); //return the (unchanged) node pointer return temp; } //function to display all the element present in the binary search tree void display(struct Node* root) { if (root != NULL) { display(root->left); if(root->count>1) { cout<<"Duplicate element is there for : "<<root->key<<endl; } else cout<<"No duplicate element of : "<<root->key<<endl; 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 tree. for(int i = 0; i < size; i++) { root = Tree(root, arr[i]); } display(root); return 0; }


More Articles of Mandeep Sheoran:

Name Views Likes
C++ program to insert an element into binary tree 6563 19
C++ program to find an element into binary tree 789 16
C++ std::is_void 590 15
C++ program to find the closest element in binary search tree 951 19
C++ program to replace every element with the least greater element on its right 590 12
C++ program to delete an element into binary tree 797 24
C++ program to find maximum element between two nodes of binary search tree 717 20
C++ std::remove_copy_if with std::vectors 562 11
C++ program to print duplicate elements from the binary search tree 3013 15
C++ program to find depth of the deepest odd level node in binary tree 588 23
C++ program to remove duplicate elements from the binary search tree 1660 20
C++ std::rotate_copy with std::vector 547 14
C++ std::copy_n with std::vector 654 22
C++ std::copy_if with std::vector 1601 18
C++ program to print all the elements of binary search tree 7568 22
C++ std::reverse_copy with std::list 615 18
C++ program to print all the elements of binary tree 1206 18
C++ program to print all full nodes in a binary tree 587 25
C++ program to find sink odd nodes in binary tree 594 15
C++ std::is_copy_assignable 617 22
C++ program to check whether a binary tree is a full binary tree or not using recursion 629 19
C++ std::is_copy_constructible 636 27
C++ program to delete an element into binary search tree 3049 18
C++ std::reverse_copy with std::vector 503 18
C++ std::rotate with std::vector 707 15
C++ program to check for symmetric binary tree using recursion 611 25
C++ program to maximum sum from a tree with adjacent levels not allowed 567 15
C++ std::copy_n with std::list 608 21
C++ program to check if two trees are identical using recursion 562 15
C++ std::copy_n 914 21
C++ std::copy_if with std::list 1067 19
C++ program to print the nodes at odd levels of a tree 580 13
C++ program to find lowest common ancestor in a binary tree 702 29
C++ program to find depth of the deepest odd level leaf node 509 13
C++ std::remove_copy_if with std::list 688 20
C++ program to add all greater values to every node in a given binary search tree 656 15

Comments