May 2017
Intermediate to advanced
340 pages
8h 16m
English
Since we have not covered the graph in detail so far, we will keep our implementation for BFS and DFS strictly for tree structure. Also, we will use the generic tree structure we have seen in Chapter 6, Understanding and Implementing Trees, (not even binary tree). We will use the same TreeNode class to define our nodes and relationship with children. So, let's now define the Tree class with BFS functionality:
class TreeNode { public $data = NULL; public $children = []; public function __construct(string $data = NULL) { $this->data = $data; } public function addChildren(TreeNode $node) { $this->children[] = $node; } } class Tree { public $root = NULL; public function __construct(TreeNode $node) { ...Read now
Unlock full access