BinarySearchTreeMap的实现

  

 1 public interface Map<K extends Comparable<K>, V> {
2 void put(K k, V v);
3
4 V get(K k);
5
6 void delete(K k);
7
8 boolean contains(K k);
9
10 boolean isEmpty();
11
12 int size();
13
14 int size(K lo, K hi);
15
16 K min();
17
18 K max();
19
20 K floor(K k);
21
22 K ceiling(K k);
23
24 // the number of keys less than key
25 int rank(K k);
26
27 K select(int k);
28
29 void deleteMin();
30
31 void deleteMax();
32
33 // keys in [lo , hi] in sorted order
34 Iterable<K> keys(K lo, K hi);
35
36 Iterable<K> keys();
37 }

Map Interface


  二叉树的定义

  在计算机科学中,二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”(left subtree)和“右子树”(right subtree)。二叉树常被用于实现二叉查找树和二叉堆。二叉树的左子节点 <  父节点 < 右子节点

  这是typical的二叉树的样子, null 代表子节点为空,从这张图可以看出,左子节点 9 小于 父节点 10 小于 右子节点 

  

1     private class Node<K, V> {
2 private K k;
3 private V v;
4 private Node<K, V> left;
5 private Node<K, V> right;
6 private int size;
7 Node(K k, V v) { this.k = k; this.v = v; }
8 Node(K k, V v, int size) { this.k = k; this.v = v; this.size = size;}
9 }

Node(节点)对象

        


   二叉树的插入操作

   假设我们依次插入 10 , 9, 15, 5 , 7 这5个元素到二叉树中。see what will happen 这是个动态图

 1     @Override
2 public void put(K k, V v) {
3 root = put(root, k, v); //root 是根节点
4 }
5
6 private Node<K, V> put(Node<K, V> node, K k, V v) {
7 if (node == null) return new Node<>(k, v, 1);
8 int cmp = node.k.compareTo(k);
9 if (cmp > 0) { //node的k大一点 放到左边的数中
10 node.left = put(node.left, k, v);
11 } else if (cmp < 0) { //node的k小一点 放到右边的数中
12 node.right = put(node.right, k, v);
13 } else node.v = v;
14
15 node.size = size(node.left) + size(node.right) + 1;
16 return node;
17 }

put operation (插入)


  二叉树的get 方法

  get方法简单来说就是要找到那个key相同的对象。比如我们要在「10 , 9, 15, 5 , 7 」上图所示中找到 7

  

  

 1     @Override
2 public V get(K k) {
3 return get(root, k);
4 }
5
6 private V get(Node<K, V> node, K k) {
7
8 if (node == null) return null; //not find
9 else if (node.k.compareTo(k) > 0) { //node的k大一点 放到左边的数中
10 return get(node.left, k);
11 } else if (node.k.compareTo(k) < 0) { //node的k小一点 放到右边的数中
12 return get(node.right, k);
13 } else { //equal
14 return node.v;
15 }
16
17 }

get operation

  


  二叉树的删除操作

  其实想象一下,当你删除一个node的时候,你需要找一个替代node来代替这个node。

  这里又分3种情况。首先假设你有如下的树结构

  

  1.第一种情况是这个删除的节点的左右节点都是null。

    比如我要删除3节点。其实只要直接把3节点reset 为null 就可以了。变成如下

  

   2.第二种情况是删除的节点的2个子节点中有一个子节点为null

     比如我要删除15。 15 的左节点是12 右节点是 null,所以符合这个情况

       这个时候只需要直接把需要删除的节点 reset 为 非空的子节点就可以了

所以在这里只需要把15的值替代为12

  

  3.第三种情况是删除的节点的2个子节点都不为null,

    这个时候其实可以有2个选择,一个是把删除的节点替换为右子节点为根节点的那个树中最小的节点

    比如我要删除10, 右节点为15(二叉树的删除操作的那个图,不是上面的那个图),15这个节点为根节点的树中总共有2个元素(15和12),12是最小的。所以把需要删除的节点替换为12。删除后如下

  

    另外一种选择是把左节点为根节点的树中最大的值取出来,把需要删除的那个节点替换为这个左节点最大的元素(2个选择没什么区别)

    

 1     @Override
