JSP Tree Structure

Source: Internet
Author: User

First, we will introduce the storage of tree structures in dB.

Use a two-dimensional table, for example, to store a tree structure:

Now, our goal is to express this tree structure:

It can be seen that there is a hierarchical relationship between them.Source code, As follows:

Now,AlgorithmThe idea is to first arrange the tree structure in the order of list. This order actually removes the order of UL and Li tags, for example:

It is actually very easy to implement this order.

Let's look at our tree.

Abcdef is actually the result of the first-order traversal of this tree.

So, if we only have one orders table, how can we generate the result of the first sequential traversal? (Do not use recursion)

Observe the orders table:

Node to fathernode is a one-to-one ing, while fathernode to node is a one-to-many ing.

That is to say, it is not easy for us to know the sibling node of a node (we need to determine the parent node first and then query the sibling node through the parent node), but it is very easy to determine the child node of a node.

For example, for all the subnodes of node A, you only need to query fathernode =.

Therefore, we can use the broad search idea to solve this problem.

Guangsearch first traverses the first layer of nodes closest to the root, puts these nodes into the queue, then extracts nodes from the queue header, and then traverses all the subnodes of the node, and put its child nodes at the end of the queue, and go down to know that all nodes are traversed until now.

Take our tree as an example.

However, if we directly use the widely searched algorithm, the sequence we get is:

Abdcef

It is different from what we expect.

Therefore, improve the algorithm.

To store our structure, create a new list. Now we have two lists: one is the queue for extensive search and the other is the list for storing results.

When a Header element is removed from a wide search and the child nodes of the element are placed in the queueLocationInsert these subnodes. So where is the corresponding location? Is the removed Header element.

For example, if the queue is in the form of "A", the current operation is to remove the header and element a of the queue and add its subnode "B D" to the queue. At the same time, locate location a in list and add "B D" to its end. In this case, the queue format is changed to "B d", and the list is changed to "a B D ". Repeat this step to remove the Header element in the queue, namely, B, and put its subnode (c) to the end of the queue, at the same time, add the child node (C) of B after the B element of list. At this time, the queue form is changed to "d c", and the list form is changed to "a B C D ". Follow this method until the search is complete, that is, the queue is empty.

At the end of this algorithm, we can get the desired sequence a B c d E F.

However, this sequence loses a very important information, that is, hierarchical information.

So let's make some improvements to the algorithm. We will introduce a struct.

<Node, level>

Use the level attribute to record the hierarchy of a node.

When will the Level Attribute be assigned a value?

Insert the child element of an element into the list.

How to assign values?

This problem is very simple. Because a child element is inserted for an element, the level of these elements to be inserted is equivalent to the level + 1 of its parent node.

Therefore, the list structure is changed to the following:

The node list sequence "abcdef" and Its Level Attribute are required.

Now, we know the node sequence and the relationship between nodes, so we can build a tree structure represented by UL and Li.

Let's take a look at the sourceCode:

Found,

(1) When the level increases, a <ul> label appears.

(2) When the level is reduced by an hour, the </ul> label will appear, and the number of </ul> labels is related to the degree of level reduction, that is, the reduced level is n, n </ul> labels should appear.

(3) Items in each list are surrounded by a <li> </LI> label pair.

From the above rules, we can write the following:Program:

Stack <string> stack = New Stack <string> (); String item = "</Ul>" ; String res = "" ; Arraylist <Sightlevelpair> List = This  . Sighttree. getlist ();  Int Lastlevel = 0 ;  Int Currlevel = 0 ;  If (List. Size ()! = 0 ) {Iterator <Sightlevelpair> iter =List. iterator ();  While  (ITER. hasnext () {sightlevelpair = ITER. Next (); currlevel = Pair. getlevel ();  If (Currlevel> Lastlevel) {res + = "<Ul>" ; Stack. Push (item );}  Else   If (Currlevel <Lastlevel ){  Int Delta = lastlevel- Currlevel;  While (Delta> 0 ) {Res + = Stack. Pop (); -- Delta ;}} res + = "<Li>" + pair. getsight (). getsightname () + "</LI>" ; Lastlevel = Currlevel ;} While (0! = Stack. Size () {res + = Stack. Pop ();}} 

We can see that there is a stack in the program, which is used to store the "</ul>" string. When a level is increased, a "<ul>" is added after the string, and a "</ul>" is pushed to the stack, in addition, when the level is reduced, the corresponding number of "</ul>" is thrown.

The results are still ideal:

A custom tag is used to generate this tree, that is, a subclass of simpletagsupport is defined to process custom tags.

To create a custom tag, click here ].

 

 

Related Article

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.