以下是使用C语言实现以上任务的代码:

  1. 二叉树的建立与删除
typedef struct node{
    int data;
    struct node* left;
    struct node* right;
}Node;

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

void delete_tree(Node* root){
    if(root == NULL){
        return;
    }
    delete_tree(root->left);
    delete_tree(root->right);
    free(root);
}
  1. 二叉树的遍历
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);
}
  1. 二叉树的叶子和深度
int count_leaf(Node* root){
    if(root == NULL){
        return 0;
    }
    if(root->left == NULL && root->right == NULL){
        return 1;
    }
    return count_leaf(root->left) + count_leaf(root->right);
}

int get_depth(Node* root){
    if(root == NULL){
        return 0;
    }
    int left_depth = get_depth(root->left);
    int right_depth = get_depth(root->right);
    return (left_depth > right_depth ? left_depth : right_depth) + 1;
}
  1. 二叉查找树的基本操作
Node* create_bst(int data){
    Node* root = (Node*)malloc(sizeof(Node));
    root->data = data;
    root->left = NULL;
    root->right = NULL;
    return root;
}

void insert_bst(Node** root, int data){
    if(*root == NULL){
        *root = create_bst(data);
        return;
    }
    if(data < (*root)->data){
        insert_bst(&((*root)->left), data);
    }
    else if(data > (*root)->data){
        insert_bst(&((*root)->right), data);
    }
}

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

Node* get_min_node(Node* root){
    if(root == NULL || root->left == NULL){
        return root;
    }
    return get_min_node(root->left);
}

Node* delete_bst(Node* root, int data){
    if(root == NULL){
        return NULL;
    }
    if(data < root->data){
        root->left = delete_bst(root->left, data);
    }
    else if(data > root->data){
        root->right = delete_bst(root->right, data);
    }
    else{
        if(root->left == NULL){
            Node* temp = root->right;
            free(root);
            return temp;
        }
        else if(root->right == NULL){
            Node* temp = root->left;
            free(root);
            return temp;
        }
        Node* temp = get_min_node(root->right);
        root->data = temp->data;
        root->right = delete_bst(root->right, temp->data);
    }
    return root;
}

int get_max_value(Node* root){
    if(root == NULL){
        return -1;
    }
    while(root->right != NULL){
        root = root->right;
    }
    return root->data;
}

int get_min_value(Node* root){
    if(root == NULL){
        return -1;
    }
    while(root->left != NULL){
        root = root->left;
    }
    return root->data;
}
C语言实现二叉树操作:建立、删除、遍历、叶子节点和深度、二叉查找树

原文地址: https://www.cveoy.top/t/topic/n5gp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录