#include <binary_search_tree.hxx>
Public Member Functions | |
const BST * | Find (const Value &data) |
void | Insert (const Value &data) |
bool | IsBlanced () const |
bool | IsValid () const |
std::size_t | MaxHeight () const |
std::size_t | MinHeight () const |
std::size_t | Size () const |
Value | GetData () const |
const BST * | GetLeftChild () const |
const BST * | GetRightChild () const |
Static Public Member Functions | |
static std::unique_ptr< BST > | Build (const IT &begin, const IT &end) |
static std::unique_ptr< BST > | BuildFromSorted (const IT &begin, const IT &end) |
static const BST * | Remove (std::unique_ptr< BST > &bst, const Value &data) |
Private Types | |
typedef std::iterator_traits< IT >::value_type | Value |
Private Member Functions | |
BST (const Value &data) | |
BST (BST &) | |
BST | operator= (BST &) |
bool | IsValid (std::unique_ptr< const BST * > &previousNode) const |
std::unique_ptr< BST > & | GetPredecessor () |
std::unique_ptr< BST > & | GetRightMostChild () |
void | SetLeftChild (std::unique_ptr< BST > bst) |
void | SetRightChild (std::unique_ptr< BST > bst) |
Private Attributes | |
std::iterator_traits< IT >::value_type | data |
std::unique_ptr< BST > | leftChild |
std::unique_ptr< BST > | rightChild |
Detailed Description
template<typename IT, typename Compare, typename IsEqual>
class huc::BST< IT, Compare, IsEqual >
A Binary Search Tree, Ordered Tree or Sorted Binary Tree divides all its sub-trees into two segments left sub-tree and right sub-tree that follow these rules:
- The left sub-tree of a node has key that respect the Compare operator (e.g. less_equal) with its parent node's key.
- The right sub-tree of a node has key that does not respect the Compare operator with its parent node's key.
- Duplicates key are inserted of the left sub-tree with respect to the Compare operator.
It keeps their keys in sorted order, so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, based on the comparison, to continue searching in the left or right subtrees.
- Advantages
- Memory efficient and managed.
- Use principe of binary search for insert, delete and lookup operations.
- Represent well hierarchies.
- Get all keys in sorted order by just doing Inorder Traversal.
- Doing order statistics, closest lower/greater elements, range queries etc. operations are easy.
- Drawbacks
- The shape of the binary search tree depends entirely on the order of insertions and deletions, and can become degenerate.
- When inserting or searching for an element in a binary search tree, the key of each visited node has to be compared with the key of the element to be inserted or found.
Definition at line 55 of file binary_search_tree.hxx.
Member Typedef Documentation
|
private |
Definition at line 57 of file binary_search_tree.hxx.
Constructor & Destructor Documentation
|
inlineprivate |
Definition at line 288 of file binary_search_tree.hxx.
|
inlineprivate |
Definition at line 289 of file binary_search_tree.hxx.
Member Function Documentation
|
inlinestatic |
Build - Construct in a naive way a Binary Search Tree given an unordered sequence of elements.
- Parameters
-
begin,end - ITs to the initial and final positions of the sequence used to build the tree. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- Complexity
- O(n * m).
- Returns
- Binary Search Tree pointer to be owned, nullptr if construction failed.
Definition at line 69 of file binary_search_tree.hxx.
|
inlinestatic |
BuildFromSorted - Construct a Balanced Binary Search Tree given an ordered sequence of elements.
- Parameters
-
begin,end - ITs to the initial and final positions of the sequence used to build the tree. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
- Complexity
- O(n).
- Warning
- the algorithm does not check the validity on data order; using this algorithm with unordored data will most likely result in an invalid BST. (Can be checked using IsValid method).
- if the compare template argument is inverting the BST (e.g greater_equal), the sequence should be sorted given the same paradigme (e.g. greater value first).
- Returns
- Binary Search Tree pointer to be owned, nullptr if construction failed.
Definition at line 100 of file binary_search_tree.hxx.
|
inline |
Find the first a binary search tree for a specific key.
- Complexity
- O(h), where h may be n in worst case balancement. Equal to log(n) with a balanced tree.
- Parameters
-
data,data value to be found within the current BST.
- Remarks
- this method may not find the data within an invalid binary search tree (cf. IsValid).
- Returns
- first BST node matching the data.
Definition at line 124 of file binary_search_tree.hxx.
|
inline |
Definition at line 283 of file binary_search_tree.hxx.
|
inline |
Definition at line 284 of file binary_search_tree.hxx.
|
inlineprivate |
Retrieve the Predecessor unique_ptr reference.
- Warning
- this method should not be called if no left child exists [assert].
- Returns
- the predecessor unique_ptr reference.
Definition at line 334 of file binary_search_tree.hxx.
|
inline |
Definition at line 285 of file binary_search_tree.hxx.
|
inlineprivate |
Retrieve the Right Most Child unique_ptr reference.
- Warning
- this method should not be called if no right child exists [assert].
- Returns
- the right most child unique_ptr reference.
Definition at line 352 of file binary_search_tree.hxx.
|
inline |
Append a new Binary Search Tree node at the right position with current value.
- Complexity
- O(n).
- Parameters
-
data data value to be added to the current BST. Member type Value is the type of the elements in the BST, defined as an alias of its first template parameter Value (IT::Value).
- Returns
- void.
Definition at line 147 of file binary_search_tree.hxx.
|
inline |
Check if the Binary Search Tree is balanced. Compare the smallest branch to the biggest one to determine the balancement.
- Returns
- wheter or not the tree is balanced (true) or not (false).
Definition at line 171 of file binary_search_tree.hxx.
|
inline |
Check validity of the Binary Search Tree. Recursively check if subtrees do not violate any of the rules defined by a BST.
- Remarks
- Using a preordering traversal, it makes sure that:
- If the node is the left child of its parent, then it must be smaller than (or equal to) the parent and it must pass down the value from its parent to its right subtree to make sure none of the nodes in that subtree is greater than the parent.
- If the node is the right child of its parent, then it must be larger than the parent and it must pass down the value from its parent to its left subtree to make sure none of the nodes in that subtree is lesser than the parent.
- Returns
- wheter or not the tree is a valid Binary Search Tree (true) or not (false).
Definition at line 185 of file binary_search_tree.hxx.
|
inlineprivate |
Check validity of the Binary Search Tree. Recursively check if subtrees do not violate any of the rules defined by a BST.
- Parameters
-
previousNode unique_ptr reference on const BST* used to keep track of the last node retrieved.
- Remarks
- Using a preordering traversal, it makes sure that:
- If the node is the left child of its parent, then it must be smaller than (or equal to) the parent and it must pass down the value from its parent to its right subtree to make sure none of the nodes in that subtree is greater than the parent.
- If the node is the right child of its parent, then it must be larger than the parent and it must pass down the value from its parent to its left subtree to make sure none of the nodes in that subtree is lesser than the parent.
- Returns
- wheter or not the tree is a valid Binary Search Tree (true) or not (false).
Definition at line 306 of file binary_search_tree.hxx.
|
inline |
Returns the biggest branch height.
Complexity O(n).
- Returns
- biggest branch height composing the tree.
Definition at line 197 of file binary_search_tree.hxx.
|
inline |
Returns the smallest branch height.
Complexity O(n).
- Returns
- smallest branch height composing the tree.
Definition at line 208 of file binary_search_tree.hxx.
|
inlineprivate |
Definition at line 290 of file binary_search_tree.hxx.
|
inlinestatic |
Removes all elements equal [IsEqual() template parameter] to the value from the BST.
- Parameters
-
bst the unique_ptr owning the bst on which the removal occurs. data to be removed from the BST. All elements with a value equivalent (IsEqual template parameter) to this are removed from the container. Member type Value is the type of the elements in the BST, defined as an alias of its first template parameter Value (IT::Value).
- Warning
- this method is destructive and may delete the bst owned by the unique_ptr. Return value may be used for inline checking as: (!Remove(bst, data)) ? tree no longer exist : tree still contains node
- Returns
- the pointer handler by the bst passed as argument, nullptr if bst has been erased (empty).
Definition at line 227 of file binary_search_tree.hxx.
|
inlineprivate |
Definition at line 363 of file binary_search_tree.hxx.
|
inlineprivate |
Definition at line 364 of file binary_search_tree.hxx.
|
inline |
Returns the number of nodes composing the BST.
Complexity O(n).
- Returns
- number of nodes composing the tree.
Definition at line 277 of file binary_search_tree.hxx.
Member Data Documentation
|
private |
Definition at line 366 of file binary_search_tree.hxx.
|
private |
Definition at line 367 of file binary_search_tree.hxx.
|
private |
Definition at line 368 of file binary_search_tree.hxx.
The documentation for this class was generated from the following file: