二叉树的深度优先遍历与广度优先遍历
发表于:2022-03-23 16:01:00浏览:263次
二叉树的深度优先遍历与广度优先遍历
① 节点结构
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);
② 深度优先