avl_tree.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include "libbinom/include/binom_impl/avl_tree.hxx"
  2. using namespace binom;
  3. using namespace binom::priv;
  4. using AVLNode = AVLTree::AVLNode;
  5. // ============================================================ Node
  6. void AVLNode::swapPosition(AVLNode& other, AVLTree& avl_tree) {
  7. auto this_position = getPosition();
  8. auto other_position = other.getPosition();
  9. if(parent != &other && other.parent != this) {
  10. std::swap(parent, other.parent);
  11. std::swap(left, other.left);
  12. std::swap(right, other.right);
  13. std::swap(depth, other.depth);
  14. } elif (parent == &other) {
  15. parent = other.parent;
  16. other.parent = this;
  17. if(other.left == this) {
  18. other.left = left;
  19. left = &other;
  20. std::swap(right, other.right);
  21. } else {
  22. other.right = right;
  23. right = &other;
  24. std::swap(left, other.left);
  25. }
  26. std::swap(depth, other.depth);
  27. } else {
  28. other.parent = parent;
  29. parent = &other;
  30. if(left == &other) {
  31. left = other.left;
  32. other.left = this;
  33. std::swap(right, other.right);
  34. } else {
  35. right = other.right;
  36. other.right = this;
  37. std::swap(left, other.left);
  38. }
  39. }
  40. if(hasLeft()) left->parent = this;
  41. if(hasRight()) right->parent = this;
  42. if(other.hasLeft()) other.left->parent = &other;
  43. if(other.hasRight()) other.right->parent = &other;
  44. switch (other_position) {
  45. case NodePosition::left: parent->left = this; break;
  46. case NodePosition::right: parent->right = this; break;
  47. case NodePosition::root: avl_tree.root = this; break;
  48. }
  49. switch (this_position) {
  50. case NodePosition::left: other.parent->left = &other; break;
  51. case NodePosition::right: other.parent->right = &other; break;
  52. case NodePosition::root: avl_tree.root = &other; break;
  53. }
  54. }
  55. void AVLNode::unpin(AVLTree& avl_tree) {
  56. if(isLeft()) parent->left = nullptr;
  57. elif(isRight()) parent->right = nullptr;
  58. else avl_tree.root = nullptr;
  59. parent = nullptr;
  60. }
  61. AVLNode::AVLNode(KeyValue key, AVLNode* parent) : parent(parent), key(std::move(key)) {}
  62. AVLNode::AVLNode(AVLNode& other)
  63. : depth(other.depth),
  64. left(other.left),
  65. right(other.right),
  66. parent(other.parent),
  67. key(other.key) {}
  68. AVLNode::AVLNode(AVLNode&& other)
  69. : depth(other.depth),
  70. left(other.left),
  71. right(other.right),
  72. parent(other.parent),
  73. key(std::move(other.key)) {}
  74. AVLNode& AVLNode::operator=(AVLNode other) { this->~AVLNode(); return *new(this) AVLNode(std::move(other)); }
  75. bool AVLNode::isRoot() const noexcept {return !parent;}
  76. bool AVLNode::isLeft() const noexcept {return isRoot() ? false : parent->left == this;}
  77. bool AVLNode::isRight() const noexcept {return isRoot() ? false : parent->right == this;}
  78. bool AVLNode::hasLeft() const noexcept {return left;}
  79. bool AVLNode::hasRight() const noexcept {return right;}
  80. bool AVLNode::hasChild() const noexcept {return left || right;}
  81. KeyValue& AVLNode::getKey() {return key;}
  82. const KeyValue& AVLNode::getKey() const {return key;}
  83. AVLNode::NodePosition AVLNode::getPosition() const {
  84. if(isLeft()) return NodePosition::left;
  85. elif(isRight()) return NodePosition::right;
  86. else return NodePosition::root;
  87. }
  88. // ============================================================ AVLTree
  89. i64 AVLTree::max(i64 a, i64 b) noexcept {return (a > b) ? a : b;}
  90. i64 AVLTree::depth(AVLNode* node) noexcept {return node ? node->depth : 0;}
  91. AVLNode* AVLTree::rotateRight(AVLNode* y) {
  92. auto y_position = y->getPosition();
  93. AVLNode* x = y->left;
  94. AVLNode* T2 = x->right;
  95. x->right = y;
  96. y->left = T2;
  97. y->depth = max(depth(y->left), depth(y->right)) + 1;
  98. x->depth = max(depth(x->left), depth(x->right)) + 1;
  99. // Update parent poionters
  100. x->parent = y->parent;
  101. y->parent = x;
  102. if(T2) T2->parent = y;
  103. // Update parent's child poionter
  104. if(y_position == AVLNode::NodePosition::left) x->parent->left = x;
  105. elif(y_position == AVLNode::NodePosition::right) x->parent->right = x;
  106. else root = x;
  107. return x;
  108. }
  109. AVLNode* AVLTree::rotateLeft(AVLNode* x) {
  110. auto x_position = x->getPosition();
  111. AVLNode* y = x->right;
  112. AVLNode* T2 = y->left;
  113. y->left = x;
  114. x->right = T2;
  115. y->depth = max(depth(y->left), depth(y->right)) + 1;
  116. x->depth = max(depth(x->left), depth(x->right)) + 1;
  117. // Update parent poionters
  118. y->parent = x->parent;
  119. x->parent = y;
  120. if(T2) T2->parent = x;
  121. // Update parent's child poionter
  122. if(x_position == AVLNode::NodePosition::left) y->parent->left = y;
  123. elif(x_position == AVLNode::NodePosition::right) y->parent->right = y;
  124. else root = y;
  125. return y;
  126. }
  127. i64 AVLTree::getBalance(AVLNode* node) { return node ? depth(node->left) - depth(node->right) : 0; }
  128. AVLNode* AVLTree::minKeyNode(AVLNode* node) noexcept {
  129. if(!node) return nullptr;
  130. while(node->left) node = node->left;
  131. return node;
  132. }
  133. AVLNode* AVLTree::minKeyNode() const noexcept {return minKeyNode(root);}
  134. AVLNode* AVLTree::maxKeyNode(AVLNode* node) noexcept {
  135. if(!node) return nullptr;
  136. while (node->right) node = node->right;
  137. return node;
  138. }
  139. AVLNode* AVLTree::maxKeyNode() const noexcept {return maxKeyNode(root);}
  140. AVLNode* AVLTree::insert(AVLNode* new_node) {
  141. AVLNode* node = root;
  142. if(!node) return root = new_node;
  143. forever {
  144. auto cmp = new_node->key.getCompare(node->key);
  145. if(cmp == KeyValue::lower) {
  146. if(node->left) { node = node->left; continue; }
  147. else { node->left = new_node; new_node->parent = node; break; }
  148. } elif(cmp == KeyValue::highter) {
  149. if(node->right) { node = node->right; continue; }
  150. else { node->right = new_node; new_node->parent = node; break; }
  151. } else return nullptr;
  152. }
  153. while(node) { // Balancing
  154. node->depth = 1 + max(depth(node->left), depth(node->right));
  155. i64 balance = getBalance(node);
  156. if (balance > 1 && getBalance(root->left) >= 0) {
  157. rotateRight(node);
  158. } elif (balance > 1 && getBalance(root->left) < 0) {
  159. rotateLeft(node->left);
  160. rotateRight(node);
  161. } elif (balance < -1 && getBalance(node->right) <= 0) {
  162. rotateLeft(node);
  163. } elif (balance < -1 && getBalance(node->right) > 0) {
  164. rotateRight(node->right);
  165. rotateLeft(node);
  166. }
  167. node = node->parent;
  168. }
  169. return new_node;
  170. }
  171. AVLNode* AVLTree::extract(KeyValue key) {
  172. AVLNode* result = nullptr;
  173. AVLNode* node = root;
  174. forever {
  175. // If node isn't finded
  176. if(!node) return nullptr;
  177. // Searching key...
  178. auto cmp = key.getCompare(node->key);
  179. if(cmp == KeyValue::lower) {node = node->left; continue;}
  180. elif(cmp == KeyValue::highter) {node = node->right; continue;}
  181. else { // Node is finded
  182. forever if(!node->left || !node->right) {
  183. AVLNode* tmp = node->left ? node->left : node->right;
  184. if(!tmp) { // If node hasn't child
  185. tmp = node;
  186. node = node->parent;
  187. tmp->unpin(self);
  188. result = tmp;
  189. } else { // If node has 1 child
  190. node->swapPosition(*tmp, self);
  191. node->unpin(self);
  192. result = node;
  193. node = tmp; // For balancing
  194. }
  195. break;
  196. } else { // If node has 2 childs (Test it!!!)
  197. // Change the position of the node
  198. // to be deleted whith the position
  199. // of the leftmost node in th right branch
  200. AVLNode* tmp = minKeyNode(node->right);
  201. node->swapPosition(*tmp, self);
  202. continue;
  203. }
  204. break;
  205. }
  206. }
  207. while(node) { // Balancing
  208. node->depth = 1 + max(depth(node->left), depth(node->right));
  209. i64 balance = getBalance(node);
  210. if (balance > 1 && getBalance(root->left) >= 0) {
  211. rotateRight(node);
  212. } elif (balance > 1 && getBalance(root->left) < 0) {
  213. rotateLeft(node->left);
  214. rotateRight(node);
  215. } elif (balance < -1 && getBalance(node->right) <= 0) {
  216. rotateLeft(node);
  217. } elif (balance < -1 && getBalance(node->right) > 0) {
  218. rotateRight(node->right);
  219. rotateLeft(node);
  220. }
  221. node = node->parent;
  222. }
  223. return result;
  224. }
  225. AVLNode* AVLTree::extract(AVLNode* node) {
  226. AVLNode* result = node;
  227. forever if(!node->left || !node->right) {
  228. AVLNode* tmp = node->left ? node->left : node->right;
  229. if(!tmp) { // If node hasn't child
  230. tmp = node;
  231. node = nullptr;
  232. tmp->unpin(self);
  233. result = tmp;
  234. } else { // If node has 1 child
  235. node->swapPosition(*tmp, self);
  236. node->unpin(self);
  237. result = node;
  238. node = tmp; // For balancing
  239. }
  240. break;
  241. } else { // If node has 2 childs (Test it!!!)
  242. // Change the position of the node
  243. // to be deleted whith the position
  244. // of the leftmost node in th right branch
  245. AVLNode* tmp = minKeyNode(node->right);
  246. node->swapPosition(*tmp, self);
  247. continue;
  248. }
  249. while(node) { // Balancing
  250. node->depth = 1 + max(depth(node->left), depth(node->right));
  251. i64 balance = getBalance(node);
  252. if (balance > 1 && getBalance(root->left) >= 0) {
  253. rotateRight(node);
  254. } elif (balance > 1 && getBalance(root->left) < 0) {
  255. rotateLeft(node->left);
  256. rotateRight(node);
  257. } elif (balance < -1 && getBalance(node->right) <= 0) {
  258. rotateLeft(node);
  259. } elif (balance < -1 && getBalance(node->right) > 0) {
  260. rotateRight(node->right);
  261. rotateLeft(node);
  262. }
  263. node = node->parent;
  264. }
  265. return result;
  266. }
  267. AVLNode* AVLTree::get(KeyValue key) const {
  268. AVLNode* node = root;
  269. forever {
  270. // If node isn't finded
  271. if(!node) return nullptr;
  272. // Searching key...
  273. auto cmp = key.getCompare(node->key);
  274. if(cmp == KeyValue::lower) {node = node->left; continue;}
  275. elif(cmp == KeyValue::highter) {node = node->left; continue;}
  276. else return node; // Node is finded
  277. }
  278. }
  279. AVLTree::Iterator AVLTree::begin() noexcept {return minKeyNode();}
  280. AVLTree::Iterator AVLTree::end() noexcept {return nullptr;}
  281. AVLTree::ReverseIterator AVLTree::rbegin() noexcept {return maxKeyNode();}
  282. AVLTree::ReverseIterator AVLTree::rend() noexcept {return nullptr;}
  283. AVLTree::ConstIterator AVLTree::cbegin() const noexcept {return minKeyNode();}
  284. AVLTree::ConstIterator AVLTree::cend() const noexcept {return nullptr;}
  285. AVLTree::ConstReverseIterator AVLTree::crbegin() const noexcept {return maxKeyNode();}
  286. AVLTree::ConstReverseIterator AVLTree::crend() const noexcept {return nullptr;}
  287. void AVLTree::clear(std::function<void(AVLNode*)> destructor) {
  288. AVLNode* node = minKeyNode();
  289. if(!node) return;
  290. if(node->hasRight()) node = node->right;
  291. while(node) {
  292. AVLNode* last = node;
  293. if(!!node->parent) {
  294. if(node->isLeft() && node->parent->hasRight()) {
  295. node = minKeyNode(node->parent->right);
  296. if(node->hasRight()) node = node->right;
  297. } elif(node->isLeft() || node->isRight())
  298. node = node->parent;
  299. } else node = nullptr;
  300. destructor(last);
  301. }
  302. root = nullptr;
  303. }
  304. AVLTree::Iterator::Iterator(AVLNode* node) : node(node) {}
  305. AVLTree::Iterator::Iterator(const Iterator& other) : node(other.node) {}
  306. AVLTree::Iterator::Iterator(Iterator&& other) : node(other.node) {}
  307. AVLTree::Iterator& AVLTree::Iterator::operator=(Iterator& other) noexcept {return *new(this) Iterator(other);}
  308. AVLTree::Iterator& AVLTree::Iterator::operator=(Iterator&& other) noexcept {return *new(this) Iterator(std::move(other));}
  309. AVLTree::Iterator& AVLTree::Iterator::operator=(AVLNode* node) noexcept {return *new(this) Iterator(node);}
  310. AVLTree::Iterator& AVLTree::Iterator::operator=(AVLNode& node) noexcept {return *new(this) Iterator(&node);}
  311. AVLTree::Iterator& AVLTree::Iterator::operator++() {
  312. if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
  313. elif(node->isLeft()) node = node->parent;
  314. elif(node->isRight()) {
  315. AVLNode* tmp = node->parent;
  316. node = node->parent->parent;
  317. while (tmp->isRight() && node) {tmp = node; node = node->parent;}
  318. } elif(node->isRoot()) node = nullptr;
  319. return *this;
  320. }
  321. AVLTree::Iterator& AVLTree::Iterator::operator--() {
  322. if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
  323. elif(node->isRight()) node = node->parent;
  324. elif(node->isLeft()) {
  325. AVLNode* tmp = node->parent;
  326. node = node->parent->parent;
  327. while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
  328. } elif(node->isRoot()) node = nullptr;
  329. return *this;
  330. }
  331. AVLTree::Iterator AVLTree::Iterator::operator++(int) {Iterator tmp(self); ++self; return tmp;}
  332. AVLTree::Iterator AVLTree::Iterator::operator--(int) {Iterator tmp(self); --self; return tmp;}
  333. const AVLTree::Iterator& AVLTree::Iterator::operator++() const {
  334. if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
  335. elif(node->isLeft()) node = node->parent;
  336. elif(node->isRight()) {
  337. AVLNode* tmp = node->parent;
  338. node = node->parent->parent;
  339. while (tmp->isRight() && node) {tmp = node; node = node->parent;}
  340. } elif(node->isRoot()) node = nullptr;
  341. return *this;
  342. }
  343. const AVLTree::Iterator& AVLTree::Iterator::operator--() const {
  344. if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
  345. elif(node->isRight()) node = node->parent;
  346. elif(node->isLeft()) {
  347. AVLNode* tmp = node->parent;
  348. node = node->parent->parent;
  349. while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
  350. } elif(node->isRoot()) node = nullptr;
  351. return *this;
  352. }
  353. const AVLTree::Iterator AVLTree::Iterator::operator++(int) const {Iterator tmp(self); ++self; return tmp;}
  354. const AVLTree::Iterator AVLTree::Iterator::operator--(int) const {Iterator tmp(self); --self; return tmp;}
  355. bool AVLTree::Iterator::operator==(Iterator other) const noexcept {return node == other.node;}
  356. bool AVLTree::Iterator::operator!=(Iterator other) const noexcept {return node != other.node;}
  357. AVLNode& AVLTree::Iterator::operator*() {return *node;}
  358. AVLNode* AVLTree::Iterator::operator->() {return node;}
  359. const AVLNode& AVLTree::Iterator::operator*() const {return *node;}
  360. const AVLNode* AVLTree::Iterator::operator->() const {return node;}
  361. AVLTree::ReverseIterator::ReverseIterator(AVLNode* node) : node(node) {}
  362. AVLTree::ReverseIterator::ReverseIterator(const ReverseIterator& other) : node(other.node) {}
  363. AVLTree::ReverseIterator::ReverseIterator(ReverseIterator&& other) : node(other.node) {}
  364. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(ReverseIterator& other) noexcept {return *new(this) ReverseIterator(other);}
  365. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(ReverseIterator&& other) noexcept {return *new(this) ReverseIterator(std::move(other));}
  366. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(AVLNode* node) noexcept {return *new(this) ReverseIterator(node);}
  367. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(AVLNode& node) noexcept {return *new(this) ReverseIterator(&node);}
  368. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator++() {
  369. if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
  370. elif(node->isRight()) node = node->parent;
  371. elif(node->isLeft()) {
  372. AVLNode* tmp = node->parent;
  373. node = node->parent->parent;
  374. while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
  375. } elif(node->isRoot()) node = nullptr;
  376. return *this;
  377. }
  378. AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator--() {
  379. if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
  380. elif(node->isLeft()) node = node->parent;
  381. elif(node->isRight()) {
  382. AVLNode* tmp = node->parent;
  383. node = node->parent->parent;
  384. while (tmp->isRight() && node) {tmp = node; node = node->parent;}
  385. } elif(node->isRoot()) node = nullptr;
  386. return *this;
  387. }
  388. AVLTree::ReverseIterator AVLTree::ReverseIterator::operator++(int) {ReverseIterator tmp(self); ++self; return tmp;}
  389. AVLTree::ReverseIterator AVLTree::ReverseIterator::operator--(int) {ReverseIterator tmp(self); --self; return tmp;}
  390. const AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator++() const {
  391. if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
  392. elif(node->isRight()) node = node->parent;
  393. elif(node->isLeft()) {
  394. AVLNode* tmp = node->parent;
  395. node = node->parent->parent;
  396. while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
  397. } elif(node->isRoot()) node = nullptr;
  398. return *this;
  399. }
  400. const AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator--() const {
  401. if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
  402. elif(node->isLeft()) node = node->parent;
  403. elif(node->isRight()) {
  404. AVLNode* tmp = node->parent;
  405. node = node->parent->parent;
  406. while (tmp->isRight() && node) {tmp = node; node = node->parent;}
  407. } elif(node->isRoot()) node = nullptr;
  408. return *this;
  409. }
  410. const AVLTree::ReverseIterator AVLTree::ReverseIterator::operator++(int) const {ReverseIterator tmp(self); ++self; return tmp;}
  411. const AVLTree::ReverseIterator AVLTree::ReverseIterator::operator--(int) const {ReverseIterator tmp(self); --self; return tmp;}
  412. bool AVLTree::ReverseIterator::operator==(ReverseIterator other) const noexcept {return node == other.node;}
  413. bool AVLTree::ReverseIterator::operator!=(ReverseIterator other) const noexcept {return node != other.node;}
  414. AVLNode& AVLTree::ReverseIterator::operator*() {return *node;}
  415. AVLNode* AVLTree::ReverseIterator::operator->() {return node;}
  416. const AVLNode& AVLTree::ReverseIterator::operator*() const {return *node;}
  417. const AVLNode* AVLTree::ReverseIterator::operator->() const {return node;}