1
1
<?php
2
2
namespace Danon \IntervalTree ;
3
3
4
- class Node {
4
+ class Node
5
+ {
5
6
6
7
public const COLOR_RED = 0 ;
7
8
public const COLOR_BLACK = 1 ;
@@ -30,14 +31,15 @@ class Node {
30
31
31
32
public $ max ;
32
33
33
- public function __construct ($ key = null , $ value = null , $ left = null , $ right = null , $ parent = null , $ color = self ::COLOR_BLACK ) {
34
+ public function __construct ($ key = null , $ value = null , $ left = null , $ right = null , $ parent = null , $ color = self ::COLOR_BLACK )
35
+ {
34
36
35
37
$ this ->left = $ left ;
36
38
$ this ->right = $ right ;
37
39
$ this ->parent = $ parent ;
38
40
$ this ->color = $ color ;
39
41
40
- $ this ->item = (object )compact ('key ' , 'value ' ); // key is supposed to be instance of Interval
42
+ $ this ->item = (object ) compact ('key ' , 'value ' ); // key is supposed to be instance of Interval
41
43
42
44
/* If not, this should by an array of two numbers */
43
45
if ($ key && is_array ($ key ) && count ($ key ) === 2 ) {
@@ -47,64 +49,73 @@ public function __construct($key = null, $value = null, $left = null, $right = n
47
49
$ this ->max = $ this ->item ->key ? clone $ this ->item ->key : null ;
48
50
}
49
51
50
- public function getValue () {
52
+ public function getValue ()
53
+ {
51
54
return $ this ->item ->value ;
52
55
}
53
56
54
-
55
- public function getKey () {
57
+ public function getKey ()
58
+ {
56
59
return $ this ->item ->key ;
57
60
}
58
61
59
- public function isNil () {
62
+ public function isNil ()
63
+ {
60
64
return ($ this ->item ->key === null && $ this ->item ->value === null &&
61
65
$ this ->left === null && $ this ->right === null && $ this ->color === Node::COLOR_BLACK );
62
66
}
63
67
64
- public function lessThan ($ otherNode ) {
68
+ public function lessThan ($ otherNode )
69
+ {
65
70
return $ this ->item ->key ->lessThan ($ otherNode ->item ->key );
66
71
}
67
72
68
- public function equalTo ($ otherNode ) {
73
+ public function equalTo ($ otherNode )
74
+ {
69
75
$ valueEqual = true ;
70
76
if ($ this ->item ->value && $ otherNode ->item ->value ) {
71
77
$ valueEqual = $ this ->item ->value ? $ this ->item ->value ->equalTo ($ otherNode ->item ->value ) :
72
- $ this ->item ->value == $ otherNode ->item ->value ;
78
+ $ this ->item ->value == $ otherNode ->item ->value ;
73
79
}
74
80
return $ this ->item ->key ->equalTo ($ otherNode ->item ->key ) && $ valueEqual ;
75
81
}
76
82
77
- public function intersect ($ otherNode ) {
83
+ public function intersect ($ otherNode )
84
+ {
78
85
return $ this ->item ->key ->intersect ($ otherNode ->item ->key );
79
86
}
80
87
81
- public function copyData ($ otherNode ) {
88
+ public function copyData ($ otherNode )
89
+ {
82
90
$ this ->item ->key = clone $ otherNode ->item ->key ;
83
91
$ this ->item ->value = $ otherNode ->item ->value ;
84
92
}
85
93
86
- public function updateMax () {
94
+ public function updateMax ()
95
+ {
87
96
// use key (Interval) max property instead of key.high
88
97
$ this ->max = $ this ->item ->key ? $ this ->item ->key ->getMax () : null ;
89
98
if ($ this ->right && $ this ->right ->max ) {
90
- $ this ->max = Interval::comparableMax ($ this ->max , $ this ->right ->max ); // static method
99
+ $ this ->max = Interval::comparableMax ($ this ->max , $ this ->right ->max ); // static method
91
100
}
92
101
if ($ this ->left && $ this ->left ->max ) {
93
102
$ this ->max = Interval::comparableMax ($ this ->max , $ this ->left ->max );
94
103
}
95
104
}
96
105
97
106
// Other_node does not intersect any node of left subtree, if this.left.max < other_node.item.key.low
98
- public function notIntersectLeftSubtree ($ searchNode ) {
107
+ public function notIntersectLeftSubtree ($ searchNode )
108
+ {
99
109
//const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method
100
110
$ high = $ this ->left ->max ->high !== null ? $ this ->left ->max ->high : $ this ->left ->max ;
101
111
return Interval::comparableLessThan ($ high , $ searchNode ->item ->key ->low );
102
112
}
103
113
104
114
// Other_node does not intersect right subtree if other_node.item.key.high < this.right.key.low
105
- public function notIntersectRightSubtree ($ searchNode ) {
115
+ public function notIntersectRightSubtree ($ searchNode )
116
+ {
106
117
//const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method
107
118
$ low = $ this ->right ->max ->low !== null ? $ this ->right ->max ->low : $ this ->right ->item ->key ->low ;
108
119
return Interval::comparableLessThan ($ searchNode ->item ->key ->high , $ low );
109
120
}
110
- }
121
+ }
0 commit comments