A binary search tree and an AVL balanced binary search tree. More...
#include <DS_BinarySearchTree.h>
Inherited by DataStructures::AVLBalancedBinarySearchTree< BinarySearchTreeType >.
Classes | |
struct | node |
Public Member Functions | |
BinarySearchTree () | |
virtual | ~BinarySearchTree () |
BinarySearchTree (const BinarySearchTree &original_type) | |
BinarySearchTree & | operator= (const BinarySearchTree &original_copy) |
unsigned int | Size (void) |
void | Clear (const char *file, unsigned int line) |
unsigned int | Height (node *starting_node=0) |
node * | Add (const BinarySearchTreeType &input, const char *file, unsigned int line) |
node * | Del (const BinarySearchTreeType &input, const char *file, unsigned int line) |
bool | IsIn (const BinarySearchTreeType &input) |
void | DisplayInorder (BinarySearchTreeType *return_array) |
void | DisplayPreorder (BinarySearchTreeType *return_array) |
void | DisplayPostorder (BinarySearchTreeType *return_array) |
void | DisplayBreadthFirstSearch (BinarySearchTreeType *return_array) |
BinarySearchTreeType *& | GetPointerToNode (const BinarySearchTreeType &element) |
Protected Types | |
enum | Direction_Types { NOT_FOUND, LEFT, RIGHT, ROOT } |
Protected Member Functions | |
unsigned int | HeightRecursive (node *current) |
node *& | Find (const BinarySearchTreeType &element, node **parent) |
node *& | FindParent (const BinarySearchTreeType &element) |
void | DisplayPostorderRecursive (node *current, BinarySearchTreeType *return_array, unsigned int &index) |
void | FixTree (node *current) |
Protected Attributes | |
node * | root |
enum DataStructures::BinarySearchTree::Direction_Types | direction |
unsigned int | BinarySearchTree_size |
A binary search tree and an AVL balanced binary search tree.
Initilize with the following structure
BinarySearchTree<TYPE>
OR
AVLBalancedBinarySearchTree<TYPE>
Use the AVL balanced tree if you want the tree to be balanced after every deletion and addition. This avoids the potential worst case scenario where ordered input to a binary search tree gives linear search time results. It's not needed if input will be evenly distributed, in which case the search time is O (log n). The search time for the AVL balanced binary tree is O (log n) irregardless of input.
Has the following member functions unsigned int Height(<index>) - Returns the height of the tree at the optional specified starting index. Default is the root add(element) - adds an element to the BinarySearchTree bool del(element) - deletes the node containing element if the element is in the tree as defined by a comparison with the == operator. Returns true on success, false if the element is not found bool IsInelement) - returns true if element is in the tree as defined by a comparison with the == operator. Otherwise returns false DisplayInorder(array) - Fills an array with an inorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. DisplayPreorder(array) - Fills an array with an preorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. DisplayPostorder(array) - Fills an array with an postorder search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. DisplayBreadthFirstSearch(array) - Fills an array with a breadth first search of the elements in the tree. USER IS REPONSIBLE FOR ALLOCATING THE ARRAY!. clear - Destroys the tree. Same as calling the destructor unsigned int Height() - Returns the height of the tree unsigned int size() - returns the size of the BinarySearchTree GetPointerToNode(element) - returns a pointer to the comparision element in the tree, allowing for direct modification when necessary with complex data types. Be warned, it is possible to corrupt the tree if the element used for comparisons is modified. Returns NULL if the item is not found
EXAMPLE
BinarySearchTree<int> A; A.Add(10); A.Add(15); A.Add(5); int* array = RakNet::OP_NEW<int >(A.Size(), __FILE__, __LINE__ ); A.DisplayInorder(array); array[0]; // returns 5 array[1]; // returns 10 array[2]; // returns 15
compress - reallocates memory to fit the number of elements. Best used when the number of elements decreases
clear - empties the BinarySearchTree and returns storage The assignment and copy constructors are defined
Definition at line 89 of file DS_BinarySearchTree.h.
enum DataStructures::BinarySearchTree::Direction_Types [protected] |
Definition at line 121 of file DS_BinarySearchTree.h.
DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree | ( | ) |
Definition at line 412 of file DS_BinarySearchTree.h.
DataStructures::BinarySearchTree< BinarySearchTreeType >::~BinarySearchTree | ( | ) | [virtual] |
DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree | ( | const BinarySearchTree< BinarySearchTreeType > & | original_type | ) |
BinarySearchTree< BinarySearchTreeType >::node * DataStructures::BinarySearchTree< BinarySearchTreeType >::Add | ( | const BinarySearchTreeType & | input, | |
const char * | file, | |||
unsigned int | line | |||
) |
void DataStructures::BinarySearchTree< BinarySearchTreeType >::Clear | ( | const char * | file, | |
unsigned int | line | |||
) | [inline] |
Definition at line 1077 of file DS_BinarySearchTree.h.
BinarySearchTree< BinarySearchTreeType >::node * DataStructures::BinarySearchTree< BinarySearchTreeType >::Del | ( | const BinarySearchTreeType & | input, | |
const char * | file, | |||
unsigned int | line | |||
) |
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayBreadthFirstSearch | ( | BinarySearchTreeType * | return_array | ) |
Definition at line 931 of file DS_BinarySearchTree.h.
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayInorder | ( | BinarySearchTreeType * | return_array | ) |
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorder | ( | BinarySearchTreeType * | return_array | ) | [inline] |
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorderRecursive | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current, | |
BinarySearchTreeType * | return_array, | |||
unsigned int & | index | |||
) | [protected] |
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPreorder | ( | BinarySearchTreeType * | return_array | ) |
node*& DataStructures::BinarySearchTree< BinarySearchTreeType >::Find | ( | const BinarySearchTreeType & | element, | |
node ** | parent | |||
) | [protected] |
BinarySearchTree< BinarySearchTreeType >::node *& DataStructures::BinarySearchTree< BinarySearchTreeType >::FindParent | ( | const BinarySearchTreeType & | element | ) | [protected] |
Definition at line 497 of file DS_BinarySearchTree.h.
void DataStructures::BinarySearchTree< BinarySearchTreeType >::FixTree | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current | ) | [protected] |
Definition at line 506 of file DS_BinarySearchTree.h.
BinarySearchTreeType *& DataStructures::BinarySearchTree< BinarySearchTreeType >::GetPointerToNode | ( | const BinarySearchTreeType & | element | ) |
Definition at line 425 of file DS_BinarySearchTree.h.
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::Height | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | starting_node = 0 |
) |
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::HeightRecursive | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current | ) | [protected] |
bool DataStructures::BinarySearchTree< BinarySearchTreeType >::IsIn | ( | const BinarySearchTreeType & | input | ) |
Definition at line 757 of file DS_BinarySearchTree.h.
BinarySearchTree< BinarySearchTreeType > & DataStructures::BinarySearchTree< BinarySearchTreeType >::operator= | ( | const BinarySearchTree< BinarySearchTreeType > & | original_copy | ) |
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::Size | ( | void | ) |
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree_size [protected] |
Definition at line 126 of file DS_BinarySearchTree.h.
enum DataStructures::BinarySearchTree::Direction_Types DataStructures::BinarySearchTree< BinarySearchTreeType >::direction [protected] |
node* DataStructures::BinarySearchTree< BinarySearchTreeType >::root [protected] |
Definition at line 119 of file DS_BinarySearchTree.h.