1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
//* searchtree.h
#include <vector>
using namespace std;
namespace st {
template<class _TYPE, class _VAL_TYPE>
class bbst
{
protected:
typedef _VAL_TYPE (valfunc) (_TYPE);
public:
/* Constructors */
bbst ();
//Default Constructor - only works with int/double/unsigned etc, number values as _TYPE
bbst (valfunc);
//Overloaded Constructor - required when _TYPE isnt an int/double/unsigned etc
//@param valfunc - a function that takes _TYPE and returns _VAL_TYPE and _VAL_TYPE must be a number type
//NOTE: this is how each node gets its number value from strings/objects etc
bbst<_TYPE, _VAL_TYPE> add_value (_TYPE);
//bbst<_TYPE, _VAL_TYPE>::add_value - adds the given value to the given tree
//@param _TYPE - the value to be added to the tree
//@return *bbst<_TYPE, _VAL_TYPE> *- the node that was added to the tree
void add_value (_TYPE[], int);
//bbst<_TYPE, _VAL_TYPE>::add_value - adds the given array to the tree
//@param _TYPE[] - the array to add to the tree
//@param int - from 0 - what index should be added to the tree
void add_value (vector<_TYPE>&);
//bbst<_TYPE, _VAL_TYPE>::add_value - adds the given vector to the tree
//@param vector<_TYPE>& - the vector to be added to the tree
/* Destructors */
void delete_node ();
//bbst<_TYPE, _VAL_TYPE>::delete_node - deletes the given node
void delete_tree ();
//bbst<_TYPE, _VAL_TYPE>::delete_tree - deletes the given tree
/* Accessor Functions */
_TYPE get_value ();
//bbst<_TYPE, _VAL_TYPE>::get_value - gets the value of the given node
unsigned int get_size ();
//bbst<_TYPE, _VAL_TYPE>::get_size - gets the size of the given node (size of its children branches + itself)
unsigned int get_depth ();
//bbst<_TYPE, _VAL_TYPE>::get_depth - gets the depth of the given node (how many layers down from 0 it is)
/* Mutator Functions */
void set_value (_TYPE);
//bbst<_TYPE, _VAL_TYPE>::set_value - sets the value of the given node
//@param _TYPE - the new value of the node
void set_autoRefresh (bool);
//bbst<_TYPE, _VAL_TYPE>::set_autoRefresh - sets the autoRefresh member, if true every time a node is moved it will check the tree if it should be refreshed
//@param bool - the new value of autoRefresh
/* Utility Functions */
bbst<_TYPE, _VAL_TYPE> find (_TYPE);
//bbst<_TYPE, _VAL_TYPE>::find - searches the tree for the given value
//@param _TYPE - the value to search for
//@return *bbst<_TYPE, _VAL_TYPE> *- the node with the closest given value
void refresh_tree ();
//bbst<_TYPE, _VAL_TYPE>::refresh_tree - refreshes the given tree (adjusts branches to minimize the search calls)
bbst (_TYPE);
//Overloaded Constructor
//@param _TYPE - the value to be used for the new node
~bbst ();
//Default Destructor
void remove (bool);
//bst<_TYPE, _VAL_TYPE>::remove - removes the given node from the tree
//@param bool - consider twins? if true it removes the node from the list of twins, if false it removes the node but twins remain attached
void insert (bbst<_TYPE, _VAL_TYPE>*);
//bst<_TYPE, _VAL_TYPE>::insert - inserts the given node at the given location
//@param bst<_TYPE, _VAL_TYPE> - the location to insert the given node
void rebalance ();
//bbst<_TYPE, _VAL_TYPE>::rebalance - rebalances the given node
void update_depth ();
//bbst<_TYPE, _VAL_TYPE>::update_depth - updates the depth of every node from the given branch
_TYPE userValue;
_VAL_TYPE value;
bool autoRefresh;
unsigned int size,
depth;
valfunc val_update;
bbst *parent,
*left,
*right,
*sibling,
*root,
*next,
*prev,
*nextLeaf,
*prevLeaf,
*nextTwin,
*prevTwin;
};
}
using namespace st;
template<class _TYPE, class _VAL_TYPE>
bbst<_TYPE, _VAL_TYPE>::bbst()
{
}
template<class _TYPE, class _VAL_TYPE>
bbst<_TYPE, _VAL_TYPE>::~bbst()
{
}
int main(int argc, char** argv)
{
st::bbst<int, int> x;
}
|