Package edu.princeton.cs.algs4
Class BTree<Key extends Comparable<Key>,Value>
- Object
-
- edu.princeton.cs.algs4.BTree<Key,Value>
-
public class BTree<Key extends Comparable<Key>,Value> extends Object
TheBTree
class represents an ordered symbol table of generic key-value pairs. It supports the put, get, contains, size, and is-empty methods. A symbol table implements the associative array abstraction: when associating a value with a key that is already in the symbol table, the convention is to replace the old value with the new value. UnlikeMap
, this class uses the convention that values cannot benull
—setting the value associated with a key tonull
is equivalent to deleting the key from the symbol table.This implementation uses a B-tree. It requires that the key type implements the
Comparable
interface and calls thecompareTo()
and method to compare two keys. It does not call eitherequals()
orhashCode()
. The get, put, and contains operations each make logm(n) probes in the worst case, where n is the number of key-value pairs and m is the branching factor. The size, and is-empty operations take constant time. Construction takes constant time.For additional documentation, see Section 6.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Constructor Summary
Constructors Constructor Description BTree()
Initializes an empty B-tree.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Value
get(Key key)
Returns the value associated with the given key.int
height()
Returns the height of this B-tree (for debugging).boolean
isEmpty()
Returns true if this symbol table is empty.static void
main(String[] args)
Unit tests theBTree
data type.void
put(Key key, Value val)
Inserts the key-value pair into the symbol table, overwriting the old value with the new value if the key is already in the symbol table.int
size()
Returns the number of key-value pairs in this symbol table.String
toString()
Returns a string representation of this B-tree (for debugging).
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returns true if this symbol table is empty.- Returns:
true
if this symbol table is empty;false
otherwise
-
size
public int size()
Returns the number of key-value pairs in this symbol table.- Returns:
- the number of key-value pairs in this symbol table
-
height
public int height()
Returns the height of this B-tree (for debugging).- Returns:
- the height of this B-tree
-
get
public Value get(Key key)
Returns the value associated with the given key.- Parameters:
key
- the key- Returns:
- the value associated with the given key if the key is in the symbol table
and
null
if the key is not in the symbol table - Throws:
IllegalArgumentException
- ifkey
isnull
-
put
public void put(Key key, Value val)
Inserts the key-value pair into the symbol table, overwriting the old value with the new value if the key is already in the symbol table. If the value isnull
, this effectively deletes the key from the symbol table.- Parameters:
key
- the keyval
- the value- Throws:
IllegalArgumentException
- ifkey
isnull
-
toString
public String toString()
Returns a string representation of this B-tree (for debugging).
-
main
public static void main(String[] args)
Unit tests theBTree
data type.- Parameters:
args
- the command-line arguments
-
-