C语言需要掌握的基础知识点之树

C语言需要掌握的基础知识点之树

在C语言中,树(Tree)是一种非常重要的非线性数据结构,它由n(n≥0)个有限节点组成的一个具有层次关系的集合。以下是树的详细介绍:

树的基本概念

树的定义

根节点:树的最顶层节点

父节点、子节点:节点之间的关系

叶子节点:没有子节点的节点

度:节点拥有的子树数

深度:从根节点到该节点的路径长度

高度:从该节点到最远叶子节点的路径长度

二叉树

二叉树节点定义

c 复制代码
typedef struct TreeNode {
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
} TreeNode;

创建新节点

c 复制代码
TreeNode* createNode(int data) {
    TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    return newNode;
}

树的遍历

深度优先遍历

c 复制代码
// 前序遍历:根->左->右
void preOrder(TreeNode* root) {
    if (root != NULL) {
        printf("%d ", root->data);
        preOrder(root->left);
        preOrder(root->right);
    }
}

// 中序遍历:左->根->右
void inOrder(TreeNode* root) {
    if (root != NULL) {
        inOrder(root->left);
        printf("%d ", root->data);
        inOrder(root->right);
    }
}

// 后序遍历:左->右->根
void postOrder(TreeNode* root) {
    if (root != NULL) {
        postOrder(root->left);
        postOrder(root->right);
        printf("%d ", root->data);
    }
}

广度优先遍历(层次遍历)

c 复制代码
void levelOrder(TreeNode* root) {
    if (root == NULL) return;
    
    TreeNode* queue[100];
    int front = 0, rear = 0;
    
    queue[rear++] = root;
    
    while (front < rear) {
        TreeNode* current = queue[front++];
        printf("%d ", current->data);
        
        if (current->left != NULL)
            queue[rear++] = current->left;
        if (current->right != NULL)
            queue[rear++] = current->right;
    }
}

二叉搜索树(BST)

插入节点

c 复制代码
TreeNode* insertBST(TreeNode* root, int data) {
    if (root == NULL) {
        return createNode(data);
    }
    
    if (data < root->data) {
        root->left = insertBST(root->left, data);
    } else if (data > root->data) {
        root->right = insertBST(root->right, data);
    }
    
    return root;
}

查找节点

c 复制代码
TreeNode* searchBST(TreeNode* root, int key) {
    if (root == NULL || root->data == key) {
        return root;
    }
    
    if (key < root->data) {
        return searchBST(root->left, key);
    } else {
        return searchBST(root->right, key);
    }
}

删除节点

c 复制代码
TreeNode* deleteBST(TreeNode* root, int key) {
    if (root == NULL) return root;
    
    if (key < root->data) {
        root->left = deleteBST(root->left, key);
    } else if (key > root->data) {
        root->right = deleteBST(root->right, key);
    } else {
        // 节点有一个或没有子节点
        if (root->left == NULL) {
            TreeNode* temp = root->right;
            free(root);
            return temp;
        } else if (root->right == NULL) {
            TreeNode* temp = root->left;
            free(root);
            return temp;
        }
        
        // 节点有两个子节点:找到右子树的最小值
        TreeNode* temp = root->right;
        while (temp && temp->left != NULL) {
            temp = temp->left;
        }
        
        root->data = temp->data;
        root->right = deleteBST(root->right, temp->data);
    }
    return root;
}

树的常用操作

计算树的高度

c 复制代码
int treeHeight(TreeNode* root) {
    if (root == NULL) {
        return 0;
    }
    
    int leftHeight = treeHeight(root->left);
    int rightHeight = treeHeight(root->right);
    
    return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}

计算节点总数

c 复制代码
int countNodes(TreeNode* root) {
    if (root == NULL) {
        return 0;
    }
    
    return 1 + countNodes(root->left) + countNodes(root->right);
}

检查是否为完全二叉树

c 复制代码
int isCompleteTree(TreeNode* root) {
    if (root == NULL) return 1;
    
    TreeNode* queue[100];
    int front = 0, rear = 0;
    int flag = 0; // 标记是否遇到空节点
    
    queue[rear++] = root;
    
    while (front < rear) {
        TreeNode* current = queue[front++];
        
        if (current == NULL) {
            flag = 1;
        } else {
            if (flag) return 0; // 如果已经遇到空节点,又遇到非空节点
            
            queue[rear++] = current->left;
            queue[rear++] = current->right;
        }
    }
    return 1;
}

完整示例

c 复制代码
#include <stdio.h>
#include <stdlib.h>

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

// 创建新节点
TreeNode* createNode(int data) {
    TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

// 中序遍历
void inOrder(TreeNode* root) {
    if (root != NULL) {
        inOrder(root->left);
        printf("%d ", root->data);
        inOrder(root->right);
    }
}

// 插入BST节点
TreeNode* insertBST(TreeNode* root, int data) {
    if (root == NULL) return createNode(data);
    
    if (data < root->data)
        root->left = insertBST(root->left, data);
    else if (data > root->data)
        root->right = insertBST(root->right, data);
    
    return root;
}

int main() {
    TreeNode* root = NULL;
    int values[] = {50, 30, 70, 20, 40, 60, 80};
    int n = sizeof(values) / sizeof(values[0]);
    
    // 构建BST
    for (int i = 0; i < n; i++) {
        root = insertBST(root, values[i]);
    }
    
    printf("中序遍历结果: ");
    inOrder(root);
    printf("\n");
    
    return 0;
}
相关推荐
4311媒体网1 小时前
C语言操作符全解析 C语言操作符详解
java·c语言·jvm
二年级程序员1 小时前
一篇文章掌握“顺序表”
c语言·数据结构
傻乐u兔2 小时前
C语言进阶————指针4
c语言·开发语言
历程里程碑2 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
2601_9491465310 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
知南x12 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言
2的n次方_13 小时前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫14 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫16 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
傻乐u兔17 小时前
C语言进阶————指针3
c语言·开发语言