Data Structure - Detailed Algorithms for Building Binary Trees and Binary Linked Lists

Regardless of whether the binary tree is sequential storage or chain storage, its establishment algorithm depends on the input order and representation of the logical structure of the binary tree.

For a binary linked list, we can input node information sequentially according to the hierarchical order structure of the binary tree, which is similar to sequential storage.

Generally, the binary tree also needs to input virtual node information while inputting node information.

The following is the specific algorithm for building a binary tree. We mainly complete it in three steps:

  1. According to the order of the nodes of the complete binary tree, input the information of the nodes in turn (the virtual node is represented by @), if the input node is not a virtual node, a new node is created.
  2. If the new node is the first node, let it be the root node, otherwise link the new node as a child to its parent node.
  3. Repeat the above two steps until the end sign # is entered.
# include "stdio.h"
# include "stdlib.h"

// 定义宏存储队列数组的最大值和二叉树结构体大小:
# define maxsize 1024
# define LEN sizeof(struct bitree)

// 建立二叉树的结构体:
// 可添加指向双亲的指针:
struct bitree {
    
    
    char data;
    struct bitree *lchild;
    struct bitree *rchild;
    // struct bitree *parent;
};

struct bitree *create() {
    
    
    // 用于输入的字符类型:
    char ch;
    // 设置指针类型数组来构成队列:
    struct bitree *queue[maxsize];
    // 队头和队尾指针变量:
    int front = 1, rear = 0;
    // 建立二叉树的指针并置空:
    struct bitree *root = NULL, *s;

    // 初始化完成:
    while ((ch = getchar()) != '#') {
    
    
        //输入一个字符
        s = NULL;
        if (ch != '@') {
    
    
            // 判断该字符是不是虚节点
            s = (struct bitree *) malloc(LEN);
            // 填入数据
            s->data = ch;
            // 左右子树置空
            s->lchild = NULL;
            s->rchild = NULL;
        }
        // 队尾指针后置
        rear++;
        // 将新节点入队
        queue[rear] = s;
        // 输入的第一个节点为根节点
        if (rear == 1) {
    
    
            root = s;
        } else {
    
    
            // 双亲和孩子都不是虚节点
            if (s && queue[front]) {
    
    
                if (rear % 2 == 0) {
    
    
                    // rear为偶数为左孩子
                    queue[front]->lchild = s;
                } else {
    
    
                    queue[front]->rchild = s;
                }
            }
            if (rear % 2 == 1) {
    
    
                front++;
            }
        }
    }
    return root;
}

// 二叉树的先序遍历序列
void preorder(struct bitree *p) {
    
    
    if (p != NULL) {
    
    
        // 访问数据
        printf("%c ", p->data);
        // 访问左子树
        preorder(p->lchild);
        // 访问右子树
        preorder(p->rchild);
    }
}

// 二叉树的中序遍历序列
void inorder(struct bitree *p) {
    
    
    if (p != NULL) {
    
    
        inorder(p->lchild);
        printf("%c ", p->data);
        inorder(p->rchild);
    }
}

// 二叉树的后续遍历序列
void postorder(struct bitree *p) {
    
    
    if (p != NULL) {
    
    
        postorder(p->lchild);
        postorder(p->rchild);
        printf("%c ", p->data);
    }
}

int main(void) {
    
    
    printf("开始建立二叉树, 输入#停止建立, 空节点用@表示\n注意输入的时候不要带有空格或是换行\n");
    struct bitree *root = create();

    printf("先序遍历序列为:");
    preorder(root);
    printf("\n");

    printf("中序遍历序列为:");
    inorder(root);
    printf("\n");

    printf("后续遍历序列为:");
    postorder(root);
    printf("\n");
}

Input sample:
insert image description here

Guess you like

Origin blog.csdn.net/wwx1239021388/article/details/130161613