473,750 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regenerate a Nested Set using parent_id structure

Hello,

I've got a Nested Set structure in MySQL 4 here with
- id
- lft
- rgt
- parent_id
- root_id

I wrote some test scripts and i discovered that the Nested Set (the
structure with lft and rgt nodes) is broken, there are some duplicate
nodes and other really bad stuff.

Luckily, the dude who developed the system has a parent_id-Structure
in the database too, and I'm pretty sure that the parent_id-
relationships are correct.

So, now my question: I'd need a Query or a PHP Script to generate a
correct node structure out of the parent_id-relationships. Any tips or
links?

Thanks guys!

Regards

Apr 26 '07 #1
1 9714
This from the guru himself (see http://tinyurl.com/2ufnrb):

To convert an adjacency list model into a nested set model, use a push
down stack algorithm. Assume that we have these tables:

-- Tree holds the adjacency model

CREATE TABLE Tree
(emp CHAR(10) NOT NULL,
boss CHAR(10));

INSERT INTO Tree
SELECT emp, boss FROM Personnel;

-- Stack starts empty, will holds the nested set model
CREATE TABLE Stack
(stack_top INTEGER NOT NULL,
emp CHAR(10) NOT NULL,
lft INTEGER,
rgt INTEGER);

BEGIN ATOMIC
DECLARE counter INTEGER;
DECLARE max_counter INTEGER;
DECLARE current_top INTEGER;

SET counter = 2;
SET max_counter = 2 * (SELECT COUNT(*) FROM Tree);
SET current_top = 1;

INSERT INTO Stack
SELECT 1, emp, 1, NULL
FROM Tree
WHERE boss IS NULL;

DELETE FROM Tree
WHERE boss IS NULL;

WHILE counter <= (max_counter - 2)
LOOP IF EXISTS (SELECT *
FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = current_top)
THEN
BEGIN -- push when top has subordinates, set lft value
INSERT INTO Stack
SELECT (current_top + 1), MIN(T1.emp), counter, NULL
FROM Stack AS S1, Tree AS T1
WHERE S1.emp = T1.boss
AND S1.stack_top = current_top;

DELETE FROM Tree
WHERE emp = (SELECT emp
FROM Stack
WHERE stack_top = current_top + 1);

SET counter = counter + 1;
SET current_top = current_top + 1;
END
ELSE
BEGIN -- pop the stack and set rgt value
UPDATE Stack
SET rgt = counter,
stack_top = -stack_top -- pops the stack
WHERE stack_top = current_top
SET counter = counter + 1;
SET current_top = current_top - 1;
END IF;
END LOOP;
END;

HIH

Apr 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3981
by: Jeff Lanfield | last post by:
First of all, I apologize if coalescing is not the right term to describe my problem. I have a tree where each node has the same set of attributes (is the same entity) but child nodes should inherit attribute values from parent node. for example, say I have the following table: (nodeId int , color varchar, phone varchar) with two rows 5, "GREEN", "555-1212"
11
2009
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
1
2684
by: Peter | last post by:
Hello, Thanks for reviewing my question. I would like to know if there is an easier way to regenerate your dataset in VS.NET when your database schema changes. I am frequently add or remove fields in my database and then on the VS.NET C# windows form side, I delete the dataset, the dataadapeter and the sqlconnection. Deleting just the dataset doesn't work - generating it from the dataadapeter creates a dataset with the old schema. Is...
8
16916
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
9
2397
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure that I use to communicate to some unmanaged code in a DLL written in C. It is not complicated, but will change and I would like to be able to sequentially access it without explicitly referring to each and every element. Here is the structure:
25
2253
by: GY2 | last post by:
I writing some documentation and I want to describe a common code structure which is used to step through all the items in a collection (e.g. each file in a subdirectory) while applying more and more restrictive filters so that only the desired items can fall all the way through. This method is so obvious and common it must have a name. What is it or at least, what is the best (short) way to describe it? For Each In If Then If Then
3
3926
by: | last post by:
I'm seeking (probably basic) guidance on the right way to split a large site that's supposed to represent one domain(mydomain.org) into many small VS.NET projects, and how to avoid issues with multiple web.config files leading to the error: "It is an error to use a section registered as allowDefinition = 'MachineToApplication'"... I'm fairly new to VS.NET and my sloppy first solution was to make one huge solution/project with just one...
0
1665
by: Scal | last post by:
Hello everyone; I try to create a web user control that has a Repeater (not that hard) to which I can add programmatically a new Repeater into ItemTemplate part. The point being to be able to build the following structure: http://pascalc.nougen.com/stuffs/tmp.html As you can see, basically I need to add a repeater to each ItemTemplate to add a LinkButton when in 1st level parent/child relation plus a TextBox control to put text in as...
8
5941
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to some functions. I have defined them in the following manner: typedef struct trans Transient; typedef struct sats Satellites;
0
9584
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9345
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8265
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6811
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4894
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.