您的当前位置:首页>全部文章 >"博客"内的文章 >文章详情
二叉树的深度优先遍历与广度优先遍历
发表于:2022-03-23 16:01:00浏览:263次TAG: #PHP


二叉树的深度优先遍历与广度优先遍历 

binarytree1.png

① 节点结构

class Node{
    
    private $data;
    private $left;
    private $right;
    
    public function __construct($data,Node $left = null,Node $right = null){
        $this->data = $data;
        $this->left = $left;
        $this->right = $right;
    }
    
    public function getData(){
        return $this->data;
    }
    
    public function getLeft(){
        return $this->left;
    }
    
    public function getRight(){
        return $this->right;
    }
}
$D = new Node("D");$E = new Node("E");$F = new Node("F");$G = new Node("G");
$B = new Node("B",$D,$E);
$C = new Node("C",$F,$G);
$A = new Node("A",$B,$C);

② 深度优先

登录 登录阅读全文
栏目分类全部>