US 7069272 B2 Abstract A system and method are disclosed for implementing dynamic set operations on data stored in a sorted array using a hybrid red-black binary search tree as a data structure. More particularly, the subject invention facilitates the implementation of a binary search to access data items stored in a sorted array by position, rather than by key value. This is accomplished by generating a balanced red-black binary search tree for a sorted array of data items wherein the information stored at each node of the tree includes: a key value; a color; a left pointer to indicate the presence of a child to the left of the node; a right pointer to indicate the presence of a child to the right of the node; a count of descendents along a branch to the left of the node; and a count of descendants along a branch to the right of the node.
Claims(19) 1. A method of accessing data items stored in a sorted array by position comprising the steps of:
a) providing a sorted array of data items;
b) generating a balanced binary search tree for the sorted array of data items using a first computer code segment based upon a set of properties that control a structure of the tree, wherein each data item in the sorted array defines a node in the tree, and information is stored at each node including:
i) a key value corresponding to a data item in the sorted array;
ii) a color selected from a group consisting of red and black;
iii) a left pointer to indicate a child to the left of the node;
iv) a right pointer to indicate a child to the right of the node;
v) a count of descendents in all sub-trees in a branch to the left of the node; and
vi) a count of descendants in all sub-trees in a branch to the right of the node; and
c) conducting a binary search of the tree using a second computer code segment to access a requested data item at a given position in the sorted array by referring to the right and left descendant counts at each node on a path from a root node of the tree to the node of the requested data item, wherein the following steps are executed recursively starting from the root node until the requested data item is found:
i) deciding a position of a current node by referring to the left and right descendant counts in the left and right branches;
ii) determining which branch of the tree to search by comparing the given position of the requested data item and the position of the current node;
iii) designating a top node in the determined branch as the current node and repeating steps from the deciding step if the requested data item is not found at the top node in the determined branch.
2. A method according to
3. A method according to
4. A method according to
5. A method according to
6. A method according to
7. A method according to
8. A method according to
9. A method according to
10. A machine readable program, tangibly embodied on a computer readable medium, containing instructions for controlling a system for accessing data items stored in a sorted array by position comprising:
a) means for generating a balanced binary search tree for a sorted array of data items based upon a set of properties controlling a structure of the tree, wherein each data item in the sorted array defines a node in the tree, and each node contains a plurality of data fields for storing information including:
i) a key value field for storing a key value corresponding to a data item in the sorted array;
ii) a color field for storing a color selected from a group consisting of red and black;
iii) a left pointer field for storing a value indicating existence of a child to the left of the node;
iv) a right pointer field for storing a value indicating existence of a child to the right of the node;
v) a left count field for storing a value equal to a total number of descendents in all sub-trees in a left branch of the node; and
vi) a right count field for storing a value equal to a total number of descendants in all sub-trees in a right branch of the node; and
b) means for conducting a binary search of the tree to access a requested data item at a given position in the sorted array by referring to the values stored in the right and left count fields at each node on a path from a root node of the tree to the node of the requested data item, wherein the following steps are executed recursively starting from the root node until the requested data item is found:
i) deciding a position of a current node by referring to the left and right descendant counts in the left and right branches;
ii) determining which branch of the tree to search by comparing the given position of the requested data item and the position of the current node;
iii) designating a top node in the determined branch as the current node and repeating steps from the deciding step if the requested data item is not found at the top node in the determined branch.
11. The machine readable program of
12. The machine readable program of
13. The machine readable program of
14. The machine readable program of
15. The machine readable program of
16. The machine readable program of
17. The machine readable program of
18. The machine readable program of
19. A method of generating a balanced binary search tree for a sorted array of data items using a computer code segment, comprising the steps of:
a) providing a plurality of nodes each having a key value corresponding to a data item in the sorted array;
b) assigning a color to each node using the computer code segment based upon a set of properties that control a structure of the tree, wherein the color of a node is selected from the group consisting of red and black;
c) assigning a role to each node in the tree using the computer code segment, wherein the role of each node is selected from the group consisting of child, parent, uncle and grandparent, depending upon the relationship of each node to a reference node; and
d) tracking a total number of descendants in a branch to the left of each node and a total number of descendants in a branch to the right of each node to facilitate a binary search of the tree by position as well as by key value using the computer code segment to execute the following steps recursively starting from a root node until a requested data item at a given position is found:
i) deciding a position of a current node by referring to the left and right descendent numbers in the left and right branches;
ii) determining which branch of the tree to search by comparing the given position of the requested data item and the position of the current node;
iii) designating a top node in the determined branch as the current node and repeating steps from the deciding step if the requested data item is not found at the top node in the determined branch.
Description A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the reproduction by anyone of the patent document or patent disclosure as it appears in the Patent and Trademark Office, patent file or records, but otherwise reserves all copyrights whatsoever. 1. Field of the Invention The subject disclosure relates to data structures, and more particularly, to a system and method for implementing dynamic set operations on data stored in a sorted array using a hybrid red-black binary search tree. 2. Background of the Related Art Binary search trees are data structures that store collections of items that can be ordered, such as integers. In a binary search tree, a data item is stored at a root. Smaller items are organized recursively in a subtree to the left of the root, while larger items are stored recursively in a subtree to the right of the root, as illustrated for example in Binary search trees support standard dynamic set operations such as search, insert and delete. A search is implemented to determine whether an item is contained in a tree, and if so, it returns the item. Insert is implemented to add an item to a collection stored in a tree, if it is not already present, and delete is implemented to remove an item from a collection stored in a tree. A binary search tree of height h can implement any standard dynamic set operation in O(h) time. Thus, set operations are fast if the height of the tree is small, but if the height of the tree is large, their performance may be no better than operations implemented with a linked list of data items. A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either red or black. By constraining the way nodes can be colored on any path from the root to a leaf, red-black trees ensure that no such path is twice as long as any other, so that the tree is approximately balanced. Thus; basic dynamic set operations on a red-black tee with n nodes take O(lg n) time in the worst case. Each node of a red-black tree contains the fields color, key, left child pointer, right child pointer and parent. If a child or the parent of the node does not exist, the corresponding pointer field of the node contains the NIL value. NIL's are regarded as pointers to external nodes (leaves) of the binary search tree, and the normal, key-bearing nodes are regarded as internal nodes of the tree. A binary search tree is a red-black tree if it satisfies the following red-black properties: 1) Every node is either red or black. 2) The root is black. 3) Every leaf (NIL) is black. 4) If a node is red, then both its children are black. 5) For each node, all paths from the node to descendant leaves contain the same number of black nodes. Often, large sets of data items are displayed in tabular form, where the data items are arranged in rows. In many applications, for instance, in investment portfolios containing a multiplicity of financial instruments, the rows of the table are maintained in sorted order, and there are frequent single row insertions and deletions which must be displayed in real-time. There are various computer applications that are currently available for displaying tabular data. Two used commonly are XRT tables for Motif based applications and JTables for Java based applications. These applications present a “view” into the table consisting of a fixed number of rows and columns which will fit onto a screen. Behind this view, is application code which supplies data to the view. In Java, this code is called a TableModel. The JTable and other similar applications retrieve data by calling the data model and requesting the value at a cell specified by row and column index. As the user scrolls around the table, there are an enormous amount of calls to the data model as different cells move into view. The clear way for the TableModel to store data is in an array of rows, where each row contains an array of columns. In the majority of table applications, this method is quite appropriate, as it provides fast access to any individual cell by indexing into the row array, then indexing into the column array. In tables where the rows are either unsorted or there are infrequent row changes, this method is ideal. If the array is sorted but unchanging, one can use any fast sorting algorithm to create the sorted array, then access data quickly. If the array changes frequently, but the order is unimportant, new rows can be added at the end of the array very quickly. A problem arises however, when the rows of the table are maintained in sorted order and there are frequent additions and deletions, as is common with investment portfolios. If an array representation is used, to insert a new row in the table, all of the rows below the new row must be moved down, in sorted order, to make room for the new row. This takes time proportional to the number of rows in the table. If the table is large and insertions are frequent, there will be a performance problem. This problem can be overcome by storing the data in a binary search tree rather than in an array of rows and columns, since set operations such as insertions and deletions take place in a standard binary search tree in O(h) time, or in an red-black tree in O(lg n) time in the worst case. However, binary search trees, including red-black trees, do not provide access to data items by their position in an array. Rather, they provide access to data items by key value. For example, if an array of data consists of a set of trades involving fixed income securities which are displayed in entry date order, a binary search tree can be generated which allows a user to perform standard dynamic set operations such as search, delete and insert in O(h) time. But when the TableModel is asked to search for and return a trade at particular position in the table, rather than one with a particular entry data, it is unable to do so. In conventional binary search trees, including red-black trees, there is no direct access to a data item by its position, unlike in an array wherein data may be retrieved by row and column index. Therefore, it would be beneficial to construct a data structure, and preferably a modified red-black tree that can facilitate assess to data items at given positions in a table of sorted rows, as quickly as rows can be found by key value. The subject invention is directed to a system and method for implementing dynamic set operations on data stored in a sorted array using a hybrid red-black binary search tree as a data structure. More particularly, the subject invention facilitates the implementation of a binary search to access data items stored in a sorted array by position or index, as well as by key value, as in conventional red-black trees. This is accomplished by generating a balanced red-black binary search tree for a sorted array of data items wherein the information stored at each node of the search tree includes: a key value corresponding to a data item in the sorted array; a color selected from the group consisting of red and black; a left pointer to indicate the presence of a child to the left of the node; a right pointer to indicate the presence of a child to the right of the node; a count of descendents along a branch to the left of the node; and a count of descendants along a branch to the right of the node. Using this data structure, a binary search can be conducted to access and retrieve a selected data item at a given position in the sorted array by reference to the right and left descendant counts at each node. With respect to sorted tabular data, the data structure of the subject invention can find rows at a given position in a sorted table as fast as it can with a given key value. As a result, rows can be added, deleted or updated in O(lg n) time. These and other aspects of the system and method of the subject invention will become more readily apparent to those having ordinary skill in the art from the following detailed description of the invention taken in conjunction with the drawings described herein below. So that those having ordinary skill in the art to which the subject invention appertains will more readily understand how to employ the same, reference may be had to the drawings wherein: Referring now to the drawings, there is illustrated in Referring to The following Java class generates the data structure of the hybrid red-black tree of the subject invention. This class, as well as other routines set forth herein, are based upon pseudocodes presented in Thomas H. Cormen, et al.,
Continuing on with the example of In addition to the key value, additional information relating to the structure of the tree is stored at each node of red-black binary tree In accordance with the subject invention, all binary searches using the hybrid red-black tree disclosed herein begin at the last row located in the data table. Therefore, it is necessary to keep track of this row as it serves as a reference point for the operation. The following is a Java class that locates an element or item in a TableModel of a JTable by index or position.
By way of example, referring to the data table of Looking to the first node of six in the right branch of the root, the left descendent count is equal to 3, and the right descendent count is equal to 2. Therefore, the ninth row must be down the left sub-branch, rather than the right sub-branch, because the eleventh and twelfth rows are obviously down the right sub-branch, based upon the right descendant count. So the search proceeds down the left sub-branch. At the parent node of the left sub-branch, it is determined that there is one descendant to the left of the node and one descendant to the right. Therefore, the ninth row in data table In addition to supporting indexed access through binary searching based on descendant counts, the data structure disclosed herein supports standard dynamic set operations of insert, delete and retrieve by key value. These operations all work normally in O(lg n) time. Insertions and deletions are accomplished as in an ordinary binary search trees. However, because such operations result in modifications to the tree, the resulting structure may violate the red-black properties set forth hereinabove. To restore the red-black properties, the colors and pointer structures of some nodes in the tree must be changed. Changes to the pointer structure of a node are accomplished through rotations which are local operations that preserve the binary-search-tree property. There are two types of rotations: left rotations and right rotations. The following routine performs a left rotation and is designed to keep track of the number of descendants on each side of the node being operated upon.
This routine performs a right rotation and is designed to keep track of the number of descendants on each side of the node being operated upon.
When a node is inserted into a red-black tree, it is colored red. If the node has a red parent, then there is a violation of the red-black properties. If the uncle is also red, the parent and uncle are colored black, and the grandparent is colored red. Then a check is made to determine if a violation of the red-black properties remains. Otherwise, one or two rotations may be needed to satisfy the red-black properties, but no more than two rotations will be required in any case. By way of example, The following routine performs an insertion procedure in a red-black tree. This Java class includes code for tracking descendant counts. Initially, the item is inserted into the tree without concern for preserving the red-black properties.
After an item has been inserted into the binary tree, the following routine is employed to re-establish the red-black properties of the tree, if necessary.
When deleting a node from a tree, if the node that was excised was red, no additional steps are needed since the red-black tree properties remain intact. Otherwise, if the excised node was black, operations proceed as if there is an extra black node in the tree, and the extra black node is moved up the branches of the tree until the red-black properties are satisfied. At most, three rotations will be required to satisfy all of the properties. By way of example, Initially, before a deletion procedure can begin, the item to be deleted must be located or looked up. The following code is used to perform the initial search for the item to be deleted.
After the item to be deleted has been located, the following code performs the deletion of the node. This routine includes code for tracking descendant counts.
This routine rebuilds the descendant counts from the given node to the root after the node has been spliced from the tree.
This routine re-establishes the red-black properties of the tree after the deletion procedure, if necessary. That is, if the deleted node was red, this routine is not performed.
Although the system and method of the subject invention has been described with respect to preferred embodiments, those skilled in the art will readily appreciate that changes and modifications may be made thereto without departing from the spirit and scope of the present invention as defined by the appended claims. Patent Citations
Non-Patent Citations
Referenced by
Classifications
Legal Events
Rotate |