The application of tree structure in program design

Source: Internet
Author: User

IntroductionIn recent years, because of various competitions have adopted free-pascal, so for the algorithm, space efficiency requirements are reduced, but the time efficiency has put forward higher requirements. This allows the player not only to master the conventional algorithm skillfully, but also bold innovation, to construct a more efficient algorithm to solve the problem. In the previous program design, the chain structure adopts more. It is true that the chain structure has the advantages of low programming complexity and easy to understand, but it has a fatal weakness: the connection between the adjacent two elements is not obvious. But the tree structure can do this very well. This is often the subject of the competition: the link between the elements, and the need to into several sets, each of which is directly or indirectly associated with the elements in the collection. The main concerns in this type of issue relate to and the merging and finding of collections, so this set is called a set. and check SetI. Chain structure and the search settwo. Tree structure and the collection 1. The chain structure of the check set the list is used to calculate and check the set: Each element in the table is set with two pointers: one to the next element in the same set, and the other to the first element of the table. A chain-based storage structure that calculates when a collection is searched the complexity of the method is only O ( 1 ), but the complexity of the algorithm when merging sets has reached O ( N ). If we want the time efficiency of the two basic operations to be relatively high, the chain storage method " Powerless " up. 2. Tree structure and the collection the use of tree structure support and the calculation of the set can meet our requirements. And the check set is different from the general tree structure, each vertex record is not its sub-node, but the parent node is recorded. Let me show you two ways of calculating the tree structure and checking the set.Nn⑴ directly in the tree query N⑵ Edge Query Edge "path compression" corresponds to the previous chain storage structure, the advantages of the tree structure is very obvious: the complexity of programming is low, time efficiency is high. (1). Search directly in the tree Inquiry the merging algorithm of the collection is simple, as long as the root node of the two tree is connected, this step is as long as the O (1) time complexity. Therefore, the time efficiency of the algorithm depends on the speed of the collection lookup. And the search efficiency of the collection and the tree The depth is linearly related. Therefore, the time complexity required for direct queries is an average of O (Logn). But in the worst case, the tree degenerated into a chain, making the algorithm complexity of each query O (N). (2). Side Check Inquiry Edge " Path Compression " In fact, we can further reduce the complexity of the algorithm found in the collection: Using " Path Compression " algorithm. The idea is simple: reduce the depth of the tree by the way in the collection's lookup process. With path compression, the time complexity for each query is very slow to grow Ackerman inverse function of a function -- α (x). For the conceivable n,α (n) is within 5.   Segment Tree when dealing with problems such as area and perimeter of graphics, it is not necessary to rely on deep mathematical knowledge, but it is very difficult to improve the efficiency of dealing with such problems. This requires fundamentally changing the basis of the algorithm -- data structures. What we're going to say here is a special kind of data structure -- Segment Tree . first look at a more basic topic: We give the N line segments on the interval to determine the size of the intervals covered by these segments. let's get to know the segment tree gradually. I. Definition of segment tree two. Add and delete segments in the segment tree three. Calculate measure and number of consecutive segments 1. Definition of Segment TreeA segment tree is a binary tree that divides an interval into a single cell range of [i,i+1], each of which corresponds to a leaf node in a segment tree. Each node uses a variable count to record the number of line segments that overwrite the node. The segment tree corresponding to the interval [1,7] is shown. There is a line segment on the interval [3,6]. (computer is weak, can't get up, hope understand)
2. Inserting and deleting segments in the segment tree inserting and deleting line segments in a segment tree is similar to using recursion to sweep two sub-nodes from one layer to the other . until the segment is able to cover the entire range represented by the node. After analysis, it is found that the time complexity of inserting and deleting segments in the segment tree is O (logn). calculate measure and number of consecutive segments The measure m of a node refers to the length of the segment in the interval represented by the node. J-i (count>0) m= 0 (count=0 and nodes are leaf nodes) LCH. M + RCH. M (count=0 and nodes are internal nodes). 3. Calculate measure and number of consecutive segments number of consecutive line lines refers to the number of line segments that are disjoint in the interval. The number of consecutive segments does not simply add up the number of consecutive segments in the two sub-nodes as measures. So we introduced two volume LBD,RBD, which indicate whether the left and right ends of the interval are covered by a line segment.            1 (the left end is covered by a segment) lbd  =  0 (the left end is not covered by a segment)              1   (right end is covered by segment) rbd  =  0 (right endpoint not covered by segment)   Line can be defined according to LBD,RBD as follows:        1        (Count > 0)         0     (count=0 and nodes are leaf nodes)  Line=  lch^. Line + rch^. line-1    (count=0 and nodes are internal nodes, lch^. RBD, rch^. LBD are 1)            lch^. Line + rch^. line        (count=0 and nodes are internal nodes,      lch^. rbd,rch^. LBD 1 for different time)                                                                                                     tree-like array IOI2001 in the MOBILE A lot of players are stumped. Although the test instructions of the problem is very simple: in a matrix, by updating the value of the element to modify the matrix state, and calculate the number of a sub-matrix and, but the difficulty is that the data is very large. Here, let me introduce a new data structure. - tree-like array . 1 , build a tree array C 2 , updating element values 3 , sub-sequence summationThe complexity of programming is improved by using a tree array, but the time efficiency of the program is greatly improved. This is the advantage of using the tree structure to reduce the scope of the search and centralize the information, so that the updated array and summation operations are implicated in as few variables as possible. 1. Create a tree array C Firstly, the problem is simplified, and the algorithm of summation of one-dimensional subsequence is investigated. Set the sequence to A[1] , A[2] ... A[n]W Algorithm 1: Calculated directly in the original sequence. It is obvious that the time complexity of updating element values is O (1), and in the worst case, the time complexity of the sum of the subsequence is O (n). algorithm 2 : Add Array b , where B[i] = a[1]+a[2]+ ... +a[i]. Because a[i] changes affect b[i]┅b[n], the algorithm complexity of updating element values in the worst case is O (n), and the algorithm for the summation of the subsequence The complexity is only O (1). Either of these algorithms takes too long (algorithm 1) to update the value of the element, or does not avoid a large number of operations (algorithm 2) on the summation of the subsequence. Is there a better way? algorithm Three: Add array c, where C[i]=a[i-2 k +1]+ ... +a[i] (k is the number of the end 0 in the binary form). can be derived from the definition of C array : C[1]=a[1] C[2]=a[1]+a[2]=c[1]+a[2] C[3]=a[3] C[4]=a[1]+a[2]+a[3]+a[4]=c[2]+c[3]+a[4] C[5]=a[5] C[6]=a[5]+a[6]=c[5]+a[6] ... ......... the structure of the array corresponds to a tree, so it is called a tree array. After statistical updating of the complexity of the algorithm for the sum of element values and subsequence, it is found that the time complexity of both operations is O (LOGN), which greatly improves the efficiency of the algorithm. 2. Updating element values theorem if A[K] is affected by the sequence of C[p 1 ],c[p 2 ] ... c[p m ]. the P 1 =k, and P i+1 =p I +2 Li (L I to P I The number of the end 0 in the binary). This gives you a way to change the value of an element: If you add X to A[k], the C array c[p 1 ], c[p 2 ] , ... .. , C[p m ] (P m ≤n<p m+1 is affected, you should also add X. For example A[1]......a[9], a[3] Add x;p1=k=3 p2=3+20=4p3=4+22=8 p4=8+23=16>9 from this, c[3], c[4], c[8] should also be added X.   3. Summation of sub-sequences the sum of the subsequence can be converted into A[1] The starting sequence A[1] ... A[k] and S. finding S in a tree array is simple: according to C[k]=a[k-2 L +1]+ ... .. +a[k] (L is k the number of the end of the binary number 0) we're from K . 1 =k departure, according to k i+1 =k I - 2 Lki (Lki is k I The number of the end 0 in the binary number) Formula One Recursive k 2 , K 3 , ... .. , K m (k m+1 =0). This results in s=c[k 1 ]+c[k 2 ]+c[k 3 ] + ... .. + c[k m ] For example, calculate a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]k1=7k2= k1-2l1=7-20=6k3= k2-2l2=6-21=4k4= k3-2l3=4-22=0 that is a[1]+a[2]+ a[3]+a[4 ]+ a[5]+a[6]+ A[7]=c[7]+c[6]+c[4] to the two-dimensional, it is also only needed to calculate the number in the matrix starting from (the first) and then by adding minus to calculate the number in the sub-matrix and.                                                                                    Summary In the above example, we can see that the use of tree structure to deal with problems, often can narrow the search scope. This allows you to correctly select a path that starts at the root of each search process, and does not result in useless searches because it is usually a path that starts with a root, the speed of traversing the tree depends largely on the depth of the tree. How to reduce the height of the tree becomes the key of the algorithm. Compared with the traditional tree, the tree-like array is not called "tree", but its core idea is the same as the tree structure.                                                                                                                                                                                           <marvolo Original, non-reprint >

Application of tree structure in program design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.