【c】c语言二叉树的创建、初始化、print

该博客介绍了如何使用C语言创建和打印字符与整型二叉树。通过递归实现从给定的字符或整数序列构建二叉树,并展示了一种自底向上的构建方式。在创建完成后,使用中序遍历展示二叉树的结构。虽然没有涵盖增删改查操作,但提供了参考链表操作的提示。
摘要由CSDN通过智能技术生成

0. 环境

ubuntu-1804
GCC-7.5.0


1. 字符二叉树

1. create & print

1.1 源码

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

#define DEBUG

typedef struct node_t{
    struct node_t *left;
    struct node_t *right;
    char val;
}NODE;

typedef struct {
    NODE *node;
    int used;
}RET;

RET create_tree(const char * p , int size){
#ifdef DEBUG
    printf("size = %d\n",size);
#endif
    if (size == 0) {
        return (RET){NULL, 0};
    }
    if (p[0] == '#') {
        return (RET){NULL, 1};
    }
    NODE *node = (NODE *)malloc(sizeof(NODE));
    node->val = p[0];
    RET left  = create_tree(p + 1,              size - 1            );
    RET right = create_tree(p + 1 + left.used,  size - 1 - left.used);
    node->left  = left.node;
    node->right = right.node;
    return (RET){
        node, 1 + left.used + right.used
    };
}

void display_tree(NODE * node){
    if (node == NULL) {
        return ;
    }
    printf("%c ",node->val);
    display_tree(node->left );
    display_tree(node->right);
}


int main(){

    /* 0. data */
    const char * p = "ABD###CE";
    /*
             A
            / \
           B   C
          / \ / 
         D  # E  
        / \
       #   #
    */
    int size = strlen(p);
#ifdef DEBUG
    printf("\n");
    for (int i = 0; i < size; ++i) {
        printf("%c ",*(p+i));
    }
    printf("\n");
#endif

    /* 1. create tree */
    RET ret = create_tree(p , size);
    NODE * root = ret.node;

    /* 2. display */
#ifdef DEBUG
    printf("root->val              = %c\n",root->val                );
    printf("root->left->val        = %c\n",root->left->val          );
    printf("root->right->val       = %c\n",root->right->val         );
    printf("root->left->left->val  = %c\n",root->left->left->val    );
    printf("root->right->left->val = %c\n",root->right->left->val   );
#endif
    display_tree(root);
    printf("\n");

    return  0;
}

1.2 LOG


A B D # # # C E
size = 8
size = 7
size = 6
size = 5
size = 4
size = 3
size = 2
size = 1
size = 0
size = 0
size = 0
root->val              = A
root->left->val        = B
root->right->val       = C
root->left->left->val  = D
root->right->left->val = E
A B D C E

2. 增删改查

略,参考链表即可


2. 整型二叉树

1. create & print

1.1 源码

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

#define DEBUG
#define NULL_NODE (-1)

typedef struct node_t{
    struct node_t *left;
    struct node_t *right;
    int val;
}NODE;

typedef struct {
    NODE *node;
    int used;
}RET;

RET create_tree(const int * p , int size){
#ifdef DEBUG
    printf("size = %d\n",size);
#endif
    if (size == 0) {
        return (RET){NULL, 0};
    }
    if (p[0] == NULL_NODE) {
        return (RET){NULL, 1};
    }
    NODE *node = (NODE *)malloc(sizeof(NODE));
    node->val = p[0];
    RET left  = create_tree(p + 1,              size - 1            );
    RET right = create_tree(p + 1 + left.used,  size - 1 - left.used);
    node->left  = left.node;
    node->right = right.node;
    return (RET){
        node, 1 + left.used + right.used
    };
}

void display_tree(NODE * node){
    if (node == NULL) {
        return ;
    }
    printf("%d ",node->val);
    display_tree(node->left );
    display_tree(node->right);
}


int main(){

    /* 0. data */
    const int arr[] = {1,2,4,NULL_NODE,NULL_NODE,NULL_NODE,3,5};
    /*
             1
            / \
           2   3
          / \ / 
         4  # 5  
        / \
       #   #
    */
    int size = sizeof(arr)/sizeof(arr[0]);
#ifdef DEBUG
    printf("\n");
    for (int i = 0; i < size; ++i){
        printf("%d ", arr[i]);
    }
    printf("\n");
#endif

    /* 1. create tree */
    RET ret = create_tree(arr , size);
    NODE * root = ret.node;

    /* 2. display */
#ifdef DEBUG
    printf("root->val              = %d\n",root->val                );
    printf("root->left->val        = %d\n",root->left->val          );
    printf("root->right->val       = %d\n",root->right->val         );
    printf("root->left->left->val  = %d\n",root->left->left->val    );
    printf("root->right->left->val = %d\n",root->right->left->val   );
#endif
    display_tree(root);
    printf("\n");

    return  0;
}

1.2 LOG

1 2 4 -1 -1 -1 3 5
size = 8
size = 7
size = 6
size = 5
size = 4
size = 3
size = 2
size = 1
size = 0
size = 0
size = 0
root->val              = 1
root->left->val        = 2
root->right->val       = 3
root->left->left->val  = 4
root->right->left->val = 5
1 2 4 3 5

