33class EXPORTED_PUBLIC
Tree {
39 struct Node* leftChild =
nullptr;
40 struct Node* rightChild =
nullptr;
41 struct Node* parent =
nullptr;
70 void reset(
Node* node,
Node* prev,
size_t n) {
88 if ((pPreviousNode == pNode->parent) && pNode->leftChild) {
89 pPreviousNode = pNode;
90 pNode = pNode->leftChild;
92 }
else if ((((pNode->leftChild) && (pPreviousNode == pNode->leftChild)) ||
93 ((!pNode->leftChild) && (pPreviousNode != pNode))) &&
94 (pPreviousNode != pNode->rightChild)) {
95 pPreviousNode = pNode;
96 }
else if ((pPreviousNode == pNode) && pNode->rightChild) {
97 pPreviousNode = pNode;
98 pNode = pNode->rightChild;
101 pPreviousNode = pNode;
102 pNode = pNode->parent;
110 typedef ::TreeIterator<E, IteratorNode, &IteratorNode::previous, &IteratorNode::next, K>
Iterator;
115 Tree() : root(0), nItems(0), m_Begin(0) {}
119 Tree(
const Tree& x) : root(0), nItems(0), m_Begin(0) {
149 void insert(
const K& key,
const E& value) {
150 bool inserted =
false;
151 Node* insertionNode = createInsertionNode(key, inserted);
152 insertionNode->element = value;
161 bool inserted =
false;
162 Node* insertionNode = createInsertionNode(key, inserted);
163 insertionNode->element = pedigree_std::move(value);
170 bool inserted =
false;
171 Node* node = createInsertionNode(key, inserted);
174 node->element = value;
180 bool tryInsert(
const K& key, E&& value) {
181 bool inserted =
false;
182 Node* node = createInsertionNode(key, inserted);
185 node->element = pedigree_std::move(value);
198 else if (n->key > key)
223 const E&
lookupRef(
const K& key,
const E& failed = E())
const {
228 else if (n->key > key)
239 bool lowerBound(
const K& key, K& foundKey, E& foundValue)
const {
240 const Node* n = root;
241 const Node* bound =
nullptr;
246 }
else if (n->key > key) {
256 foundKey = bound->key;
257 foundValue = bound->element;
263 bool floorBound(
const K& key, K& foundKey, E& foundValue)
const {
264 const Node* n = root;
265 const Node* bound =
nullptr;
270 }
else if (n->key > key) {
280 foundKey = bound->key;
281 foundValue = bound->element;
292 else if (n->key > key)
306 else if (n->key > key)
316 while (n->leftChild || n->rightChild)
318 size_t hl = height(n->leftChild);
319 size_t hr = height(n->rightChild);
338 if (n->parent->leftChild == n)
339 n->parent->leftChild = 0;
341 n->parent->rightChild = 0;
346 int b = balanceFactor(n);
347 if ((b < -1) || (b > 1))
363 bool take(
const K& key, E& element) {
368 else if (n->key > key)
377 element = pedigree_std::move(n->element);
384 traverseNode_Remove(root);
409 m_Begin->reset(root, 0, nItems);
421 m_Begin->reset(root, 0, nItems);
437 void copyFrom(
const Tree& other) {
440 traverseNode_Insert(other.root);
444 m_Begin =
new IteratorNode(root, 0, nItems);
447 void rotateLeft(Node* n) {
449 Node* y = n->rightChild;
451 n->rightChild = y->leftChild;
452 if (y->leftChild != 0)
453 y->leftChild->parent = n;
455 y->parent = n->parent;
458 else if (n == n->parent->leftChild)
459 n->parent->leftChild = y;
461 n->parent->rightChild = y;
466 void rotateRight(Node* n) {
467 Node* y = n->leftChild;
469 n->leftChild = y->rightChild;
470 if (y->rightChild != 0)
471 y->rightChild->parent = n;
473 y->parent = n->parent;
476 else if (n == n->parent->leftChild)
477 n->parent->leftChild = y;
479 n->parent->rightChild = y;
485 size_t height(Node* n) {
495 if (n->leftChild != 0)
496 tempL = n->leftChild->height;
497 if (n->rightChild != 0)
498 tempR = n->rightChild->height;
515 int balanceFactor(Node* n) {
516 return static_cast<int>(height(n->rightChild)) -
static_cast<int>(height(n->leftChild));
519 void rebalanceNode(Node* n) {
523 int balance = balanceFactor(n);
526 if (balanceFactor(n->leftChild) > 0)
530 rotateLeft(n->leftChild);
536 }
else if (balance > 1) {
537 if (balanceFactor(n->rightChild) < 0)
541 rotateRight(n->rightChild);
550 void traverseNode_Insert(Node* n) {
553 insert(n->key, n->element);
554 traverseNode_Insert(n->leftChild);
555 traverseNode_Insert(n->rightChild);
558 void traverseNode_Remove(Node* n) {
562 Node* left = n->leftChild;
563 Node* right = n->rightChild;
564 n->leftChild =
nullptr;
565 n->rightChild =
nullptr;
567 traverseNode_Remove(left);
568 traverseNode_Remove(right);
572 Node* createInsertionNode(
const K& key,
bool& inserted) {
574 Node* parent =
nullptr;
575 Node* current = root;
577 if (key == current->key)
580 current = key > current->key ? current->rightChild : current->leftChild;
583 Node* node =
new Node;
587 node->parent = parent;
588 IteratorNode* beginning = m_Begin;
590 beginning =
new IteratorNode;
601 m_Begin->reset(root,
nullptr, 1);
602 }
else if (key > parent->key) {
603 parent->rightChild = node;
605 parent->leftChild = node;
609 const int balance = balanceFactor(parent);
610 if (balance < -1 || balance > 1)
611 rebalanceNode(parent);
612 parent = parent->parent;
620 mutable IteratorNode* m_Begin;