跳到内容

二叉树 ​

树形结构与二叉搜索树 ​

链表是线性的,每个节点只有一个后继。二叉树则让每个节点最多有两个分支——左子树和右子树,适合表达有层次关系的数据。

有一种特别有用的二叉树叫二叉搜索树(BST):对任意节点,它左子树里所有值都小于该节点,右子树里所有值都大于该节点。这个性质让查找、插入都能沿着一条路径往下走,每次比较排除一半子树,效率接近二分查找。更妙的是,对 BST 做中序遍历会按从小到大顺序输出所有节点。

节点与插入 ​

二叉树节点有两个指针:左孩子和右孩子。插入时从根开始比较,比当前节点小就往左走,大就往右走,走到空位就把新节点放进去:

c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *left;
    struct Node *right;
} Node;

Node *node_create(int data)
{
    Node *n = malloc(sizeof(Node));
    n->data = data;
    n->left = NULL;
    n->right = NULL;
    return n;
}

Node *tree_insert(Node *root, int data)
{
    if (root == NULL)
        return node_create(data);
    if (data < root->data)
        root->left = tree_insert(root->left, data);
    else if (data > root->data)
        root->right = tree_insert(root->right, data);
    return root;
}

void tree_inorder(Node *root)
{
    if (root == NULL)
        return;
    tree_inorder(root->left);
    printf("%d ", root->data);
    tree_inorder(root->right);
}

void tree_free(Node *root)
{
    if (root == NULL)
        return;
    tree_free(root->left);
    tree_free(root->right);
    free(root);
}

int main(void)
{
    Node *root = NULL;
    int values[] = {50, 30, 70, 20, 40, 60, 80};
    int n = sizeof(values) / sizeof(values[0]);

    for (int i = 0; i < n; i++)
        root = tree_insert(root, values[i]);

    printf("中序遍历: ");
    tree_inorder(root);
    printf("\n");

    tree_free(root);
    return 0;
}

输出:

text
中序遍历: 20 30 40 50 60 70 80

插入顺序是 50、30、70、20、40、60、80。50 成为根;30 比 50 小,挂在 50 左侧;70 比 50 大,挂在右侧;20 比 50 小往左、比 30 小再往左,挂在 30 的左侧;以此类推。最终树形如下:

text
        50
       /  \
      30   70
     / \   / \
    20 40 60 80

中序遍历的访问顺序是"左子树、根、右子树",递归地把最小的节点最先访问到,所以输出正好是升序排列。这并非巧合——BST 的定义保证了中序遍历一定得到有序序列,这是它最重要的性质之一。

tree_insert 是递归的:空子树就创建节点返回;否则比较后递归插入左或右子树,并用返回值更新对应的指针。递归天然契合树的分形结构——每个子树本身就是一棵树,处理方式和整棵树完全相同。

注:本例中相等的值(data == root->data)不做任何操作,直接返回,即不允许重复。如果业务上需要保留重复值,可以把相等的情况也归入某一侧(比如 >= 走右边),但这样会破坏严格的 BST 定义,查找时要相应处理。

三种遍历的对比 ​

二叉树有三种深度优先遍历,区别在于访问根节点的时机。下面用同一棵树演示全部三种:

c
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *left;
    struct Node *right;
} Node;

Node *node_create(int data)
{
    Node *n = malloc(sizeof(Node));
    n->data = data;
    n->left = NULL;
    n->right = NULL;
    return n;
}

Node *tree_insert(Node *root, int data)
{
    if (root == NULL)
        return node_create(data);
    if (data < root->data)
        root->left = tree_insert(root->left, data);
    else if (data > root->data)
        root->right = tree_insert(root->right, data);
    return root;
}

void preorder(Node *root)
{
    if (root == NULL) return;
    printf("%d ", root->data);
    preorder(root->left);
    preorder(root->right);
}

void inorder(Node *root)
{
    if (root == NULL) return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}

void postorder(Node *root)
{
    if (root == NULL) return;
    postorder(root->left);
    postorder(root->right);
    printf("%d ", root->data);
}

void tree_free(Node *root)
{
    if (root == NULL) return;
    tree_free(root->left);
    tree_free(root->right);
    free(root);
}

int main(void)
{
    Node *root = NULL;
    int values[] = {50, 30, 70, 20, 40, 60, 80};
    int n = sizeof(values) / sizeof(values[0]);
    for (int i = 0; i < n; i++)
        root = tree_insert(root, values[i]);

    printf("前序: ");
    preorder(root);
    printf("\n中序: ");
    inorder(root);
    printf("\n后序: ");
    postorder(root);
    printf("\n");

    tree_free(root);
    return 0;
}

输出:

text
前序: 50 30 20 40 70 60 80
中序: 20 30 40 50 60 70 80
后序: 20 40 30 60 80 70 50

三种遍历对应根节点的三种访问时机:

遍历方式访问顺序特点
前序根、左、右先输出根,反映树的形状
中序左、根、右BST 下得到升序序列
后序左、右、根根最后输出,适合释放

前序遍历先输出根 50,再递归左子树(30、20、40),再右子树(70、60、80),所以是 50 30 20 40 70 60 80。后序遍历先处理完两棵子树才输出根,所以根 50 排在最后,得到 20 40 30 60 80 70 50。

释放整棵树用的是后序:必须先把左右子树都释放完,最后才释放根节点——如果先释放根,就丢失了指向子树的指针,子树就泄漏了。tree_free 的结构和 postorder 完全一致,只是把 printf 换成了 free。

要点 ​

  • 二叉树每个节点最多两个子节点;二叉搜索树满足左小右大。
  • BST 的插入是递归的:比当前小走左、大走右,走到空位插入。
  • 中序遍历(左、根、右)对 BST 一定输出升序序列。
  • 前序先访问根,后序最后访问根;释放整棵树必须用后序。
  • 递归天然契合树的分形结构:每个子树的处理方式与整棵树相同。
○读完本节了吗?多端同步已开启 ·