PHP数组实现二叉树的数据结构
class treearray
{
private $size;
private $p_array;
public function __construct($size,$n_root)
{
$this->size=$size;
$this->p_array[0]=$n_root;
for ($i=1;$i<$size;$i++){
$this->p_array[$i]=0;
}
}
private function check($index){
if($index<0||$index>$this->size){
return false;
}
return true;
}
public function add($index,$type,$data){
$flag=$this->check($index);
if(!$flag){
return $flag;
}
if($type==0){
$index=$index*2+1;
$flag=$this->check($index);
if(!$flag){
return $flag;
}
$this->p_array[$index]=$data;
}
if($type==1){
$index=$index*2+2;
$flag=$this->check($index);
if(!$flag){
return $flag;
}
$this->p_array[$index]=$data;
}
return true;
}
public function search($index){
$flag=$this->check($index);
if(!$flag){
return $flag;
}
return $this->p_array[$index];
}
public function del($index){
$flag=$this->check($index);
if(!$flag){
return $flag;
}
$this->p_array[$index]=0;
return true;
}
public function all(){
return $this->p_array;
}
}
$tree=new treearray(10,8);
$flag=$tree->add(0,0,7);
$tree->add(0,1,9);
$tree->add(1,0,5);
$tree->add(1,1,6);
$tree->add(2,0,1);
$tree->add(2,1,2);
$all=$tree->all();
print_r($all);
$node5=$tree->search(5);
print_r($node5);
PHP类实现二叉树的链式存储结构
class node{
public $leftNode;
public $rightNode;
public $index;
public $data;
public function __construct($data,$index=0,$pre=null,$leftNode=null,$rightNode=null)
{
$this->data=$data;
$this->index=$index;
}
public function search($index){
if($this->index==$index){
return $this;
}
if($this->leftNode!=null){
if($this->leftNode->index==$index){
return $this->leftNode;
}
$temp=$this->leftNode->search($index);
if($temp!=null){
return $temp;
}
}
if($this->rightNode!=null){
if($this->rightNode->index==$index){
return $this->rightNode;
}
$temp= $this->rightNode->search($index);
if($temp!=null){
return $temp;
}
}
return null;
}
public function preTraverse(){
if($this->leftNode!=null){
$this->leftNode->preTraverse();
}
echo $this->data;
if($this->rightNode!=null){
$this->rightNode->preTraverse();
}
}
public function inTraverse(){
echo $this->data;
if($this->leftNode!=null){
$this->leftNode->inTraverse();
}
if($this->rightNode!=null){
$this->rightNode->inTraverse();
}
}
public function afterTraverse(){
if($this->leftNode!=null){
$this->leftNode->afterTraverse();
}
if($this->rightNode!=null){
$this->rightNode->afterTraverse();
}
echo $this->data;
}
}
class Tree{
public $r_root;
public function __construct($node)
{
$this->r_root=$node;
}
public function search($index){
return $this->r_root->search($index);
}
public function add($index,$type,$data){
$indexNode=$this->r_root->search($index);
if(!$indexNode){
return false;
}
if($type==0){
$index=2*$indexNode->index+1;
$node=new node($data,$index);
$indexNode->leftNode=$node;
return true;
}
if($type==1){
$index=2*$indexNode->index+2;
$node=new node($data,$index);
$indexNode->rightNode=$node;
return true;
}
return false;
}
public function preTraverse(){
$this->r_root->preTraverse();
}
public function inTraverse(){
$this->r_root->inTraverse();
}
public function afterTraverse(){
$this->r_root->afterTraverse();
}
}
$root_node=new node(8);
$tree=new Tree($root_node);
var_dump($tree->search(0)->data);
$tree->add(0,0,7);
$tree->add(0,1,9);
$tree->add(1,0,5);
$tree->add(1,1,6);
$tree->add(2,1,2);
$tree->add(2,0,1);
var_dump($tree->search(4)->data);
$tree->preTraverse();
echo "\n";
$tree->inTraverse();
echo "\n";
$tree->afterTraverse();