Summary of the invention
In order to solve the problem that prior art exists, the invention provides a kind of grouped data storage means based on directory tree, can save memory headroom effectively, simplify the moving of the storage of grouped data and split catalog, increase and deletion action simultaneously.
The present invention also provides a kind of split catalog querying method based on directory tree, can retrieve the split catalog that needs inquiry fast, obtains the grouped data that this split catalog comprises.
In the grouped data storage means based on directory tree of the present invention, each node on the described directory tree comprises child nodes pointer, brotgher of node pointer and data pointer at least;
The storage means of described grouped data comprises:
A, set up the root node of described directory tree, the brotgher of node pointer that makes this root node is for empty, and data pointer is empty, and sets up a new node, the new node that makes the child nodes pointed of described root node be set up is with the one-level split catalog of described new node corresponding to grouped data;
B, utilize recursive algorithm to set up respectively and all split catalogs at different levels of described grouped data node of mapping one by one, corresponding each node, if the pairing split catalog of this node comprises the split catalog of next stage, one of the child nodes pointed that then makes this node corresponding to this node the node of corresponding split catalog next stage split catalog, and make this node data pointer for empty, otherwise, make this node child nodes pointer for empty, and make this node data pointed be used to preserve the memory headroom of the grouped data that the pairing split catalog of this node comprised; If comprised all nodes in the described directory tree corresponding to corresponding split catalog peer of this node institute split catalog, the brotgher of node pointer that then makes this node is for empty, otherwise, one of the brotgher of node pointed that makes this node corresponding to this node corresponding split catalog catalogue at the same level and be not included in node in the described directory tree.
Step B is described utilize recursive algorithm in described directory tree, set up with all split catalogs of described grouped data one by one all nodes of mapping comprise step:
C, make that newly-established node is a present node, and judge whether the pairing split catalog of present node has the next stage split catalog, if have, the data pointer that present node then is set is for empty, set up a new node again, the new node that the child nodes pointed that makes present node is set up in this step, and be provided with described new node corresponding to present node a next stage split catalog of corresponding split catalog, return this step C then; Otherwise the child nodes pointer that present node is set is for empty, and the data pointer of present node is pointed to the memory headroom that is used to preserve the grouped data that the corresponding split catalog of present node comprised, execution in step D then;
D: judge whether the pairing split catalog of present node has the split catalog at the same level that does not join in this directory tree, if have, then set up a new node, the new node that makes this step of brotgher of node pointed of present node be set up, and be provided with described new node corresponding to present node a split catalog at the same level that does not join in this directory tree of corresponding split catalog, return step C then; Otherwise, turn back to present node the node of corresponding split catalog upper level split catalog correspondence, and judge whether this node is root node, if, then finish, otherwise, make that this node is a present node, and return this step D.
The method of the invention further comprises adds grouped data to certain split catalog, is specially:
A1: in described directory tree, find node that should split catalog according to the split catalog that will add to;
A2: first pointer element that this node data pointer array of pointers pointed is set is current pointer element;
A3: judge whether current pointer element memory headroom pointed is empty, if, then in internal memory, ask the memory headroom of a fixed size, and make this pointer element point to the memory headroom of new application, carry out A4 then; Otherwise, judge whether current pointer element memory headroom pointed is full, if it is the next pointer element of array of pointers that the current pointer element then is set, and returns this step then; Otherwise, execution in step A4;
A4: in the memory headroom of current pointer element directed, add the assortment data information that to add.
Based on the grouped data storage means based on directory tree of the present invention, the present invention gives a kind of split catalog querying method based on directory tree, and each node on the described directory tree comprises child nodes pointer, brotgher of node pointer and data pointer at least;
The querying method of described split catalog comprises:
A, find the pairing node of this split catalog described catalogue seeds according to the split catalog that will inquire about;
B, judge that whether the child nodes pointer of this node is empty, if then this node is joined in the container, then execution in step d; Otherwise, execution in step c;
C, read the whole left branch of this node by recursive algorithm and comprise all leaf nodes, and all leaf nodes that will read join in the container successively, then execution in step c;
D, read leaf node successively from described container, the memory headroom according to reading leaf node data pointer points to obtains all grouped datas of storing in the described memory headroom, returns to the user.
Each node on the directory tree of the present invention further comprise be used to identify this node the node identification of corresponding split catalog, when setting up each node, make the class indication of the node identification of each node by the corresponding split catalog of this node;
Described method further comprises: in internal memory, generate the node identification and the mapping table that points to this node place physical memory space pointer of a split catalog corresponding node in advance, and the mapping relations between the pointer of the node identification of when setting up each node, in the mapping table that generates in advance, preserving this node and this node place memory headroom of sensing;
Step a is described to find the node at described this split catalog place to comprise according to the split catalog that will inquire about, browse:
A1, in described mapping table, find the pointer of the node place memory headroom that points to the described split catalog correspondence that will inquire about;
A2, directly navigate to the pairing node of the described split catalog that will inquire about, browse by described this pointer.
This shows, use the y-bend directory tree to replace existing many fork directory tree storage classified informations can obtain following beneficial effect:
At first, owing to each size of node of y-bend directory tree is fixed, and occupation space is very little, therefore, use y-bend directory tree storage classified information save memory resource greatly, make internal memory can store more grouped data, the storage efficiency of the internal memory of raising.
In addition, because the moving of the generation of binary tree and node, increase, the deletion action algorithm is simple, expense is little, and in the process that certain node is moved, increases or deletes, do not need to consider problems such as the distribution of his father's node storage space and recovery, make and use the y-bend directory tree grouped data to be managed simple.
Embodiment
For making purpose of the present invention, technical scheme and advantage clearer, below with reference to the accompanying drawing embodiment that develops simultaneously, the present invention is described in further detail.
Consider that binary tree is a kind of data structure that is widely used in data search, its generation and node searching scheduling algorithm comparative maturity, and fast convergence rate, therefore core concept of the present invention is to utilize binary tree structure storage grouped data.
A preferred embodiment of the present invention has provided the structure of each node of binary tree structure directory tree (being called for short the y-bend directory tree) that is used for storing grouped data, as shown in Figure 2, each node comprise be used to identify this node the node identification (ID) of corresponding split catalog, a child nodes pointer that points to this node next stage child node, brotgher of node pointer that points to another node in this node brother of node and sensing are used to preserve the data pointer of grouped data memory headroom.
The method of the above-mentioned y-bend directory tree storage of the described use of a preferred embodiment of the invention grouped data mainly may further comprise the steps as shown in Figure 3:
A: set up root node, promptly in internal memory, open up a block space and store the node ID of this root node, its child nodes pointer, brotgher of node pointer and data pointer, the node ID that this root node is set is 0, makes its brotgher of node pointer be empty (NULL), and data pointer also is NULL.
B: set up a new node, the new node that makes this step of child nodes pointed of described root node be set up, and to make the node ID of described new node be the class indication of an one-level split catalog.
Described here class indication is a sign predetermined, can each split catalog of unique identification grouped data, for example, in the present embodiment, the class indication of one-level split catalog computer is 1 in the goods catalogue that the front was described, the class indication of bullion is 2, and the class indication of mobile phone is 3; The class indication of secondary classification catalogue notebook is 11, and the class indication of desktop computer is 12; The class indication of reclassify catalogue IBM is 111, and the class indication of DELL is 112 or the like.Like this, the class indication by the ID of present node is set to certain split catalog just can be mapped present node and a split catalog.Need to prove that the sign of each split catalog can adopt any form in the present invention,, and be not limited to the form that above preferred embodiment provides as long as can each split catalog of unique identification.In this step, the node ID that described new node is set is that the class indication of an one-level split catalog comes down to described new node and an one-level split catalog are mapped.
This step is described, and to set up the described method of setting up root node of new node and steps A identical, promptly opens up a block space and store the node ID of this node, child nodes pointer, brotgher of node pointer and data pointer in internal memory.
In follow-up step, will create the y-bend directory tree that is used to store described grouped data by recursive algorithm, specifically comprise:
C: make that described new node is a present node, judge whether the pairing split catalog of present node has the next stage split catalog, if having, execution in step D then; Otherwise, execution in step E.
D: the data pointer that present node is set is NULL, set up a new node again, make the new node of the child nodes pointed of present node in the foundation of this step, and the class indication of the node ID of described new node by a split catalog in the next stage split catalog of the corresponding split catalog of present node be set, be about to described new node corresponding to present node a next stage split catalog of corresponding split catalog, return step C then.
In this step, if described present node is corresponding to a n fraction class catalogue, the class indication of the node ID of the described new node n+1 fraction class catalogue that will be comprised for this n fraction class catalogue then, the corresponding secondary classification catalogue of present node notebook for example, then the node ID of described new node just can be the class indication 111 of reclassify catalogue IBM or the class indication 112 of reclassify catalogue DELL.
E: the child nodes pointer that present node is set is NULL, and the data pointer of present node is pointed to the memory headroom that is used to preserve the grouped data that the corresponding split catalog of present node comprised, execution in step F then.
The memory headroom that this step is described to be used to preserve the grouped data that the corresponding split catalog of present node comprised can be size arbitrarily and memory headroom that can dynamic expansion.
In another preferred embodiment of the present invention, carry out the memory source management for convenience, in this step, an array of pointers can be set further, and the data pointer of present node pointed to this array of pointers, wherein, each pointer element of this pointer data will be pointed to the memory headroom of fixed size respectively, and the memory headroom of described fixed size will be used to write down the grouped data that the pairing split catalog of each leaf node is comprised.How many number n of pointer element that described array of pointers comprises can according to the preestablishing of grouped data, and such as being set at 10, each leaf node just can have the memory headroom of 10 fixed sizes like this.But be not when each leaf node is created, to be this leaf node application n piece memory headroom with regard to unification, but demand assigned, when a memory headroom is filled with, just can apply for next memory headroom, and each application only distributes a memory headroom.Above-mentioned employing fixed size memory headroom and demand assigned method mainly are in order to improve Memory Allocation efficient, needed dynamic expansion operation to memory headroom when avoiding adopting size memory headroom storage arbitrarily grouped data effectively.
F: judge present node whether have in all catalogues at the same level of corresponding split catalog and do not join in this y-bend directory tree, if having, execution in step G then; Otherwise, execution in step H.
G: set up a new node, the new node that makes this step of brotgher of node pointed of present node be set up, and the class indication that do not add at the same level catalogue in this y-bend directory tree of the node ID of described new node by the corresponding split catalog of present node be set, be about to described new node corresponding to present node a split catalog at the same level of corresponding split catalog, return step C then.
In this step, if described present node is corresponding to a n fraction class catalogue, then the node ID of described new node will be the class indication of another n fraction class catalogue of not adding described y-bend directory tree, the corresponding secondary classification catalogue of present node notebook for example, then the node ID of described new node just can be the class indication 12 of the secondary classification catalogue desktop computer that do not add this y-bend directory tree or the class indication 13 of secondary classification catalogue office equipment.
H: turn back to present node the node of corresponding split catalog upper level split catalog correspondence, and judge whether this node is root node, if then finish; Otherwise, execution in step I.
I: make that this node is a present node, and return step F.
By carrying out above-mentioned steps A~I, the y-bend directory tree that is used to store grouped data has been set up and has been finished, and all nodes of described y-bend directory tree will with all split catalogs mapping one by one of described grouped data.Fig. 4 has shown the goods catalogue synoptic diagram that adopts above-mentioned y-bend directory tree storage.As shown in Figure 4, the child nodes pointer of root node will point to the extensive stock data and obtain an one-level split catalog after through a subseries, for example the one-level child node of computer catalogue correspondence.The child nodes pointer of this one-level child node will point to for example secondary child node of notebook catalogue correspondence of a secondary classification catalogue that this one-level split catalog obtains through secondary classification; And its brotgher of node pointer will point to another one-level split catalog, and for example the one-level child node of bullion catalogue correspondence is so analogized.Leaf node in the above-mentioned y-bend directory tree, the data pointer of the node of branch class catalogues such as for example corresponding IBM or DELL will point to an array of pointers, the pointer element of described array of pointers will be pointed to the memory headroom of storage merchandise news that each split catalog comprises, i.e. physical memory piece.
Be described in detail in the method for inquiring about the grouped data that certain split catalog comprised in the above-mentioned y-bend directory tree below in conjunction with Fig. 5, as shown in Figure 5, this method mainly may further comprise the steps:
A1, in the y-bend directory tree, find the pairing node of described this split catalog according to the class indication of the split catalog that will inquire about or browse;
Since the node ID of each node be exactly this node the class indication of corresponding split catalog, the described method of searching corresponding node of this step can be that the ID with this split catalog travels through described y-bend directory tree as index, finds the node of described split catalog correspondence;
A2, judge whether the child nodes pointer of this node is NULL, if then carry out a3; Otherwise execution in step a4;
A3, this node is joined in the container, then execution in step a5;
The described container of this step is an interim memory headroom that is used for temporarily depositing leaf node;
A4, read the whole left branch of this node by recursive algorithm and comprise all leaf nodes, and all leaf nodes that will read join in the container successively, then execution in step a5;
A5, read leaf node successively from described container, the memory headroom according to reading leaf node data pointer points to obtains all grouped datas of storing in the described memory headroom, returns to the user.
If what the data pointer of the leaf node of above-mentioned y-bend directory tree pointed to is an array of pointers, also need further to find the memory headroom of storage grouped data at step a5 by this array of pointers.
Next the method for adding grouped data in the above-mentioned y-bend directory tree, the method for for example adding certain merchandise news to one split catalog will be described in detail in.
If the data pointer of the leaf node of above-mentioned y-bend directory tree points to be size arbitrarily and memory headroom that can dynamic expansion, then at first find the node at described this split catalog place according to the class indication of the split catalog that will add to; The grouped data that needs are added is added in this node data pointer memory headroom pointed then.If this memory headroom is full, also need at first by internal memory dynamic expansion operation this node data pointer of expansion memory headroom pointed.
If what the data pointer of the leaf node of above-mentioned y-bend directory tree pointed to is an array of pointers, then need to carry out following steps, as shown in Figure 6:
B1: the node that in the y-bend directory tree, finds described this split catalog place according to the class indication of the split catalog that grouped data will be added to;
The method that this step adopted is identical with the described method of step a1;
B2: first pointer element that this node data pointer array of pointers pointed is set is current pointer element;
B3: judge whether current pointer element memory headroom pointed is NULL, if, execution in step b4 then; Otherwise, execution in step b6;
B4:, and make this pointer element point to the memory headroom of new application, execution in step b5 then to the memory headroom of a fixed size of internal memory application;
B5: in current pointer element memory headroom pointed, add the assortment data information that to add, then end;
B6: judge whether current pointer element memory headroom pointed is full, if, execution in step b7 then; Otherwise, execution in step b5;
B7: it is the next pointer element of array of pointers that the current pointer element is set, and returns step b3 then.
In this step, if the current pointer element is last pointer element of array of pointers, and the memory headroom of its sensing is full, then can't add the respective classified data in current directory tree, should return error message and give the user.
From above preferred embodiment as can be seen, because each size of node of y-bend directory tree is fixed, and occupation space is very little, therefore, use the y-bend directory tree to replace many fork directory tree storage classified informations save memory resource greatly, thereby can store more grouped data, and obtain higher storage efficiency.In addition; because the moving of the generation of binary tree and node, increase, the deletion action algorithm is simple, expense is little; and in the process that certain node is moved, increases or deletes; only need to revise the pointed of its father node; and do not need to consider problems such as the distribution of his father's node storage space and recovery; it is simple to make that use y-bend directory tree manages grouped data, can point to the alter operation of split catalog under the environment of the normal operation of system, and not need to shut down.
Through test, in the application of reality, if 1,000 ten thousand commodity are arranged, have the split catalog of 2000 minimums, use the method for above-mentioned y-bend directory tree, the node that system may exist, comprise that leaf node and non-leaf node are 3000 altogether, each node occupation space is about 16 bytes (node ID wherein, the child nodes pointer, each needs 4 bytes brotgher of node pointer and data pointer), and every commodity only need 4 bytes of memory spaces under the situation that only storing commodity identifies, the needed total memory headroom of so all nodes and commodity is about 3000 * 16+10,000,000 * 4=40.048 megabyte.Can all leave in the calculator memory space fully, thereby can greatly improve the recall precision of data.
Inquiry in above-mentioned y-bend directory tree, browse the method for the grouped data that certain split catalog comprises and in the y-bend directory tree, add in the method for grouped data, all need at first to find the node at described this split catalog place according to the class indication of split catalog by the method for traversal y-bend directory tree, this traversing operation will expend the more time.
Search each split catalog for convenience, another preferred embodiment of the present invention generates the node ID and the mapping table that points to this node place physical memory block pointer of a split catalog corresponding node in advance in internal memory, and generate in the process of described y-bend directory tree carrying out above-mentioned steps A~I, further carry out following operation at step C: the mapping relations between the node ID of in the mapping table that generates in advance, preserving the described present node of step C and the pointer of sensing present node place memory headroom.
Like this, since the node ID of each node be exactly this node the class indication of corresponding split catalog, therefore, in above-mentioned y-bend directory tree, inquire about, browse the method for the grouped data that certain split catalog comprises and in the y-bend directory tree, add in the method for grouped data can be at first directly according to the class indication of the split catalog that grouped data will be added to, in the node ID of described split catalog corresponding node and the mapping table that points to this node place physical memory block pointer, find the pointer that points to the corresponding node place memory headroom of this split catalog, directly navigate to the node of described split catalog correspondence then by described pointer, and need not to carry out y-bend catalogue traversal of tree.For example, 1,000 ten thousand commodity are arranged now, be distributed in below 1000 leaves, like this, 10,000 commodity are arranged under each leaf node on average.If the user need browse certain split catalog, 200,000 commodity have been comprised under this split catalog, can directly navigate to the corresponding node of this split catalog by the mapping relations between node ID of preserving in advance and the pointer that points to this node place memory headroom, therefore, the commodity sum that system need retrieve has only 20W, and do not need from whole 1,000 ten thousand commodity, to filter, and, the split catalog of next stage is searched in the every selection of user, the commodity sum of required retrieval all can reduce fast, thereby improved the search efficiency of node widely, further improved the efficiency of management of grouped data.