Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

DataStructures::BinarySearchTree< BinarySearchTreeType > Class Template Reference

A binary search tree and an AVL balanced binary search tree. More...

#include <DS_BinarySearchTree.h>

Inherited by DataStructures::AVLBalancedBinarySearchTree< BinarySearchTreeType >.

Collaboration diagram for DataStructures::BinarySearchTree< BinarySearchTreeType >:
Collaboration graph
[legend]

List of all members.

Classes

struct  node

Public Member Functions

 BinarySearchTree ()
virtual ~BinarySearchTree ()
 BinarySearchTree (const BinarySearchTree &original_type)
BinarySearchTreeoperator= (const BinarySearchTree &original_copy)
unsigned int Size (void)
void Clear (const char *file, unsigned int line)
unsigned int Height (node *starting_node=0)
nodeAdd (const BinarySearchTreeType &input, const char *file, unsigned int line)
nodeDel (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

noderoot
enum
DataStructures::BinarySearchTree::Direction_Types 
direction
unsigned int BinarySearchTree_size

Detailed Description

template<class BinarySearchTreeType>
class DataStructures::BinarySearchTree< BinarySearchTreeType >

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

Note:
The template type must have the copy constructor and assignment operator defined and must work with >, <, and == All elements in the tree MUST be distinct The assignment operator is defined between BinarySearchTree and AVLBalancedBinarySearchTree as long as they are of the same template type. However, passing a BinarySearchTree to an AVLBalancedBinarySearchTree will lose its structure unless it happened to be AVL balanced to begin with Requires queue_linked_list.cpp for the breadth first search used in the copy constructor, overloaded assignment operator, and display_breadth_first_search.

Definition at line 89 of file DS_BinarySearchTree.h.


Member Enumeration Documentation

template<class BinarySearchTreeType>
enum DataStructures::BinarySearchTree::Direction_Types [protected]
Enumerator:
NOT_FOUND 
LEFT 
RIGHT 
ROOT 

Definition at line 121 of file DS_BinarySearchTree.h.


Constructor & Destructor Documentation

template<class BinarySearchTreeType >
DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree (  ) 

Definition at line 412 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >
DataStructures::BinarySearchTree< BinarySearchTreeType >::~BinarySearchTree (  )  [virtual]

Definition at line 419 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType >
DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree ( const BinarySearchTree< BinarySearchTreeType > &  original_type  ) 

Definition at line 978 of file DS_BinarySearchTree.h.

Here is the call graph for this function:


Member Function Documentation

template<class BinarySearchTreeType>
BinarySearchTree< BinarySearchTreeType >::node * DataStructures::BinarySearchTree< BinarySearchTreeType >::Add ( const BinarySearchTreeType &  input,
const char *  file,
unsigned int  line 
)

Definition at line 675 of file DS_BinarySearchTree.h.

Here is the caller graph for this function:

template<class BinarySearchTreeType >
void DataStructures::BinarySearchTree< BinarySearchTreeType >::Clear ( const char *  file,
unsigned int  line 
) [inline]

Definition at line 1077 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

Here is the caller graph for this function:

template<class BinarySearchTreeType>
BinarySearchTree< BinarySearchTreeType >::node * DataStructures::BinarySearchTree< BinarySearchTreeType >::Del ( const BinarySearchTreeType &  input,
const char *  file,
unsigned int  line 
)

Definition at line 537 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType>
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayBreadthFirstSearch ( BinarySearchTreeType *  return_array  ) 

Definition at line 931 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

Here is the caller graph for this function:

template<class BinarySearchTreeType>
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayInorder ( BinarySearchTreeType *  return_array  ) 

Definition at line 770 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType>
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorder ( BinarySearchTreeType *  return_array  )  [inline]

Definition at line 897 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType>
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorderRecursive ( typename BinarySearchTree< BinarySearchTreeType >::node current,
BinarySearchTreeType *  return_array,
unsigned int &  index 
) [protected]

Definition at line 917 of file DS_BinarySearchTree.h.

Here is the caller graph for this function:

template<class BinarySearchTreeType>
void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPreorder ( BinarySearchTreeType *  return_array  ) 

Definition at line 841 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType>
node*& DataStructures::BinarySearchTree< BinarySearchTreeType >::Find ( const BinarySearchTreeType &  element,
node **  parent 
) [protected]

Here is the caller graph for this function:

template<class BinarySearchTreeType>
BinarySearchTree< BinarySearchTreeType >::node *& DataStructures::BinarySearchTree< BinarySearchTreeType >::FindParent ( const BinarySearchTreeType &  element  )  [protected]

Definition at line 497 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

Here is the caller graph for this function:

template<class BinarySearchTreeType >
void DataStructures::BinarySearchTree< BinarySearchTreeType >::FixTree ( typename BinarySearchTree< BinarySearchTreeType >::node current  )  [protected]

Definition at line 506 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType>
BinarySearchTreeType *& DataStructures::BinarySearchTree< BinarySearchTreeType >::GetPointerToNode ( const BinarySearchTreeType &  element  ) 

Definition at line 425 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

Here is the caller graph for this function:

template<class BinarySearchTreeType >
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::Height ( typename BinarySearchTree< BinarySearchTreeType >::node starting_node = 0  ) 

Definition at line 382 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType >
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::HeightRecursive ( typename BinarySearchTree< BinarySearchTreeType >::node current  )  [protected]

Definition at line 392 of file DS_BinarySearchTree.h.

Here is the caller graph for this function:

template<class BinarySearchTreeType>
bool DataStructures::BinarySearchTree< BinarySearchTreeType >::IsIn ( const BinarySearchTreeType &  input  ) 

Definition at line 757 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >
BinarySearchTree< BinarySearchTreeType > & DataStructures::BinarySearchTree< BinarySearchTreeType >::operator= ( const BinarySearchTree< BinarySearchTreeType > &  original_copy  ) 

Definition at line 1023 of file DS_BinarySearchTree.h.

Here is the call graph for this function:

template<class BinarySearchTreeType >
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::Size ( void   ) 

Definition at line 376 of file DS_BinarySearchTree.h.

Here is the caller graph for this function:


Member Data Documentation

template<class BinarySearchTreeType>
unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree_size [protected]

Definition at line 126 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType>
enum DataStructures::BinarySearchTree::Direction_Types DataStructures::BinarySearchTree< BinarySearchTreeType >::direction [protected]
template<class BinarySearchTreeType>
node* DataStructures::BinarySearchTree< BinarySearchTreeType >::root [protected]

Definition at line 119 of file DS_BinarySearchTree.h.


The documentation for this class was generated from the following file: