C语言实现二叉搜索树

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

typedef struct node
{
    int data;
    struct node * left;
    struct node * right;
} Node;

typedef struct {
    Node * root;
} Tree;

void insert(Tree * tree, int value) {
    Node * node = (Node *)malloc(sizeof(Node));
    node->data = value;
    node->left = NULL;
    node->right = NULL;

    if (tree->root == NULL)
    {
        tree->root = node;
    } else {
        Node * temp = tree->root;
        while (temp != NULL)
        {
            if (value < temp->data)
            {
                if (temp->left == NULL)
                {
                    temp->left = node;
                    return;
                } else {
                    temp = temp->left;
                }
            } else {
                if (temp->right == NULL)
                {
                    temp->right = node;
                    return;
                } else {
                    temp = temp->right;
                }
            }
        }
    }
}

void preorder(Node * node) {
    if (node != NULL)
    {
        printf("%d ", node->data);
        preorder(node->left);
        preorder(node->right);
    }
}

void inorder(Node * node) {
    if (node != NULL)
    {
        inorder(node->left);
        printf("%d ", node->data);
        inorder(node->right);
    }
}

void postorder(Node * node) {
    if (node != NULL)
    {
        postorder(node->left);
        postorder(node->right);
        printf("%d ", node->data);
    }
}

// 求树的高度
int get_height(Node * node) {
    if (node == NULL)
    {
        return 0;
    } else {
        int left_h = get_height(node->left);
        int right_h = get_height(node->right);
        int max = left_h;
        if (right_h > left_h)
        {
            max = left_h;
        }
        return max + 1;
    }
}

int get_maximum(Node * node) {
    if (node == NULL)
    {
        return -1;
    } else {
        int left_max = get_maximum(node->left);
        int right_max = get_maximum(node->right);
        int current = node->data;
        int max = left_max;
        if (right_max > left_max)
        {
            max = right_max;
        }

        if (current > max)
        {
            max = current;
        }
        
        return max;
    }
    
}

// 二叉搜索树
int main(void) {
    int arr[] = {6,3,8,2,5,1,7};
    Tree tree;
    tree.root = NULL;
    for (int i = 0; i < 7; i++)
    {
        insert(&tree, arr[i]);
    }
    printf("\n");
    preorder(tree.root);
    printf("\n");
    inorder(tree.root);
    printf("\n");
    postorder(tree.root);

    int h = get_height(tree.root);
    printf("\n树的高度为 %d\n", h);
    int max = get_maximum(tree.root);
    printf("\n树的最大值 %d\n", max);
    return 0;
}

运行效果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值