|
| 1 | +<?php |
| 2 | + |
| 3 | +class Node { |
| 4 | + private $_val; |
| 5 | + |
| 6 | + /** |
| 7 | + * Parent's node. |
| 8 | + * @access private |
| 9 | + * @type Node |
| 10 | + */ |
| 11 | + private $_parent; |
| 12 | + |
| 13 | + /** |
| 14 | + * Left-child's node. |
| 15 | + * @access private |
| 16 | + * @type Node |
| 17 | + */ |
| 18 | + private $_left; |
| 19 | + |
| 20 | + /** |
| 21 | + * Right-child's node. |
| 22 | + * @access private |
| 23 | + * @type Node |
| 24 | + */ |
| 25 | + private $_right; |
| 26 | + |
| 27 | + public function __construct($el) { |
| 28 | + $this->_val = $el; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Get node's value. |
| 33 | + * @access public |
| 34 | + * @return Node |
| 35 | + */ |
| 36 | + public function val() { |
| 37 | + return $this->_val; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get left-child's node. |
| 42 | + * @access public |
| 43 | + * @return Node |
| 44 | + */ |
| 45 | + public function left() { |
| 46 | + return $this->_left; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Set left-child's node. |
| 51 | + * @access public |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public function setLeft(Node $node) { |
| 55 | + $this->_left = $node; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Get right-child's node. |
| 60 | + * @access public |
| 61 | + * @return Node |
| 62 | + */ |
| 63 | + public function right() { |
| 64 | + return $this->_right; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Set right-child's node. |
| 69 | + * @access public |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function setRight(Node $node) { |
| 73 | + $this->_right = $node; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get parent's node. |
| 78 | + * @access public |
| 79 | + * @return Node |
| 80 | + */ |
| 81 | + public function parent() { |
| 82 | + return $this->_parent; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set parent's node. |
| 87 | + * @access public |
| 88 | + * @return public |
| 89 | + */ |
| 90 | + public function setParent(Node $node) { |
| 91 | + $this->_parent = $node; |
| 92 | + } |
| 93 | + |
| 94 | + public function __toString() { |
| 95 | + return $this->val() === NULL ? 'NULL' : $this->val() . ''; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class BinarySearchTree { |
| 100 | + private $_root; |
| 101 | + private $_sentinel; |
| 102 | + |
| 103 | + public function __construct() { |
| 104 | + $this->_sentinel = new Node(NULL); |
| 105 | + |
| 106 | + $this->_root = $this->_sentinel; |
| 107 | + $this->_root->setParent($this->_sentinel); |
| 108 | + $this->_root->setLeft($this->_sentinel); |
| 109 | + $this->_root->setRight($this->_sentinel); |
| 110 | + } |
| 111 | + |
| 112 | + public function insert($val) { |
| 113 | + if (!is_object($val) || getclass($val) !== 'Node') { |
| 114 | + $newNode = new Node($val); |
| 115 | + } else { |
| 116 | + $newNode = $val; |
| 117 | + } |
| 118 | + $newNode->setLeft($this->_sentinel); |
| 119 | + $newNode->setRight($this->_sentinel); |
| 120 | + |
| 121 | + $node = $this->_root; |
| 122 | + $parent = $this->_sentinel; |
| 123 | + while ($node !== $this->_sentinel) { |
| 124 | + $parent = $node; |
| 125 | + if ($newNode->val() < $node->val()) { |
| 126 | + $node = $node->left(); |
| 127 | + } else { |
| 128 | + $node = $node->right(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if ($parent === $this->_sentinel) { |
| 133 | + $newNode->setParent($this->_sentinel); |
| 134 | + $this->_root = $newNode; |
| 135 | + } else { |
| 136 | + if ($newNode->val() < $parent->val()) { |
| 137 | + $parent->setLeft($newNode); |
| 138 | + } else { |
| 139 | + $parent->setRight($newNode); |
| 140 | + } |
| 141 | + $newNode->setParent($parent); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public function search($val) { |
| 146 | + $node = $this->_root; |
| 147 | + |
| 148 | + while ($node !== $this->_sentinel && $node->val() !== $val) { |
| 149 | + if ($val < $node->val()) { |
| 150 | + $node = $node->left(); |
| 151 | + } else { |
| 152 | + $node = $node->right(); |
| 153 | + } |
| 154 | + } |
| 155 | + return $node; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +$BST = new BinarySearchTree; |
| 160 | +$BST->insert(15); |
| 161 | +$BST->insert(6); |
| 162 | +$BST->insert(18); |
| 163 | +$BST->insert(3); |
| 164 | +$BST->insert(7); |
| 165 | +$BST->insert(17); |
| 166 | +$BST->insert(20); |
| 167 | +$BST->insert(2); |
| 168 | +$BST->insert(4); |
| 169 | +$BST->insert(13); |
| 170 | +$BST->insert(9); |
| 171 | + |
| 172 | +function test_searching($num) { |
| 173 | + global $BST; |
| 174 | + |
| 175 | + $node = $BST->search($num); |
| 176 | + if ($node->val() !== NULL) { |
| 177 | + echo "Node with value $num is found\n"; |
| 178 | + echo "Parent's node = " . $node->parent() . "\n"; |
| 179 | + echo "Left-child's node = " . $node->left() . "\n"; |
| 180 | + echo "Right-child's node = " . $node->right() . "\n"; |
| 181 | + } else { |
| 182 | + echo "Node with value $num is NOT found\n"; |
| 183 | + } |
| 184 | + echo "\n"; |
| 185 | +} |
| 186 | + |
| 187 | +test_searching(7); |
| 188 | +test_searching(15); |
| 189 | +test_searching(3); |
| 190 | +test_searching(2); |
0 commit comments