DSA TREE
unknown
plain_text
2 years ago
992 B
9
Indexable
#include<iostream>
using namespace std;
struct node{
int data ;
node *left ;
node *right ;
};
class A{
public :
struct node* get(int data){
node *newnode = new node;
newnode->data=data;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node* insert(struct node* root , int data){
if(root ==NULL){
return get(data);
}
if (data<root->data){
root->left=insert(root->left,data);
}
else if (data>root->data){
root->right =insert(root->right,data);
}
return root;
}
void display(struct node* root){
if (root !=NULL){
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
};
int main(){
node *root=NULL;
int n,data;
cout<<"\n\t How many values you want to enter ?";
cin>>n;
A a1;
cout<<"\n\t Enter value ";
cin>>data;
root =a1.insert(root,data);\
cout<<"\n\t Enter value ";
cin>>data;
a1.insert(root ,data);
a1.display(root);
}Editor is loading...