Package net.cscott.jutil
Class BinaryTree<K>
java.lang.Object
net.cscott.jutil.BinaryTree<K>
- Type Parameters:
K- the type of the elements in this tree
- Direct Known Subclasses:
RedBlackTree
A
BinaryTree is a Tree where each node has
at most two children.
All elements of a given BinaryTree t must
be mutually comparable, either inherently or through an external
Comparator.
Unlike a TreeSet, duplicate elements
are allowed in a BinaryTree.
FSK: We should probably have a Tree interface by now... Sometimes you want to expose the fact that you're working with a Tree, instead of abstract it into a Set or what-have-you. Have to think about adding that.
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Comparator<? super K> protected final BinaryTree<K>.Node -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty tree which accepts only mutually comparable elements.BinaryTree(Comparator<? super K> c) Creates an empty tree which usescto determine element ordering. -
Method Summary
Modifier and TypeMethodDescriptionConstructs a node forkand inserts it into this.booleanReturns true ifkis present inthis.protected voiddeleteNode(BinaryTree<K>.Node z) Spliceszout from this.dump()dump(BinaryTree<K>.Node x) protected voidinsertNode(BinaryTree<K>.Node z) Inserts z into some appropriate position inthis.protected BinaryTree<K>.NodeCreates aBinaryTree<K>.Noden for this such that n.key == k.maximum()Returns the maximum element ofthis.protected BinaryTree<K>.Nodemaximum(BinaryTree<K>.Node x) Finds the maximum Node n (in the subtree rooted at x).minimum()Returns the minimum element ofthis.protected BinaryTree<K>.Nodeminimum(BinaryTree<K>.Node x) Finds the minimum Node n (in the subtree rooted at x).voidprotected BinaryTree<K>.Noderoot()protected BinaryTree<K>.Nodesearch(BinaryTree<K>.Node x, Object k) Finds the Node n (in the subtree rooted at x) such that n.key = k.protected BinaryTree<K>.NodesetLeft(BinaryTree<K>.Node p, BinaryTree<K>.Node l) Sets the left child of p.protected BinaryTree<K>.NodesetParent(BinaryTree<K>.Node c, BinaryTree<K>.Node p) Sets the parent of p.protected BinaryTree<K>.NodesetRight(BinaryTree<K>.Node p, BinaryTree<K>.Node r) Sets the right child of p.protected BinaryTree<K>.NodesetRoot(BinaryTree<K>.Node r) protected BinaryTree<K>.Nodesuccessor(BinaryTree<K>.Node x) Returns the successor of x in the sorted order determined by an inorder tree walk.protected voidswapPositions(BinaryTree<K>.Node a, BinaryTree<K>.Node b) Switches the positions ofaandbwithinthis.
-
Field Details
-
NIL
-
comp
-
-
Constructor Details
-
BinaryTree
public BinaryTree()Creates an empty tree which accepts only mutually comparable elements.- See Also:
-
BinaryTree
Creates an empty tree which usescto determine element ordering.
-
-
Method Details
-
makeNode
Creates aBinaryTree<K>.Noden for this such that n.key == k. Factory method forBinaryTree<K>.Node. Every construction of aBinaryTree<K>.Nodetakes place through this method; thus subclasses ofRedBlackTreecan associate new data with their own nodes by extending theBinaryTree<K>.Nodeclass and overriding this method.Note that makeNode must deal with the case when key is
null, regardless of whether the tree itself allows null elements, because the NIL sentinel node hasnullas its key. -
setLeft
Sets the left child of p. Modifies: this, p, l Effects: p.left_post = l. Returns: p.left_pre. -
setRight
Sets the right child of p. Modifies: this, p, r Effects: p.right_post = r. Returns: p.right_pre. -
setParent
Sets the parent of p. Modifies: this, c, p Effects: c.parent_post = p. Returns: c.parent_pre. -
swapPositions
Switches the positions ofaandbwithinthis. Both must be in the tree; one may be the other's parent. Subclasses are expected to swap any data derived from the positions as well. -
root
-
setRoot
-
add
Constructs a node forkand inserts it into this. Uses nodes' own methods to dispatch -
remove
-
contains
Returns true ifkis present inthis. -
minimum
Returns the minimum element ofthis. Requires: this is non-empty. -
maximum
Returns the maximum element ofthis. Requires: this is non-empty. -
search
Finds the Node n (in the subtree rooted at x) such that n.key = k. From CLR, pg 248. -
minimum
Finds the minimum Node n (in the subtree rooted at x). From CLR, pg 248. -
maximum
Finds the maximum Node n (in the subtree rooted at x). From CLR, pg 248. -
successor
Returns the successor of x in the sorted order determined by an inorder tree walk. From CLR, pg 249. -
insertNode
Inserts z into some appropriate position inthis. requires: (z.left == z.right == NIL) modifies: this, z From CLR, pg 251. -
deleteNode
Spliceszout from this. Based on CLR, pg 253. modifies: this, z -
dump
-
dump
-