Introduction to algorithms the 14th chapter of data structure expansion
Some engineering applications require only standard data structures, but there are many other applications that require a little innovation and transformation of existing data structures, but only rarely do we need to create new types of data structures, and more often, by storing additional information to expand a standard data structure, This data structure is then written with new operations to support the required applications. However, the expansion of the data structure is not always straightforward, because the new information must be able to be updated and maintained by the normal operation of the data structure.
14.1 Dynamic Sequential Statistics
order-static Tree : On the basis of a red-black tree, at each node plus a size field that represents the (inner) node tree of the subtree (including the node itself) with that node, and the Size field of the Sentinel T.nil (leaf node) is defined as 0, So there is the equation \ (x.size=x.left.size + x.right.size + 1\) .
The sequential statistics tree can determine any ordinal statistic in O (LGN) time, or it can determine the rank of any element in O (LGN) time, that is, its position in the linear order of the set.
In a sequential statistics tree, the keywords are not required to be different, and if the keywords are the same, the rank is defined as the position of the output when the middle sequence traverses the tree.
Finds an element with a given rank
# 返回以x为根的子树中秩为i的结点os_select(x,i): ==+1 if== r: return x elseif< r: return os_select(x.left, i) else: return os_select(x.right, i-r)
Each recursive call is dropped one layer in the sequential statistics tree, so the os_select run time is O (h) i.e. O (LGN).
Determines the rank of an element
# 返回树T中x在中序遍历T对应的线性序中x的位置os_rank(T,x): =+1 = x # while循环保持下列不变式: # 每次循环开始前, r为以y为根的子树中x的秩 while!= T.root: if== y.p.right: =++1 = y.p return r
Maintenance of subtree size
The insertion and deletion of red and black trees is broadly divided into two stages: looking for the insertion position, then coloring and rotating to maintain the red and black structure. To maintain additional information for sequential statistics, in the first stage, for each node from the root to the insertion location path, size is added 1, and the size of the new node is 1. For the second stage, coloring does not affect the tree structure, only the rotation affects the local structure, only need to add the following code on the basis of the rotation operation:
# 对 left_rotation(T,x) 和 right_rotation(T,x)==++1
It is obvious that the maintenance order statistics tree only requires O (LGN) time.
14.2 How to expand data structures
General steps for expanding data structures:
- Select an underlying data structure.
- Identify additional information to be maintained in the underlying data structure.
- Verify that the basic modification operation on the underlying data structure maintains additional information.
- Design some new operations.
Sometimes the data structure is not designed to design new operations, but to use additional information to speed up existing operations.
theorem 14.1 (expansion of the red-black tree) : Set \ (f\) is the attribute of the red-black tree \ (t\) expansion of n nodes, and is assumed to any node \ (x\) , \ (f\) The value depends only on the node \ (x\) , \ (x.left\) and \ (x.right\) information, and may include \ (x.left.f\) and \ (x.right.f\ ) . Then we can maintain the \ (f\) value of all nodes of \ (t\) during insert and delete, and do not affect the O (LGN) asymptotic time performance of two operations.
14.3 Interval Tree
Closed interval \ ([t_1,t_2]\) can be represented as an object \ (i\) , where \ (I.low = t_1\) is a low-end point, \ (I.high = t_2\) For high-end points.
Any two (closed interval)\ (i\) and \ (i^{'}\) satisfies the interval three-point law:
- \ (i\) and \ (i^{'}\) overlap: \ (and I.low \le i^{'}.high and i^{'}.low \le ' i.high\).
- \ (i\) on \ (i^{'}\) left: \ (I.high < i^{'}.low\) .
- \ (i\) to the right of \ (i^{'}\) : \ (i^{'}.high < i.low\) .
For open interval and half open interval, the law of the three points is still set up, but the conditions are changed.
interval tree: A red-black tree maintained on a dynamic set, where each element \ (x\) contains a range \ (x.int\) . The specific way of the interval tree expression set is: Fetch \ (x.int.low\) is the key word for the middle sequence traversal (linear order); In addition to its own interval information, each node \ (x\) contains additional information \ (x.max\), which is the maximum value of the endpoints of all the intervals in the subtree rooted in \ (x\) .
Since \ (X.max = max (X.int.high, X.left.max, X.right.max) \) , by theorem 14.1, insert and delete operation time is O (LGN).
Operations supported by the interval tree:
- Interval_insert (T,X): Inserts the element x containing the interval attribute int into the interval tree T
- Interval_delete (t,x): delete element x from interval tree T
- Interval_search (T,i): Returns a pointer to element x in the interval tree T, which overlaps the x.int with I. Returns T.nil if this element does not exist.
Interval_search (t,i): X=T.root# While loop ensures that the direction of the search is right # that is, if there is an interval that overlaps with I, then you can find it on the subtrees tree. while(x!=T.nil) and(I does notOverlap X.int):# because the maximum endpoint value of the interval on the left subtree of X is larger than the low point of I # If there are no bands on the left dial hand tree that overlap with I, then you can launch # on the right subtree, the lower end point is larger than the high point of I # So I can only find it on the left subtree # if(X.left!=T.nil) and(X.left.Max >=I.low): X=X.left# If the left dial hand tree is empty, it must be found on the right subtree # If the maximum endpoint value of the interval on the left dial hand tree is smaller than the low point of I, there must be no interval between the left subtree and I overlap Else: X=X.rightreturnX
Introduction to Algorithms Reading Notes-14th chapter-Expansion of data structures