public class BSTreeSetTester<K extends java.lang.Comparable<K>> extends java.lang.Object implements SetTesterADT<K>
The BST rebalances if a specified threshold is exceeded (explained below). If rebalanceThreshold is non-positive (<=0) then the BST DOES NOT rebalance. It is a basic BStree. If the rebalanceThreshold is positive then the BST does rebalance. It is a BSTreeB where the last B means the tree is balanced.
Rebalancing is triggered if the absolute value of the balancedFfactor in any BSTNode is > to the rebalanceThreshold in its BSTreeSetTester. Rebalancing requires the BST to be completely rebuilt.
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
isBalanced
True iff tree is balanced, i.e., if rebalanceThreshold is NOT exceeded
by absolute value of balanceFactor in any of the tree's BSTnodes.Note if
rebalanceThreshold is non-positive, isBalanced must be true.
|
(package private) int |
numKeys
Number of items in this tree
|
(package private) int |
rebalanceThreshold
rebalanceThreshold <= 0 DOES NOT REBALANCE (BSTree).
rebalanceThreshold >0 rebalances the tree (BSTreeB). |
(package private) BSTNode<K> |
root
Root of this tree
|
Constructor and Description |
---|
BSTreeSetTester(int rbt)
Constructs an empty BSTreeSetTester with a given rebalanceThreshold.
|
Modifier and Type | Method and Description |
---|---|
void |
add(K key)
Add node to binary search tree.
|
void |
clear()
Clears the tree, i.e., removes all the keys in the tree.
|
boolean |
contains(K key)
Returns true iff the key is in the binary search tree.
|
void |
displayTree(int maxDisplayLevels)
Displays the top maxNumLevels of the tree.
|
private void |
displayTreeHelper(BSTNode<K> n,
int curDepth,
int maxDisplayLevels) |
java.util.Iterator<K> |
iterator()
Return an iterator for the binary search tree.
|
void |
rebalance()
Rebalances the tree by:
1.
|
int |
size()
Returns the number of keys in the tree.
|
private BSTNode<K> |
sortedArrayToBST(K[] keys,
int start,
int stop)
Recursively rebuilds a binary search tree from a sorted array.
|
java.util.List<K> |
subSet(K minValue,
K maxValue)
Returns the sorted list of keys in the tree that are in the specified
range (inclusive of minValue, exclusive of maxValue).
|
int numKeys
int rebalanceThreshold
boolean isBalanced
public BSTreeSetTester(int rbt)
rbt
- the rebalance thresholdpublic void add(K key)
add
in interface SetTesterADT<K extends java.lang.Comparable<K>>
key
- the key to add into the BSTjava.lang.IllegalArgumentException
- if the key is nullDuplicateKeyException
- if the key is a duplicatepublic void rebalance()
private BSTNode<K> sortedArrayToBST(K[] keys, int start, int stop)
keys
- the sorted array of keysstart
- the first index of the part of the array usedstop
- the last index of the part of the array usedpublic boolean contains(K key)
contains
in interface SetTesterADT<K extends java.lang.Comparable<K>>
key
- the key to searchjava.lang.IllegalArgumentException
- if key is nullpublic java.util.List<K> subSet(K minValue, K maxValue)
subSet
in interface SetTesterADT<K extends java.lang.Comparable<K>>
minValue
- the minimum value of the desired range (inclusive)maxValue
- the maximum value of the desired range (exclusive)java.lang.IllegalArgumentException
- if either minValue or maxValue is
null, or minValue is larger than maxValuepublic java.util.Iterator<K> iterator()
public void clear()
clear
in interface SetTesterADT<K extends java.lang.Comparable<K>>
public int size()
size
in interface SetTesterADT<K extends java.lang.Comparable<K>>
public void displayTree(int maxDisplayLevels)
displayTree
in interface SetTesterADT<K extends java.lang.Comparable<K>>
maxDisplayLevels
- from the top of the BST that will be displayed