二叉树的建立与遍历(C语言简单实现)

#include 
#include 
#include 

#define MAXSIZE 1024

//二叉树节点
typedef struct tNode{
    char data;
    tNode *lchild;
    tNode *rchild;
}bitree;

//调用函数声明
bitree * Create();
int LayerTraverse();
int InOrder();
int PreOrder();

//建立二叉树
bitree * Create(){
    char ch;
    bitree *root,*s;
    printf("Please input the char\nIf it's null input '@'\nEnd with '#'\n");
    bitree *Q[MAXSIZE];
    int front, rear;
    root = NULL; front = 1; rear = 0;
    getchar();
    while ((ch = getchar())!='#')
    {
        s = NULL;
        if (ch != '@'){
            s = (bitree *)malloc(sizeof(bitree));
            s->data = ch;
            s->lchild = NULL;
            s->rchild = NULL;

        }
        rear++;
        Q[rear] = s;
        if (rear == 1) root = s;
        else{
            if (s && Q[front]){
                if (rear % 2 == 0) Q[front]->lchild = s;
                else Q[front]->rchild = s;
            }
            if (rear % 2 == 1) front++;
        }

    }
    return root;

}

//广度遍历二叉树
int LayerTraverse(bitree *head){

    int front, rear;
    bitree *Q[MAXSIZE];
    bitree *s;
    if (head != NULL){
        rear = 1; front = 0;
        Q[rear] = head;
        while (front < rear){
            front++;
            s = Q[front];
            printf("%c ",s->data);
            if (s->lchild != NULL){
                rear++;
                Q[rear] = s->lchild;

            }
            if (s->rchild != NULL){
                rear++;
                Q[rear] = s->rchild;
            }
        }

    }

    getchar();
    return 0;
}

//中序非递归遍历
int InOrder(bitree *head){
    int top;
    bitree *stack[MAXSIZE];
    bitree *s;
    if (head != NULL){
        top = -1;
        s = head;
        while ((top != -1) || (s != NULL)){
            while (s!=NULL)
            {
                if (top == MAXSIZE - 1){
                    printf("overflow\n");
                    return 0;
                }
                else
                {
                    top++;
                    stack[top] = s;
                    s = s->lchild;
                }

            }
            s = stack[top];
            top--;
            printf("%c ",s->data);
            s = s->rchild;
        }
    }

    return 0;
}

//前序递归遍历
int PreOrder(bitree *head){

    if (head != NULL){
        printf("%c ",head->data);
        PreOrder(head->lchild);
        PreOrder(head->rchild);
    }
    return 0;
}

//
int main(){

    int select;
    bitree *head1,*head2;
    head1 = head2 = NULL;

    do{
        printf("\n*******************************\n");
        printf("\nPress to select function:\n");
        printf("\n1 to build a bitree\n");
        printf("\n2 to layer traversal\n");
        printf("\n3 to middle traversal\n");
        printf("\n4 to front traversal\n");
        printf("\n0 to exit\n");
        printf("\n*******************************\n\n");

        scanf_s("%d",&select);

        if (select < 0 || select>5){
            printf("Input error.Please input again\n");
            continue;
        }
        switch (select)
        {
        case 0:{
            return 0;
        }
        case 1:{
            printf("The bitree you want to build in layer traversal order:\n");
            head1 = Create();
            LayerTraverse(head1);
            getchar();
            break;
        }
        case 2:{
            printf("Layer traversal:\n");
            LayerTraverse(head1);
            getchar();
            break;
        }
        case 3:{
            printf("InOrder traversal:\n");
            InOrder(head1);
            getchar();
            break;
        }
        case 4:{
            printf("PreOrder traversal:\n");
            PreOrder(head1);
            getchar();
            break;
        }

        default:
            break;
        }
    } while (select != 0);

    return 0;
}

你可能感兴趣的:(c++基础)