-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBtree.cpp
More file actions
82 lines (72 loc) · 1.04 KB
/
Btree.cpp
File metadata and controls
82 lines (72 loc) · 1.04 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* binary search tree */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
};
int main()
{
struct node *root;
int x[30],n,i;
struct node *bst(int [],int );
void inorder(struct node *);
printf("enter the no of elements ");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
root=bst(x,n);
inorder(root);
return(0);
}
struct node *bst(int x[30],int n)
{
int i;
struct node *tree,*p,*q,*h;
tree=(struct node *)malloc(sizeof(struct node));
tree->data=x[0];
tree->left=NULL;
tree->right=NULL;
for(i=1;i<n;i++)
{
p=tree;
while(p!=NULL)
{
q=p;
h=(struct node *)malloc(sizeof(struct node));
h->data=x[i];
h->left=NULL;
h->right=NULL;
if(h->data<p->data)
{
p=p->left;
}
else
{
p=p->right;
}
}
if(h->data<q->data)
{
q->left=h;
}
else
{
q->right=h;
}
}
return(tree);
}
void inorder(struct node *pq)
{
if(pq!=NULL)
{
inorder(pq->left);
printf("%d\t",pq->data);
inorder(pq->right);
}
}