-
Notifications
You must be signed in to change notification settings - Fork 5
/
treebasic.cpp
103 lines (102 loc) · 1.58 KB
/
treebasic.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* left;
struct node * right;
};
struct node * getnode()
{
struct node * temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
void insertleft(struct node*root,int x)
{
struct node* temp,*p;
temp = getnode();
temp->info =x;
p = root;
if(root==NULL)
{
root = temp;
}
else
{
while(p->left!=NULL)
{
p = p->left;
}
p->left = temp;
}
}
void insertright(struct node*root,int x)
{
struct node* temp,*p;
temp = getnode();
temp->info =x;
p = root;
if(root==NULL)
{
root = temp;
}
else
{
while(p->right!=NULL)
{
p = p->right;
}
p->right = temp;
}
}
int searchnode(struct node*root,int x)
{
int z,y;
if(root==NULL)
{
return 0;
}
if(root->info==x)
{
return 1;
}
z = searchnode(root->left,x);
y = searchnode(root->right,x);
return (x||y);
}
int main()
{
struct node * root;
int i,x,y;
root = getnode();
while(1)
{
printf("1) insert node to the left\n2) insert node to the right\n3) search node\n4)Exit...\n");
scanf("%d",&i);
switch(i)
{
case 1:printf("Enter element to be inserted\n");
scanf("%d",&x);
insertleft(root,x);break;
case 2:printf("Enter element to be inserted\n");
scanf("%d",&x);
insertright(root,x);break;
case 3: printf("Enter element to be searched\n");
scanf("%d",&x);
y = searchnode(root,x);
if(y==1)
{
printf("%d found\n",x);
}
else if(y==0)
{
printf("%d not found\n",x);
}break;
case 4: exit(0);
}
}
return 0;
}