-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeTraversal.php
154 lines (132 loc) · 3.88 KB
/
TreeTraversal.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Tree Traversal Problems
*
* We will assemble a tree and do a breadth first traversal
* then we will do a depth first traversal
*
* @category InterviewQuestions
* @package TreeTraversal
* @author Michael Reeves <[email protected]>
*
*/
class Node
{
private $_value;
private $_leftNode;
private $_rightNode;
/**
* @param $value int
* @param Node $leftNode
* @param Node $rightNode
*/
public function __construct($value, Node $leftNode = null, Node $rightNode = null)
{
$this->_value = $value;
$this->_leftNode = $leftNode;
$this->_rightNode = $rightNode;
}
/**
* @return array
*/
public function getChildren()
{
return array($this->_leftNode, $this->_rightNode);
}
public function getValue()
{
return $this->_value;
}
public function getLeftNode()
{
return $this->_leftNode;
}
public function getRightNode()
{
return $this->_rightNode;
}
public function countChildren()
{
$children = $this->getChildren();
$childCount = 0;
foreach ($children as $child) {
if ($child !== null) {
$childCount++;
}
}
return $childCount;
}
/**
* Yes, this should be in its own class. This is just a toy example...
*
* @return Node
*/
public static function getTestTree()
{
// Need to create child nodes first so we don't have to make a bunch of utility methods to add nodes as children
// Level 4 nodes
$level4Node1 = new Node(8);
$level4Node2 = new Node(9);
$level4Node3 = new Node(10);
$level4Node4 = new Node(11);
$level4Node5 = new Node(12);
$level4Node6 = new Node(13);
$level4Node7 = new Node(14);
$level4Node8 = new Node(15);
// Level 3 Nodes
$level3Node1 = new Node(4, $level4Node1, $level4Node2);
$level3Node2 = new Node(5, $level4Node3, $level4Node4);
$level3Node3 = new Node(6, $level4Node5, $level4Node6);
$level3Node4 = new Node(7, $level4Node7, $level4Node8);
// Level 2 Nodes
$level2Node1 = new Node(2, $level3Node1, $level3Node2);
$level2Node2 = new Node(3, $level3Node3, $level3Node4);
// Level 1 Root Node
$level1Node1 = new Node(1, $level2Node1, $level2Node2);
return $level1Node1;
}
/**
* @return array $flatTree
*/
public function traverseTreeBreadthwise()
{
$flatTree = array();
$flatTree[] = $this->getValue();
$children = $this->getChildren();
while ($children) {
$newChildren = array();
foreach($children as $child) {
if(!empty($child)) {
$flatTree[] = $child->getValue();
$newChildren = array_merge($newChildren, $child->getChildren());
}
}
$children = $newChildren;
}
return $flatTree;
}
/**
* @param array $flatTree
* @return array
*/
public function traverseTreeDepthwise($flatTree = array())
{
$flatTree[] = $this->getValue();
$children = $this->getChildren();
if ($this->countChildren() > 0) {
foreach($children as $child) {
if ($child !== null) {
$flatTree = $child->traverseTreeDepthwise($flatTree);
}
}
}
return $flatTree;
}
}
$rootNode = Node::getTestTree();
echo '<h2>Tree Structure</h2>';
echo '<pre>'.var_export($rootNode, true).'</pre>';
echo '<h2>Tree Structure Traversed Breadthwise</h2>';
echo '<pre>'.var_export($rootNode->traverseTreeBreadthwise(), true).'</pre>';
echo '<h2>Tree Structure Traversed Depthwise</h2>';
echo '<pre>'.var_export($rootNode->traverseTreeDepthwise(), true).'</pre>';