-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.php
58 lines (50 loc) · 1.57 KB
/
Node.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
<?php
//Copyright Christopher Thomas 2003 (cst at andrew dot cmu dot edu)
//You may use and redistribute freely as long as you keep this copyright
//notice. Feel free to modify as you want
class Node {
public $left;
public $right;
public $data = array();
public $index;
function Node() {
$this->left = null;
$this->right = null;
}
function search($key) {
if ($this->index == $key){
return $this;
}
if ($this->left != null && $this->index < $key){
return $this->left->search($key);
}
if ($this->right != null && $this->index > $key){
return $this->right->search($key);
}
return false;
}
function addItem($key, $data = array()) {
if ($this->index == $key) {
$this->data[] = $data;
return true;
}
if ($this->left != null && $key < $this->index){
return $this->left->addItem($key, $data);
}
if ($this->right != null && $key > $this->index){
return $this->right->addItem($key, $data);
}
if ($this->left == null && $key < $this->index) {
$this->left = new Node();
$this->left->index = $key;
$this->left->data[] = $data;
return true;
}
if ($this->right == null && $key > $this->index) {
$this->right = new Node();
$this->right->index = $key;
$this->right->data[] = $data;
return true;
}
}
}