php二叉树基本遍历和非递归遍历

tree.php

ch=$data;
		$this->lchild=null;
		$this->rchild=null;
	}
	
}

//封装自己数据类型
class mydata{
	public $BinNode;
	public $status;
	public function __construct($data,$status){
		$this->BinNode=$data;
		$this->status=$status;
	}
}

class twofucktree{

	//初始化
	public function init_tree()
	{
	/*

	    A
	B		F
  C			   G
D   E	     H

	*/
		$nodeA= new BinNode('A');
		$nodeB= new BinNode('B');
		$nodeC= new BinNode('C');
		$nodeD= new BinNode('D');
		$nodeE= new BinNode('E');
		$nodeF= new BinNode('F');
		$nodeG= new BinNode('G');
		$nodeH= new BinNode('H');

		$nodeA->lchild = $nodeB;
		$nodeA->rchild = $nodeF;
		$nodeB->rchild = $nodeC;
		$nodeC->lchild = $nodeD;
		$nodeC->rchild = $nodeE;
		$nodeF->rchild = $nodeG;
		$nodeG->lchild = $nodeH;

		return $nodeA;
	}

	//求树的高度
	public function gao($root,&$height){
		if($root==null)
		{
			return;
		}

		$lheight = $this->gao($root->lchild,$height);
		$rheight = $this->gao($root->rchild,$height);

		$height = $lheight>$rheight?$lheight+1:$rheight+1;

		return $height;
	}
	//先序
	public function xianxu($root)
	{
		if($root==null)
		{
			return;
		}
		echo $root->ch." ";
		$this->xianxu($root->lchild);
		$this->xianxu($root->rchild);

	}
	//中序
	public function zhongxu($root)
	{
		if($root==null)
		{
			return;
		}
		$this->zhongxu($root->lchild);
		echo $root->ch." ";
		$this->zhongxu($root->rchild);

	}

	//后序
	public function houxu($root)
	{
		if($root==null)
		{
			return;
		}
		$this->houxu($root->lchild);
		$this->houxu($root->rchild);
		echo $root->ch." ";
	}

	//叶子节点数
	public function yezi($root,&$geshu)
	{
		if($root==null)
		{
			return;
		}

		if($root->lchild==null && $root->rchild==null)
		{
			$geshu++;
		}
		$this->yezi($root->lchild,$geshu);
		$this->yezi($root->rchild,$geshu);
	}

	//非递归遍历
	public function feidigui($root)
	{
		if(null==$root)
		{
			return;
		}

		// $mydata = new mydata($root,false);
		// echo $mydata->BinNode->ch;
		$stack = new my_stack();
		//接受返回的链表
		$lk=$stack->init_stack();
		//压入栈
		$stack->push_link($lk,new mydata($root,false));

		while($stack->get_size($lk)>0)
		{

			$nownode = $stack->get_top($lk);
			$stack->pop_stack($lk);
			if($nownode->status==true)
			{
				echo $nownode->BinNode->ch." ";
				$nownode=null;
				continue;
			}else{
				//先序遍历 根左右  押入顺序 右左根
				if($nownode->BinNode->rchild!=null)
				{
					$stack->push_link($lk,new mydata($nownode->BinNode->rchild,false));
				}

				if($nownode->BinNode->lchild!=null)
				{
					$stack->push_link($lk,new mydata($nownode->BinNode->lchild,false));
				}


				$nownode->status=true;
				$stack->push_link($lk,$nownode);


			}
		}



	}



}


$tree = new twofucktree();

$root = $tree->init_tree();


$tree->xianxu($root);
echo "
"; $tree->zhongxu($root); echo "
"; $tree->houxu($root); echo "
"; $gao=0; $tree->gao($root,$gao); echo "树的高度height={$gao}
"; $geshu=0; $tree->yezi($root,$geshu); echo "叶子节点的个数{$geshu}个
"; echo "非递归遍历
"; $tree->feidigui($root); /* A B C D E F G H B D C E A F H G D E C B H G F A 树的高度height=4 叶子节点的个数3个 非递归遍历 A B C D E F G H */ ?>

非递归遍历用到栈 

zhan.php 

data=$data;
        $this->next=null;
    }

}

//栈的类
class  my_stack{

	//初始化
	public function init_stack(){
		$lk = new linklist();
		$header = new linknode(-1);
		$lk->linknode = $header;
		$lk->size=0;
		return $lk;
	}

	//入栈
	public function push_link($lk,$data){
		//echo '11';exit;
		$new_node = new linknode($data);

		$new_node->next = $lk->linknode->next;
		$lk->linknode->next=$new_node;
		$lk->size++;
		// return $lk;
	}

	//获取长度
	public function get_size($lk)
	{
		return $lk->size;
	}

	//获得栈顶元素
	//
	public function get_top($lk)
	{
		$current = $lk->linknode->next;
		return $current->data;
	}

	//出栈
	public function  pop_stack($lk)
	{
		$del_node = $lk->linknode->next;
		$lk->linknode->next = $del_node->next;
		$lk->size--;
		$del_node=null;
	}
} 
/*
 $stack = new my_stack();
 //接受返回的链表
 $lk=$stack->init_stack();
 //入栈
 $stack->push_link($lk,10);
 $stack->push_link($lk,20);
 $stack->push_link($lk,30);
//遍历 判断元素>0 然后获得栈顶元素,输出后 再弹出栈
 while($stack->get_size($lk)>0){
  		$res= $stack->get_top($lk);
  		echo $res."
"; $stack->pop_stack($lk); } 30 20 10 */ ?>

php二叉树基本遍历和非递归遍历_第1张图片

 

用到队列和栈 如果这里没贴出来  都可以参考之前文章的 

基本上队列和栈 都是用链表实现的 比数组的麻烦些 

如果是数组 直接下标0入栈 size++  出栈直接取size-1 然后size--就ok了  

 

 

你可能感兴趣的:(php)