#include <stdio.h>
#include <stdlib.h>

typedef char TElement;

typedef struct TNode{
    TElement data;
    struct TNode* left;
    struct TNode* right;
}TNode;

#define Visitnode(n) (printf("[%c]", (n)->data))

void preorder(TNode* n){
    if(n != NULL){
        Visitnode(n);
        preorder(n->left);
        preorder(n->right);
    }
}

void inorder(TNode* n){
    if(n != NULL){
        inorder(n->left);
        Visitnode(n);
        inorder(n->right);
    }
}

void postorder(TNode* n){
    if(n != NULL){
        postorder(n->left);
        postorder(n->right);
        Visitnode(n);
    }
}

#define MAX_SIZE 100
typedef TNode* Element;

Element data[MAX_SIZE];
int front = 0;
int rear = 0;

int init(){
    front = rear = 0;
}

int is_empty(){
    return front == rear;
}

int is_full(){
    return (rear+1)%MAX_SIZE == front;
}

void enqueue(Element item){
    if(is_full()){
        return;
    }
    rear = (rear+1)%MAX_SIZE;
    data[rear] = item;
}

int dequeue(){
    if(is_empty()){
        return NULL;
    }
    front = (front+1)%MAX_SIZE;
    return data[front];
}

void levelorder(TNode* root){
    if(root != NULL){
        init();
        enqueue(root);

        while(!is_empty()){
            TNode* n = dequeue();

            if(n != NULL){
                Visitnode(n);
                enqueue(n->left);
                enqueue(n->right);
            }
        }
    }
}






Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: