473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

algorithm help

Hey everyone,

I'm having a little trouble writing an algorithm to create a
hierarchal structure from a database which looks like this:

Code:

parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |
So coming out of the database, I get an array which looks like this:

Code:

Array
(
[0] =Array
(
[parent_id] =1
[child_id] =2
)

[1] =Array
(
[parent_id] =2
[child_id] =3
)

[2] =Array
(
[parent_id] =3
[child_id] =4
)
....
I would like to put this into an array which represents the following
structure:

Code:

1
\
2
\
3
/ \
4 5
/ \
6 7
Can anyone point me in the right direction? Let me know if this isn't
clear - I can try to elaborate.

Thanks!

Ben

Oct 19 '07 #1
4 1574
On 19 Oct, 20:06, uidzer0 <ben.lemasur...@gmail.comwrote:
Hey everyone,

I'm having a little trouble writing an algorithm to create a
hierarchal structure from a database which looks like this:

Code:

parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |
<snip>
>
1
\
2
\
3
/ \
4 5
/ \
6 7
Some relational DBMS provide tree walking SQL extensions (like
Oracle's 'connect by') but they're not portable, and you don't say
what DBMS you use.

You're data already *represents* that structure - just perhaps not in
a very useful way.

Its probably a lot simpler if you do it in PHP code. There are any
number of ways to solve this, but its worth observing that you're code
is probably an over-simplification of the data structure. e.g. you
might go with a right threaded binary tree:

// this is a template of the node structure
$node=array( $current=-1, $next=-1, $first_child=-1,
$payload=array() );
// e.g. 5,4,7, blah
// 7,6,-1,foo
// 6,5,-1,bar
// 4,3,-1,tog
// 3,2,5, zip
// 2,1,3
Which work very nicely when you want to dynamically tinker with the
structure and perform lots of tree walks (hint: use recursive
functions). And they scale really nicely.

Or simply use nested arrays. Do you really need someone to explain how
to code this? Admittedly a single pass algorithm is a little tricky,
but if you can't get your head around a multipass solution, you're
going to be back here a lot asking for help.

C.

Oct 19 '07 #2
uidzer0 <be************@gmail.comwrote:
I'm having a little trouble writing an algorithm to create a
hierarchal structure from a database which looks like this:
parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |
I would like to put this into an array which represents the following
structure:
1
\
2
\
3
/ \
4 5
/ \
6 7
if you understand french language :

<http://sql.developpez.com/arborescence/>

--
@@@@@
E -00 comme on est very beaux dis !
' `) /
|\_ =="
Oct 19 '07 #3

"The Natural Philosopher" <a@b.cwrote in message
news:11****************@proxy02.news.clara.net...
uidzer0 wrote:
>Hey everyone,

I'm having a little trouble writing an algorithm to create a
hierarchal structure from a database which looks like this:

Code:

parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |
So coming out of the database, I get an array which looks like this:

Code:

Array
(
[0] =Array
(
[parent_id] =1
[child_id] =2
)

[1] =Array
(
[parent_id] =2
[child_id] =3
)

[2] =Array
(
[parent_id] =3
[child_id] =4
)
...
I would like to put this into an array which represents the following
structure:

Code:

1
\
2
\
3
/ \
4 5
/ \
6 7
Can anyone point me in the right direction? Let me know if this isn't
clear - I can try to elaborate.

Thanks!

Ben


Try a recursive call to the database..
that's wretched! your db is needlessly put through the ringer!

if you're going to have the db do it, then put the recursion in a udf in the
db. that way there's only one call to the db and one result set returned.
plenty of examples of this on the net for any db you want to use.
Oct 22 '07 #4
On 19 Oct, 20:06, uidzer0 <ben.lemasur...@gmail.comwrote:
Hey everyone,

I'm having a little trouble writing an algorithm to create a
hierarchal structure from a database which looks like this:

Code:

parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |

So coming out of the database, I get an array which looks like this:

Code:

Array
(
[0] =Array
(
[parent_id] =1
[child_id] =2
)

[1] =Array
(
[parent_id] =2
[child_id] =3
)

[2] =Array
(
[parent_id] =3
[child_id] =4
)
...

I would like to put this into an array which represents the following
structure:

Code:

1
\
2
\
3
/ \
4 5
/ \
6 7

Can anyone point me in the right direction? Let me know if this isn't
clear - I can try to elaborate.

Thanks!

Ben
http://del.icio.us/Captain_Paralytic/hierarchical

Oct 22 '07 #5

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

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
17
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
6
by: Bernie Yaeger | last post by:
I need a little help with an algorithm. Let's say I have an array with 15 items. I want to find "Fern" where there are "Able", "Me Also", "Zimmerman", etc in no particular order. Forget about...
10
by: Nemok | last post by:
Hi, I am trying to write an additive encryption algorithm in C++ that will encrypt a text by adding a random numer to each character in a string. The code looks similar to this: for(int...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
6
by: StephQ | last post by:
I need to implement an algorithm that takes as input a container and write some output in another container. The containers involved are usually vectors, but I would like not to rule out the...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.