|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.BinarySearchTree
Implements the Dictionary
interface as a binary search tree
from Chapter 12 of Introduction to Algorithms, Second
edition. Objects inserted into a binary search tree must implement
the Comparable
interface.
When extending this class, you must instantiate a new
nil
of the proper type and override constructors along
with the other methods. See RedBlackTree
for an example.
Nested Class Summary | |
protected class |
BinarySearchTree.Node
Inner class for a node of a binary search tree. |
static interface |
BinarySearchTree.Visitor
Interface for when we visit a node during a walk. |
Field Summary | |
protected BinarySearchTree.Node |
nil
Sentinel, replaces NIL in the textbook's code. |
protected BinarySearchTree.Node |
root
Root of the binary search tree. |
Constructor Summary | |
BinarySearchTree()
Creates a binary search tree with just a nil ,
which is the root. |
Method Summary | |
void |
delete(java.lang.Object node)
Removes a node from the tree. |
static java.lang.Comparable |
dereference(java.lang.Object node)
Returns the data stored in a node. |
protected void |
inorderWalk(BinarySearchTree.Node x,
BinarySearchTree.Visitor visitor)
Performs an inorder walk of the the subtree rooted at a node, applying a Visitor to each node in the subtree. |
void |
inorderWalk(BinarySearchTree.Visitor visitor)
Traverses the tree in inorder applying a Visitor
to each node. |
java.lang.Object |
insert(java.lang.Comparable data)
Inserts data into the tree, creating a new node for this data. |
boolean |
isNil(java.lang.Object node)
Returns true if the given node is the sentinel
nil , false otherwise. |
java.lang.Object |
iterativeSearch(java.lang.Comparable k)
Searches the tree for a node with a given key. |
java.lang.Object |
maximum()
Returns the node with the maximum key in the tree. |
java.lang.Object |
minimum()
Returns the node with the minimum key in the tree. |
protected void |
postorderWalk(BinarySearchTree.Node x,
BinarySearchTree.Visitor visitor)
Performs a postorder walk of the the subtree rooted at a node, applying a Visitor to each node in the subtree. |
void |
postorderWalk(BinarySearchTree.Visitor visitor)
Traverses the tree in postorder applying a Visitor
to each node. |
java.lang.Object |
predecessor(java.lang.Object node)
Returns the predecessor of a given node in an inorder walk of the tree. |
protected void |
preorderWalk(BinarySearchTree.Node x,
BinarySearchTree.Visitor visitor)
Performs a preorder walk of the the subtree rooted at a node, applying a Visitor to each node in the subtree. |
void |
preorderWalk(BinarySearchTree.Visitor visitor)
Traverses the tree in preorder applying a Visitor
to each node. |
protected java.lang.Object |
search(BinarySearchTree.Node x,
java.lang.Comparable k)
Searches the subtree rooted at a given node for a node with a given key. |
java.lang.Object |
search(java.lang.Comparable k)
Searches the tree for a node with a given key. |
protected void |
setNil(BinarySearchTree.Node node)
Sets the sentinel nil to a given node. |
java.lang.Object |
successor(java.lang.Object node)
Returns the successor of a given node in an inorder walk of the tree. |
java.lang.String |
toString()
Returns a multiline String representation of the
tree, representing the depth of each node by two spaces per
depth preceding the String representation of the
node. |
protected void |
treeInsert(BinarySearchTree.Node z)
Inserts a node into the tree. |
protected java.lang.Object |
treeMaximum(BinarySearchTree.Node x)
Returns the node with the maximum key in the subtree rooted at a node. |
protected java.lang.Object |
treeMinimum(BinarySearchTree.Node x)
Returns the node with the minimum key in the subtree rooted at a node. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected BinarySearchTree.Node root
protected BinarySearchTree.Node nil
Constructor Detail |
public BinarySearchTree()
nil
,
which is the root.
Method Detail |
protected void setNil(BinarySearchTree.Node node)
nil
to a given node.
node
- The node that nil
is set to.public boolean isNil(java.lang.Object node)
true
if the given node is the sentinel
nil
, false
otherwise.
node
- The node that is being asked about.public void inorderWalk(BinarySearchTree.Visitor visitor)
Visitor
to each node.
visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the tree.protected void inorderWalk(BinarySearchTree.Node x, BinarySearchTree.Visitor visitor)
Visitor
to each node in the subtree.
x
- Root of the subtree.visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the
subtree.public void preorderWalk(BinarySearchTree.Visitor visitor)
Visitor
to each node.
visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the tree.protected void preorderWalk(BinarySearchTree.Node x, BinarySearchTree.Visitor visitor)
Visitor
to each node in the subtree.
x
- Root of the subtree.visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the
subtree.public void postorderWalk(BinarySearchTree.Visitor visitor)
Visitor
to each node.
visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the tree.protected void postorderWalk(BinarySearchTree.Node x, BinarySearchTree.Visitor visitor)
Visitor
to each node in the subtree.
x
- Root of the subtree.visitor
- Object implementing Visitor
whose
visit
method is applied to each node in the
subtree.public java.lang.String toString()
String
representation of the
tree, representing the depth of each node by two spaces per
depth preceding the String
representation of the
node.
toString
in class java.lang.Object
public java.lang.Object search(java.lang.Comparable k)
search
in interface Dictionary
k
- The key being searched for.
Node
object with key
k
if such a node exists, or a reference to the
sentinel nil
if no node has key k
.
The Node
class is opaque to methods outside this
class.protected java.lang.Object search(BinarySearchTree.Node x, java.lang.Comparable k)
x
- Root of the subtree.k
- The key being searched for.
Node
object with key
k
if such a node exists, or a reference to the
sentinel nil
if no node has key k
.
The Node
class is opaque to methods outside this
class.public java.lang.Object iterativeSearch(java.lang.Comparable k)
k
- The key being searched for.
Node
object with key
k
if such a node exists, or a reference to the
sentinel nil
if no node has key k
.
The Node
class is opaque to methods outside this
class.public java.lang.Object minimum()
Node
object with the minimum key in the
tree, or the sentinel nil
if the tree is empty.protected java.lang.Object treeMinimum(BinarySearchTree.Node x)
x
- Root of the subtree.
Node
object with the minimum key in the
tree, or the sentinel nil
if the tree is empty.public java.lang.Object maximum()
Node
object with the maximum key in the
tree, or the sentinel nil
if the tree is empty.protected java.lang.Object treeMaximum(BinarySearchTree.Node x)
x
- Root of the subtree.
Node
object with the maximum key in the
tree, or the sentinel nil
if the tree is empty.public java.lang.Object successor(java.lang.Object node)
node
- The node whose successor is returned.
node
has a successor, it is returned.
Otherwise, return the sentinel nil
.public java.lang.Object predecessor(java.lang.Object node)
node
- The node whose predecessor is returned.
node
has a predecessor, it is returned.
Otherwise, return the sentinel nil
.public java.lang.Object insert(java.lang.Comparable data)
insert
in interface Dictionary
data
- Data to be inserted into the tree.
Node
object created.
The Node
class is opaque to methods outside this
class.protected void treeInsert(BinarySearchTree.Node z)
z
- The node to insert.public void delete(java.lang.Object node)
delete
in interface Dictionary
node
- The node to be removed.
DeleteSentinelException
- if there is an attempt to
delete the sentinel nil
.
java.lang.ClassCastException
- if node
does not
reference a Node
object.public static java.lang.Comparable dereference(java.lang.Object node)
node
- The node whose data is returned.
java.lang.ClassCastException
- if node
does not
reference a Node
object.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |