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. Java_进程与线程

    进Process&Thread 区别 进程 线程 根本区别 作为资源分配的单位 调度和执行的单位 开销 每个进程都有独立的代码和数据空间(进程上下文), 进程间的切换会有较大的开销 线程可以看 ...

  2. 最简单的基于FFmpeg的直播系统开发移动端例子:IOS 视频解码器

    本文记录IOS平台下基于FFmpeg的视频解码器.该示例C语言的源代码来自于<最简单的基于FFMPEG+SDL的视频播放器>.相关的概念就不再重复记录了. 源代码 项目的目录结构如图所示. ...

  3. c#习题之3

    Dictionary<int, char> d = new Dictionary<int, char>(); int i=0; string s = "Welcome ...

  4. JS函数命名规范

    语法规范: 任何合法的javascript标识符都可以作为函数的名称. 约定俗成的内容:(非ECMAScript语法,但是为了便于开发者理解和识别,约定的函数命名规范.) 命名方法: 小驼峰式命名法 ...

  5. Docker - 解决容器内获取的时间和主机的时间不一样的问题

    问题背景 分别在容器和主机下执行 date 命令 可以看到,时间是完全不一样的 解决方案 在运行容器时,挂载 /etc/localtime  目录 docker run -d -v /etc/loca ...

  6. Effective Modern C++ ——条款5 优先选择auto,而非显式型别声明

    条款5 对于auto ,他的好处不仅仅是少打一些字这么简单. 首先在声明的时候, 使用auto会让我们养成初始化的习惯: auto x;//编译不通过必须初始化. 再次对于auto而言,它可以让我们定 ...

  7. KepServer与S7-1200PLC之间的OPC通信配置

    对于学习上位机开发,有一种通信方式是必须要了解的,那就是OPC是OLE for Process Control的简称,然而随着技术的不断发展,人们开始对它有了新的定义,比如Open Platform ...

  8. prometheus监控golang服务实践

    一.prometheus基本原理介绍 prometheus是基于metric采样的监控,可以自定义监控指标,如:服务每秒请求数.请求失败数.请求执行时间等,每经过一个时间间隔,数据都会从运行的服务中流 ...

  9. 异步FIFO学习笔记

  10. 微服务通信之feign的配置隔离

    前言 由上文我们知道针对某一个Feign接口,我们可以给他设置特定的配置类.那如果现在有一个服务,我们只想对A服务配置一个拦截器拦截请求而不影响其他服务,那应该怎么做呢? 一.feign接口配置 由前 ...