forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ancestors_of_given_number_BST.cpp
183 lines (154 loc) · 3.19 KB
/
ancestors_of_given_number_BST.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Given a Binary Search Tree, the program finds the Youngest and Oldest
member of given two numbers.
Youngest member is the Parent of the given 2 nodes only if they are siblings.
Oldest member is the root node of the BST.
*/
#include<iostream>
using namespace std;
struct BST
{
int a,count = 0, level = 0;
BST *left = NULL;
BST *right = NULL;
};
//points to the root node
BST *start = NULL;
int l1,l2;
//function to convert array into Binary Search Tree with shortest height
BST *arr_to_BST(int arr[20], int beg, int end)
{
BST *ptr;
if(beg <= end)
{
ptr = new BST;
ptr->left = NULL;
ptr->right = NULL;
ptr->a = arr[(beg+end)/2];
int mid = (beg+end)/2;
ptr->left = arr_to_BST(arr,beg,mid-1);
ptr->right = arr_to_BST(arr,mid+1,end);
return ptr;
}
else
return NULL;
}
//function to print Binary Search Tree
void Display_BST(BST *ptr)
{
cout<<"\na:"<<ptr->a;
if(ptr->left != NULL)
cout<<"\tleft:"<<ptr->left->a;
if(ptr->right != NULL)
cout<<"\tright:"<<ptr->right->a;
if(ptr->left != NULL)
Display_BST(ptr->left);
if(ptr->right != NULL)
Display_BST(ptr->right);
}
//function to assign level values to corresponding nodes
void level_assign(BST *ptr, int value=0)
{
ptr->level = value;
if(ptr->left != NULL)
level_assign(ptr->left,value+1);
if(ptr->right != NULL)
level_assign(ptr->right,value+1);
}
//returns level of the number
int find_number(BST *ptr,int n)
{
if(ptr->a == n)
return ptr->level;
else if(n < ptr->a)
find_number(ptr->left,n);
else
find_number(ptr->right,n);
}
//function to print the value of youngest member for given numbers
void Display(BST *ptr, int n1, int n2)
{
if(n1 < ptr->a && n2 > ptr->a)
{
cout<<"\nyoungest:"<<ptr->a;
return;
}
else if(ptr->level == (l1-1) || ptr->level == (l2-1))
{
cout<<"\nyoungest:"<<ptr->a;
return;
}
if(ptr->left != NULL && (n1 < ptr->a || n2 < ptr->a))
Display(ptr->left,n1,n2);
else if(ptr->right != NULL && (n1 > ptr->a || n2 > ptr->a))
Display(ptr->right, n1, n2);
}
int main()
{
int n, arr[20], n1, n2, T;
cout<<"\nEnter number of elements:";
cin>>n;
cout<<"\nEnter sorted array:";
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}
start = arr_to_BST(arr, 0, n-1);
level_assign(start);
cout<<"\nEnter number of test cases:";
cin>>T;
Display_BST(start);
while(T--)
{
cout<<"\nEnter first number:";
cin>>n1;
cout<<"\nEnter second number:";
cin>>n2;
l1 = find_number(start,n1);
l2 = find_number(start,n2);
//print the eldest member of Binary Search Tree
cout<<"\neldest:"<<start->a;
if(n1 > n2)
{
if(n2 < start->a && n1 > start->a)
;
else
Display(start, n2, n1);
}
else
{
if(n1 < start->a && n2 > start->a)
;
else
Display(start, n1, n2);
}
}
return 0;
}
/*
For example:
The sample output screen will be:
Enter number of elements:7
Enter sorted array:
12 34 45 56 67 78 89
a:56 left:34 right:78
a:34 left:12 right:45
a:12
a:45
a:78 left:67 right:89
a:67
a:89
//the program runs for n test cases
Enter number of test cases:3
Enter first number:78
Enter second number:89
eldest:56
youngest:56
Enter first number:12
Enter second number:45
eldest:56
youngest:34
Enter first number:34
Enter second number:78
eldest:56
*/