Thread: Re: [GENERAL] Tree structure

Re: [GENERAL] Tree structure

From
"K.T."
Date:
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@webline.dk>
To: pgsql-general@postgreSQL.org <pgsql-general@postgreSQL.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?
>
>