Solution of tree problem

xiaoxiao2021-03-06  122

This is a multi-level tree (Tree), in fact, the principle is similar to the structure of our forum, we are the main data structure of this to achieve [ID] long integer [NUM_REPLIED] double precision [Num_Followed] double Accuracy [NUM_LASTTIME] Double-precision type When a customer is the top: first-level customer, it should be num_replied = num_followed if it is the second-level customer should be: Num_rePlied = new time code Num_Followed = The last level number Num_Replied is also Say, secondary or non-primary classification customers should be num_replied <> num_followed and update Num_LastTime when updating Num_LastTime when a lower customer or a lower customer is added, and it is to say that the entire customer tree is complete, as long as Num_LastTime The same, must be the same principal classification, which is easy to get a complete customer tree, get the first step of the category Num_LastTime, according to two time yards, the whole tree structure is organized but the problem Yes: If we have to get all the superiors of a certain customer or all the lower levels? According to the above data structure, a customer can exist aspects of the same level of relationships, as long as he is not a primary classification, all Num_Replied is the same, and Num_LastTime is a level; but they may not belong to The same superior customers, all, the above structure cannot be directly completed, do the following changes: the data structure after the change [ID] long integer [Mark] long integer [Num_Replied] double precision [num_followed] double precision [Num_LastTime] double-precision type adds a Mark field, used to represent the number of customers, first-level customers with 0, 2nd 1 represented, according to such push ... So, give all the following levels of a customer This is the same, indicating the same level customer 2.mark> The customer's mark3.num_replied> The customer's NUM_REPLIED is here, you can find all the superiors of a customer, you should also know how to do it. If you think too much Trouble, I hope to use a field to express the affiliated relationship like your data structure. I have to learn how to scientifically and efficiently encode we can come to 32-bit binary strings to represent a customer code such as: 0111 0010 0001 0100 0111 0001 0101 0111 The front four bits used to represent the first level of customers, if it is a customer, then his back should be 0 is similar to: 0001 0000 00 00 00 00 00 00 00 00 If it is the second level Customers, the second paragraph should have a number, such as 0001 0000 0010 0000 00 00 00 00 0000 If it is one of the next level, there is also: 0001 0000 00000000 0000 0000 000 0000 like a secondary push, unfortunately this algorithm is used :

Bits and calculations, and this is not supported in VBS and Access, but unfortunately. Another 2 classic tree algorithm is in the construction of website, often requires processing of trees with tree-type data structures such as commodity classification, column classification, forum topics. If these categories are not encoded, the efficiency of the program is very low. So how do I design a highly efficient encoding algorithm? Here is a highly efficient classification algorithm. I am in many of our company's web applications, including e-commerce, download sites, news distribution systems, have applied such encoding algorithms, and the effect is very good. Problems to be resolved in the classification algorithm in the construction of the website, the application of classification algorithms is very common. When designing an electronic store, you should involve a commodity classification; when designing a publishing system, it is related to the column or channel classification; when the design software downloads such a program, it is related to the software classification; so, wait. It can be said that classification is a very common problem. I often interview some programmers, and I have no exception to ask them some questions about classification algorithms. Here's a few questions I often ask. Do you think you can answer it easily ^ _ ^. 1, classification algorithm often manifest as a tree representation and traversal problem. So, then, if you express the tree class classification with a table in the database, should there be a few fields? 2, how to quickly restore a tree from this table; 3, how to determine if a classification is another classification; 6, how to add classification; these problems are not easy to answer if the number of classifications of the classification and the number of classifications per level. This article tries to solve these problems. Classified data structure We know: The classified data structure is actually a tree. In the "Data Structure" course, everyone may learn TREE algorithms. Since we use the database in the construction of our website, we will talk from the storage of Tree in the database. To simplify the problem, we assume that each node only needs to reserve this information. We need to numbered each node. There are many ways to numbered. The commonly used in the database is the automatic number. This is like this in Access, SQL Server, Oracle. Suppose the number field is ID. In order to indicate a node ID1 is the parent node of another node ID2, we need to keep a field in the database, indicating which node belongs to the son. Ten this field name FatherID. As the ID2 here, its FatherID is ID1. In this way, we get the data sheet definition of Catalog Catalog: CREATE TABLE [Catalog] ([ID] [INT] NOT NULL, [NAME] [NVARCHAR] (50) Not null, [FatherId] [INT] NOT NULL); Agreement: We agree to use -1 as the top-level father code. Classification of -1. This is a virtual classification. It is not recorded in the database. How to restore the biggest advantage of the Catalog definition above a tree is that it can easily recover a tree-classification tree. To make a clearer display algorithm, we first consider a simple question: How to display the next class classification of a classification.

We know, to check the next class classification of a classification FID, the SQL statement is very simple: select name from catalog where fitherid = FID shows these categories, we simply use

  • : <% Rem Oconn - - Database connection, calling getChildren When you have opened REM FID ----- Current Classified number Function getChildren (Oconn, FID) strsql = "select id, name from catalog where fatherid =" & FID set RScatalog = Oconn.execute (strsql) %>

  • New Post(0)