Binary Search Tree

Time limit: 5000ms
Memory limit: 256mb

Description:
Please complete the missing code of the BST ADT, such that the program could output a correct answer.

-------------------------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>

struct TreeNode {
    int data;
    struct TreeNode * leftchild;
    struct TreeNode * rightchild;
    struct TreeNode * parent;
};

struct TreeNode *createNode();
struct TreeNode *createRoot(int x);
void Insert(struct TreeNode * root, int x);
struct TreeNode * Lchild(struct TreeNode * root);
struct TreeNode * Rchild(struct TreeNode * root);
struct TreeNode * parent(struct TreeNode * root);
int Data(struct TreeNode * root);
bool isEmpty(struct TreeNode * root);
struct TreeNode * search(struct TreeNode * root, int key);
struct TreeNode * successor(struct TreeNode *x);
struct TreeNode * minimum(struct TreeNode * root);

int main()
{
    int n, x, key;
    struct TreeNode * root = NULL;
    scanf("%d", &n);
    for (int i = 0; i < n; i++){
        scanf("%d", &x);
        if (root == NULL){
            root = createRoot(x);
        }
        else{
            Insert(root, x);
        }
    }
    scanf("%d", &key);

    struct TreeNode *p = search(root, key);
    if (p == NULL) {
        printf("NULL\n");
    }
    else {
        printf("%d\n", Data(p));
        struct TreeNode *s = successor(p);

        if (s == NULL) {
            printf("NULL\n");
        }
        else {
            printf("%d\n", Data(s));
        }
    }

    return 0;
}

struct TreeNode *createNode()
{
    struct TreeNode *node = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    node->data = 0;
    node->leftchild = NULL;
    node->rightchild = NULL;
    node->parent = NULL;
}

void Insert(struct TreeNode * root, int x)
{
    struct TreeNode *node = root;
    struct TreeNode *parent = NULL;
    bool isLeftChild = false;

    while(node != NULL) {
        parent = node;

        //suppose that the elements are unique
        if (x < Data(node)) {
            isLeftChild = true;
            node = Lchild(node);
        }
        else {
            isLeftChild = false;
            node = Rchild(node);
        }
    }

    node = createNode();
    node->data = x;
    node->parent = parent;
    if (isLeftChild) {
        parent->leftchild = node;
    }
    else {
        parent->rightchild = 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;
}

struct TreeNode * minimum(struct TreeNode * root) {
    struct TreeNode * node = root;
    while (!isEmpty(node) && !isEmpty(Lchild(node))){
        node = Lchild(node);
    }

    return node;
}

struct TreeNode * search(struct TreeNode * root, int key) {
    if (isEmpty(root)){
        return NULL;
    }

    //write down your code here
}

struct TreeNode *successor(struct TreeNode *x)
{
    // write down your code here
}


-------------------------------------------End of Code-------------------------------------------

Input:
The first line contains an integer N > 0;
The second line is the values of  N positive integers, which will be used to create the BST tree
The Third line is the target key value which we want to search in the BST.

Output:
if the target key value is found, please output the key value in the first line, otherwise please output "NULL".
if the target key value is found, please output its successor in the second line. If it has no successor, please output "NULL".

Sample Input 1:
6
6 5 3 8 7 9
8
Sample Output 1:
8
9


Sample Input 2:
8
23 30 10 44 55 18 24 35
54
Sample Output 2:
NULL

Sample Input 3:
5
8 9 10 7 6
10
Sample Output 3:
10
NULL
Submit