Range reporting 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.
1. Insert
2. range reporting of a range [x, y].
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, 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>
// Comparison function for integers
int compare_ints(const void *a, const void *b) {
int int_a = *((int *)a);
int int_b = *((int *)b);
if (int_a < int_b) return -1;
else if (int_a > int_b) return 1;
else return 0;
}
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
}
typedef struct store_result_vector{
int* arr;
int size;
} result_vector;
// write down your code here
void rangeReporting(struct TreeNode *root, int left, int right, result_vector* result){
// write down your code here
}
int main() {
int n, x, i;
int Q;
struct TreeNode *root = NULL;
scanf("%d %d", &n, &Q);
for (i = 0; i < n; i++) {
scanf("%d", &x);
if (root == NULL) {
root = createRoot(x);
} else {
Insert(root, x);
}
}
int left, right;
result_vector *result = (result_vector *)malloc(sizeof(result_vector));
result->arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < Q; i++) {
scanf("%d", &left);
scanf("%d", &right);
result->size = 0;
rangeReporting(root, left, right, result);
// Sort the array using qsort()
qsort(result->arr, result->size, sizeof(int), compare_ints);
for (int i = 0; i < result->size - 1; i++) {
printf("%d, ", result->arr[i]);
}
printf("%d\n", result->arr[result->size - 1]);
}
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Input:
The first line contains two non-negative integers N and Q;
N is the number of integers in the BST and Q is the number of queries.
The second line contains N positive integers, a_0, a_1, ..., a_(N-1);
Then Q lines immediately follow, each of which includes two positive integers x, y, which forms the range we need to report;
Output:
All elements in the BST within the range. From output of the left to the right, from small elements to large elements.
Sample Input 1:
10 1
7 3 5 9 10 6 11 19 14 1
2 5
Sample Output 1:
3, 5
Sample Input 2:
10 2
7 3 5 9 10 6 11 19 14 1
4 6
19 20
Sample Output 2:
5, 6
19
Submit