Some learning experiences on dynamic tree and LCT (continuous update)

Source: Internet
Author: User

What is a dynamic tree?

Dynamic tree problem refers to the problem of dynamically maintaining related information on a tree.

In the general dynamic tree problem, we will be asked to maintain a forest consisting of a number of subtrees nodes that have a disordered root tree. This data structure is also required to support the partitioning of the tree (delete edges), merging (plus edges), and certain operations (path operations) on the path to the root of a point. Sometimes, a dynamic tree problem involves some operations (subtree operations) on a sub-tree of a point, and the dynamic tree problem involving subtree manipulation is more complex and requires more complex data structures. However, Konjac Konjac does not do this kind of high-level topic, so I will talk about a few simple operations.

Some notes about LCT.

LCT (Link-cut Trees) is a data structure to solve this kind of dynamic tree problem. This data structure can achieve the above dynamic tree problem in the time of averaging O (log n).

LCT Detailed Introduction, we can refer to Yang Zhe's "qtree Solution of some research", here Paste Library Link: https://wenku.baidu.com/view/75906f160b4e767f5acfcedb.html

Simply put, the LCT is divided into tree chains, dividing the sons of the nodes. The tree chain is divided according to the node tree size to divide the weight of the son, and the data structure to maintain the heavy chain, and LCT will divide the son into virtual, real two sons, the corresponding edge is called virtual edge or real edge, and at any time a node at most will have a real son (probably not). Because the tree shape will change, so LCT is not strictly dividing the actual son, but dynamic change, it also uses the data structure to maintain the real chain (continuous real edge), and with more flexible splay.

Some basic definitions of LCT:

1. Depth: The greater the depth (small), the longer the distance to the root node path (short).

2. Solid Edge: A non-leaf node, to one of its sons a special edge, called the real edge, the non-leaf node to its other sons is the edge of the virtual edge. Note that for some non-leaf nodes, the edges that it has with its sons may all be imaginary edges.

3. Real path (chain): A path of non-elongation, which is connected by the end of several solid edges, is called the real path. By the definition of real edges, the point depth on the real path is different and is a continuous natural number. Among them, the non-elongation means that the lightest node on a real path is u, then the edges between the parent nodes of U and u cannot be real edges, and the deepest node of a real path is V, and the edges connected by V and its sons are all imaginary edges.

4. Father of Real path: it can be concluded that each real path is connected by a virtual edge, for a real path, we define its real path to the father (Path_parent) for the father of its lightest node, such as:

Where the red path is the real path, then for 2-5-6 this real path, its real path of the father is 2 of the father, that is, 1.

Some basic operations with LCT

Maintain the real path with splay: Since each real path is composed of a solid side that is connected by the end-to-end, any two points on the real path are the ancestors and descendants of the relationship. In other words, if you sort the nodes with a depth as a keyword, we will get a unique sequence of ordered nodes.

Based on this idea, we maintain this node sequence with the balance tree, the depth of the nodes in the left subtree of each node in the balance tree is smaller than the node in the real path, and the right subtree is larger than the node, so the leftmost node of the balance tree should be the head of the path, and the right side of the node should be the tail of the

Some basic operations (note: In the course of the operation does not need to consider the shape of the splay, but only to consider the current tree morphology): 1. Access (x): Constructs a chain from the beginning of x to the root node.

This is the operation for a node x, which turns all edges on the path of x to the root node into real edges, and of course, in order to preserve the nature of the real and imaginary edges, some of the original real edges become virtual edges accordingly. Note that this action turns the solid edge below X into a virtual edge. The steps for this action are as follows:

(1): If the node x is not the tail of its real path, that is, X has a child node connected to it with a real edge, then the edge needs to be "disconnected" (the disconnection is not to delete the edge, but only to turn it into a virtual edge). The method is to first rotate the node x with the splay operation to the root node of the balance tree, and then x must have a right subtree, so the x is separated from the right subtree of x, and the path_parent of the right subtree of x is set to X.

(2): If node x is located in the balance tree containing the root node, then the process is over; otherwise, the step (3);

(3): Set Y to the path_parent of the balance tree where x is located. Rotate y with the splay operation to the root node of the balance tree to which it belongs, and replace the right subtree of y with the balance tree where x is located, thus achieving an upward extension of the real path. Of course, it's not over, we need to separate the right subtree of y, y the original right child to remember P. At this point P's path_parent is Y, then proceed to step (2).

When actually implemented, we do not need to display maintenance out of path_parent, we can slightly change the implementation in Splay, that is, we consider the FA of the root node in splay, we do not need to set it to 0, but instead set it to path_parent, so that each point of the father, Either the point on the real path, or the path_parent of its real path, in particular, the path_parent of the root node of the tree is 0. So a point, its FA of the left and right son may not be for it, so judge root node conditions also need to be slightly modified. Here is a diagram of the implementation process (red side for solid side, Parkinson's disease):

Because the maintenance of the keyword is a depth, so the deletion of the former splay must have X on the sub-tree that section, so the x through the splay rotation to the root node, the right subtree of X is directly separated from it, and the right subtree of the path_parent to X (actual operation does not need)

It is important to note that the path_parent of the original right subtree becomes y when the right subtree of y is replaced by the balance tree of X.

2.Findroot (x): Locate the root node of the tree where node x is located.

With Access (x), it's easy to find the root of the tree. Just by executing access (x) on the node x, we have the X in the same balance tree as the root node we are looking for. Then, the root node to be found must be the head of the real path (because of the smallest depth), which is the leftmost node in the balance tree.

Icon:

3.Makeroot (x): nodes x becomes the root node of the tree in which it is located.

First we look at the following diagram:

For a tree, the parent-child relationship is determined, and the figure is directed to the son by the father.

So what if we take 2 as the root of the whole tree? Then it will grow like this:

If we haven't found the law yet, we'll take 5 for the root of the whole tree:

Yes, who is the root, that is, the point to the root node path parent-child relationship is reversed, so the operation is equivalent to the path from node x to the current root node in the direction of all the tree edge to reverse. First we execute Access (x) and we have taken the path out. Because the path is maintained with the balance tree, it is necessary to perform the interval rollover operation of the balance tree, and we can draw on the idea of the marker (lazy operation) of the segment tree to complete the counter-order operation by marking the balance tree node. This is reversed for the entire balance tree, so the marker should be hit at the root node of the balance tree. After marking, note the other operations (e.g. after rotation) and the current and updated.

4.Link (x, y): Make X a child of Y, that is, an edge (x, y)

This requires only one makeroot (x) operation, followed by the FA of X as Y.

Icon:

5.Cut (x, y): Delete this edge of the tree (x, y)

When the operation is performed, the x is first set to the root node of the root tree, ensuring that Y must be the child of X. Then we perform an Access operation on Y, and we merge X and y into the same balance tree. At this point, the splay operation on y makes it the root node of the balance tree, while the left subtree of y and Y is separated, and the edge deletion is done. (If there is an edge (x, y) in the tree, the left subtree of y must have only the X node).

Icon:

6.Select (x, y): Takes out (x, y) the path in the tree for interval operation.

Once Makeroot (x) is placed at the root of its tree, and then an Access (y) operation is performed, the X and Y and all points on their path are integrated into the same balance tree. At this point, we mark the root node of the balance tree or query the information directly.

Icon:

And then there are some strange examples of LCT, here to one: BZOJ2049: Cave exploration, Topics and Problems Portal: (La la la)

Some learning experiences on dynamic tree and LCT (continuous update)

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.