20 #ifndef KERNEL_UTILITIES_TREE_H 21 #define KERNEL_UTILITIES_TREE_H 23 #include "pedigree/kernel/compiler.h" 24 #include "pedigree/kernel/processor/types.h" 25 #include "pedigree/kernel/utilities/Iterator.h" 32 template <
class K,
class E>
41 struct Node *leftChild =
nullptr;
42 struct Node *rightChild =
nullptr;
43 struct Node *parent =
nullptr;
58 : value(node), pNode(node), pPreviousNode(prev)
79 void reset(
Node *node,
Node *prev,
size_t n)
99 if ((pPreviousNode == pNode->parent) && pNode->leftChild)
101 pPreviousNode = pNode;
102 pNode = pNode->leftChild;
106 (((pNode->leftChild) && (pPreviousNode == pNode->leftChild)) ||
107 ((!pNode->leftChild) && (pPreviousNode != pNode))) &&
108 (pPreviousNode != pNode->rightChild))
110 pPreviousNode = pNode;
112 else if ((pPreviousNode == pNode) && pNode->rightChild)
114 pPreviousNode = pNode;
115 pNode = pNode->rightChild;
120 pPreviousNode = pNode;
121 pNode = pNode->parent;
129 typedef ::TreeIterator<
130 E,
IteratorNode, &IteratorNode::previous, &IteratorNode::next, K>
136 Tree() : root(0), nItems(0), m_Begin(0)
142 Tree(
const Tree &x) : root(0), nItems(0), m_Begin(0)
173 void insert(
const K &key,
const E &value)
175 Node *insertionNode = createInsertionNode(key);
176 insertionNode->element = value;
185 Node *insertionNode = createInsertionNode(key);
186 insertionNode->element = pedigree_std::move(value);
199 else if (n->key > key)
209 const E &
lookupRef(
const K &key,
const E &failed=E())
const 216 else if (n->key > key)
233 else if (n->key > key)
242 void remove(
const K &key)
249 else if (n->key > key)
259 while (n->leftChild || n->rightChild)
261 size_t hl = height(n->leftChild);
262 size_t hr = height(n->rightChild);
285 if (n->parent->leftChild == n)
286 n->parent->leftChild = 0;
288 n->parent->rightChild = 0;
294 int b = balanceFactor(n);
295 if ((b < -1) || (b > 1))
307 traverseNode_Remove(root);
330 m_Begin =
new IteratorNode(root, 0, nItems);
334 m_Begin->reset(root, 0, nItems);
338 return Iterator(m_Begin);
360 void copyFrom(
const Tree &other)
364 traverseNode_Insert(other.root);
368 m_Begin =
new IteratorNode(root, 0, nItems);
371 void rotateLeft(Node *n)
374 Node *y = n->rightChild;
378 if (y->leftChild != 0)
379 y->leftChild->parent = n;
381 y->parent = n->parent;
384 else if (n == n->parent->leftChild)
385 n->parent->leftChild = y;
387 n->parent->rightChild = y;
392 void rotateRight(Node *n)
394 Node *y = n->leftChild;
396 n->leftChild = y->rightChild;
397 if (y->rightChild != 0)
398 y->rightChild->parent = n;
400 y->parent = n->parent;
403 else if (n == n->parent->leftChild)
404 n->parent->leftChild = y;
406 n->parent->rightChild = y;
412 size_t height(Node *n)
423 if (n->leftChild != 0)
424 tempL = n->leftChild->height;
425 if (n->rightChild != 0)
426 tempR = n->rightChild->height;
445 int balanceFactor(Node *n)
447 return static_cast<int>(height(n->rightChild)) -
448 static_cast<int>(height(n->leftChild));
451 void rebalanceNode(Node *n)
456 int balance = balanceFactor(n);
459 if (balanceFactor(n->leftChild) >
464 rotateLeft(n->leftChild);
473 else if (balance > 1)
475 if (balanceFactor(n->rightChild) <
480 rotateRight(n->rightChild);
491 void traverseNode_Insert(Node *n)
495 insert(n->key, n->element);
496 traverseNode_Insert(n->leftChild);
497 traverseNode_Insert(n->rightChild);
500 void traverseNode_Remove(Node *n)
505 Node *left = n->leftChild;
506 Node *right = n->rightChild;
507 n->leftChild =
nullptr;
508 n->rightChild =
nullptr;
510 traverseNode_Remove(left);
511 traverseNode_Remove(right);
515 Node *createInsertionNode(
const K &key)
517 Node *insertionNode =
nullptr;
521 insertionNode =
new Node;
522 insertionNode->key = key;
524 root = insertionNode;
530 m_Begin =
new IteratorNode(root, 0, nItems);
535 Node *currentNode = root;
537 bool inserted =
false;
540 if (key > currentNode->key)
542 if (currentNode->rightChild ==
545 insertionNode =
new Node;
546 insertionNode->key = key;
547 insertionNode->parent = currentNode;
548 currentNode->rightChild = insertionNode;
553 currentNode = currentNode->rightChild;
556 else if (key == currentNode->key)
559 insertionNode = currentNode;
564 if (currentNode->leftChild ==
567 insertionNode =
new Node;
568 insertionNode->key = key;
569 insertionNode->parent = currentNode;
570 currentNode->leftChild = insertionNode;
575 currentNode = currentNode->leftChild;
584 int b = balanceFactor(currentNode);
585 if ((b < -1) || (b > 1))
587 rebalanceNode(currentNode);
589 currentNode = currentNode->parent;
593 return insertionNode;
599 IteratorNode *m_Begin;
const E & lookupRef(const K &key, const E &failed=E()) const
void insert(const K &key, E &&value)
ConstIterator begin() const
Tree & operator=(const Tree &x)
void insert(const K &key, const E &value)
bool contains(const K &key) const
An iterator applicable for many data structures.
void erase(Iterator iter)
ConstIterator end() const
E lookup(const K &key) const