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

Implementing a tree

Hi all,

I'd like to implement a tree of "tags" for a blog I'm writing for fun in PHP.
Here's what a single tag looks like:
CREATE TABLE tags
(
name varchar(30) not null default '',
id_self integer(12) not null primary key,
id_parent integer(10) not null default 0,
);
INSERT INTO tags VALUES ('root of the tree', 0, 0);
Each tag has a name, a unique id to identify itself with and a parent's id,
and all this will be stored in a database, but stored in no particular order.

I'm a little stumped as to how to reconstruct the tree. Part of the problem
is that suppose my first read to the database yeilds:

name = "physics"
id_self = 21
id_parent = 2
but further down in the database, this record exists:

name = "science"
id_self = 2
id_parent = 0

in other words, it's possible that children may be read before parents.
I've noticed that some people have implemented a tree class. Since this is
supposed to be a *fun* project for me, I'd rather write it myself. But I
find myself staring at the keyboard, not knowing how to start.

How can I reconstruct this tree?

Thanks!
Pete
Sep 16 '05 #1
2 1727
nospam said the following on 16/09/2005 18:55:
Hi all,

I'd like to implement a tree of "tags" for a blog I'm writing for fun in PHP.
Here's what a single tag looks like:
CREATE TABLE tags
(
name varchar(30) not null default '',
id_self integer(12) not null primary key,
id_parent integer(10) not null default 0,
);
INSERT INTO tags VALUES ('root of the tree', 0, 0);
Each tag has a name, a unique id to identify itself with and a parent's id,
and all this will be stored in a database, but stored in no particular order.

I'm a little stumped as to how to reconstruct the tree. Part of the problem
is that suppose my first read to the database yeilds:

name = "physics"
id_self = 21
id_parent = 2
but further down in the database, this record exists:

name = "science"
id_self = 2
id_parent = 0

in other words, it's possible that children may be read before parents.

One simple way round this would be to load each tree element into a flat
array at first, each element keyed by its own ID. Then loop through
every element in turn, get its parent's ID, and then call an addChild()
function or something, e.g:

class Node
{
public $id;
public $parentId;
public $children;

public function addChild($childObject)
{
$children[$childObject->id] = $childObject;
}
}

// assume that $flatArray is the array of unconnected Nodes that have
// been constructed from the database query.

foreach ($node in $flatArray)
{
$flatArray[$node->parentId]->addChild($node);
}
(This example assumes you're using PHP 5).

--
Oli
Sep 16 '05 #2
Oli Filth said the following on 16/09/2005 19:10:
nospam said the following on 16/09/2005 18:55:
Hi all,

I'd like to implement a tree of "tags" for a blog I'm writing for fun
in PHP.
Here's what a single tag looks like:
CREATE TABLE tags
( name varchar(30) not null default '',
id_self integer(12) not null primary key,
id_parent integer(10) not null default 0,
);
INSERT INTO tags VALUES ('root of the tree', 0, 0);
Each tag has a name, a unique id to identify itself with and a
parent's id,
and all this will be stored in a database, but stored in no particular
order.

I'm a little stumped as to how to reconstruct the tree. Part of the
problem
is that suppose my first read to the database yeilds:

name = "physics"
id_self = 21
id_parent = 2
but further down in the database, this record exists:

name = "science"
id_self = 2
id_parent = 0

in other words, it's possible that children may be read before parents.
One simple way round this would be to load each tree element into a flat
array at first, each element keyed by its own ID. Then loop through
every element in turn, get its parent's ID, and then call an addChild()
function or something, e.g:

class Node
{
public $id;
public $parentId;
public $children;

public function addChild($childObject)
{
$children[$childObject->id] = $childObject;

^
Oops, that should be:
$this->children[$childObject->id] = $childObject; }
}

// assume that $flatArray is the array of unconnected Nodes that have
// been constructed from the database query.

foreach ($node in $flatArray)
{
$flatArray[$node->parentId]->addChild($node);
}
(This example assumes you're using PHP 5).

--
Oli
Sep 16 '05 #3

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

Similar topics

3
by: Br | last post by:
I'm going to go into a fair bit of detail as I'm hoping my methods may be of assistance to anyone else wanting to implement something similar (or totally confusing:) One of systems I've...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
2
by: dos.fishing | last post by:
I know what a binary space partition tree is, but could someone explain what a 1 dimensional BSP tree is? What is stored in the nodes and what is stored in the leaves? What order? Is it correct...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
2
by: poornima ramesh | last post by:
I want to add a refresh button on top of a page used in an application. This page consists of a tree like structure. So when the tree is completely expanded and some change is done on a object ,...
6
by: csharpula csharp | last post by:
Hello , I would like to build a tree structure in c# using the arraylist or hash table. what is the best way to implement it if I want to add children and to print my tree. Thank you! ***...
1
by: Christopher | last post by:
I hate to start from scratch as the need for it isn't as great as the work it would take. But, it sure would be nice if I had some data structure that represented a generic tree. I heard using...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
5
by: nandor.sieben | last post by:
I am using a small set of functions that implements an n-ary tree. The library is disscusses here: ...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.