A simple binary tree input and output operation for C language review data structure

The input-output only operation of a simple binary tree for C language review

1: Structure

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

2: Function declaration

pNode createNode(_Data value);//Create node
void printTree(pNode father);//Tree Printer
int getHigh(pNode father);//Get tree height
void destoryTree(pNode father);//Destruction tree
void createByLeft(pNode father);//Create tree
void printByFirst(pNode father);//Preorder printing
void printByCenter(pNode father);//Middle order printing
void printByEnd(pNode father);//Post order printing

3: Special variable declaration

#define _Data char
#define _ArrayMaxSize 100

4: Function implementation

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];   //Storage queue
    int f = 0, r = 0;   //Front pointer back pointer
    res[f++] = father;
    int count = 0;
    int heigh = getHigh(father) + 1;
    while(r != f){
//        Tree circulation
        pNode cRes[_ArrayMaxSize];   //Print queue
        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){

            //Layer cycle
            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);
    }
}

(Note: when printing the tree, when the height is greater than 4, the printing will fail (reason: I just want to review simply, so there is no simple way to go back and judge whether the grandfather node of two nodes is a brother node. When the parent node is a brother node, it needs to print - 2 spaces, and the child nodes are stacked in turn. Finally, it is more important to calculate the location of the root node, but I do not A lot of time to study this part of the code, and I will fix this BUG when I have time , so when you need to print, in order to be beautiful, please avoid more than 4 layers. In addition, the default root node is ×, and the height is increased by 1)

5: Test method

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

    createByLeft(node);

    printf("Height of tree:%d\n", getHigh(node));
    printf("First order output:");
    printByFirst(node);
    printf("\n First order output:");
    printByCenter(node);
    printf("\n First order output:");
    printByEnd(node);

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

    destoryTree(node);
}

6: Test results

Test data:

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

Tags: C

Posted on Thu, 02 Jan 2020 07:47:05 -0500 by [n00b]