-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.cpp
More file actions
62 lines (60 loc) · 1.21 KB
/
bst.cpp
File metadata and controls
62 lines (60 loc) · 1.21 KB
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
#include <bits/stdc++.h>
using namespace std;
struct node
{
int key;
struct node *left,*right;
};
struct node * newnode(int item)
{
struct node *temp =(struct node*)malloc(sizeof(struct node));
temp->key=item;
temp->left=temp->right=NULL;
return temp;
}
void inorder(struct node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<<root->key<<" ";
inorder(root->right);
}
}
struct node * insert(struct node* node,int key)
{
if(node==NULL)
return newnode(key);
else if(key<node->key)
node->left =insert(node->left,key);
else
node->right=insert(node->right,key);
return node;
}
struct node* search(struct node* root,int se)
{
if(root->key==se||root == NULL)
return root;
if(root->key>se)
search(root->left,se);
search(root->right,se);
}
using namespace std;
int main()
{
struct node* root= NULL;
struct node * t=NULL;
root=insert(root,50);
insert(root,30);
insert(root,70);
insert(root,42);
insert(root,57);
inorder(root);
t= search(root,30);
if(t)
cout<<"element found";
else
cout<<"not found";
//t=search(root,30);
//t=search(root,10);
}