Find k-th Smallest Element in BST
Time limit: 500ms
Memory limit: 256mb
Description:
Please complete the missing code of the BST ADT, such that the program could output a correct answer. For simplicity, we assume that the elements are unique.
1. insertion
2. find k-th smallest element
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, , am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct TreeNode {
int data;
int leftsize;
int rightsize;
struct TreeNode * leftchild;
struct TreeNode * rightchild;
struct TreeNode * parent;
};
struct TreeNode *createNode() {
struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
node->data = 0;
node->leftsize = 0;
node->rightsize = 0;
node->leftchild = NULL;
node->rightchild = NULL;
node->parent = NULL;
return node;
}
struct TreeNode *createRoot(int x) {
struct TreeNode * root = createNode();
root->data = x;
return root;
}
struct TreeNode * Lchild(struct TreeNode * root) {
return root->leftchild;
}
struct TreeNode * Rchild(struct TreeNode * root) {
return root->rightchild;
}
struct TreeNode * parent(struct TreeNode *root) {
return root->parent;
}
int Data(struct TreeNode * root) {
return root->data;
}
bool isEmpty(struct TreeNode * root) {
return root == NULL;
}
// insert element x into BST root
void Insert(struct TreeNode * root, int x) {
//write down your code here
}
// find the k-th smallest element in BST root
int kthSmallest(struct TreeNode * root, int k)
{
if (k > root->leftsize + root->rightsize + 1) {
printf("There exists only %d nodes.\n", root->leftsize + root->rightsize + 1);
return -1;
}
// write down your code here
}
int main() {
int n, x, i, k;
struct TreeNode * root = NULL;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &x);
if (root == NULL){
root = createRoot(x);
}
else{
Insert(root, x);
}
}
scanf("%d", &k);
int data_k = kthSmallest(root, k);
if (data_k == -1) {
printf("NULL\n");
}
else {
printf("%d\n", data_k);
}
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Input:
The first line contains a non-negative integer N;
The second line contains N positive integers, a_0, a_1, ..., a_(N-1);
The third line contains a positive integer k;
Output:
The k-th smallest element in BST.
Sample Input 1:
10
7 3 5 9 10 6 11 19 14 1
6
Sample Output 1:
9
Sample Input 2:
10
7 3 5 9 10 6 11 19 14 1
11
Sample Output 2:
There exists only 10 nodes.
NULL
Submit