Intrusive Containers
Loading...
Searching...
No Matches
BalTreeIn.h File Reference

Intrusive Binary Tree (balenced). More...

Include dependency graph for BalTreeIn.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  BalTreeInNode< R, N, K, s, n >
 Intrusive Binary Tree, Node. More...
 
class  BalTreeInRoot< R, N, K, s, n >
 Intrusive Binary Tree, Root. More...
 

Detailed Description

Intrusive Binary Tree (balenced).

This file defines a pair of templates (BalTreeInRoot and BalTreeInNode) that implement an intrusive binary tree.

Overview

BalTreeInRoot and BalTreeInNode provide a simple API to allow classes to provide a basic Tree <-> Node relationship (1 Tree Root holding many Nodes) as a Binary Tree.

Note that this class makes no attempt to "balence" the tree, but is designed that it can be derived from by a class to implement any needed balencing, but does supply the rotations to do that balencing.

To create this relationship, the class to act as the tree root derives from the template TreeInRoot, and the class to act as the Node from TreeInNode, both with a first parameter of the class name of the List class, a second parameter of the Node class, and a 3rd type parameter for the type for the Key to use in searching the tree. There is an optional integral 4rd parameter to allow the classes to inherent from these multiple times if you need to represent multiple simultaneous relationships. This inheritance should be public, or you need to add the TreeInRoot and TreeInNode templates as friends.

At the point of declaration, both classes need to be declared, so you will typically have a forward of the other class before the definition of the class. At the point of usage of members, generally the full definition of both classes is needed to instance the code for the template.

The user of the class will need to define two member functions to configure the tree. int TreeInRoot::compare(N& node1, N& node2) const; and int TreeInRoot::compare(N& node, K key) const;

Inside this file, the following types have the following meanings:

R: The "user" type that will be derived from TreeInRoot

N: The "user" type that will be derived from TreeInNode

K: The "user" type that represents the Key for the tree.

n: The integer parameter for the template to allow multiple usage

List: The particular instance of TreeInRoot<R, N, K, n> being used

Node: The particular instance of TreeInNode<R, N, K, n> being used

node: will have type N*, to be used when walking the list.

Invariant

From TreeIn.h:

Given:

  • R& root
  • N& node

TreeInRoot:

  • if root.m_base != nullptr:
    • root.m_base-> m_root == &root
    • root.m_base-> m_parent == nullptr

TreeInNode:

  • if node.m_root == NULL: // Free Node
    • node.m_parent == NULL
    • node.m_left == NULL
    • node.m_right == NULL
  • if node.m_root != NULL and node.m_parent == NULL // Base Node
    • node.m_root->m_base == &node
  • if node.m_parent != nullptr: // Child Node
    • Either node.m_parent->m_left or node.m_parent->m_right == &node
  • if node.m_left != nullptr
    • node.m_left->m_parent = &node
    • node.m_left->m_root == node.m_root
    • node.m_root->compare(node, *m_left) <= 0
    • node.m_root->compare(*m_left, node) >= 0
  • if node.m_right != nullptr:
    • node.m_right->m_parent = &node
    • node.m_right->m_root == node.m_root
    • node.m_root->compare(node, *m_right) >= 0
    • node.m_root->compare(*m_right, node) <= 0