473,398 Members | 2,335 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,398 software developers and data experts.

Pimp my function

My function GetTourDescendants works, but I do not really have a recursive
brain (and run from lisp like a vampire from a crucifix) and am a little
wary of recursion in PHP. And besides, the function is ugly as sin. So,
this question is not will this work (it does) or how to make it work (it
does), but whether there is a better way or not to do this.

A tour may have any number of children, but in most cases have no more
than dozen (the tour with the greatest number of children has about 200).
Child tours also may have children, and so forth, but the number of
"generations" is not, in practice, more than six.

The function GetTourChildren($tour) returns a list (one-dimensional,
numerical indexed array) of children of $tour or an empty list
if a tour has no children.

The purpose of GetTourDescendants($tour) is to produce a list of
descendants of $tour, in other words, the children, the children of
children, and so forth. The purpose of this list is sanity checking
of the tour structure, the main one of which is to test for uniqueness
as a tour should not be a descendant of itself, no tour should be the
child of two different tours (in the same hierarchy), tours should not be
entered twice as a child of a given tour, and so forth.

So far as I can describe it, this is how GetTourDescendants works:

Well, actually the heavy lifting is done by GetTourDescendants_inner,
and GetTourDescendants just lops off the first cell of the return
because I cannot get GetTourDescendants_inner to work without returning
the parent tour as its own descendant.

So GetTourDescendants_inner goes this way:

The end condition is a tour has no children. However, this condition
might not be reached if in some way the tour structure has become
corrupt and a tour is its own descendant, so every time a tour is
added to the descendants list the list is checked for uniqueness.

If a tour has no children, it is added to the list, and the list is
returned.

If a tour has chidren, it is added to the list and the function
recurs on each child.

You may notice that GetTourDescendants_inner initializes $list if it is
called with one argument, which never happens here, since it is first called
by the wrapper GetTourDescendants. That is because I would like
GetTourDescendants_inner to stand alone, called from the outside with one
argument and called recursively with two.

function GetTourDescendants($tour){
$list = array();
$all = GetTourDescendants_inner($tour,$list);
return array_slice($all,1);
}

function GetTourDescendants_inner(){
$numargs = func_num_args();
$tour = func_get_arg(0);
if($numargs < 2){$list = array();
}else{ $list = func_get_arg(1);}
if(!GetTourChildren($tour)){$list[] = $tour;
if(count($list) != count(array_unique($list))){
die("Bad Tours: Get Tour Descendants not unique.");}
return $list;}
$list[] = $tour;
if(count($list) != count(array_unique($list))){
die("Bad Tours: Get Tour Descendants not unique.");}
foreach(GetTourChildren($tour) as $child){
$list = GetTourDescendants_inner($child,$list);
}
return $list;
}

--
Lars Eighner <http://larseighner.com/ <http://myspace.com/larseighner>
Countdown: 574 days to go.
Owing to googlegroups not screening users to eliminate spammers and other
USENET abusers, I do not see most posts from googlegroups.
Jun 26 '07 #1
1 1266
On 26.06.2007 08:40 Lars Eighner wrote:
My function GetTourDescendants works, but I do not really have a recursive
brain (and run from lisp like a vampire from a crucifix) and am a little
wary of recursion in PHP. And besides, the function is ugly as sin. So,
this question is not will this work (it does) or how to make it work (it
does), but whether there is a better way or not to do this.
[snip]
hi there

a general purpose tree-walker algorithm is as simple as
$node = array(
'data' =whatever data,
'children' =array of nodes or empty
);

function walk($node) {
// do something with $node['data']
if(isset($node['children']))
foreach($node['children'] as $child)
walk($child);
}

a more generic approach would be to pass a callback function to the
walker, so that you can use it for different things
function walk($node, $callback) {
call_user_func($callback, $node);
if(isset($node['children']))
foreach($node['children'] as $child)
walk($child);
}
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 26 '07 #2

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.