二叉树

baseFunc.h


#include 
using namespace std;
#define ElementType int
#define QElementType BiTree


typedef struct BiTNode{
    ElementType data;
    struct BiTNode *lchild,*rchild;
}*BiTree;

typedef struct PtrStack{
    BiTree *top;
    BiTree *base;
    int length;
}*Stack;

typedef struct QueueNode{
    struct QueueNode *next;
    QElementType data;
}*LNode;
typedef struct circleQueue{
    QueueNode *front,*rear;
}LQueue;

//链栈操作
void initLinkStack(Stack &S);
void Push(Stack &S,BiTree x);
void Pop(Stack &S,BiTree &x);
void getTop(Stack S,BiTree &x);
bool isEmpty(Stack S);


//队列操作
void initQueue(LQueue &Q);
void EnQueue(LQueue &Q,BiTree T);
void DeQueue(LQueue &Q,BiTree &T);
bool isEmpty(LQueue Q);



//二叉树操作
bool CreateBiTree(BiTree &T);
void PreOrder(BiTree T);
void Inorder(BiTree T);
void Postorder(BiTree T);
void Preorder2(BiTree T);
void Inorder2(BiTree T);
void Postorder2(BiTree T);
void Levelorder(BiTree T);

baseFunc.cpp


//
// Created by Vsion on 2019/11/6.
//

#include "header/baseFunc.h"



bool  CreateBiTree(BiTree &T)
{
    int ch;
    cout<<"先序输入字符"<>ch;
    if(ch == 0)  T = nullptr;
    else
    {
        if(! (T = (BiTNode*) malloc (sizeof(BiTNode))))   return false;
        T->data = ch;
        CreateBiTree(T->lchild) ;
        CreateBiTree(T->rchild) ;
    }
    return true;
}

void PreOrder(BiTree T){
    if (T){
        cout<data<lchild);
        PreOrder(T->rchild);
    }
}
void Inorder(BiTree T){
    if (T){
        Inorder(T->lchild);
        cout<data<rchild);
    }
}

void Postorder(BiTree T){
    if (T){
        Postorder(T->lchild);
        Postorder(T->rchild);
        cout<data<data<lchild;
        }else{
            Pop(S,p);
            p = p->rchild;
        }
    }
}
void Inorder2(BiTree T){
    Stack S;
    S = new PtrStack;
    initLinkStack(S);
    BiTree p = T;
    while (p||!isEmpty(S)){
        if (p){
            Push(S,p);
            p = p->lchild;
        }
        else{
            Pop(S,p);
            cout<data;
            p = p->rchild;
        }
    }
}
void Postorder2(BiTree T){
    Stack S;
    BiTree r;
    S = new PtrStack;
    initLinkStack(S);
    BiTree p = T;
    while (p||!isEmpty(S)){
        if (p){
            Push(S,p);
            p = p->lchild;
        }else{
            getTop(S,p);
            if (p->rchild&&p->rchild!=r){
                p = p->rchild;
                Push(S,p);
                p = p->lchild;
            }else{
                Pop(S,p);
                cout<data<data<lchild) EnQueue(Q,p->lchild);
        if (p->rchild) EnQueue(Q,p->rchild);

    }

}

void initLinkStack(Stack &S){
    S->base = new BiTree;
    S->top = S->base;
    S->length = 0;
}
void Push(Stack &S,BiTree x){
    *S->top++ = x;
    S->length++;
}
void Pop(Stack &S,BiTree &x){
    x = *--S->top;
    S->length--;
}
void getTop(Stack S,BiTree &x){
    x = *--S->top;
    S->top++;
}

bool isEmpty(Stack S){
    return S->length <= 0;
}




void initQueue(LQueue &Q){
    Q.front = Q.rear = new QueueNode;
    Q.front->next = nullptr;
}
void EnQueue(LQueue &Q,QElementType data){
    LNode p;
    p = new QueueNode;
    p->data = data;
    p->next = nullptr;
    Q.rear->next = p;
    Q.rear = p;
}
void DeQueue(LQueue &Q,QElementType &x){
    LNode p;
    p = new QueueNode;
    p = Q.front->next;
    x = p->data;

    Q.front->next = p->next;

    if (Q.rear == p) Q.rear = Q.front;
    delete p;
}
bool isEmpty(LQueue Q){
    return Q.front == Q.rear;
}

你可能感兴趣的:(二叉树)