数据结构与算法分析(Java语言描述)(16)—— 二叉搜索树基础、节点插入、查找

这里写图片描述

这里写图片描述


基础


// 二叉搜索树
// 由于Key需要能够进行比较,所以需要extends Comparable<Key>
public class BinarySearchTree<Key extends Comparable<Key>, Value> {

    // 树中的节点为私有的类,外界不需要了解二叉搜索树节点的具体实现
    private class Node{
        private Key key;
        private Value value;
        private Node left, right;

        Node(Key key, Value value){
            this.key = key;
            this.value = value;
            this.left = null;
            this.right = null;
        }
    }

    private Node root; // 根节点
    private int count; // 树中的节点个数

    // 构造函数,默认构造一棵空二叉搜索树
    public BinarySearchTree() {
        root = null;
        count = 0;
    }

    // 返回二叉搜索树的节点个数
    public int size() {
        return count;
    }

    // 返回二叉搜索树是否为空
    public boolean isEmpty() {
        return count == 0;
    }
}

节点插入


    // 向二叉搜索树中插入一个新的(key,value)数据对
    public void insert(Key key, Value value) {
        root = insert(root, key, value);
    }

    // 向以 node 为根的二叉搜索树中插入新节点(key,value),使用递归算法
    private Node insert(Node node, Key key, Value value) {
        // 递归到底的情况
        if (node == null) {
            count++;
            return new Node(key, value);
        }

        // 如果要插入的 key == node.key,更新 node.value
        if (key.compareTo(node.key) == 0) {
            node.value = value;

            // 如果要插入的 key > node.key,向右子树 insert
        } else if (key.compareTo(node.key) > 0) {
            node.right = insert(node.right, key, value);

            // 如果要插入的 key < node.key,向左子树 insert
        } else {
            node.left = insert(node.left, key, value);
        }

        // 返回 insert (key,value) 后 二叉搜索树的根节点
        return node;
    }

查找


    // 查看二叉搜索树中是否包含 key
    public boolean contain(Key key) {
        return contain(root, key);
    }

    // 查看以 node 为根的二叉搜索树中是否包含键值为 key 的节点,使用递归算法
    private boolean contain(Node node, Key key) {
        // 递归到底
        if (node == null) {
            return false;
        }

        if (key.compareTo(node.key) == 0) {
            return true;
        } else if (key.compareTo(node.key) > 0) {
            return contain(node.right, key);
        } else { // key < node->key
            return contain(node.left, key);
        }
    }
    // 在二叉搜索树中搜索键key所对应的值。如果这个 key 不存在, 则返回null
    public Value search(Key key) {
        return search(root, key);
    }

    // 在以 node 为根的二叉搜索树中搜索 key 所对应的 value,递归算法
    private Value search(Node node, Key key) {
        // 递归到底,二叉搜索树中不存在键值 key ,返回 null
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) == 0) {
            return node.value;
        } else if (key.compareTo(node.key) > 0) {
            return search(node.right, key);
        } else { // key < node.key
            return search(node.left, key);
        }
    }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值