-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMockInterviewPractice11.php
77 lines (67 loc) · 1.66 KB
/
MockInterviewPractice11.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
<?php
/**
* DevCafe Solutions Limited operates as a software development company with the following role hierarchy.
CEO
-CTO
--Senior Architect
---Software Engineer
--Quality Assurance Engineer
--User Interface Designer
-CFO
-CMO
-COO
* Question:
* Given the role above, design a tree data structure that mimics this organization structure
*
*
*/
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)
{
$this->root = $node;
}
public function traverse(TreeNode $node, int $level = 0)
{
if ($node) {
echo str_repeat("-", $level);
echo $node->data . "\n";
foreach ($node->children as $childNode) {
$this->traverse($childNode, $level + 1);
}
}
}
}
$ceo = new TreeNode("CEO");
$cto = new TreeNode("CTO");
$cfo = new TreeNode("CFO");
$cmo = new TreeNode("CMO");
$coo = new TreeNode("COO");
$seniorArchitect = new TreeNode("Senior Architect");
$softwareEngineer = new TreeNode("Software Engineer");
$userInterfaceDesigner = new TreeNode("User Interface Designer");
$qualityAssuranceEngineer = new TreeNode("Quality Assurance Engineer");
$ceo->addChildren($cto);
$ceo->addChildren($cfo);
$ceo->addChildren($cmo);
$ceo->addChildren($coo);
$cto->addChildren($seniorArchitect);
$seniorArchitect->addChildren($softwareEngineer);
$cto->addChildren($qualityAssuranceEngineer);
$cto->addChildren($userInterfaceDesigner);
$tree = new Tree($ceo);
$tree->traverse($tree->root);