SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
DataStructures::BinarySearchTree< BinarySearchTreeType > Class Template Reference

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

`#include <slikenet/DS_BinarySearchTree.h>`

Inheritance diagram for DataStructures::BinarySearchTree< 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

## 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 = SLNet::OP_NEW<int >(A.Size(), _FILE_AND_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 97 of file DS_BinarySearchTree.h.

## Class Documentation

struct DataStructures::BinarySearchTree::node

### template<class BinarySearchTreeType>
struct DataStructures::BinarySearchTree< BinarySearchTreeType >::node

Definition at line 102 of file DS_BinarySearchTree.h.

Class Members
BinarySearchTreeType * | item |
node * | left |
node * | right |

## Member Enumeration Documentation

template<class BinarySearchTreeType>

enum DataStructures::BinarySearchTree::Direction_Types

protected

Enumerator:

  NOT_FOUND |
  LEFT |
  RIGHT |
  ROOT |

Definition at line 129 of file DS_BinarySearchTree.h.

## Constructor & Destructor Documentation

template<class BinarySearchTreeType >

DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree | ( |  | ) |

Definition at line 420 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >

DataStructures::BinarySearchTree< BinarySearchTreeType >::~BinarySearchTree | ( |  | ) |

virtual

Definition at line 427 of file DS_BinarySearchTree.h.

References _FILE_AND_LINE_.

template<class BinarySearchTreeType >

DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree | ( | const BinarySearchTree< BinarySearchTreeType > & | original_type | ) |

Definition at line 980 of file DS_BinarySearchTree.h.

References _FILE_AND_LINE_, DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree_size, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, DataStructures::QueueLinkedList< QueueType >::Pop(), DataStructures::QueueLinkedList< QueueType >::Push(), DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right, DataStructures::BinarySearchTree< BinarySearchTreeType >::root, and DataStructures::QueueLinkedList< QueueType >::Size().

## 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 680 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

Referenced by DataStructures::AVLBalancedBinarySearchTree< BinarySearchTreeType >::Add().

template<class BinarySearchTreeType >

void DataStructures::BinarySearchTree< BinarySearchTreeType >::Clear | ( | const char * | file,
|  | unsigned int | line
| ) |  |

inline

Definition at line 1079 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, SLNet::OP_DELETE(), and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

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

Definition at line 542 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, SLNet::OP_DELETE(), and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

Referenced by DataStructures::AVLBalancedBinarySearchTree< BinarySearchTreeType >::Del().

template<class BinarySearchTreeType >

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

Definition at line 933 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, DataStructures::QueueLinkedList< QueueType >::Pop(), DataStructures::QueueLinkedList< QueueType >::Push(), return, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right, and DataStructures::QueueLinkedList< QueueType >::Size().

template<class BinarySearchTreeType >

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

Definition at line 772 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, return, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorder | ( | BinarySearchTreeType * | return_array | ) |

inline

Definition at line 899 of file DS_BinarySearchTree.h.

References return.

template<class BinarySearchTreeType >

void DataStructures::BinarySearchTree< BinarySearchTreeType >::DisplayPostorderRecursive | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current,
|  | BinarySearchTreeType * | return_array,
|  | unsigned int & | index
| ) |  |

protected

Definition at line 919 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

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

Definition at line 843 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, return, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType>

BinarySearchTree< BinarySearchTreeType >::node *& DataStructures::BinarySearchTree< BinarySearchTreeType >::Find | ( | const BinarySearchTreeType & | element,
|  | node ** | parent
| ) |  |

protected

Definition at line 446 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

BinarySearchTree< BinarySearchTreeType >::node *& DataStructures::BinarySearchTree< BinarySearchTreeType >::FindParent | ( | const BinarySearchTreeType & | element | ) |

protected

Definition at line 502 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >

void DataStructures::BinarySearchTree< BinarySearchTreeType >::FixTree | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current | ) |

protected

Definition at line 511 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

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

Definition at line 433 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item.

template<class BinarySearchTreeType >

unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::Height | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | starting_node = `0` | ) |

Definition at line 390 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >

unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::HeightRecursive | ( | typename BinarySearchTree< BinarySearchTreeType >::node * | current | ) |

protected

Definition at line 400 of file DS_BinarySearchTree.h.

References DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, and DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right.

template<class BinarySearchTreeType >

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

Definition at line 759 of file DS_BinarySearchTree.h.

template<class BinarySearchTreeType >

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

Definition at line 1025 of file DS_BinarySearchTree.h.

References _FILE_AND_LINE_, DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree_size, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::item, DataStructures::BinarySearchTree< BinarySearchTreeType >::node::left, DataStructures::QueueLinkedList< QueueType >::Pop(), DataStructures::QueueLinkedList< QueueType >::Push(), DataStructures::BinarySearchTree< BinarySearchTreeType >::node::right, DataStructures::BinarySearchTree< BinarySearchTreeType >::root, and DataStructures::QueueLinkedList< QueueType >::Size().

Referenced by DataStructures::AVLBalancedBinarySearchTree< BinarySearchTreeType >::operator=().

template<class BinarySearchTreeType >

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

Definition at line 384 of file DS_BinarySearchTree.h.

## Member Data Documentation

template<class BinarySearchTreeType>

unsigned int DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree_size

protected

Definition at line 134 of file DS_BinarySearchTree.h.

Referenced by DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree(), and DataStructures::BinarySearchTree< BinarySearchTreeType >::operator=().

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 127 of file DS_BinarySearchTree.h.

Referenced by DataStructures::BinarySearchTree< BinarySearchTreeType >::BinarySearchTree(), and DataStructures::BinarySearchTree< BinarySearchTreeType >::operator=().

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

- Source/include/slikenet/DS_BinarySearchTree.h