C语言复习数据结构之简单的二叉树输入和输出操作

版权声明:转载请通知,复制请通知 如有兴趣欢迎加群:653460549 - Java资源分享 https://blog.csdn.net/qq_42038407/article/details/82709092

C语言复习之简单的二叉树的仅输入输出操作

1:结构体

typedef struct TreeNode{
    _Data value;
    struct TreeNode * father;
    struct TreeNode * right;
    struct TeenNode * left;
}* pNode, Node;

2:函数声明

pNode createNode(_Data value);//创建节点
void printTree(pNode father);//树打印器
int getHigh(pNode father);//获得树高
void destoryTree(pNode father);//销毁树
void createByLeft(pNode father);//创建树
void printByFirst(pNode father);//先序打印
void printByCenter(pNode father);//中序打印
void printByEnd(pNode father);//后序打印

3:特殊变量声明

#define _Data char
#define _ArrayMaxSize 100

4:函数实现

pNode createNode(_Data value) {
    pNode node = (pNode) malloc(sizeof(Node));

    node->father = NULL;
    node->left = NULL;
    node->right = NULL;
    node->value = value;

    return node;
}

void createByLeft(pNode father) {
    _Data value;
    scanf("%c", &value);
    if (value != '#') {
        pNode left = createNode(value);
        father->left = left;
        createByLeft(left);
    }

    scanf("%c", &value);
    if (value != '#') {
        pNode right = createNode(value);
        father->right = right;
        createByLeft(right);
    }
}

void destoryTree(pNode father) {
    if (father->right != NULL) {
        destoryTree(father->right);
    }
    if (father->left != NULL) {
        destoryTree(father->left);
    }

    free(father);
}

int getHigh(pNode father) {
    int left = 1, right = 1;
    if (father->left != NULL) {
        left += getHigh(father->left);
    }

    if (father->right != NULL) {
        right += getHigh(father->right);
    }

    return left>right?left:right;
}

void printTree(pNode father) {
    pNode res[_ArrayMaxSize];   //存储列队
    int f = 0, r = 0;   //前指针 后指针
    res[f++] = father;
    int count = 0;
    int heigh = getHigh(father) + 1;
    while(r != f){
//        树循环
        pNode cRes[_ArrayMaxSize];   //打印队列
        int cf = 0, cr = 0;
        while(r != f){
            cRes[cf++] = res[(r++)%_ArrayMaxSize];
        }

        int spaceS = (1 << (heigh - count))+ (heigh - count) - 1;
        for(int i = 0; i < spaceS; i++) printf(" ");

        int flag = 1;
        while(cr != cf){

            //层循环
            pNode temp = cRes[cr++];
            if(temp != NULL){
                res[(f++)%_ArrayMaxSize] = temp->left;
                res[(f++)%_ArrayMaxSize] = temp->right;
                printf("%c", temp->value);
            } else{
                printf("#");
            }
            if(flag > 0){
                for(int j = (1 << (heigh - count + 1)); j >= 0; j--) printf(" ");
            }else{
                for(int j = (1 << (heigh - count + 1)); j > 1; j--) printf(" ");
            }
            flag *= -1;

        }
        printf("\n");
        count++;
    }

}

void printByFirst(pNode father){
    if(father != NULL){
        printf("%c", father->value);
        if(father->left != NULL){
            printByFirst(father->left);
        }
        if(father->right != NULL){
            printByFirst(father->right);
        }
    }
}

void printByCenter(pNode father){
    if(father != NULL){
        if(father->left != NULL){
            printByFirst(father->left);
        }
        printf("%c", father->value);
        if(father->right != NULL){
            printByFirst(father->right);
        }
    }

}

void printByEnd(pNode father){
    if(father != NULL){
        if(father->left != NULL){
            printByFirst(father->left);
        }
        if(father->right != NULL){
            printByFirst(father->right);
        }
        printf("%c", father->value);
    }
}

(注意:此中打印树的时候,当高度大于4,打印会失效(原因:我只想简单的复习,所以没有简单的办法回去判断两个节点的祖父节点是否为兄弟节点,当父节点为兄弟节点的时候,需要打印-2空格,子节点依次叠加,最后计算根节点的位置更是重要,可是我没有大量时间钻研这部分代码,等有时间,会将这个BUG修复!),所以需要打印的时候,为了美观,请避免超过4层,此外,默认根节点为#,高度加1)

5:测试方式

int main() {
    pNode node  = createNode('#');

    createByLeft(node);

    printf("树的高度:%d\n", getHigh(node));
    printf("先序输出:");
    printByFirst(node);
    printf("\n先序输出:");
    printByCenter(node);
    printf("\n先序输出:");
    printByEnd(node);

    printf("\n");
    printTree(node);

    destoryTree(node);
}

6:测试结果

测试数据:

ACH##I##DJ##K##BEL##M##FN##O##

猜你喜欢

转载自blog.csdn.net/qq_42038407/article/details/82709092