1. 程式人生 > >C語言複習資料結構之簡單的二叉樹輸入和輸出操作

C語言複習資料結構之簡單的二叉樹輸入和輸出操作

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##