-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupingTree.php
More file actions
120 lines (94 loc) · 2.58 KB
/
groupingTree.php
File metadata and controls
120 lines (94 loc) · 2.58 KB
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
#!/usr/bin/php
<?php
//Requires php version 5.4 to do function()['someindex']
Class GroupingTree
{
private $db = array('count' => 0);
private $searchQueue = array();
public function store_in_tree($inputString)
{
$strArray = str_split($inputString);
$pointer = &$this->db;
foreach ($strArray as $key => $char)
{
if (!isset($pointer[$char]))
{
$pointer[$char] = array('count' => 0);
}
$pointer['count'] ++;
$pointer = &$pointer[$char];
}
}
public function printr()
{
print_r($this->db);
}
private function pathpointer($path)
{
$pointer = $this->db;
foreach ($path as $char)
{
$pointer = &$pointer[$char];
}
return $pointer;
}
private function print_path($path)
{
$output = '$db';
foreach ($path as $index)
{
$output .= '[' . $index . ']';
}
return $output;
}
/*
Adds a all children of a path to searchQueue
*/
private function add_children_to_searchQueue($parentPath)
{
foreach ($this->pathpointer($parentPath) as $childKey => $child)
{
if ($childKey == "count")
{
continue;
}
$childPath = $parentPath;
$childPath[] = $childKey;
$this->searchQueue[] = $childPath;
}
}
function get_highest_count($maxCount)
{
//reset searchQueue
$this->searchQueue = array();
//By adding a blank array, we add a blank top, ie the root of the tree to start from
$this->searchQueue[] = array();
//Location of that count
$highestNode = array();
$highestCount = 0;
// Search each node that's in the list.
// If count exceeds max, add all children to searchQueue
// if not, compare to highestCount and add if exceeds highest count.
while(count($this->searchQueue) > 0)
{
$nextNode = array_pop($this->searchQueue);
echo "Next node " . $this->print_path($nextNode). ", count: " . $this->pathpointer($nextNode)['count'] . "\n";
//If over max, add children and move on
if ($this->pathpointer($nextNode)['count'] >= $maxCount)
{
echo "Count too big, adding children\n";
//Add children to searchQueue
$this->add_children_to_searchQueue($nextNode);
}
elseif ($this->pathpointer($nextNode)['count'] > $highestCount)
{
//Find highest count
$highestCount = $this->pathpointer($nextNode)['count'];
$highestNode = $nextNode;
echo "highest count now $highestCount with node " .$this->print_path($highestNode) . "\n";
}
echo "Finished examing node, count of searchQueue: " . count($this->searchQueue) . "\n\n";
}
echo "And the winner is : " .$this->print_path($highestNode). " with count of $highestCount with the limit count being $maxCount\n";
}
}