473,473 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help with celko's tree model

hello i have implemented joe celko's model to store heirarchy and it
works well and nice
i have a question about SQL

this is the actual table

member side left right
------------------------------------------
nancy L 1 36
andrew L 4 21
steven R 5 12
ina L 6 7
david R 10 11
margaret L 13 20
ann R 14 15
laura L 18 19
janet R 24 35
michael L 25 30
dan R 26 27
ron L 28 29
robert R 33 34
the Side column is to tell its left, or right. this is a binary
heirarcy.
i have this problem i have to solve, im still banging my head. If
given the member

'Nancy' , i need to find left-most(Laura) and right-most(Robert)
'Janet' = left most is ron, right most is robert
'Andrew = left most is laura, right most is David
Hope u get my plan. could u help me with the sql ?
Jul 20 '05 #1
3 3025
On 24 Oct 2004 23:04:40 -0700, Nick Chan wrote:
hello i have implemented joe celko's model to store heirarchy and it
works well and nice (snip)the Side column is to tell its left, or right. this is a binary
heirarcy.
Hi Nick,

You seem to have a slight misunderstanding about Joe Celko's nested set
model. A bit of googling got me this quote (from a message in another
newsgroup, written by Joe Celko himself):

"The nested set model has an implied ordering of siblings which
the adjacency list model does not."

From the context, it was clear that Joe meant that for each pair of
siblings, the one with the lower lft and rgt values should be considered
to be to the left of the one with the higher lft and rgt values.

I've drawn a picture of your hierarchy with the "left" descendant always
on the left side and the "right" descendant always on the right side; then
I re-assigned the lft and rgt numbers according to Joe Celko's nested set
model. The result looks like this:

member lft rgt
-------------------------
nancy 1 26
andrew 2 15
margaret 3 8
laura 4 5
ann 6 7
steven 9 14
ina 10 11
david 12 13
janet 16 25
michael 17 22
ron 18 19
dan 20 21
robert 23 24

i have this problem i have to solve, im still banging my head. If
given the member

'Nancy' , i need to find left-most(Laura) and right-most(Robert)
'Janet' = left most is ron, right most is robert
'Andrew = left most is laura, right most is David


After re-arranging the lft and rgt values as above, it's not too hard
anymore:

SELECT o.member AS member, l.member AS leftmost, r.member AS rightmost
FROM hierarchy AS o
INNER JOIN hierarchy AS l
ON l.rgt = (SELECT MIN(rgt)
FROM hierarchy
WHERE lft BETWEEN o.lft AND o.rgt)
INNER JOIN hierarchy AS r
ON r.lft = (SELECT MAX(lft)
FROM hierarchy
WHERE lft BETWEEN o.lft AND o.rgt)
WHERE o.member IN ('nancy', 'janet', 'andrew')
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
>> I have implemented Joe Celko's model to store heirarchy and it
works well and nice I have a question about SQL <<

You might want to buy a copy of my book TREES & HIERARCHIES IN SQL :)
This is the actual table .. the Side column is to tell its left, or right. This is a binary heirarchy. <<

I have a better model for binary trees! Remember "heapsort" from your
first data structures/algorithms class in college?

CREATE TABLE Heap
(member CHAR(15) NOT NULL,
place INTEGER NOT NULL PRIMARY KEY
CHECK(place > 1);

root = 1
place of left child of member (n) = (2*n)
place of right child of member (n) = (2*n +1)

Use integer division to travel toward the root of the tree.

('Somebody', 2, 3) is missing in the data you posted, but it looks
like ('nancy', 1) is the root of the tree.
'Nancy', I need to find left-most(Laura) and right-most(Robert) <<


Is this correct? I keep going left (or right) until I get to a leaf
node in the tree or to a node without a left (or right) child:

A
/ \
/ \
B C
/ \
/ \
D E

leftmost(A) = B
rightmost(A) = E
leftmost(C) = D
rightmost(C) = E
leftmost(D) = NULL
rightmost(D) = NULL

So the leftmost(n) member is MAX(2* .. (2*n) ..) if the whole
generated set is in the heap. The rightmost(n) member is MAX(2* ..
(2*n+1) ..+1) +1) if the whole generated set is in the heap.

You can use a loop, recursion or a look-up table for the math.
Jul 20 '05 #3
Saw the first part of your question on experts-exchange and asked a
buddy of mine (Corey Aldebol) how his tree model would work for your
problem. Take a look here for his reply:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=41772

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4

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

Similar topics

18
by: Guinness Mann | last post by:
Greetings, I think I saw an article yesterday in which you told someone not to make an Identity column a primary key. I seem to remember you saying something like "It undermines the entire...
28
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below...
4
by: Angel Cat | last post by:
I have 2 tables joined together by the IDs, People and the pets they own PEOPLE ID NAME 1 JohnSMith 2 JaneDoe PETS ID PET
7
by: grawsha2000 | last post by:
Hi, I'm designing a simple database for filing system: There are two levels of files (both look_up tables): tlkpFile1, tlkpSubFile1 and a transaction table, tblFilings, for filings (when...
2
by: Elliot | last post by:
I'm having trouble structuring the data in an investment database. The basics are: * Investors invest in Deals * Investors can be either Individuals or Entities * Entities may or may not...
8
by: Dip | last post by:
Hello Experts, Here is the code to flatten a PC hierarchy into a level based table. It works fine. SELECT t1.TASK_ID AS TASK_LV1, t2.TASK_ID AS TASK_LV2, t3.TASK_ID AS TASK_LV3, t4.TASK_ID AS...
22
by: pbd22 | last post by:
hi. I am having probelms with an update statement. every time i run it, "every" row updates, not just the one(s) intended. so, here is what i have. i have tried this with both AND and OR and...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
11
by: lenygold via DBMonster.com | last post by:
I am tryieng to convert our time consuming recursive queries too very efficient queries based on nested set model. The only problem is to convert an adjacency list model into a nested set model,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
1
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
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.