二叉搜索树模板(java)

小白一枚,希望我对二叉搜索树的理解可以帮到你们一点

可以高效的进行查找,插入,
删除元素 - 动态的维护数据,它的复杂度只有O(logn),而普通数组进行这些操作时,它的复杂度达到O(n).
二叉树:
每个节点的键值大于左孩子;
每个节点的键值小于右孩子;
以左右孩子为根的子树仍为二叉搜索树

多说无益,具体见代码:



----------
## Java版 ##

package 二叉搜索树模板;

import java.util.Scanner;

public class MainDemo {
    // 初始化二叉搜索树
    static class Tree{
        int key;//key值
        int value;//value值
        Tree left;//左孩子
        Tree right;//有孩子

        //构造方法
        Tree(int key, int value){
            this.key = key;
            this.value = value;
        }
    }
    static Tree root;//根节点
    static int num = 0;//节点的个数

    //建立二叉搜索树 
    public static void insert(int key, int value) {
        root = insert(root, key, value);
    }
    public static Tree insert(Tree node, int key, int value) {
        if(node == null) {//节点为空,说明二叉树中不存在key-value值,需添加上
            return new Tree(key, value);
        }
        if(key == node.key) {//二叉树中存在key值,则用新的value值替换
            node.value = value;
        }
        else if(key < node.key) {//访问左孩子
            node.left = insert(node.left, key, value);
        }
        else {//访问右孩子
            node.right = insert(node.right, key, value);
        }
        return node;
    }

    //二叉树中是否存在key值
    public static boolean contain(int key) {
        return contain(root, key);
    }
    public static boolean contain(Tree tree, int key) {
        if(tree == null) {
            return false;
        }
        if(key == tree.key) {
            return true;
        }
        else if(key < tree.key) {//访问左孩子
            return contain(tree.left, key);
        }
        else {//访问右孩子
            return contain(tree.right, key);
        }
    }

    //查询key值所对应的value值(前提二叉树中存在key值,若不存在,则先用contain检验)
    //下面代码只是key值存在的情况
    public static int search(int key) {
        return search(root, key);
    }
    public static int search(Tree tree, int key) {
        if(key == tree.key) {
            return tree.value;
        }
        else if(key < tree.key ) {//访问左孩子
            return search(tree.left, key);
        }
        else {//访问右孩子
            return search(tree.right, key);
        }
    }

    //查询二叉树中的最小值
    public static int SearchMin() {
        if(root != null) {
            Tree min = SearchMin(root);
            return min.key;
        }
        return -1;//表示不存在
    }
    public static Tree SearchMin(Tree tree) {
        if(tree.left == null) {
            return tree;
        }
            return SearchMin(tree.left);
    }

    //查询二叉树中的最大值
    public static int SearchMax() {
        if(root != null) {
            Tree max = SearchMax(root);
            return max.key;
        }
        return -1;//表示不存在
    }
    public static Tree SearchMax(Tree tree) {
        if(tree.right == null) {
            return tree;
        }
        return SearchMax(tree.right);
    }

    //删除最小值(前提二叉树不为空)
    public static void deleteMin() {
        if(root != null)
            root = deleteMin(root);
    }
    public static Tree deleteMin(Tree tree) {
        if(tree.left == null) {//找到最小值
            Tree rightTree = tree.right;
            tree = null;
            return rightTree;
        }
        tree.left = deleteMin(tree.left);//访问左孩子
        return tree;
    }

    //删除最大值(前提二叉树不为空)
    public static void deleteMax() {
        if(root != null) {
            root = deleteMax(root);
        }
    }
    public static Tree deleteMax(Tree tree) {
        if(tree.right == null) {
            Tree leftTree = tree.left;
            tree = null;
            return leftTree;
        }
        tree.right = deleteMax(tree.right);
        return tree;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        insert(1, 1);
        insert(2, 2);
        insert(3, 3);
        insert(4, 4);
        insert(5, 5);
        insert(6, 6);
        System.out.println(contain(1));//是否存在key值
        System.out.println(search(1));//查询key所对应的value值
        System.out.println(SearchMin());//最小值
        System.out.println(SearchMax());//最大值   
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值