CODE
Код:
Код: Выделить всё
id nam pid
----------
1 ass 0
2 loop 1
3 zxx 0
4 flip 2в такую структуру:
вернее, как организовать отступы?
Код:
Код: Выделить всё
ass
loop
flip
zxxКод: Выделить всё
id nam pid
----------
1 ass 0
2 loop 1
3 zxx 0
4 flip 2Код: Выделить всё
ass
loop
flip
zxxКод: Выделить всё
ass
loop
flip
zxx
Код: Выделить всё
class Node
{
public $name;
public $children;
public $num;
function __construct($name,$num)
{
$this->name=$name;
$this->children=array();
$this->num=$num;
}
function new_child($child_node)
{
$this->children[]=$child_node;
}
}
class Hash
{
public $table;
private $counter;
function __construct()
{
$this->table=array();
$this->counter=-1;
}
function new_node($name,$parent_num=FALSE)
{
$this->counter++;
$new_node=new Node($name,$this->counter);
if($parent_num===FALSE) $this->table[]=$new_node;
else
{
self::set_child($new_node,$parent_num);
}
}
private function set_child($child,$parent_num,$table=FALSE)
{
if($table===FALSE) $table=$this->table;
foreach($table as $k=>$v)
{
if($v->num==$parent_num)
{
$table[$k]->new_child($child);
return TRUE;
}
if(!empty($v->children))
{
if(self::set_child($child,$parent_num,$v->children)) return TRUE;
else return FALSE;
}
}
}
function find_node($name,$table=FALSE)
{
if($table==FALSE) $table=$this->table;
foreach($table as $v)
{
if($v->name==$name) return $v->num;
if(!empty($v->children))
{
$ret=self::find_node($name,$v->children);
if($ret) return $ret;
else return FALSE;
}
}
}
function show_hash($table=FALSE,$level=-1)
{
if($table===FALSE) $table=$this->table;
foreach($table as $k=>$v)
{
$level++;
echo str_repeat("-",$level).$v->name."
";
if(!empty($v->children))
{
self::show_hash($v->children,$level);
}
$level--;
}
}
}
Код: Выделить всё
$hash=new Hash();
$hash->new_node("ass");
$hash->new_node("loop",$hash->find_node("ass"));
$hash->new_node("flip",$hash->find_node("loop"));
$hash->new_node("zxx");
$hash->new_node("flip2",$hash->find_node("loop"));
$hash->new_node("lalala",$hash->find_node("flip"));
$hash->show_hash();
, просто мне хотелось поэксперементировать