# PHP完全二叉树定义与实现方法示例

PHP代码实现（暂时实现添加节点、层次遍历节点，删除节点后续更新）

<?php
class Node{
public \$value;
public \$leftNode;
public \$rightNode;
}
function findEmpytNode(\$node, \$parent = null){
if(empty(\$node->value)){
return \$node;
}else{
if(empty(\$node->leftNode->value)){
return \$node->leftNode;
}else if(empty(\$node->rightNode->value)){
return \$node->rightNode;
}else{
if(empty(\$parent) || \$node->value == \$parent->rightNode->value){
return findEmpytNode(\$node->leftNode, \$node);
}else{
return findEmpytNode(\$parent->rightNode, \$node);
}
}
}
}
/* æ·»å� è��ç�¹ */
\$emptyNode = findEmpytNode(\$node);
setNode(\$emptyNode, \$value);
}
/* è®¾ç½®è��ç�¹ */
function setNode(\$node, \$value){
\$node->value = \$value;
\$node->leftNode = new Node();
\$node->rightNode = new Node();
}
/* æ��å�° */
function printTree(\$node, \$parent = null){
if(empty(\$node->value)) return ;
echo \$node->leftNode->value;
echo \$node->rightNode->value;
if(empty(\$parent) || \$node->value == \$parent->rightNode->value){
printTree(\$node->leftNode, \$node);
}else{
printTree(\$parent->rightNode, \$node);
}
}