2. 增删改查

略,参考链表即可

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: #include <stdio.h> struct node { int data; struct node *left; struct node *right; }; /* function to create a new node of tree and returns pointer */ struct node* newNode(int data) { struct node* node = (struct node*)malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /* function for inorder traversal */ void inorder(struct node* root) { if (root != NULL) { inorder(root->left); printf("%d ", root->data); inorder(root->right); } } /* function to insert data recursively */ struct node* insert(struct node* node, int data) { /* if the tree is empty, return a new node */ if (node == NULL) return newNode(data); /* otherwise, recur down the tree */ if (data <= node->data) node->left = insert(node->left, data); else node->right = insert(node->right, data); /* return the (unchanged) node pointer */ return node; } /* Driver program to test above functions */ int main() { /* Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 */ struct node *root = NULL; root = insert(root, 50); insert(root, 30); insert(root, 20); insert(root, 40); insert(root, 70); insert(root, 60); insert(root, 80); // print inoder traversal of the BST inorder(root); return 0; }答:这是用C语言构建排序二叉树的具体代码:#include <stdio.h>struct node {int data;struct node *left;struct node *right;};/* function to create a new node of tree and returns pointer */struct node* newNode(int data){struct node* node = (struct node*)malloc(sizeof(struct node));node->data = data;node->left = NULL;node->right = NULL;return(node);}/* function for inorder traversal */void inorder(struct node* root){if (root != NULL){inorder(root->left);printf("%d ", root->data);inorder(root->right);}}/* function to insert data recursively */struct node* insert(struct node* node, int data){/* if the tree is empty, return a new node */if (node == NULL) return newNode(data);/* otherwise, recur down the tree */if (data <= node->data)node->left = insert(node->left, data);elsenode->right = insert(node->right, data);/* return the (unchanged) node pointer */return node;}/* Driver program to test above functions */int main(){/* Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 */struct node *root = NULL;root = insert(root, 50);insert(root, 30);insert(root, 20);insert(root, 40);insert(root, 70);insert(root, 60);insert(root, 80);// print inoder traversal of the BSTinorder(root);return 0;} ### 回答2: 以下是用C语言构建排序二叉树的具体代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct node { int data; struct node* left; struct node* right; } Node; // 创建新的节点 Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } // 将节点插入二叉树 Node* insertNode(Node* root, int data) { // 如果根节点为空,则将数据创建为根节点 if (root == NULL) { root = createNode(data); return root; } // 如果数据小于根节点的值,则将数据插入到左子树中 else if (data < root->data) { root->left = insertNode(root->left, data); } // 如果数据大于等于根节点的值,则将数据插入到右子树中 else { root->right = insertNode(root->right, data); } return root; } // 前序遍历二叉树(根-左-右) void preOrder(Node* root) { if (root == NULL) { return; } printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } int main() { Node* root = NULL; int inputData[] = { 6, 4, 8, 2, 5, 7, 9 }; // 插入数组中的元素到二叉树int i; for (i = 0; i < sizeof(inputData) / sizeof(inputData[0]); i++) { root = insertNode(root, inputData[i]); } printf("前序遍历结果:"); preOrder(root); return 0; } ``` 这段代码首先定义了一个节点结构体,结构体中包含节点数据和指向左右子节点的指针。然后定义了创建节点的函数`createNode`和插入节点的函数`insertNode`。在插入节点的函数中,如果根节点为空,则将数据创建为根节点;如果数据小于根节点的值,则将数据插入到左子树中;如果数据大于等于根节点的值,则将数据插入到右子树中。最后定义了一个前序遍历二叉树的函数`preOrder`,用来递归遍历二叉树并输出节点的值。在主函数中,首先初始化根节点为NULL,然后将数组中的元素都插入到二叉树中,最后调用前序遍历函数输出排序后的结果。 ### 回答3: 要构建一个排序二叉树,首先需要定义二叉树的结构。可以使用如下的C语言代码定义一个二叉树的结构: ```c struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; }; ``` 接下来,可以通过递归的方式来构建排序二叉树。具体代码如下: ```c struct TreeNode* insertIntoBST(struct TreeNode* root, int val) { if (root == NULL) { // 如果二叉树为空,则创建一个新的节点作为根节点 struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode)); newNode->val = val; newNode->left = NULL; newNode->right = NULL; return newNode; } if (val < root->val) { // 如果插入的值小于当前节点的值,则递归地插入到左子树中 root->left = insertIntoBST(root->left, val); } else { // 如果插入的值大于等于当前节点的值,则递归地插入到右子树中 root->right = insertIntoBST(root->right, val); } return root; } ``` 在上述代码中,`insertIntoBST`函数用于向排序二叉树中插入一个新的节点。当二叉树为空时,创建一个新节点作为根节点。如果要插入的值小于当前节点的值,则递归地插入到左子树中;如果要插入的值大于等于当前节点的值,则递归地插入到右子树中。最后返回根节点。 通过多次调用`insertIntoBST`函数,就可以依次插入多个节点,从而构建出一个排序二叉树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过得精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值