OJ14-02

读取字符串abcdefghij,然后层次建树建立一颗二叉树,然后中序遍历输出 hdibjeafcg,后序遍历输出 hidjebfgca,层序遍历输出abcdefghij,注意不要输出汉字

#include 
#include 

typedef char BiElemType;
typedef struct BiTNode {
    BiElemType data;
    struct BiTNode *lChild;
    struct BiTNode *rChild;//左右节点
} BiTNode, *BiTree;
//辅助队列
typedef struct tag {
    BiTree p;//树的某一个节点,指针类型,保存申请节点的指针
    struct tag *pnext;
} tag_t, *ptag_t;


typedef struct LinkNode {
    BiTree data;
    struct LinkNode *next;
} LinkNode;
typedef struct {
    LinkNode *front, *rear;//链表头,链表尾
} LinkQueue;


void initQueue(LinkQueue &q) {
    q.front = q.rear = (LinkNode *) malloc(sizeof(LinkNode));
    q.front->next = NULL;
}

bool isEmpty(LinkQueue q) {
    if (q.rear == q.front) { return true; } else { return false; }
}

bool enQueue(LinkQueue &q, BiTree num) {
    LinkNode *p = (LinkNode *) malloc(sizeof(LinkNode));
    p->data = num;
    p->next = NULL;
    q.rear->next = p;//尾指针的next指向p,从尾部入队
    q.rear = p;//rear指向新的尾部
    return true;
}

bool deleteQueue(LinkQueue &q, BiTree &e) {
    if (q.front == q.rear) {
        return false;
    }
    LinkNode *pc = q.front->next;//拿到被删节点
    q.front->next = pc->next;
    e = pc->data;
    free(pc);
    if (q.rear == pc) {
        q.rear = q.front;
    }
    return true;
}


//层序遍历、广度优先遍历
void levelOrder(BiTree t) {
    LinkQueue q;
    initQueue(q);//初始化队列
    BiTree p;//存储出队节点
    enQueue(q, t);//根节点入队
    while(!isEmpty(q)){
        deleteQueue(q,p);
        putchar(p->data);
        if(p->lChild){
            enQueue(q, p->lChild);
        }
        if(p->rChild){
            enQueue(q, p->rChild);
        }
    }
}

void inOrder(BiTree p) {
    if (p) {
        inOrder(p->lChild);
        printf("%c", p->data);
        //递归
        inOrder(p->rChild);
    }
}

void postOrder(BiTree p) {
    if (p) {
        postOrder(p->lChild);
        postOrder(p->rChild);
        printf("%c", p->data);
        //递归
    }
}
int main() {
    BiTree p;//指向新申请的树节点
    BiTree tree = NULL;//初始化根节点
    //队头,队尾,新节点,新节点父元素
    ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pucr = NULL;
    char c;
    while (scanf("%c", &c)) {
        if (c == '\n') {
            break;;
        }
        p = (BiTree) calloc(1, sizeof(BiTNode));
        p->data = c;
        listpnew = (ptag_t) calloc(1, sizeof(tag_t));//给队列节点申请空间
        listpnew->p = p;
        if (tree == NULL) {
            tree = p;
            //第一个节点既是队列头也是队列尾
            phead = listpnew;
            ptail = listpnew;
            pucr = listpnew;
        } else {
            ptail->pnext = listpnew;
            ptail = listpnew;
            //将数放入左孩子
            if (pucr->p->lChild == NULL) {
                pucr->p->lChild = p;
            } else if (pucr->p->rChild == NULL) {
                pucr->p->rChild = p;
                pucr = pucr->pnext;
            }
        }
    }
    inOrder(tree);
    printf("\n");
    postOrder(tree);
    printf("\n");
    //层序遍历、广度优先遍历
    levelOrder(tree);
    return 0;
}

你可能感兴趣的:(考研C,C++数据结构,算法,数据结构)