|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectAVL.AVLTree<Key,Data>
public class AVLTree<Key,Data>
Implementation of a generic AVL Tree. The data stored in the tree
are of the type Data
and the associated keys are of
the type Key
. A
Comparator
is used to compare keys.
Code has been taken from the course textbook:
Cormen, Leiserson, Rivest, Stein:
Introduction to Algorithm (2nd edition).
McGraw-Hill (2001), ISBN: 0070131511
Field Summary | |
---|---|
protected java.util.Comparator<Key> |
c
A comparator of Key objects |
protected Node<Key,Data> |
root
Root of the AVL tree. |
protected int |
size
Number of items in the tree |
Constructor Summary | |
---|---|
AVLTree(java.util.Comparator<Key> comparator)
The constructor. |
Method Summary | |
---|---|
protected void |
delete(Node<Key,Data> node)
Removes a node from the tree. |
void |
deleteKey(Key k)
Removes a node with the given key from the tree. |
void |
insert(Key key,
Data data)
Inserts a key and a data item into the tree, creating a new node for this key and data pair. |
void |
print()
Prints the whole tree |
Data |
search(Key k)
Searches the tree for a node with a given key. |
protected Node<Key,Data> |
search(Node<Key,Data> node,
Key k)
Searches the subtree rooted at a given node for a node with a given key. |
int |
size()
The number of elements in the tree. |
protected Node<Key,Data> |
successor(Node<Key,Data> node)
Returns the successor of a given node in an inorder walk of the tree. |
protected void |
treeInsert(Node<Key,Data> z)
Inserts a node into the tree. |
protected Node<Key,Data> |
treeMinimum(Node<Key,Data> 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, toString, wait, wait, wait |
Field Detail |
---|
protected Node<Key,Data> root
protected int size
protected java.util.Comparator<Key> c
Constructor Detail |
---|
public AVLTree(java.util.Comparator<Key> comparator)
comparator
- A comparator of Key objects.Method Detail |
---|
public int size()
public Data search(Key k)
k
- The key being searched for.
protected Node<Key,Data> search(Node<Key,Data> node, Key k)
node
- Root of the subtree.k
- The key being searched for.
Node
object with key
k
if such a node exists.
An exception NoSuchElementException is thrown if no node exists.protected Node<Key,Data> treeMinimum(Node<Key,Data> x)
x
- Root of the subtree.
Node
object with the minimum key in the
tree, or the sentinel nil
if the tree is empty.protected Node<Key,Data> successor(Node<Key,Data> node)
node
- The node whose successor is returned.
node
has a successor, it is returned.
Otherwise, return the sentinel null
.public void insert(Key key, Data data)
key
- Key to be inserted into the tree.data
- Data to be inserted into the tree.protected void treeInsert(Node<Key,Data> z)
z
- The node to insert.public void deleteKey(Key k)
k
- The key to be removed.protected void delete(Node<Key,Data> node)
node
- The node to be removed.
do nothing when deleting a null node null
.public void print()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |