#include <avltree.h>
Public Member Functions | |
| AVLTree () | |
| The default constructor. | |
| virtual | ~AVLTree () |
| The destructor. | |
| bool | insert (Key const &_key, Data const &_data) |
| Inserts data into the tree. | |
| bool | erase (Key const &_key) |
| Deletes a node from the tree, specified by the node's key. | |
| bool | erase (Key const &_key, Data const &_data) |
| Deletes a node from the tree, specified by the node's key and data. | |
| bool | find (Key const &_key, Data &_data) const |
| Finds a node in the tree and copies the data from that node to a specified location. | |
| Data | find (Key const &_key) const |
| Finds a node in the tree and returns the data at that node. | |
| bool | exists (Key const &_key) const |
| Tests whether a key is in the tree or not. | |
| void | empty () |
| Empties the entire tree. | |
| size_t | size () const |
| Indicates the size of the tree. | |
| bool | replace (Key const &_key, Data const &_data) |
| Change the data at the given node. | |
| DArray< Data > * | ConvertToDArray () const |
| Converts the tree data into a linearized DArray. | |
| DArray< Key > * | ConvertIndexToDArray () const |
| Converts the tree keys into a linearized DArray. | |
| size_t | mem_usage () const |
| Returns the overhead caused by the data structure. | |
Protected Types | |
| enum | Result { OK, BALANCE, INVALID } |
| Result of tree operation. More... | |
Protected Member Functions | |
| void | rotateLeft (AVLNode< Key, Data > **_node) |
| Rotate tree left. | |
| void | rotateRight (AVLNode< Key, Data > **_node) |
| Rotate tree right. | |
| Result | balanceLeftGrown (AVLNode< Key, Data > **_node) |
| Rebalance tree. | |
| Result | balanceRightGrown (AVLNode< Key, Data > **_node) |
| Rebalance tree. | |
| Result | balanceLeftShrunk (AVLNode< Key, Data > **_node) |
| Rebalance tree. | |
| Result | balanceRightShrunk (AVLNode< Key, Data > **_node) |
| Rebalance tree. | |
| bool | replaceWithHighest (AVLNode< Key, Data > *_target, AVLNode< Key, Data > **_subtree, Result *_result) |
| Replace node. | |
| bool | replaceWithLowest (AVLNode< Key, Data > *_target, AVLNode< Key, Data > **_subtree, Result *_result) |
| Replace node. | |
| Result | insert (AVLNode< Key, Data > **_parent, AVLNode< Key, Data > **_node, Key const &_key, Data const &_data) |
| Add object. | |
| Result | erase (AVLNode< Key, Data > **_node, Key const &_key) |
| Remove object. | |
| Result | erase (AVLNode< Key, Data > **_node, Key const &_key, Data const &_data) |
| Remove object. | |
| AVLNode< Key, Data > * | findNode (Key const &_key) const |
| Find a node in the tree. | |
| void | RecursiveConvertIndexToDArray (DArray< Key > *_darray, AVLNode< Key, Data > *_btree) const |
| Recursively convert the tree's keys into a DArray. | |
| void | RecursiveConvertToDArray (DArray< Data > *_darray, AVLNode< Key, Data > *_btree) const |
| Recursively convert the tree's data into a DArray. | |
| bool | valid (const AVLNode< Key, Data > *_node) const |
| Verifies that a node is valid. | |
Protected Attributes | |
| AVLNode< Key, Data > * | m_root |
| The root node. | |
| size_t | m_size |
| The current tree size. | |
Private Member Functions | |
| AVLTree (const AVLTree< Key, Data > &) | |
| Private copy constructor. | |
| AVLTree< Key, Data > & | operator= (const AVLTree< Key, Data > &) |
| Private assignment operator. | |
AVL trees are quite useful for tasks that require very fast and well-balanced trees. Due to the ruleset implemented internally, AVL trees enforce a maximum height of 1.44*log(n).
enum Result [protected] |
Private copy constructor.
If your code needs to invoke the copy constructor, you've probably written the code wrong. A tree copy is generally unnecessary, and in cases that it is, it can be achieved by other means.
Rebalance tree.
Rebalance tree after left side has grown
| _node | Pointer to current node pointer to balance |
Rebalance tree.
Rebalance tree after left side has shrunk
| _node | Pointer to current node pointer to balance |
Rebalance tree.
Rebalance tree after right side has grown
| _node | Pointer to current node pointer to balance |
Rebalance tree.
Rebalance tree after right side has shrunk
| _node | Pointer to current node pointer to balance |
| DArray<Key>* ConvertIndexToDArray | ( | ) | const |
| DArray<Data>* ConvertToDArray | ( | ) | const |
| void empty | ( | ) | [inline] |
Empties the entire tree.
References AVLTree< Key, Data >::m_root, and AVLTree< Key, Data >::m_size.
| bool erase | ( | Key const & | _key, | |
| Data const & | _data | |||
| ) |
Deletes a node from the tree, specified by the node's key and data.
| _key | The key of the node to delete. | |
| _data | The data of the node to delete. |
| bool erase | ( | Key const & | _key | ) |
Deletes a node from the tree, specified by the node's key.
| _key | The key of the node to delete. |
Remove object.
Remove object from tree and rebalance, taking the key and data into account
| _node | Pointer to current node pointer | |
| _key | Identifier of node to remove | |
| _data | Data identifier of node to remove |
Remove object.
Remove object from tree and rebalance
| _node | Pointer to current node pointer | |
| _key | Identifier of node to remove |
| bool exists | ( | Key const & | _key | ) | const |
Tests whether a key is in the tree or not.
| _key | The key of the node to find. |
| Data find | ( | Key const & | _key | ) | const |
Finds a node in the tree and returns the data at that node.
| _key | The key of the node to find. |
| bool find | ( | Key const & | _key, | |
| Data & | _data | |||
| ) | const |
Finds a node in the tree and copies the data from that node to a specified location.
| _key | The key of the node to find. | |
| _data | On return, will contain the data at the node. If not found, _data does not change. |
| AVLNode<Key, Data>* findNode | ( | Key const & | _key | ) | const [protected] |
Find a node in the tree.
Get a pointer to a node with the specified key value
| _key | Identifier of node to remove |
| bool insert | ( | Key const & | _key, | |
| Data const & | _data | |||
| ) |
Inserts data into the tree.
| _key | The key of the data. | |
| _data | The data to insert. |
| Result insert | ( | AVLNode< Key, Data > ** | _parent, | |
| AVLNode< Key, Data > ** | _node, | |||
| Key const & | _key, | |||
| Data const & | _data | |||
| ) | [protected] |
Add object.
Insert object in tree and rebalance
| _parent | Pointer to parent node pointer | |
| _node | Pointer to current node pointer | |
| _key | Key to insert | |
| _data | Data to insert |
| size_t mem_usage | ( | ) | const |
Returns the overhead caused by the data structure.
Private assignment operator.
If your code needs to invoke the assignment operator, you've probably written the code wrong. A tree copy is generally unnecessary, and in cases that it is, it can be achieved by other means.
| void RecursiveConvertIndexToDArray | ( | DArray< Key > * | _darray, | |
| AVLNode< Key, Data > * | _btree | |||
| ) | const [protected] |
Recursively convert the tree's keys into a DArray.
| _darray | Array to insert keys into | |
| _btree | The node being traversed |
| void RecursiveConvertToDArray | ( | DArray< Data > * | _darray, | |
| AVLNode< Key, Data > * | _btree | |||
| ) | const [protected] |
Recursively convert the tree's data into a DArray.
| _darray | Array to insert data into | |
| _btree | The node being traversed |
| bool replace | ( | Key const & | _key, | |
| Data const & | _data | |||
| ) |
Change the data at the given node.
| _key | The key of the node to be modified. | |
| _data | The data to insert. |
| bool replaceWithHighest | ( | AVLNode< Key, Data > * | _target, | |
| AVLNode< Key, Data > ** | _subtree, | |||
| Result * | _result | |||
| ) | [inline, protected] |
Replace node.
Replace a node with the highest-ranking item in subtree
| _target | Pointer to node to be replaced | |
| _subtree | Pointer to subtree pointer to search | |
| _result | Pointer to result variable to tell caller if further checks are needed |
| bool replaceWithLowest | ( | AVLNode< Key, Data > * | _target, | |
| AVLNode< Key, Data > ** | _subtree, | |||
| Result * | _result | |||
| ) | [inline, protected] |
Replace node.
Replace a node with the lowest-ranking item in subtree
| _target | Pointer to node to be replaced | |
| _subtree | Pointer to subtree pointer to search | |
| _result | Pointer to result variable to tell caller if further checks are needed |
| void rotateLeft | ( | AVLNode< Key, Data > ** | _node | ) | [inline, protected] |
Rotate tree left.
Rotate tree left around the given node
| _node | Pointer to current node pointer to rotate |
| void rotateRight | ( | AVLNode< Key, Data > ** | _node | ) | [inline, protected] |
Rotate tree right.
Rotate tree right around the given node
| _node | Pointer to current node pointer to rotate |
| size_t size | ( | ) | const [inline] |
| bool valid | ( | const AVLNode< Key, Data > * | _node | ) | const [inline, protected] |
Verifies that a node is valid.
| _node | A node pointer. |
1.5.8