T
- Any object which implements the Comparable interface is acceptedpublic class BinarySearchTree<T extends java.lang.Comparable<T>>
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private java.util.ArrayList<T> |
postorderWalkResult |
private java.util.ArrayList<T> |
preorderWalkResult |
private BinarySearchNode<T> |
rootNode |
private java.util.ArrayList<T> |
sortedData |
Constructor and Description |
---|
BinarySearchTree()
constructor for BinarySearchTree class.
|
Modifier and Type | Method and Description |
---|---|
void |
delete(BinarySearchNode<T> nodeToDelete)
deletes the specified node from the tree
|
java.util.ArrayList<T> |
getInorderWalkResult() |
java.util.ArrayList<T> |
getPostorderWalkResult() |
java.util.ArrayList<T> |
getPreorderWalkResult() |
BinarySearchNode<T> |
getRootNode() |
java.util.ArrayList<T> |
getSortedData() |
void |
inorderTreeWalk(BinarySearchNode<T> node,
boolean shouldClear)
by calling this method the sorted data will be stored in the
|
void |
insert(T newData)
to insert a new node with new data into the tree
|
void |
postorderTreeWalk(BinarySearchNode<T> node,
boolean shouldClear)
by calling this method all the data in this tree will be walked through
|
void |
preorderTreeWalk(BinarySearchNode<T> node,
boolean shouldClear)
by calling this method all the data in this tree will be walked through
|
java.lang.String |
toString()
Returns a string representation of the object.
|
void |
transplant(BinarySearchNode<T> firstSubtreeRoot,
BinarySearchNode<T> secondSubtreeRoot)
this method replaces one subtree as a child of its parent
with another subtree.
|
BinarySearchNode<T> |
treeMaximum(BinarySearchNode<T> startingNode) |
BinarySearchNode<T> |
treeMinimum(BinarySearchNode<T> startingNode) |
BinarySearchNode<T> |
treePredecessor(BinarySearchNode<T> node) |
BinarySearchNode<T> |
treeSearch(BinarySearchNode<T> pivotNode,
T searchingValue) |
BinarySearchNode<T> |
treeSuccessor(BinarySearchNode<T> node) |
private BinarySearchNode<T extends java.lang.Comparable<T>> rootNode
private final java.util.ArrayList<T extends java.lang.Comparable<T>> preorderWalkResult
public BinarySearchTree()
public void inorderTreeWalk(BinarySearchNode<T> node, boolean shouldClear)
node
- the node of the subtree to print. if you want it to print the whole tree,
pass the root node to it.shouldClear
- if true, the ArrayList containing the result of inorder walk gets cleared. if false, the result is kept
and concatenated with the result of the previous call.public void preorderTreeWalk(BinarySearchNode<T> node, boolean shouldClear)
node
- the node of the subtree to print. if you want it to print the whole tree,
pass the root node to it.shouldClear
- if true, the ArrayList containing the result of preorder walk gets cleared. if false, the result is kept
and concatenated with the result of the previous call.public void postorderTreeWalk(BinarySearchNode<T> node, boolean shouldClear)
node
- the node of the subtree to print. if you want it to print the whole tree,
pass the root node to it.shouldClear
- if true, the ArrayList containing the result of postorder walk gets cleared. if false, the result is kept
and concatenated with the result of the previous call.public BinarySearchNode<T> getRootNode()
public java.util.ArrayList<T> getSortedData()
public java.util.ArrayList<T> getPreorderWalkResult()
public java.util.ArrayList<T> getPostorderWalkResult()
public java.util.ArrayList<T> getInorderWalkResult()
public void insert(T newData)
newData
- data of type T which is the same type as the type specified when creating BinarySearchTree classpublic BinarySearchNode<T> treeSearch(BinarySearchNode<T> pivotNode, T searchingValue)
pivotNode
- the node that its value is compared to searchingValue parametersearchingValue
- the value to search for in the treepublic BinarySearchNode<T> treeMinimum(BinarySearchNode<T> startingNode)
startingNode
- the node to start finding the minimum from.
if you want the minimum node of the whole tree, pass the root node to this
method;otherwise, it will find the minimum value in the subtree.public BinarySearchNode<T> treeMaximum(BinarySearchNode<T> startingNode)
startingNode
- the node to start finding the maximum from.
if you want the minimum node of the whole tree, pass the root node to this
method;otherwise, it will find the maximum value in the subtree.public BinarySearchNode<T> treeSuccessor(BinarySearchNode<T> node)
node
- the node which we want its successorpublic BinarySearchNode<T> treePredecessor(BinarySearchNode<T> node)
node
- the node which we want its predecessorpublic void transplant(BinarySearchNode<T> firstSubtreeRoot, BinarySearchNode<T> secondSubtreeRoot)
firstSubtreeRoot
- the root of first subtreesecondSubtreeRoot
- the root of second subtreepublic void delete(BinarySearchNode<T> nodeToDelete)
nodeToDelete
- the node that you want to delete from the treepublic java.lang.String toString()
java.lang.Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString
in class java.lang.Object