1
1
<?php
2
+ declare (strict_types=1 );
2
3
3
4
class Tree implements JsonSerializable
4
5
{
5
- private $ id ;
6
- private $ children = [];
6
+ private $ id ;
7
+ private $ children = [];
7
8
8
- public function __construct (int $ id , array $ children = [])
9
- {
10
- $ this ->id = $ id ;
11
- $ this ->children = $ children ;
12
- }
9
+ public function __construct (int $ id , array $ children = [])
10
+ {
11
+ $ this ->id = $ id ;
12
+ $ this ->children = $ children ;
13
+ }
13
14
14
- public function getId (): int { return $ this ->id ; }
15
+ public function getId (): int
16
+ {
17
+ return $ this ->id ;
18
+ }
15
19
16
- public function getChildren (): array { return $ this ->children ; }
20
+ public function getChildren (): array
21
+ {
22
+ return $ this ->children ;
23
+ }
17
24
18
- public function addChild (Tree $ child ): void { $ this ->children [] = $ child ; }
25
+ public function addChild (Tree $ child ): void
26
+ {
27
+ $ this ->children [] = $ child ;
28
+ }
19
29
20
- public function jsonSerialize (): array
21
- {
22
- return [
23
- 'id ' => $ this ->id ,
24
- 'children ' => $ this ->children
25
- ];
26
- }
30
+ public function jsonSerialize (): array
31
+ {
32
+ return [
33
+ 'id ' => $ this ->id ,
34
+ 'children ' => $ this ->children ,
35
+ ];
36
+ }
27
37
}
28
38
29
39
class TreeTraversal
30
40
{
31
- public static function dfs_recursive (Tree $ tree ): void
32
- {
33
- if ($ tree ->getId ()) echo $ tree ->getId () . PHP_EOL ;
34
- foreach ($ tree ->getChildren () as $ child ) static ::dfs_recursive ($ child );
35
- }
36
-
37
- public static function dfs_recursive_postorder (Tree $ tree ): void
38
- {
39
- foreach ($ tree ->getChildren () as $ child ) static ::dfs_recursive_postorder ($ child );
40
- echo $ tree ->getId () . PHP_EOL ;
41
- }
42
-
43
- public static function dfs_recursive_inorder_binary (Tree $ tree ): void
44
- {
45
- switch (count ($ tree ->getChildren ())) {
46
- case 2 :
47
- static ::dfs_recursive_inorder_binary ($ tree ->getChildren ()[0 ]);
48
- echo $ tree ->getId () . PHP_EOL ;
49
- static ::dfs_recursive_inorder_binary ($ tree ->getChildren ()[1 ]);
50
- break ;
51
- case 1 :
52
- static ::dfs_recursive_inorder_binary ($ tree ->getChildren ()[0 ]);
53
- echo $ tree ->getId () . PHP_EOL ;
54
- break ;
55
- case 0 :
56
- echo $ tree ->getId () . PHP_EOL ;
57
- break ;
58
- default :
59
- throw new InvalidArgumentException ('Not a binary tree! ' );
60
- break ;
41
+ public static function DFSRecursive (Tree $ tree ): void
42
+ {
43
+ if ($ tree ->getId ()) {
44
+ echo $ tree ->getId () . PHP_EOL ;
45
+ }
46
+ foreach ($ tree ->getChildren () as $ child ) {
47
+ static ::DFSRecursive ($ child );
48
+ }
61
49
}
62
- }
63
50
64
- public static function dfs_stack (Tree $ tree ): void
65
- {
66
- $ stack = [$ tree ];
67
- $ temp = null ;
51
+ public static function DFSRecursivePostorder (Tree $ tree ): void
52
+ {
53
+ foreach ($ tree ->getChildren () as $ child ) {
54
+ static ::DFSRecursivePostorder ($ child );
55
+ }
56
+ echo $ tree ->getId () . PHP_EOL ;
57
+ }
68
58
69
- while (null !== ($ temp = array_pop ($ stack ))) {
70
- echo $ temp ->getId () . PHP_EOL ;
71
- foreach ($ temp ->getChildren () as $ child ) $ stack [] = $ child ;
59
+ public static function DFSRecursiveInorderBinary (Tree $ tree ): void
60
+ {
61
+ switch (count ($ tree ->getChildren ())) {
62
+ case 2 :
63
+ static ::DFSRecursiveInorderBinary ($ tree ->getChildren ()[0 ]);
64
+ echo $ tree ->getId () . PHP_EOL ;
65
+ static ::DFSRecursiveInorderBinary ($ tree ->getChildren ()[1 ]);
66
+ break ;
67
+ case 1 :
68
+ static ::DFSRecursiveInorderBinary ($ tree ->getChildren ()[0 ]);
69
+ echo $ tree ->getId () . PHP_EOL ;
70
+ break ;
71
+ case 0 :
72
+ echo $ tree ->getId () . PHP_EOL ;
73
+ break ;
74
+ default :
75
+ throw new InvalidArgumentException ('Not a binary tree! ' );
76
+ break ;
77
+ }
72
78
}
73
- }
74
79
75
- public static function dfs_queue (Tree $ tree ): void
76
- {
77
- $ stack = [$ tree ];
78
- $ temp = null ;
80
+ public static function DFSStack (Tree $ tree ): void
81
+ {
82
+ $ stack = [$ tree ];
83
+ $ temp = null ;
84
+
85
+ while (null !== ($ temp = array_pop ($ stack ))) {
86
+ echo $ temp ->getId () . PHP_EOL ;
87
+ foreach ($ temp ->getChildren () as $ child ) {
88
+ $ stack [] = $ child ;
89
+ }
90
+ }
91
+ }
79
92
80
- while (null !== ($ temp = array_shift ($ stack ))) {
81
- echo $ temp ->getId () . PHP_EOL ;
82
- foreach ($ temp ->getChildren () as $ child ) $ stack [] = $ child ;
93
+ public static function DFSQueue (Tree $ tree ): void
94
+ {
95
+ $ stack = [$ tree ];
96
+ $ temp = null ;
97
+
98
+ while (null !== ($ temp = array_shift ($ stack ))) {
99
+ echo $ temp ->getId () . PHP_EOL ;
100
+ foreach ($ temp ->getChildren () as $ child ) {
101
+ $ stack [] = $ child ;
102
+ }
103
+ }
83
104
}
84
- }
85
105
}
86
106
87
- function generate_tree (int $ num_of_rows , int $ num_of_children , int $ id = -1 ): Tree
107
+ function generate_tree (int $ numOfRows , int $ numOfChildren , int $ id = -1 ): Tree
88
108
{
89
- if ($ id === -1 ) $ id = 1 ;
90
- $ node = new Tree ($ id );
109
+ if ($ id === -1 ) {
110
+ $ id = 1 ;
111
+ }
112
+ $ node = new Tree ($ id );
91
113
92
- if ($ num_of_rows > 1 )
93
- for ($ i = 0 ; $ i < $ num_of_children ; $ i ++) {
94
- $ child = generate_tree ($ num_of_rows - 1 , $ num_of_children , $ id * 10 + $ i + 1 );
95
- $ node ->addChild ($ child );
114
+ if ($ numOfRows > 1 ) {
115
+ for ($ i = 0 ; $ i < $ numOfChildren ; $ i ++) {
116
+ $ child = generate_tree ($ numOfRows - 1 , $ numOfChildren , $ id * 10 + $ i + 1 );
117
+ $ node ->addChild ($ child );
118
+ }
96
119
}
97
120
98
- return $ node ;
121
+ return $ node ;
99
122
}
100
123
101
124
$ node = generate_tree (3 , 3 );
102
- echo sprintf ('TreeTraversal in JSON format: %s%s%s ' , PHP_EOL , json_encode ($ node ), PHP_EOL );
103
125
104
126
echo 'DFS Recursive: ' . PHP_EOL ;
105
- TreeTraversal::dfs_recursive ($ node );
127
+ TreeTraversal::DFSRecursive ($ node );
106
128
107
129
echo 'DFS Recursive Postorder: ' . PHP_EOL ;
108
- TreeTraversal::dfs_recursive_postorder ($ node );
130
+ TreeTraversal::DFSRecursivePostorder ($ node );
109
131
110
132
echo 'DFS Stack: ' . PHP_EOL ;
111
- TreeTraversal::dfs_stack ($ node );
133
+ TreeTraversal::DFSStack ($ node );
112
134
113
135
echo 'DFS Queue: ' . PHP_EOL ;
114
- TreeTraversal::dfs_queue ($ node );
136
+ TreeTraversal::DFSQueue ($ node );
115
137
116
- // If you want try binary order non-binary tree
117
- // Comment generation of new tree bellow
118
- // If you do that, exception will be thrown
138
+ // If you want to try to run binary order on a non-binary tree,
139
+ // comment out the generation of the new tree below.
140
+ // If you do that, an exception will be thrown
119
141
$ node = generate_tree (3 , 2 );
120
142
echo 'DFS Recursive Inorder Binary: ' . PHP_EOL ;
121
- TreeTraversal::dfs_recursive_inorder_binary ($ node );
143
+ TreeTraversal::DFSRecursiveInorderBinary ($ node );
0 commit comments