-
Notifications
You must be signed in to change notification settings - Fork 0
/
kLeastValueBST.cc
81 lines (73 loc) · 1.75 KB
/
kLeastValueBST.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stack>
using namespace std;
typedef struct elementT {
struct elementT *left;
struct elementT *right;
int data;
} element;
void getKLeastSub(element *ele, int *num, int k) {
if(!ele)
return;
getKLeastSub(ele->left, num, k);
(*num)++;
if(*num == k)
printf("%d:%d\n", *num, ele->data);
getKLeastSub(ele->right, num, k);
}
int getKLeastValue(element *root, int k) {
int num = 0;
getKLeastSub(root, &num, k);
}
int getKLeastValueIterative(element *root, int k) {
stack<element *>s;
element *curr = root;
int num = 0;
while(true) {
if(curr) {
s.push(curr);
curr = curr->left;
} else {
if(s.empty())
break;
curr = s.top();
s.pop();
num++;
if(num == k)
return curr->data;
curr = curr->right;
}
}
}
void getKLargestSub(element *ele, int *num, int k) {
if(!ele)
return;
getKLargestSub(ele->right, num, k);
(*num)++;
if(*num == k)
printf("%d:%d\n", *num, ele->data);
getKLargestSub(ele->left, num, k);
}
void getKLargestValue(element *root, int k) {
int num = 0;
getKLargestSub(root, &num, k);
}
element *makeNode(int d) {
element *ele = (element *)malloc(sizeof(element));
ele->data = d;
return ele;
}
main()
{
element *root = makeNode(50);
root->left = makeNode(25);
root->left->left = makeNode(12);
root->left->right = makeNode(30);
root->right = makeNode(75);
root->right->left = makeNode(60);
getKLeastValue(root, 10);
getKLargestValue(root, 2);
printf("%d\n", getKLeastValueIterative(root, 2));
}