二叉搜索树

.h

#ifndef _BINARYSEARCHTREE_H
#define _BINARYSEARCHTREE_H


struct tree_node;
typedef int element_type;
typedef struct tree_node* search_tree;

struct tree_node{
    element_type element;
    search_tree left;
    search_tree right;
};

/*将一棵树置空
实质上是树的后序遍历*/
search_tree make_empty(search_tree root);
/*寻找元素*/
search_tree find(search_tree root,element_type e);
/*寻找最小元素*/
search_tree find_min(search_tree root);
search_tree find_max(search_tree root);
/*插入元素
当向空树插入元素时,树的根节点发生变化,因此insert()返回树的根节点
当元素已经存在于树中,insert不操作
插入的元素必定成为新的叶子,它的父节点必定是未插入时树的叶子*/
search_tree insert(search_tree root,element_type e);
/*删除元素*/
search_tree delete(search_tree root,element_type e);
element_type retrieve(search_tree root);
#endif

.c

#include "BinarySearchTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>


static search_tree delete_min(search_tree root);
static void fatal_error(const char* msg);

search_tree make_empty(search_tree root){
    if(root != NULL){
        make_empty(root->left);
        make_empty(root->right);
        free(root);
    }
    return NULL;
}

search_tree find(search_tree root,element_type e){
    if(root == NULL){
        return NULL;
    }
    if(e < root->element){//元素在左子树
        return find(root->left,e);
    }else if(e > root->element){
        return find(root->right,e);
    }else{
        return root;//找到元素
    }
}

search_tree find_min(search_tree root){
    if(root == NULL){
        return NULL;
    }else if(root->left == NULL){
        return root;
    }else{
        return find_min(root->left);
    }
}

search_tree find_max(search_tree root){
    if(root == NULL){
        return NULL;
    }
    while(root->right!=NULL){
        root = root->right;
    }
    return root;
}

search_tree insert(search_tree root,element_type e){
    if(root == NULL){//空树
        root = malloc(sizeof(struct tree_node));
        if(root == NULL){
            fatal_error("malloc failed");
        }
        root->element = e;
        root->left = root->right = NULL;
    }else {
        search_tree father;//父节点
        search_tree node = root;
        while(node != NULL){
            father = node;
            if(e < node->element){//插入左子树中
                node = node->left;
            }else if(e > node->element){
                node = node->right;
            }else{//元素已存在于树中,直接返回
                return root;
            }
        }
        node = malloc(sizeof(struct tree_node));
        if(node == NULL){
            fatal_error("malloc failed");
        }
        node->element = e;
        node->left = node->right = NULL;
        //判断在叶子的左孩子还是右孩子
        if(e < father->element){
            father->left = node;
        }else{
            father->right = node;
        }
    }
    return root;
}
#if 0
search_tree insert(search_tree root,element_type e){
    if(root == NULL){
        root = malloc(sizeof(struct tree_node));
        if(root == NULL){
            fatal_error("malloc failed");
        }
        root->element = e;
        root->left = root->right = NULL;
    }else if(e < root->element){
        root->left = insert(root->left,e);
    }else if(e > root->element){
        root->right = insert(root->right,e);
    }
    return root;
}
#endif
search_tree delete(search_tree root,element_type e){
    if(root == NULL){
        return NULL;
    }else if(e < root->element){
        root->left = delete(root->left,e);
    }else if(e > root->element){
        root->right = delete(root->right,e);
    }else if(root->left && root->right){
        search_tree temp = find_min(root->right);
        root->element = temp->element;
        // root->right = delete_min(root->right);
        root->right = delete(root->right,e);
    }else{
        search_tree temp = root;
        if(root->left == NULL){//无左子树
            root = root->right;
        }else{
            root = root->left;
        }
        free(temp);     
    }
    return root;
}

search_tree delete_min(search_tree root){
    if(root == NULL){
        return NULL;
    }else if(root->left == NULL){
        search_tree right = root->right;
        free(root);
        return right;
    }else{
        root->left = delete_min(root->left);
        return root;
    }
}
#if 0
static delete_min(search_tree root){
    if(root == NULL){
        return NULL;
    }else if(root->left == NULL){
        search_tree right = root->right;
        free(root);
        return right;
    }else{
        search_tree father = root;
        search_tree node = root;
        while(node->left != NULL){
            father = node;
            node = node->left;
        }
        father->left = node->right;
        free(node);
        return father;
    }
}
#endif

element_type retrieve(search_tree root){
    if(root) return root->element;
}

void fatal_error(const char* msg){
    fprintf(stderr,"%s\n",msg);
    exit(1);
}

test

#include "BinarySearchTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void print_tree(search_tree tree,int depth){
    if(tree != NULL){
        for(int i = 0;i<depth;++i){
            printf("  ");
        }
        printf("%d\n",retrieve(tree));
        print_tree(tree->left,depth+1);
        print_tree(tree->right,depth+1);
    }
}

int main(int argc,char** argv){
    search_tree tree=NULL;
    tree = insert(tree,7);
    assert(tree != NULL);
    insert(tree,3);
    insert(tree,9);
    insert(tree,4);
    insert(tree,2);
    insert(tree,5);
    insert(tree,8);
    print_tree(tree,0);

    printf("min=%d\n",retrieve(find_min(tree)));
    printf("max=%d\n",retrieve(find_max(tree)));
    printf("find 4 =%d\n",retrieve(find(tree,4)));

    tree = delete(tree,4);
    print_tree(tree,0);
    tree = delete(tree,8);
    print_tree(tree,0);
    tree = make_empty(tree);
    print_tree(tree,0);

    return 0;
}

out

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值