forked from rmkcodingclub/AmazonProblems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem26.c
193 lines (160 loc) · 3.85 KB
/
problem26.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
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
184
185
186
187
188
189
190
191
192
193
//By Sathish Kumar S, ECE
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
/* just add elements to test */
/* NOTE: A sorted array results in skewed tree */
int ele[] = { 20, 8, 22, 4, 12, 10, 14 };
/* same alias */
typedef struct node_t node_t;
/* Binary tree node */
struct node_t
{
int data;
node_t* left;
node_t* right;
};
/* simple stack that stores node addresses */
typedef struct stack_t stack_t;
/* initial element always NULL, uses as sentinal */
struct stack_t
{
node_t* base[ARRAY_SIZE(ele) + 1];
int stackIndex;
};
/* pop operation of stack */
node_t *pop(stack_t *st)
{
node_t *ret = NULL;
if( st && st->stackIndex > 0 )
{
ret = st->base[st->stackIndex];
st->stackIndex--;
}
return ret;
}
/* push operation of stack */
void push(stack_t *st, node_t *node)
{
if( st )
{
st->stackIndex++;
st->base[st->stackIndex] = node;
}
}
/* Iterative insertion
Recursion is least preferred unless we gain something
*/
node_t *insert_node(node_t *root, node_t* node)
{
/* A crawling pointer */
node_t *pTraverse = root;
node_t *currentParent = root;
// Traverse till appropriate node
while(pTraverse)
{
currentParent = pTraverse;
if( node->data < pTraverse->data )
{
/* left subtree */
pTraverse = pTraverse->left;
}
else
{
/* right subtree */
pTraverse = pTraverse->right;
}
}
/* If the tree is empty, make it as root node */
if( !root )
{
root = node;
}
else if( node->data < currentParent->data )
{
/* Insert on left side */
currentParent->left = node;
}
else
{
/* Insert on right side */
currentParent->right = node;
}
return root;
}
/* Elements are in an array. The function builds binary tree */
node_t* binary_search_tree(node_t *root, int keys[], int const size)
{
int iterator;
node_t *new_node = NULL;
for(iterator = 0; iterator < size; iterator++)
{
new_node = (node_t *)malloc( sizeof(node_t) );
/* initialize */
new_node->data = keys[iterator];
new_node->left = NULL;
new_node->right = NULL;
/* insert into BST */
root = insert_node(root, new_node);
}
return root;
}
node_t *k_smallest_element_inorder(stack_t *stack, node_t *root, int k)
{
stack_t *st = stack;
node_t *pCrawl = root;
/* move to left extremen (minimum) */
while( pCrawl )
{
push(st, pCrawl);
pCrawl = pCrawl->left;
}
/* pop off stack and process each node */
while( pCrawl = pop(st) )
{
/* each pop operation emits one element
in the order
*/
if( !--k )
{
/* loop testing */
st->stackIndex = 0;
break;
}
/* there is right subtree */
if( pCrawl->right )
{
/* push the left subtree of right subtree */
pCrawl = pCrawl->right;
while( pCrawl )
{
push(st, pCrawl);
pCrawl = pCrawl->left;
}
/* pop off stack and repeat */
}
}
/* node having k-th element or NULL node */
return pCrawl;
}
/* Driver program to test above functions */
int main(void)
{
node_t* root = NULL;
stack_t stack = { {0}, 0 };
node_t *kNode = NULL;
int k = 5;
/* Creating the tree given in the above diagram */
root = binary_search_tree(root, ele, ARRAY_SIZE(ele));
kNode = k_smallest_element_inorder(&stack, root, k);
if( kNode )
{
printf("kth smallest elment for k = %d is %d", k, kNode->data);
}
else
{
printf("There is no such element");
}
getchar();
return 0;
}