其他分享
首页 > 其他分享> > 数据结构//C——静态——链式二叉树

数据结构//C——静态——链式二叉树

作者:互联网

# include <stdio.h>
# include <malloc.h>

typedef  struct TNode{
    char data;
    struct TNode * pL_child;
    struct TNode * pR_child;
} NODE, * pNODE;

struct TNode * Create_tree();
void Pre_traverse(pNODE);
void IN_traverse(pNODE);
void Post_traverse(pNODE);

int main (void){

    pNODE pNode = Create_tree();

    Pre_traverse(pNode);
    printf("\n");

    IN_traverse(pNode);
    printf("\n");

    Post_traverse(pNode);

    return 0;
}
struct TNode * Create_tree(){
    pNODE pA = (pNODE) malloc(sizeof(NODE));
    pNODE pB = (pNODE) malloc(sizeof(NODE));
    pNODE pC = (pNODE) malloc(sizeof(NODE));
    pNODE pD = (pNODE) malloc(sizeof(NODE));
    pNODE pE = (pNODE) malloc(sizeof(NODE));

    pA->data = 'A';
    pB->data = 'B';
    pC->data = 'C';
    pD->data = 'D';
    pE->data = 'E';

    pA->pL_child = pB;
    pA->pR_child = pC;
    pB->pL_child = NULL;
    pB->pR_child = NULL;
    pC->pL_child = pD;
    pC->pR_child = NULL;
    pD->pL_child = NULL;
    pD->pR_child = pE;
    pE->pL_child = NULL;
    pE->pR_child = NULL;

    return pA;
}
void Pre_traverse(pNODE pNode){
/*
 * 先      访问  根节点
 * 再 先序 访问  左子树
 * 再 先序 访问  右子树
 */
    if(NULL != pNode){
        printf("%c\t", pNode->data);
        if(NULL != pNode->pL_child){
            Pre_traverse(pNode->pL_child);
        }
        if(NULL != pNode->pR_child){
            Pre_traverse(pNode->pR_child);
        }
    }
}
void IN_traverse(pNODE pNode){
    if(NULL != pNode){
        if(NULL != pNode->pL_child){
            IN_traverse(pNode->pL_child);
        }
        printf("%c\t", pNode->data);
        if(NULL != pNode->pR_child){
            IN_traverse(pNode->pR_child);
        }
    }
}
void Post_traverse(pNODE pNode){
    if(NULL != pNode){
        if(NULL != pNode->pL_child){
            Post_traverse(pNode->pL_child);
        }
        if(NULL != pNode->pR_child){
            Post_traverse(pNode->pR_child);
        }
        printf("%c\t", pNode->data);
    }
}

标签:pR,traverse,NULL,pNode,链式,pNODE,二叉树,child,数据结构
来源: https://blog.csdn.net/qq_62819483/article/details/122765579