Skip to content

Commit 4c25b65

Browse files
committed
Create problem26.c
1 parent 7208b5f commit 4c25b65

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

problem26.c

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
//By Sathish Kumar S, ECE
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
6+
7+
/* just add elements to test */
8+
/* NOTE: A sorted array results in skewed tree */
9+
int ele[] = { 20, 8, 22, 4, 12, 10, 14 };
10+
11+
/* same alias */
12+
typedef struct node_t node_t;
13+
14+
/* Binary tree node */
15+
struct node_t
16+
{
17+
int data;
18+
19+
node_t* left;
20+
node_t* right;
21+
};
22+
23+
/* simple stack that stores node addresses */
24+
typedef struct stack_t stack_t;
25+
26+
/* initial element always NULL, uses as sentinal */
27+
struct stack_t
28+
{
29+
node_t* base[ARRAY_SIZE(ele) + 1];
30+
int stackIndex;
31+
};
32+
33+
/* pop operation of stack */
34+
node_t *pop(stack_t *st)
35+
{
36+
node_t *ret = NULL;
37+
38+
if( st && st->stackIndex > 0 )
39+
{
40+
ret = st->base[st->stackIndex];
41+
st->stackIndex--;
42+
}
43+
44+
return ret;
45+
}
46+
47+
/* push operation of stack */
48+
void push(stack_t *st, node_t *node)
49+
{
50+
if( st )
51+
{
52+
st->stackIndex++;
53+
st->base[st->stackIndex] = node;
54+
}
55+
}
56+
57+
/* Iterative insertion
58+
Recursion is least preferred unless we gain something
59+
*/
60+
node_t *insert_node(node_t *root, node_t* node)
61+
{
62+
/* A crawling pointer */
63+
node_t *pTraverse = root;
64+
node_t *currentParent = root;
65+
66+
// Traverse till appropriate node
67+
while(pTraverse)
68+
{
69+
currentParent = pTraverse;
70+
71+
if( node->data < pTraverse->data )
72+
{
73+
/* left subtree */
74+
pTraverse = pTraverse->left;
75+
}
76+
else
77+
{
78+
/* right subtree */
79+
pTraverse = pTraverse->right;
80+
}
81+
}
82+
83+
/* If the tree is empty, make it as root node */
84+
if( !root )
85+
{
86+
root = node;
87+
}
88+
else if( node->data < currentParent->data )
89+
{
90+
/* Insert on left side */
91+
currentParent->left = node;
92+
}
93+
else
94+
{
95+
/* Insert on right side */
96+
currentParent->right = node;
97+
}
98+
99+
return root;
100+
}
101+
102+
/* Elements are in an array. The function builds binary tree */
103+
node_t* binary_search_tree(node_t *root, int keys[], int const size)
104+
{
105+
int iterator;
106+
node_t *new_node = NULL;
107+
108+
for(iterator = 0; iterator < size; iterator++)
109+
{
110+
new_node = (node_t *)malloc( sizeof(node_t) );
111+
112+
/* initialize */
113+
new_node->data = keys[iterator];
114+
new_node->left = NULL;
115+
new_node->right = NULL;
116+
117+
/* insert into BST */
118+
root = insert_node(root, new_node);
119+
}
120+
121+
return root;
122+
}
123+
124+
node_t *k_smallest_element_inorder(stack_t *stack, node_t *root, int k)
125+
{
126+
stack_t *st = stack;
127+
node_t *pCrawl = root;
128+
129+
/* move to left extremen (minimum) */
130+
while( pCrawl )
131+
{
132+
push(st, pCrawl);
133+
pCrawl = pCrawl->left;
134+
}
135+
136+
/* pop off stack and process each node */
137+
while( pCrawl = pop(st) )
138+
{
139+
/* each pop operation emits one element
140+
in the order
141+
*/
142+
if( !--k )
143+
{
144+
/* loop testing */
145+
st->stackIndex = 0;
146+
break;
147+
}
148+
149+
/* there is right subtree */
150+
if( pCrawl->right )
151+
{
152+
/* push the left subtree of right subtree */
153+
pCrawl = pCrawl->right;
154+
while( pCrawl )
155+
{
156+
push(st, pCrawl);
157+
pCrawl = pCrawl->left;
158+
}
159+
160+
/* pop off stack and repeat */
161+
}
162+
}
163+
164+
/* node having k-th element or NULL node */
165+
return pCrawl;
166+
}
167+
168+
/* Driver program to test above functions */
169+
int main(void)
170+
{
171+
node_t* root = NULL;
172+
stack_t stack = { {0}, 0 };
173+
node_t *kNode = NULL;
174+
175+
int k = 5;
176+
177+
/* Creating the tree given in the above diagram */
178+
root = binary_search_tree(root, ele, ARRAY_SIZE(ele));
179+
180+
kNode = k_smallest_element_inorder(&stack, root, k);
181+
182+
if( kNode )
183+
{
184+
printf("kth smallest elment for k = %d is %d", k, kNode->data);
185+
}
186+
else
187+
{
188+
printf("There is no such element");
189+
}
190+
191+
getchar();
192+
return 0;
193+
}

0 commit comments

Comments
 (0)