-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.h
42 lines (37 loc) · 851 Bytes
/
bst.h
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
#ifndef BST_H
#define BST_H
class BST {
public:
int data;
BST *left, *right;
BST() {
data = 0;
left = right = nullptr;
}
BST(int data) {
this->data = data;
left = right = nullptr;
}
void insert(int data) {
if (this->data < data) {
if (this->right == nullptr) { this->right = new BST(data); }
else { this->right->insert(data); }
}
else {
if (this->left == nullptr) { this->left = new BST(data); }
else { this->left->insert(data); }
}
}
int nth_node(int n) {
int result = 0;
nth_node_helper(this, &n, &result);
return result;
}
void nth_node_helper(BST *curr, int *n, int *result) {
if (curr == nullptr) { return; }
nth_node_helper(curr->left, n, result);
if (--(*n) == 0) { *result = curr->data; return; }
nth_node_helper(curr->right, n, result);
}
};
#endif // BST_H