Re: [GENERAL] Tree structure

From: "K(dot)T(dot)" <kanet(at)calmarconsulting(dot)com>
To: "Kaare Rasmussen" <kar(at)webline(dot)dk>, <pgsql-general(at)postgreSQL(dot)org>
Subject: Re: [GENERAL] Tree structure
Date: 1999-02-26 11:44:50
Message-ID: 001001be617d$6c5ea1e0$6adaa5ce@p2-400-death
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Its been a while since I wrote this and its kinda fuzzy at this hour, but
this will give you a general direction to go and you can work out the
specifics...
If you know the depth of the tree then you can create a field of a specified
length and store something like:

tree_field varchar(16)
A
AA
AB
ABA
ABB
ABC
AC

This allows you to select all records that are in level "B" fairly easily
with a "like" clause. Note that everything has a root of "A".

Or you can create two fields:

Parent/Child
A/B
B/E
E/G
B/F
A/C
A/D

This one is confusing and there is no easy way to show the tree in text.
You cannot use the same number for a node. eg. if your first level is "A"
then none of your sub levels can be "A". So use the sequence generator to
generate a unique key for each node. Then your select would look something
like this:
Select * from hierachy_table where parent_key = child_key and parent_key
= "A";
This should give you all branches under the "A" node. To get the
indention...hmmm try ordering the records by parent/child and writing code
that loops thru the records? That one stumps me...gotta sleep on it :)

DISCLAIMER: This might not be 100% accurate...its a seat of the pants
memory. Anyone who knows this please feel free to correct my errors :)

-----Original Message-----
From: Kaare Rasmussen <kar(at)webline(dot)dk>
To: pgsql-general(at)postgreSQL(dot)org <pgsql-general(at)postgreSQL(dot)org>
Date: Friday, February 26, 1999 3:02 AM
Subject: [GENERAL] Tree structure

>I can't figure this one out. I need a tree structure like this
>
>Number Pointer
>1 0
>2 1
>3 1
>4 2
>5 0
>6 1
>7 5
>
>This should somehow show up like this
>Number
>1
>2
>4
>3
>6
>5
>7
>
>The whole excercise is because I'd like to show a tree structure:
>
>1
>- 2
>- - 4
>- 3
>- 6
>5
>- 7
>
>Is this possible with PostgreSQL?
>
>

Browse pgsql-general by date

  From Date Subject
Next Message Roderick A. Anderson 1999-02-26 13:40:27 SQL/Relational Design Text Book recommendations
Previous Message Peter T Mount 1999-02-26 11:00:30 Re: [GENERAL] Tree structure