I'm having problems updating adding children to the nodes of a tree I'm trying to build. Any help appreciated. The code goes like this:
class Node {
var $id;
var $children;
var $no_children;
function Node($nodeid) {
printf("Constructing object (%s)...<br>", $nodeid);
$this->id = $nodeid;
$this->no_children = 0;
}
function AddChild($child) {
printf("Adding child %s to parent %s", $child->id, $this->id);
$this->children[$this->no_children++] = $child;
reset($this->children);
printf(" Node %s now has %s children<br>", $this->id, count($this->children));
}
function FindNode($nodeid) {
if ($this->children) {
while(list($key, $val) = each($this->children)) {
if($val->id == $nodeid) {
$node = $val; break;
} else {
$node = $val->FindNode($nodeid);
if($node) break;
}
}
reset($this->children);
}
else {
printf("Node has no children<br>");
}
return $node;
}
function Show() {
printf("Node<br>----<br>Id: %s<br>Number of children: %s<br>", $this->id, $this->no_children);
if ($this->children) {
while(list($key, $val) = each($this->children)) {
$val->Show();
}
reset($this->children);
}
printf("----<br>");
}
} #class Node
$dummyroot = new Node("0");
$new_node = new Node("100");
$dummyroot->AddChild($new_node);
$new_node = new Node("200");
$dummyroot->AddChild($new_node);
$new_node = new Node("300");
$dummyroot->AddChild($new_node);
$new_node = new Node("400");
$dummyroot->AddChild($new_node);
$new_node = new Node("500");
$dummyroot->AddChild($new_node);
$new_node = new Node("600");
$dummyroot->AddChild($new_node);
$new_node = new Node("110");
$parent = $dummyroot->FindNode("100");
$parent->AddChild($new_node);
$new_node = new Node("120");
$parent = $dummyroot->FindNode("100");
$parent->AddChild($new_node);
$new_node = new Node("220");
$parent = $dummyroot->FindNode("200");
$parent->AddChild($new_node);
$new_node = new Node("330");
$parent = $dummyroot->FindNode("300");
$parent->AddChild($new_node);
$dummyroot->Show();
which produces this result:
Constructing object (0)...
Constructing object (100)...
Adding child 100 to parent 0 Node 0 now has 1 children
Constructing object (200)...
Adding child 200 to parent 0 Node 0 now has 2 children
Constructing object (300)...
Adding child 300 to parent 0 Node 0 now has 3 children
Constructing object (400)...
Adding child 400 to parent 0 Node 0 now has 4 children
Constructing object (500)...
Adding child 500 to parent 0 Node 0 now has 5 children
Constructing object (600)...
Adding child 600 to parent 0 Node 0 now has 6 children
ObjectConstructing object (110)...
ObjectAdding child 110 to parent 100 Node 100 now has 1 children
Constructing object (120)...
Adding child 120 to parent 100 Node 100 now has 1 children
Constructing object (220)...
Node has no children
Adding child 220 to parent 200 Node 200 now has 1 children
Constructing object (330)...
Node has no children
Node has no children
Adding child 330 to parent 300 Node 300 now has 1 children
Node
Id: 0
Number of children: 6
Node
Id: 100
Number of children: 0
Node
Id: 200
Number of children: 0
Node
Id: 300
Number of children: 0
Node
Id: 400
Number of children: 0
Node
Id: 500
Number of children: 0
Node
Id: 600
Number of children: 0