Level Order

Time limit: 5000ms
Memory limit: 256mb

-------------------------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 <string.h>

// tree structure
struct treeNode {
    int value;
    struct treeNode* left;
    struct treeNode* right;
};

typedef struct treeNode* TreeNode;


// Queue ADT

struct queueNode {
    TreeNode value; // each node in the Queue contains a TreeNode
    struct queueNode* next;
    struct queueNode* prev;
};

typedef struct queueNode* QueueNode;

struct queue {
    int capacity;
    QueueNode head;
    QueueNode tail;
    int size;
};

typedef struct queue* QueueADT;

QueueNode AllocateEmptyQueueNode() {
    QueueNode newNode = (QueueNode) malloc(sizeof(struct queueNode));
    newNode->value = NULL;
    newNode->next = NULL;
    newNode->prev = NULL;
    return newNode;
}

QueueADT CreateQueue(int capacity) {
    QueueADT q = (QueueADT) malloc(sizeof(struct queue));
    q->capacity = capacity;
    q->size = 0;
    q->head = AllocateEmptyQueueNode();
    q->tail = AllocateEmptyQueueNode();
    q->head->next = q->tail;
    q->tail->prev = q->head;
    return q;
}

int IsEmpty(QueueADT q) {
    return q->size == 0;
}

int IsFull(QueueADT q) {
    return q->size == q->capacity;
}

void Enqueue(QueueADT q, TreeNode value) {
    if (q->size < q->capacity) {
        QueueNode tmp = AllocateEmptyQueueNode();
        tmp->value = value;

        QueueNode prev = q->tail->prev;
        prev->next = tmp;
        tmp->prev = prev;

        tmp->next = q->tail;
        q->tail->prev = tmp;

        q->size++;
    }
}

TreeNode Dequeue(QueueADT q) {
    if (!IsEmpty(q)) {
        q->size--;

        TreeNode v;
        QueueNode tmp = q->head->next;
        q->head->next = tmp->next;
        tmp->next->prev = q->head;
        v = tmp->value;
        free(tmp);

        return v;
    } else {
        return NULL;
    }
}

// BST

TreeNode CreateTreeNode(int value) {
    TreeNode newNode = (TreeNode) malloc(sizeof(struct treeNode));
    newNode->value = value;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

TreeNode Insert(TreeNode root, int value) {
    if (root == NULL) {
        return CreateTreeNode(value);
    }

    if (value > root->value) {
        root->right = Insert(root->right, value);
    } else if (value < root->value) {
        root->left = Insert(root->left, value);
    }

    return root;
}

void PrintNode(TreeNode root) {
    if (root != NULL) {
        printf("%d\n", root->value);
    }
}

// level order traversal
// Use the function PrintNode to print the value in a tree node
// number of nodes is smaller than 20000
void LevelOrder(TreeNode root) {
    // write your code here

}


int main() {
    int n, value;
    scanf("%d", &n);

    TreeNode root = NULL;

    for (int i=0;i<n;++i) {
        scanf("%d", &value);
        root = Insert(root, value);
    }

    LevelOrder(root);
}



-----------------------------------------End of Code-----------------------------------------
Description:
Implement the level order traversal operation of the binary search tree with BFS.
A tree can be regarded as a directed unweighted graph where the direction of each edge is from the low level node to the high level node.
By default, we say that the root has the lowest level, 0. And its two children has the level 1, and etc.
Level order traversal technique is defined as a method to traverse a tree such that all nodes present in the same level are traversed completely before traversing the next level.
For each level, it always traverses nodes from left to right.
Tips: You should use PrintNode function to print each node.

Input:
The first line contains one integer: N;
The next line contains N Integers, which are used to build the binary search tree.

Output:
The level order traversal sequence.

Sample Input 1:
7
4 2 1 3 6 5 7

Sample Output 1:
4 2 6 1 3 5 7

Submit