Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Database

The SquareList Data Structure


Deleting

Deleting is similar to inserting, although differences do exist. When a node is deleted, the depth of the vertical list is checked. If the depth of the list equals maxSize-1, then no modifications need to be made to the list. If the depth of the list equals maxSize-2 (or some constant c), then a node is shifted into the list to increase its depth; see Listing Three.

Listing Three

public int delete(int value)
{
    slNode top = new slNode(findVertList(value));
    slNode toDelete = findInVertList(top, value);
    if(toDelete == null)
        return -1;
    size--;
    // Top node 
    if(toDelete.getParent() == null)
        deleteParentNode(toDelete);
    // Bottom node in sublist
    else if(toDelete.getChild() == toDelete)
        deleteBottomNode(top);
    // Found node in sublist
    else 
        deleteMiddleNode(top, toDelete);
    updatemaxDepth();
    delFixUp ();
    return value;
}

Delete must find the value of the node to be deleted. It begins by examining [n] top nodes to find the appropriate vertical list. Then, [n] nodes in the vertical list are searched to find the node. The removal of the node is simply a matter of manipulating pointers, so it is performed in constant time. The use of delFixUp yields a running time of O([n]).

After the completion of a delete operation, delFixUp is called. If a deleted node had a depth equal to maxDepth, then delFixUp would not need to be executed. delFixUp does one of three things when called. If a list has a depth of maxDepth-2, then a search is performed for a list that has a depth of maxSize. When that list is found, then either shiftLeft or shiftRight is called to balance the depth of the list. If no lists are found to have a depth equal to maxSize, then a node is shifted from the last list into the list that is too short via shiftLeft; see Listing Four.

Listing Four

private void delFixUp (slNode theTop)
{
    // Nothing needs to be done.
    if(theTop.getDepth() == maxDepth -1)
        return;
    slNode temp = getFullList();
    if(temp.getValue() > theTop.getValue())
        // This situation should only occur if the list goes from 
       // square + 1 to square
        if((temp == head.getLeft()) && (temp.getDepth() == 1))
        {
            // Move the last node left one list
            putInVertList(temp.getLeft(), temp);
            // Only want to shiftLeft if node is not placed in the short list
            if(theTop != temp.getLeft())
                shiftLeft(temp.getLeft(), ONE);
            // Set the respective pointers
            head.setLeft(temp.getLeft());
            temp.getLeft().setRight(head);
    }
        else shiftLeft(temp, ONE);
    else if(temp.getValue() < theTop.getValue())
        shiftRight(temp, ONE);      
    else ;  // Do nothing.  Depths of lists are appropriate
}



The reason for requiring all vertical lists (except the last list) to be no shorter than maxSize-1 is so that when the list goes from square+1 to square, the deletion of one node results in a list that is perfectly square. getFullList returns the top node of the list with depth [n]. A comparison is made between that top node and the deleted node. If the top node is smaller than the deleted node, then shiftRight is called to move nodes from the left to the right. If the top node is greater than the deleted node, then the nodes are shifted from right to left with the shiftLeft routine.

If the depth of a vertical list needs to be corrected, getFullList examines [n] nodes in the list of top nodes. Then either shiftRight or shiftLeft is used to move the nodes, which have a stated running time of O([n]).

Testing

To empirically test the data structure, I created a SquareList of size n before adding or deleting any nodes. I then performed three tests that involved inserting 100 nodes into the list. After the completion of those trials, I then executed three trials of deleting 100 nodes from the list. My strategy was as follows: Within the code, counters were placed inside loops. The counter was incremented with each iteration of the loop, and the value was returned upon completion. After creating the list, the counter was reset to 0. Then, 100 nodes were either inserted in or deleted from the list. For example, in trial number 1 with 1000 nodes, the first node was inserted into a list of 1000 nodes, and the 100th insertion was into a list with 1099 nodes. Table 2 shows the data, while Figure 6 graphs it. The average value of the counter for insertion and deletion is highlighted as well as the value of n. The last two columns show the ratio of insert or delete as a ratio of n.

Table 2: Test results.

Figure 6: Test results plot.

Conclusion

Clearly, there are changes you could make to improve the efficiency of the SquareList. For instance, findVertList could search left or right to find the appropriate vertical list. If users know in advance that data will be somewhat ordered, this could be a significant advantage. Alternatively, putInVertList could search from the bottom up as well as from the top down to find the correct location of a node. The SquareList may also be more efficient if calls to shiftLeft and shiftRight are delayed until a time that requires that the list be "squared." Programmers could also implement dummy nodes to make the implementation more convenient.

Acknowledgement

Thanks to Dr. John C. Lusth for his guidance on this project.


Mark is a contract programmer for Adecco Technical. He can be contacted at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.