-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathKthSmallestElement.cpp
More file actions
76 lines (64 loc) · 1.44 KB
/
Copy pathKthSmallestElement.cpp
File metadata and controls
76 lines (64 loc) · 1.44 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
#include <iostream>
using namespace std;
// A BST node
struct Node {
int data;
Node *left, *right;
Node(int x)
{
data = x;
left = right = NULL;
}
};
// Recursive function to insert an key into BST
Node* insert(Node* root, int x)
{
if (root == NULL)
return new Node(x);
if (x < root->data)
root->left = insert(root->left, x);
else if (x > root->data)
root->right = insert(root->right, x);
return root;
}
// Function to find k'th largest element in BST
// Here count denotes the number of nodes processed so far
Node* kthSmallest(Node* root, int& k)
{
// base case
if (root == NULL)
return NULL;
// search in left subtree
Node* left = kthSmallest(root->left, k);
// if k'th smallest is found in left subtree, return it
if (left != NULL)
return left;
// if current element is k'th smallest, return it
k--;
if (k == 0)
return root;
// else search in right subtree
return kthSmallest(root->right, k);
}
// Function to find k'th largest element in BST
void printKthSmallest(Node* root, int k)
{
// maintain index to count number of nodes processed so far
int count = 0;
Node* res = kthSmallest(root, k);
if (res == NULL)
cout << "There are less than k nodes in the BST";
else
cout << "K-th Smallest Element is " << res->data;
}
// main function
int main()
{
Node* root = NULL;
int keys[] = { 20, 8, 22, 4, 12, 10, 14 };
for (int x : keys)
root = insert(root, x);
int k = 3;
printKthSmallest(root, k);
return 0;
}