2 public void delete(K k) {
3 delete(root, k);
4 }
5 //delete the k in the node tree and reset the size prorperty of this tree and subtrees to correct value
6 private Node<K, V> delete(Node<K, V> node, K k) {
7 if (node == null) return null; //没有找到这个node
8
9 int cmp = node.k.compareTo(k);
10 if (cmp > 0) {
11 node.left = delete(node.left, k);
12 node.size = size(node.left) + size(node.right) + 1;
13 return node;
14 } else if (cmp < 0) {
15 node.right = delete(node.right, k);
16 node.size = size(node.left) + size(node.right) + 1;
17 return node;
18 } else { //hit the key
19 if (node.right == null) //if the right node is null then just replace this node with left node
20 return node.left;
21 else if (node.left == null) // if the left node is null then just replace this node with right node
22 return node.right;
23 else {
24 return deleteMin(node.right); // if both the subnodes are not null replace this node with the smallest node in the right sub node
25 }
26 }
27 }
28
29 //删除从参数node开始的最小的node
30 private Node<K, V> deleteMin(Node<K, V> node) {
31 return delete(node, min(node));
32 }
33
34 private Node<K, V> deleteMax(Node<K, V> node) {
35 return delete(node, max(node));
36 }
37
38 @Override
39 public void deleteMin() {
40 deleteMin(root);
41 }
42
43 @Override
44 public void deleteMax() {
45 deleteMax(root);
46 }
47
48 @Override
49 public K min() {
50 return min(root);
51 }
52
53 //get the smallest node in the given node
54 private K min(Node<K, V> node) {
55 if (node == null) return null;
56 for (; node.left != null; node = node.left);
57 return node.k;
58 }
59
60 @Override
61 public K max() {
62 return max(root);
63 }
64 //get the most max node in the given node
65 private K max(Node<K, V> node) {
66 if (node == null) return null;
67 for (node = root; node.right != null; node = node.right);
68 return node.k;
69 }

delete operation 删除操作


   分析

    BinarySearchTree 有一个最大的缺点,就是如果插入的元素是ordered,比如我插入 1 2 3 4 5  6 这样子,元素都会排在一边。这样子查找起来路径很长,效率很低。

    如果插入的元素是随机的,那么所有的get put 操作的时间复杂度应该是 和 log2(N) 成正比的

    具体的实现可以参考这个。https://github.com/Cheemion/algorithms/blob/master/src/com/algorithms/tree/BinarySearchTreeMap.java

    有什么错误的地方欢迎大家指正哈

    

Tree--二叉树BinarySearchTree的更多相关文章

  1. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  2. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  3. [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

    4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...

  4. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  5. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  6. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  8. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. 如何使用捷映App制作朋友圈九宫格视频图片合集

    在刷朋友圈的过程中,看到九宫格的视频图片合集,带有酷炫的音效和动效,展现效果非常好,那么我们该如何制作呢?下面我为大家介绍. 第一步,我们需要在各大应用市场搜索[捷映视频制作]App,点击[安装]. ...

  2. Docker(7)- docker images 命令详解

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 作用 列出所有的本地镜像 语法格 ...

  3. python3爬虫应用--爬取网易云音乐(两种办法)

    一.需求 好久没有碰爬虫了,竟不知道从何入手.偶然看到一篇知乎的评论(https://www.zhihu.com/question/20799742/answer/99491808),一时兴起就也照葫 ...

  4. ubuntu下安装国际版QQ

    在网上看到了好多的ubuntu下安装QQ的方法 好多 下面是看别人的文章 来测试的一篇 ubuntu下 安装国际版QQ http://www.ubuntukylin.com/applications/ ...

  5. 面试题:对NotNull字段插入Null值 有啥现象?

    Hi,大家好!我是白日梦. 今天我要跟你分享的话题是:"对NotNull字段插入Null值有啥现象?" 一. 推荐阅读 首发地址:https://mp.weixin.qq.com/ ...

  6. 关于Wrapper Class

    public class RunTest{ public static void main(String[] args) { Integer ten=new Integer(10); Long nin ...

  7. 线程范围内的环境变量---ThreadLocal

    package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.Random; p ...

  8. fio的配置使用

    将fio-2.1.10.tar.gz拷贝到linux服务器的/usr/src/下 解压源码包: root@grandocean:/usr/src# tar xvf fio-2.1.10.tar.gz ...

  9. Python_爬虫_urllib解析库

    简介:提取网页保存到txt文件中 + 解析txt文件内容,取出内容 from urllib import request import re.json url="http://www.163 ...

  10. kettle连接报错

    如何查看端口号 cmd命令行 输入mysql -u root -p密码进入mysql 输入show global variables like 'port'; 就可以看到端口号3306 测试数据库链接 ...