473,388 Members | 1,215 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,388 software developers and data experts.

Converting an existing structure to a tree structure

Hello, I was wondering how I would convert an existing structure to a tree. What I want is basically a family tree. What I have is the following:

array (

array(id => 123, p1 => 567, p2 => 765),
array(id => 321, p1 => 567, p2 => 890),
array(id => 333, p1 => 123, p2 => null),
array(id => 444, p1 => null, p2 => null),
etc.

);

It's an array of associative arrays showing each element in the family tree as well the their parent(s). An element can have one, two, or zero parents. If they have one parent, it is ALWAYS p1. If an element has no parents, it is not necessarily the root, because if there are two parents, one parent can have no parents and the other can still have one or two. And of course, there doesn't necessarily have to have just one root.

What I ultimately want is to display the tree graphically, but I am way too dumb to be able to convert this into a tree structure. I actually saw a post on this site asking a similar question (http://bytes.com/topic/php/answers/4403 -algorithm-creating-tree-structure-linke d-list), but it only deals with one parent. It might be a simple transition to add another parent, but I'm just not grasping this very well. If anyone has any insight into this or can point me in the right direction it would be greatly appreciated.

Thanks.
Jun 3 '10 #1
3 2378
ok, doing some research I realize that because a child can have more than 1 parent this is not a tree, but rather a graph.
Jun 3 '10 #2
Atli
5,058 Expert 4TB
Family structures aren't really trees either. They are more like webs. What allows us to draw them as trees is only focusing on a single string of that web, and only drawing "related" connections, while ignoring branches that go outside that.

Meaning, in a typical family tree you see a target at the top; the root, and it's descendants. What you don't see is it's parents, or links to the extended family of external influences (mates). Drawing *all* the connections would be far to extensive to actually make sense on a simple tree.

And I am only talking here about a family, where you can be fairly certain descendants are only connected to the root via the tier above them, when in actuality there is nothing forbidding a distant descendant from connecting directly to the root, which would be near impossible to plot on a family-tree-like structure.

What you need to do is figure out which part of the structure you want to plot. What exactly do you want to see on you graph? What is the purpose of your drawing?

For example, if my array contained the following data:
Expand|Select|Wrap|Line Numbers
  1. array(
  2.     array(id => 1, p1 => null, p2 => null),
  3.     array(id => 2, p1 => 1, p2 => 100),
  4.     array(id => 3, p1 => 1, p2 => 2),
  5.     array(id => 4, p1 => 3, p2 => 1),
  6.     array(id => 5, p1 => 100, p2 => 4)
  7. )
If I were to draw this, it might look something like:
Name:  web1.jpeg
Views: 388
Size:  3.3 KB
Drawing all the links, you couldn't really draw this as a normal family tree, and it would be nearly impossible to draw this as something like a file-tree (some nodes would have to appear in two categories). At least not without filtering out links that aren't needed.

If my goal was to select all direct descendants of the entry with the ID 1, I could simply ignore all links that aren't needed for that purpose. I would start at the top, listing all direct descendants and ignoring parent entries that aren't needed, and work my way down each branch. That could leave me with this:
Name:  desc1.jpeg
Views: 376
Size:  1.8 KB

The question is, what do you need to see? Once you figure that out you can start filtering the data to achieve that.
Jun 3 '10 #3
Thanks. Basically, something that makes this a lot simpler than it could be is that I would be drawing a "tree" relative to only one node. For example, I would be going to the web page for node X, and the tree would be relative to that node. In other words, we would see the parents of X, the parent's parents, and so forth. We would also see siblings of X, and children and descendants of X. However, we would not need to see the parents of siblings (unless it is one of X's parents), and we would not need to see parents of descendants (unless they are X).
Jun 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
1
by: Srihari | last post by:
I'm trying to develop a tree structure using javascript. The node values of the tree are generating from a mysql table depending on login. The tree structure contains 3 sub levels. I developed...
4
by: pearsons_11114 | last post by:
Newbie to .NET from Java, have a question about requirements/conventions for source tree layout. In Java there a convention-bordering-on-requirement that a source file's location in the source tree...
0
by: Matthias Langbein | last post by:
Hi all, I'm currently developing a WebService proxy for existing services. The existing services expect Java DTOs. I want to do the transformtion with JibX, so there is my question: The...
9
by: Steve Edwards | last post by:
Hi, I have a class which contains a single ptr to another instance which is considered its parent, and an array of ptrs to its children class Nodes{ .... Nodes *mSuper;
8
by: =?ISO-8859-1?Q?m=E9choui?= | last post by:
Problem: - You have tree structure (XML-like) that you don't want to create 100% in memory, because it just takes too long (for instance, you need a http request to request the information from...
4
by: Anni V | last post by:
Hi I request you to kindly help me with the code for a reusable tree structure component that displays a tree view as displayed in the attachment. Consider a table named : Employee with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...

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.