This algorithm will insert/delete N elements in O(N log(N)) time using O(N) space.
| Modifier and Type | Field and Description |
|---|---|
| private NodeAllocator | allocator
Where to allocate nodes from. |
| private DataValueDescriptor[] | deletedKey
Set, as a side effect of deleteLeftMost (only), to the key from the node that was deleted from the tree. |
| private Node | head
Special head node for the tree. |
| private int | height
The current height of the tree. |
| public static final int | INSERT_DUPLICATE
Returned from insert when the row which was inserted was a duplicate. |
| public static final int | INSERT_FULL
Returned from insert when the row was not able to be inserted because the set was full. |
| public static final int | INSERT_OK
Returned from insert when the row was inserted without incident. |
| private int | lastAux
Read by the getLastAux() method. |
| private int | nextAux
Set by the setNextAux() method. |
| private MergeSort | sort
The sort this set is associated with. |
| private boolean | subtreeShrunk
Set, as a side effect of deleteLeftMost and rotateRight, if the subtree they're working on decreased in height. |
| Access | Constructor and Description |
|---|---|
| pack-priv | SortBuffer(MergeSort sort)
Construct doesn't do anything, callers must call init and check its return code. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | |
| public void | |
| private String | |
| pack-priv void | |
| private void | |
| private Node | deleteLeftmost(Node thisNode)
Delete the node with the lowest key from the subtree defined by 'thisNode', maintaining balance in the subtree. |
| private int | |
| pack-priv int | |
| pack-priv void | |
| pack-priv boolean | |
| pack-priv int | |
| public void | |
| private void | |
| pack-priv DataValueDescriptor[] | |
| pack-priv void | |
| private Node | rotateRight(Node thisNode)
Perform either a single or double rotation, as appropriate, to get the subtree 'thisNode' back in balance, assuming that the right subtree of 'thisNode' is higher than the left subtree. |
| pack-priv void | setNextAux(int aux)
Arrange that the next node allocated in the tree have it's aux field set to the argument. |
| allocator | back to summary |
|---|---|
| private NodeAllocator allocator Where to allocate nodes from. | |
| deletedKey | back to summary |
|---|---|
| private DataValueDescriptor[] deletedKey Set, as a side effect of deleteLeftMost (only), to the key from the node that was deleted from the tree. This field is only valid after a call to deleteLeftMost. | |
| head | back to summary |
|---|---|
| private Node head Special head node for the tree. Head.rightLink is the root of the tree. | |
| height | back to summary |
|---|---|
| private int height The current height of the tree. | |
| INSERT_DUPLICATE | back to summary |
|---|---|
| public static final int INSERT_DUPLICATE Returned from insert when the row which was inserted was a duplicate. The set will have aggregated it in. | |
| INSERT_FULL | back to summary |
|---|---|
| public static final int INSERT_FULL Returned from insert when the row was not able to be inserted because the set was full. | |
| INSERT_OK | back to summary |
|---|---|
| public static final int INSERT_OK Returned from insert when the row was inserted without incident. | |
| lastAux | back to summary |
|---|---|
| private int lastAux Read by the getLastAux() method. This value is read out of the last node that was deallocated from the tree. | |
| nextAux | back to summary |
|---|---|
| private int nextAux Set by the setNextAux() method. This value is stuffed into the aux field of the next node that's allocated. | |
| sort | back to summary |
|---|---|
| private MergeSort sort The sort this set is associated with. | |
| subtreeShrunk | back to summary |
|---|---|
| private boolean subtreeShrunk Set, as a side effect of deleteLeftMost and rotateRight, if the subtree they're working on decreased in height. This field is only valid after a call to deleteLeftMost or rotateRight. | |
| SortBuffer | back to summary |
|---|---|
| pack-priv SortBuffer(MergeSort sort) Construct doesn't do anything, callers must call init and check its return code. | |
| capacity | back to summary |
|---|---|
| pack-priv int capacity() Return the number of elements this sorter can sort. It's the capacity of the node allocator minus one because the sorter uses one node for the head. | |
| check | back to summary |
|---|---|
| public void check() | |
| checkNode | back to summary |
|---|---|
| private String checkNode(Node n) | |
| close | back to summary |
|---|---|
| pack-priv void close() | |
| debug | back to summary |
|---|---|
| private void debug(String s) | |
| deleteLeftmost | back to summary |
|---|---|
| private Node deleteLeftmost(Node thisNode) Delete the node with the lowest key from the subtree defined by 'thisNode', maintaining balance in the subtree. Returns the node that should be the new root of the subtree. This method sets this.subtreeShrunk if the subtree of thisNode decreased in height. Saves the key that was in the deleted node in 'deletedKey'. | |
| depth | back to summary |
|---|---|
| private int depth(Node n) | |
| getLastAux | back to summary |
|---|---|
| pack-priv int getLastAux() Retrieve the aux value from the last node deallocated from the tree. | |
| grow | back to summary |
|---|---|
| pack-priv void grow(int percent) Grow by a certain percent if we can | |
| init | back to summary |
|---|---|
| pack-priv boolean init() Initialize. Returns false if the allocator couldn't be initialized. | |
| insert | back to summary |
|---|---|
| pack-priv int insert(DataValueDescriptor[] k) throws StandardException Insert a key k into the tree. Returns true if the key was inserted, false if the tree is full. Silently ignores duplicate keys. See Knuth Vol. 3, Sec. 6.2.3, pp. 455-457 for the algorithm. | |
| back to summary | |
|---|---|
| public void print() | |
| printRecursive | back to summary |
|---|---|
| private void printRecursive(Node n, int depth) | |
| removeFirst | back to summary |
|---|---|
| pack-priv DataValueDescriptor[] removeFirst() Return the lowest key and delete it from the tree, preserving the balance of the tree. | |
| reset | back to summary |
|---|---|
| pack-priv void reset() | |
| rotateRight | back to summary |
|---|---|
| private Node rotateRight(Node thisNode) Perform either a single or double rotation, as appropriate, to get the subtree 'thisNode' back in balance, assuming that the right subtree of 'thisNode' is higher than the left subtree. Returns the node that should be the new root of the subtree. These are the cases depicted in diagrams (1) and (2) of Knuth (p. 454), and the node names reflect these diagrams. However, in deletion, the single rotation may encounter a case where the "beta" and "gamma" subtrees are the same height (b.balance == 0), so the resulting does not always shrink. Note This code will not do the "mirror image" cases. It only works when the right subtree is the higher one (this is the only case encountered when deleting leftmost nodes from the tree). | |
| setNextAux | back to summary |
|---|---|
| pack-priv void setNextAux(int aux) Arrange that the next node allocated in the tree have it's aux field set to the argument. | |