[4d65515] | 1 | /*
|
---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_INTERNAL_RBTREE_NODE
|
---|
| 30 | #define LIBCPP_INTERNAL_RBTREE_NODE
|
---|
| 31 |
|
---|
| 32 | namespace std::aux
|
---|
| 33 | {
|
---|
| 34 | enum class rbcolor
|
---|
| 35 | {
|
---|
| 36 | red, black
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | template<class T>
|
---|
| 40 | struct rbtree_node
|
---|
| 41 | {
|
---|
| 42 | T value;
|
---|
| 43 | rbcolor color;
|
---|
| 44 |
|
---|
| 45 | rbtree_node* parent;
|
---|
| 46 | rbtree_node* left;
|
---|
| 47 | rbtree_node* right;
|
---|
| 48 |
|
---|
| 49 | template<class... Args>
|
---|
| 50 | rbtree_node(Args&&... args)
|
---|
| 51 | : value{forward<Args>(args)...}, color{rbcolor::red},
|
---|
| 52 | parent{}, left{}, right{}
|
---|
| 53 | { /* DUMMY BODY */ }
|
---|
| 54 |
|
---|
| 55 | rbtree_node* grandparent() const
|
---|
| 56 | {
|
---|
| 57 | if (parent)
|
---|
| 58 | return parent->parent;
|
---|
| 59 | else
|
---|
| 60 | return nullptr;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | rbtree_node* brother() const
|
---|
| 64 | {
|
---|
| 65 | if (parent)
|
---|
| 66 | {
|
---|
| 67 | if (this == parent->left)
|
---|
| 68 | return parent->right;
|
---|
| 69 | else
|
---|
| 70 | return parent->left;
|
---|
| 71 | }
|
---|
| 72 | else
|
---|
| 73 | return nullptr;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | rbtree_node* uncle() const
|
---|
| 77 | {
|
---|
| 78 | if (grandparent())
|
---|
| 79 | {
|
---|
| 80 | if (parent == grandparent()->left)
|
---|
| 81 | return grandparent()->right;
|
---|
| 82 | else
|
---|
| 83 | return grandparent()->left;
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | return nullptr;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | bool is_left_child() const
|
---|
| 90 | {
|
---|
| 91 | if (parent)
|
---|
| 92 | return parent->left == this;
|
---|
| 93 | else
|
---|
| 94 | return false;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void rotate_left()
|
---|
| 98 | {
|
---|
| 99 | // TODO:
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void rotate_right()
|
---|
| 103 | {
|
---|
| 104 | // TODO:
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | rbtree_node* find_smallest()
|
---|
| 108 | {
|
---|
| 109 | auto res = this;
|
---|
| 110 | while (res->left)
|
---|
| 111 | res = res->left;
|
---|
| 112 |
|
---|
| 113 | return res;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | rbtree_node* find_largest()
|
---|
| 117 | {
|
---|
| 118 | auto res = this;
|
---|
| 119 | while (res->right)
|
---|
| 120 | res = res->right;
|
---|
| 121 |
|
---|
| 122 | return res;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | void add_left_child(rbtree_node* node)
|
---|
| 126 | {
|
---|
| 127 | if (left)
|
---|
| 128 | return;
|
---|
| 129 |
|
---|
| 130 | left = node;
|
---|
| 131 | node->parent = this;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void add_right_child(rbtree_node* node)
|
---|
| 135 | {
|
---|
| 136 | if (right)
|
---|
| 137 | return;
|
---|
| 138 |
|
---|
| 139 | right = node;
|
---|
| 140 | node->parent = this;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | ~rbtree_node()
|
---|
| 144 | {
|
---|
| 145 | // TODO: delete recursively or iteratively?
|
---|
| 146 | }
|
---|
| 147 | };
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | #endif
|
---|