-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfortyEighth.cpp
83 lines (83 loc) · 1.61 KB
/
fortyEighth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;
struct node {
int data;
node* right;
node* left;
};
node* root;
node* getNewNode(int x)
{
node* temp = new node();
temp->data = x;
temp->right = NULL;
temp->left = NULL;
return temp;
}
void insert(node* tt, int x) {
if (tt == NULL) {
tt = getNewNode(x);
}
else{
node* mark;
node* ttt = tt;
while (ttt != NULL) {
if (ttt->data < x){
mark = ttt;
ttt = ttt->right;
}
else if (ttt->data > x ) {
mark = ttt;
ttt = ttt->left;
}
}
if (mark->data > x) {
mark->left = getNewNode(x);
}
else
mark->right = getNewNode(x);
}
root = tt;
}
void display(node* tt) {
if (tt != NULL) {
display(tt->left);
std::cout << tt->data << " ";
display(tt->right);
}
}
void find(int x) {
node* temp = root;
if (temp == NULL) {
std::cout << "No tree" << '\n';
}
else
{
while(temp != NULL)
if (temp->data == x) {
std::cout << "Element is found." << '\n';
break;
}
else if (temp->data > x) {
temp = temp->left;
}
else
temp = temp->right;
}
}
int main(int argc, char const *argv[]) {
root = NULL;
insert(root, 6);
insert(root, 7);
insert(root, 4);
insert(root, 3);
insert(root, 2);
insert(root, 8);
insert(root, 1);
insert(root, 5);
insert(root, 222);
display(root);
std::cout << '\n';
find(3);
return 0;
}