public class BST<K extends Comparable<K>,V> extends Object
在由N个随机键构造的二叉查找树中,插入和查找平均所需的比较次数为~2lnN(约1.39lgN)
二叉查找树得以广泛应用的一个重要原因就是它能够保持键的有序性,因此它可以作为实现有序符号表API中的众多方法的基础。 这使得符号表的用例不仅能够通过键还能通过键的相对顺序来访问键值对
在一棵二叉查找树中,所有操作在最坏情况下所需的时间都和树的高度成正比
8+[52+K(类型字节)+V(类型字节)+对齐填充]*n(n为元素个数)
B=24+48*n+(4+K+V+对齐填充)*n
| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
BST.Node<K extends Comparable<K>,V>
16+8+K(类型字节)+8+V(类型字节)+4+8*2+对齐填充
B=52+K(类型字节)+V(类型字节)+对齐填充
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected BST.Node<K,V> |
ceil(BST.Node<K,V> x,
K key) |
K |
ceil(K key) |
protected BST.Node<K,V> |
delete(BST.Node<K,V> x,
K key)
一般情况下这段代码的效率不错,但对于大规模的应用来说可能会有一点问题
|
void |
delete(K key) |
void |
deleteMax() |
protected BST.Node<K,V> |
deleteMax(BST.Node<K,V> x) |
void |
deleteMin() |
protected BST.Node<K,V> |
deleteMin(BST.Node<K,V> x) |
int |
depth()
树的高度
|
protected int |
depth(BST.Node<K,V> x) |
protected BST.Node<K,V> |
floor(BST.Node<K,V> x,
K key) |
K |
floor(K key) |
protected BST.Node<K,V> |
get(BST.Node<K,V> x,
K key) |
V |
get(K key) |
boolean |
isEmpty() |
boolean |
isLeaf() |
Iterable<K> |
keys()
查找所有键(中序遍历)
|
protected void |
keys(BST.Node<K,V> x,
LLinkedQueue<K> queue) |
protected void |
keys(BST.Node<K,V> x,
LLinkedQueue<K> queue,
K lo,
K hi) |
Iterable<K> |
keys(K lo,
K hi)
查找所有在区间[lo,hi]的键
|
protected void |
keysGe(BST.Node<K,V> x,
LLinkedQueue<K> queue,
K lo) |
Iterable<K> |
keysGe(K lo)
查找所有不小于lo的键
|
protected void |
keysLe(BST.Node<K,V> x,
LLinkedQueue<K> queue,
K hi) |
Iterable<K> |
keysLe(K hi)
查找所有不大于hi的键
|
K |
max() |
protected BST.Node<K,V> |
max(BST.Node<K,V> x) |
K |
min() |
protected BST.Node<K,V> |
min(BST.Node<K,V> x) |
static <K extends Comparable<K>,V> |
of(K key) |
static <K extends Comparable<K>,V> |
of(K key,
V val) |
protected BST.Node<K,V> |
put(BST.Node<K,V> x,
K key,
V val) |
void |
put(K key,
V val) |
protected BST.Node<K,V> |
select(BST.Node<K,V> x,
int k) |
K |
select(int k)
返回排名为k的节点key
|
int |
size() |
protected int |
size(BST.Node<K,V> x) |
protected int |
size(BST.Node<K,V> x,
K lo,
K hi) |
int |
size(K lo,
K hi)
返回在区间[lo,hi]的节点数量
|
protected int |
sizeGe(BST.Node<K,V> x,
K lo) |
int |
sizeGe(K lo)
返回不小于key的节点数量
|
protected int |
sizeLe(BST.Node<K,V> x,
K hi) |
int |
sizeLe(K hi)
返回不大于key的节点数量
|
protected BST.Node<K extends Comparable<K>,V> root
public static <K extends Comparable<K>,V> BST<K,V> of(K key, V val)
public static <K extends Comparable<K>,V> BST<K,V> of(K key)
public final int size()
public final int sizeLe(K hi)
public final int sizeGe(K lo)
public final boolean isEmpty()
public final boolean isLeaf()
public final K min()
public final K max()
public final int depth()
public final K select(int k)
public final void deleteMin()
public final void deleteMax()
public final void delete(K key)
protected final BST.Node<K,V> delete(BST.Node<K,V> x, K key)
protected final void keys(BST.Node<K,V> x, LLinkedQueue<K> queue)
Copyright © 2022. All rights reserved.