#include <iostream>
using namespace std;
struct node
{
int data;
node*left;
node* right;
};
class tree
{
private: node *parent, *start, *cur, *temp;
public:
tree()
{
start = NULL;
}
void create(int x)
{
if (start == NULL)
{
start = new node;
start->left = NULL;
start->data = x;
start->right = NULL;
}
else
{
cur = start;
while (cur != NULL)
{
if (cur->data == x)
{
cout << "element already exist";
}
else if (cur->data < x)
{
parent = cur;
cur = cur->right;
}
else
{
parent = cur;
cur = cur->left;
}
temp = new node;
temp->data = x;
temp->left = NULL;
temp->right = NULL;
if (parent == NULL)
{
parent = temp;
}
else if (parent->data > x)
parent->left = temp;
else
parent->right = temp;
}
}
}
};
int main()
{
int val;
tree obj;
for (int i = 0; i < 6; i++)
{
cout << "Enter vale to Tree: ";
cin >> val;
obj.create(val);
}
}
using namespace std;
struct node
{
int data;
node*left;
node* right;
};
class tree
{
private: node *parent, *start, *cur, *temp;
public:
tree()
{
start = NULL;
}
void create(int x)
{
if (start == NULL)
{
start = new node;
start->left = NULL;
start->data = x;
start->right = NULL;
}
else
{
cur = start;
while (cur != NULL)
{
if (cur->data == x)
{
cout << "element already exist";
}
else if (cur->data < x)
{
parent = cur;
cur = cur->right;
}
else
{
parent = cur;
cur = cur->left;
}
temp = new node;
temp->data = x;
temp->left = NULL;
temp->right = NULL;
if (parent == NULL)
{
parent = temp;
}
else if (parent->data > x)
parent->left = temp;
else
parent->right = temp;
}
}
}
};
int main()
{
int val;
tree obj;
for (int i = 0; i < 6; i++)
{
cout << "Enter vale to Tree: ";
cin >> val;
obj.create(val);
}
}