时间:2021-07-01 10:21:17 帮助过:4人阅读
PHP代码实现(暂时实现添加节点、层次遍历节点,删除节点后续更新)
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); } } } } /* 添加节点 */ function addNode($node, $value){ $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); } } $head = new Node(); setNode($head, 1); addNode($head, 2); addNode($head, 3); addNode($head, 4); addNode($head, 5); addNode($head, 6); printTree($head);
以上就介绍了 PHP实现完全二叉树,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。