-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathBinaryTree.php
49 lines (39 loc) · 1.2 KB
/
BinaryTree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace DataStructure\Tree;
class BinaryTree
{
public $root = null;
public function __construct(BinaryNode $node)
{
$this->root = $node;
}
public function isEmpty()
{
return $this->root === null;
}
public function traverse(BinaryNode $node, int $level = 0)
{
if ($node) {
echo str_repeat('-', $level) . $node->data . PHP_EOL;
if ($node->left) {
$this->traverse($node->left, $level + 1);
}
if ($node->right) {
$this->traverse($node->right, $level + 1);
}
}
}
}
require './BinaryNode.php';
$root = new BinaryNode('root');
$tree = new BinaryTree($root);
$semiFinal1 = new BinaryNode("Semi Final 1");
$semiFinal2 = new BinaryNode("Semi Final 2");
$quarterFinal1 = new BinaryNode("Quarter Final 1");
$quarterFinal2 = new BinaryNode("Quarter Final 2");
$quarterFinal3 = new BinaryNode("Quarter Final 3");
$quarterFinal4 = new BinaryNode("Quarter Final 4");
$semiFinal1->addChildren($quarterFinal1, $quarterFinal2);
$semiFinal2->addChildren($quarterFinal3, $quarterFinal4);
$root->addChildren($semiFinal1, $semiFinal2);
$tree->traverse($tree->root);