/** * Put node in sub tree * @param Node $node * @param KeyInterface $key * @param $value * @return Node */ private function _put(Node $node, KeyInterface $key, $value) { if (!$node->getLength()) { return new Node($key, $value); } $cmp = $node->getKey()->compare($key); if ($cmp > 0) { $node->setLeft($this->_put($node->getLeft(), $key, $value)); } else { if ($cmp < 0) { $node->setRight($this->_put($node->getRight(), $key, $value)); } else { $node->setValue($value); } } return $node; }
/** * Put node in sub tree * @param Node $node * @param KeyInterface $key * @param $value * @return Node */ private function _put(Node $node, KeyInterface $key, $value) { if (!$node->getLength()) { return new Node($key, $value, NODE::RED); } $cmp = $node->getKey()->compare($key); if ($cmp > 0) { $node->setLeft($this->_put($node->getLeft(), $key, $value)); } else { if ($cmp < 0) { $node->setRight($this->_put($node->getRight(), $key, $value)); } else { $node->setValue($value); } } if ($node->getRight()->isRed() && !$node->getLeft()->isRed()) { $node = $this->rotateLeft($node); } if ($node->getLeft()->isRed() && $node->getLeft()->getLeft()->isRed()) { $node = $this->rotateRight($node); } if ($node->getLeft()->isRed() && $node->getRight()->isRed()) { $this->flipColors($node); } return $node; }