<?php
class
unorderedTree{
protected
$nodeId
=0;
protected
$depth
=0;
protected
$nodesCount
=0;
public
$degree
=
" to be implent"
;
protected
$rootid
=null;
protected
$nodes
=
array
();
protected
$userVisitFunction
=null;
public
function
__construct(){
}
public
function
__destruct(){
unset(
$this
->nodes) ;
}
public
function
getTreeDepth(){
return
$this
->depth;
}
public
function
getCount(){
return
$this
->NodesCount;
}
public
function
getDegree(){
return
$this
->degree;
}
public
function
getNode(
$nodeId
){
if
(isset(
$this
->Nodes[
$nodeId
])){
return
$this
->Nodes[
$nodeId
];
}
else
{
return
false;
}
}
public
function
getId(){
return
$this
->nodeId;
}
public
function
getNodeHeight(
$nodeId
){
if
(
array_key_exists
(
$nodeId
,
$this
->nodes) ){
$height
=1;
$visitedNodesIds
=
array
();
$cid
=
$nodeId
;
while
( isset(
$cid
) ) {
if
( !in_array(
$cid
,
$visitedNodesIds
) ){
if
(
$this
->rootid===
$cid
){
return
$height
;
}
$visitedNodesIds
[]=
$cid
;
$cid
=
$this
->nodes[
$cid
]->parentId;
$height
++;
}
else
{
return
false;
}
}
return
false;
}
else
{
return
false;
}
}
public
function
getRoot(){
return
(!
is_null
(
$this
->rootid) ) &&
$this
->nodes[
$this
->rootid];
}
public
function
getSubNodes(
$nodeId
){
if
(isset(
$this
->nodes[
$nodeId
])){
$result
=
array
();
$toVisitNodeIds
=
array
();
$toVisitedNodeIds
[]=
$nodeId
;
$result
[]=
$this
->nodes[
$nodeId
]->id;
array_shift
(
$toVisitedNodeIds
);
$toVisitedNodeIds
=
array_merge
(
$toVisitedNodeIds
,
$this
->nodes[
$nodeId
]->childrenIds);
while
(!
empty
(
$toVisitedNodeIds
)){
$toVisitNodeId
=
array_shift
(
$toVisitedNodeIds
);
$result
[]=
$this
->nodes[
$toVisitNodeId
]->id;
$toVisitedNodeIds
=
array_merge
(
$toVisitedNodeIds
,
$this
->nodes[
$toVisitNodeId
]->childrenIds);
}
return
$result
;
}
else
{
return
false;
}
}
public
function
getSubTree(
$nodeid
){
}
public
function
setId(
$nodeId
){
$this
->nodeId=
$nodeId
;
return
$this
;
}
public
function
seekId(){
$this
->nodeId++;
return
$this
->nodeId;
}
public
function
setVisitFunction(
$userFunction
){
$this
->userVisitFunction=
$userFunction
;
}
public
function
insertNode(
$parent_id
=null ,
$data
=null){
$node
=
new
stdclass;
$node
->data =
$data
;
$this
->nodeCount++;
$this
->seekId();
$node
->id =
$this
->getId();
if
( (
is_null
(
$parent_id
)) &&
is_null
(
$this
->rootid)){
$node
->parentId = null;
$node
->childrenIds =
array
();
$this
->depth=1;
$this
->rootid=
$node
->id;
$this
->nodes [
$node
->id]=
$node
;
return
$this
;
}
elseif
( isset(
$this
->nodes[
$parent_id
]) ||
is_null
(
$parent_id
) ){
if
(
is_null
(
$parent_id
)){
$parent_id
=
$this
->rootid;
}
$node
->parentId =
$parent_id
;
$node
->childrenIds =
array
();
$depth
=
$this
->getNodeHeight(
$parent_id
);
$this
->depth=max(
$depth
+1,
$this
->depth);
$this
->nodes[
$parent_id
]->childrenIds []=
$node
->id;
$this
->nodes [
$node
->id]=
$node
;
return
$this
;
}
else
{
return
$this
;
}
}
public
function
append(
$parent_id
=null ,
$data
=null){
return
$this
->insertNode(
$parent_id
,
$data
);
}
public
function
b(
$nodeId
=null){
return
$this
->breadthTraversal(
$nodeId
);
}
public
function
breadthTraversal(
$nodeId
=null){
if
(
is_null
(
$this
->rootid)){
die
(
"此树为空树,不可访问"
);
}
else
{
if
(
is_null
(
$nodeId
) || (
$this
->rootid===
$nodeId
) ){
$nodeId
=
$this
->rootid;
}
$toVisitNodeIds
=
array
();
$toVisitedNodeIds
[]=
$nodeId
;
$this
->visit(
$this
->nodes[
$nodeId
]);
array_shift
(
$toVisitedNodeIds
);
$toVisitedNodeIds
=
array_merge
(
$toVisitedNodeIds
,
$this
->nodes[
$nodeId
]->childrenIds);
while
(!
empty
(
$toVisitedNodeIds
)){
$toVisitNodeId
=
array_shift
(
$toVisitedNodeIds
);
$this
->visit(
$this
->nodes[
$toVisitNodeId
]);
$toVisitedNodeIds
=
array_merge
(
$toVisitedNodeIds
,
$this
->nodes[
$toVisitNodeId
]->childrenIds);
}
}
return
$this
;
}
public
function
d(
$nodeId
=null){
return
$this
->depthTraversall(
$nodeId
);
}
public
function
depthTraversall(
$nodeId
=null){
if
(
is_null
(
$this
->rootid)){
die
(
"此树为空树,不可访问"
);
}
else
{
if
(
is_null
(
$nodeId
)){
$nodeId
=
$this
->rootid;
}
$toVisitNodeIds
=
array
();
$toVisitedNodeIds
[]=
$nodeId
;
$this
->visit(
$this
->nodes[
$nodeId
]);
array_shift
(
$toVisitedNodeIds
);
$toVisitedNodeIds
=
array_merge
(
$this
->nodes[
$nodeId
]->childrenIds,
$toVisitedNodeIds
);
while
(!
empty
(
$toVisitedNodeIds
)){
$toVisitNodeId
=
array_shift
(
$toVisitedNodeIds
);
$this
->visit(
$this
->nodes[
$toVisitNodeId
]);
$toVisitedNodeIds
=
array_merge
(
$this
->nodes[
$toVisitNodeId
]->childrenIds,
$toVisitedNodeIds
);
}
}
return
$this
;
}
public
function
visit(
$node
){
if
(
is_null
(
$this
->userVisitFunction )){
return
$node
->id;
}
else
{
return
call_user_func(
$this
->userVisitFunction,
$node
,
$this
);
}
}
}
?>