c_cpp C语言中的二叉搜索树实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C语言中的二叉搜索树实现相关的知识,希望对你有一定的参考价值。

/* Binary Search Tree Implementation in C */
/* Harish R */

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

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

struct TreeNode* makeEmpty(struct TreeNode* root)
{
    if(root != NULL)
    {
        makeEmpty(root->left);
        makeEmpty(root->right);
        free(root);
    }
    return NULL;
}

struct TreeNode* insert(struct TreeNode* root, int x)
{
    if(root == NULL)
    {
        root = malloc(sizeof(struct TreeNode));
        root->data = x;
        root->left = root->right = NULL;
    }
    else if(x < root->data)
        root->left = insert(root->left, x);
    else if(x > root->data)
        root->right = insert(root->right, x);
    return root;
}

struct TreeNode* findMin(struct TreeNode* root)
{
    if(root == NULL)
    	return NULL;
    else if(root->left == NULL)
    	return root;
    else
    	return findMin(root->left);
}

struct TreeNode* findMax(struct TreeNode* root)
{
    if(root == NULL)
    	return NULL;
    else if(root->right == NULL)
    	return root;
    else
    	return findMax(root->right);
}

struct TreeNode* find(struct TreeNode* root, int x)
{
    if(root == NULL)
        return NULL;
    else if(x < root->data)
        return find(root->left, x);
    else if(x > root->data)
        return find(root->right, x);
    else
        return root;
}

int findHeight(struct TreeNode* root)
{
    int lefth, righth;
    if(root == NULL)
        return -1;
    lefth = findHeight(root->left);
    righth = findHeight(root->right);
    return (lefth > righth ? lefth : righth)+1;
}

struct TreeNode* delete(struct TreeNode* root, int x)
{
    struct TreeNode* temp;
    if(root == NULL)
        return NULL;
    else if(x < root->data)
        root->left = delete(root->left, x);
    else if(x > root->data)
        root->right = delete(root->right, x);
    else if(root->left && root->right)
    {
        temp = findMin(root->right);
        root->data = temp->data;
        root->right = delete(root->right, root->data);
    }
    else
    {
        temp = root;
        if(root->left == NULL)
            root = root->right;
        else if(root->right == NULL)
            root = root->left;
        free(temp);
    }
    return root;
}

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

int main()
{
    struct TreeNode *root;
    struct TreeNode *temp;
    root = NULL;
    root = insert(root, 15);
    root = insert(root, 10);
    root = insert(root, 25);
    root = insert(root, 7);
    printf("Height: %d\n", findHeight(root));
    root = insert(root, 13);
    root = insert(root, 18);
    root = insert(root, 30);
    printf("Height: %d\n", findHeight(root));
    root = insert(root, 3);
    root = insert(root, 8);
    root = insert(root, 16);
    printf("Height: %d\n", findHeight(root));
    root = insert(root, 22);
    root = insert(root, 35);
    inorder(root); printf("\n");
    temp = findMax(root);
    printf("Max Element: %d\n", temp->data);
    temp = findMin(root);
    printf("Min Element: %d\n", temp->data);
    root = delete(root, 8);
    root = delete(root, 16);
    inorder(root); printf("\n");
    root = delete(root, 18);
    inorder(root); printf("\n");
    root = delete(root, 10);
    inorder(root); printf("\n");
    root = delete(root, 35);
    inorder(root); printf("\n");
    root = makeEmpty(root);
    return 0;
}



以上是关于c_cpp C语言中的二叉搜索树实现的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 是有效的二叉搜索树。验证BST

c_cpp 独特的二叉搜索树。给定n,生成存储值1 ... n的所有结构上唯一的BST(二叉搜索树)。

Cuda C中的二叉树搜索 - 并行

c_cpp 设S = {x1,x2,...,xn}是一个有序集合,且x1,x2,...,xn表示有序集合的二叉搜索树利用二叉树的顶点存储有序集中的元素,而且具有性质:存储于每个顶点中的元素x大于其左子

C ++中的二叉搜索树,叶子为空值,不适用于参考参数

C中的二叉树 - 多数据