-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCA-bst.c
124 lines (124 loc) · 2.34 KB
/
LCA-bst.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left;
struct node* right;
};
void bst_insert (struct node* root, int newdata)
{
if (root->data>=newdata)
{
if (root->left==NULL)
{
struct node* new_node=(struct node*) malloc (sizeof(struct node));
new_node->data=newdata;
new_node->left=new_node->right=NULL;
root->left=new_node;
return;
}
else
{
bst_insert (root->left, newdata);
return;
}
}
else
{
if (root->right==NULL)
{
struct node* new_node=(struct node*) malloc (sizeof(struct node));
new_node->data=newdata;
new_node->left=new_node->right=NULL;
root->right=new_node;
return;
}
else
{
bst_insert (root->right, newdata);
return;
}
}
}
void build_bst (struct node** head, int newdata)
{
struct node* temp;
temp=(*head);
if (temp==NULL)
{
struct node* new_node=(struct node*) malloc (sizeof(struct node));
new_node->data=newdata;
new_node->left=new_node->right=NULL;
*head=new_node;
return;
}
else
{
bst_insert (temp, newdata);
return;
}
}
void inorder (struct node* root)
{
if (root==NULL)
return;
inorder (root->left);
printf ("%d ",root->data);
inorder (root->right);
return;
}
struct node* lca (struct node* root, int p, int q)
{
struct node* ancestor;
if ((p<=root->data) && (q<=root->data))
{
ancestor=lca (root->left, p, q);
return ancestor;
}
else if ((p>root->data) && (q>root->data))
{
ancestor=lca (root->right, p, q);
return ancestor;
}
else
return root;
}
int main()
{
struct node* header=NULL;
struct node* ancestor;
int opt, newdata, p, q;
opt=5;
while (opt)
{
printf("\n1. enter en element into the tree\n2. inorder\n3. find lowest common ancestor of two numbers in the tree\n0. end of program\n\nchoose one option: ");
scanf("%d",&opt);
if(opt==1)
{
printf("enter the number: ");
scanf("%d",&newdata);
build_bst (&header, newdata);
continue;
}
if(opt==2)
{
printf("the inorder traversal is:\t");
inorder (header);
continue;
}
if(opt==3)
{
printf("the tree elements are:\t\t");
inorder (header);
printf("\nenter the two node numbers from this list:\n");
scanf("%d",&p);
scanf("%d",&q);
ancestor=lca (header, p, q);
printf("the ancestor of %d and %d is %d.", p, q, ancestor->data);
continue;
}
if(opt==0)
printf("end of program\n");
}
return 0;
}