"Data Structure - C Language Implementation Tree (tree)" implements a binary tree, and includes preorder, inorder, and postorder traversal

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

// 定义二叉树的节点结构体
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

// 创建一个新的节点
struct TreeNode* new_node(int val) {
    struct TreeNode* node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
    node->val = val;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// 插入节点
struct TreeNode* insert_node(struct TreeNode* root, int val) {
    if (root == NULL) {
        root = new_node(val);
        return root;
    }
    if (val < root->val) {
        root->left = insert_node(root->left, val);
    } else {
        root->right = insert_node(root->right, val);
    }
    return root;
}

// 前序遍历
void preorder_traversal(struct TreeNode* root) {
    if (root != NULL) {
        printf("%d ", root->val);
        preorder_traversal(root->left);
        preorder_traversal(root->right);
    }
}

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

// 后序遍历
void postorder_traversal(struct TreeNode* root) {
    if (root != NULL) {
        postorder_traversal(root->left);
        postorder_traversal(root->right);
        printf("%d ", root->val);
    }
}

int main() {
    // 创建一个二叉树
    struct TreeNode* root = new_node(5);
    insert_node(root, 3);
    insert_node(root, 7);
    insert_node(root, 2);
    insert_node(root, 4);
    insert_node(root, 6);
    insert_node(root, 8);

    // 前序遍历二叉树并输出结果
    printf("前序遍历结果:");
    preorder_traversal(root);
    printf("\n");

    // 中序遍历二叉树并输出结果
    printf("中序遍历结果:");
    inorder_traversal(root);
    printf("\n");

    // 后序遍历二叉树并输出结果
    printf("后序遍历结果:");
    postorder_traversal(root);
    printf("\n");

    return 0;
}

In this example, we first define a TreeNodestructure named struct to represent the nodes of the binary tree. We also define functions new_nodefor creating a new node and initializing its value and child node pointers.

In insert_nodethe function, we implement the function of inserting nodes into the binary tree. If the tree is empty, we create a new node and return it. If the node value is smaller than the root node, it is inserted into the left subtree, otherwise it is inserted into the right subtree.

In preorder_traversalthe function, we implement the function of traversing the binary tree in preorder. We first visit the root node, then traverse the left

subtree, and finally traverse the right subtree. In inorder_traversalthe function, we implement the function of traversing the binary tree in order. We first traverse the left subtree, then visit the root node, and finally traverse the right subtree. In postorder_traversalthe function, we implement the function of post-order traversal of the binary tree. We traverse the left subtree first, then the right subtree, and finally visit the root node.

In mainthe function, we first create a new binary tree and insert some nodes into it. Then, we call the and functions respectively preorder_traversalto inorder_traversalperform postorder_traversalpre-order, in-order and post-order traversal on this binary tree, and output the results of the traversal.

Note that the above code is just a simple example to demonstrate the implementation of binary tree and pre-order, in-order and post-order traversal. In fact, in practical applications, some additional factors need to be considered, such as the balance of the binary tree, the algorithmic efficiency of inserting and deleting nodes, and so on.

Guess you like

Origin blog.csdn.net/Like_Bamboo/article/details/129683389