using functionning luke bst

This commit is contained in:
hugogogo
2022-06-20 19:05:52 +02:00
parent 8c181b6407
commit c7114c1db2
8 changed files with 1056 additions and 11 deletions

148
headers/bst.hpp Normal file
View File

@@ -0,0 +1,148 @@
#ifndef BST_HPP
# define BST_HPP
# include <memory> // std::allocator
# include <cstddef> // NULL, size_t, ptrdiff_t
# include <algorithm> // max()
# include <functional> // std::less()
# include "reverse_iterator.hpp"
# include "equal.hpp"
# include "lexicographical_compare.hpp"
# include "pair.hpp"
# include "bst_node.hpp"
# include "bst_iterator.hpp"
namespace ft {
template <
typename Key,
typename T,
typename Compare = std::less<Key>,
typename Allocator = std::allocator< ft::pair<const Key, T> >
> class Bst
{
public:
typedef Key key_type;
typedef T mapped_type;
typedef pair<const Key, T> value_type;
typedef Compare key_compare;
typedef Allocator allocator_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef Bst_iterator<Key, T, Compare, Allocator> iterator;
typedef Bst_const_iterator<Key, T, Compare, Allocator> const_iterator;
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
// Member functions
explicit Bst(const Compare& comp = Compare(), const Allocator& alloc = Allocator() );
template < typename InputIt >
Bst(InputIt first, InputIt last, const Compare& comp = Compare(), const Allocator& alloc = Allocator());
Bst(const Bst& src);
~Bst();
Bst& operator=(const Bst& rhs);
// Element access
T& operator[](const Key& key);
// Iterators
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;
// Capacity
bool empty() const;
size_type size() const;
size_type max_size() const;
// Modifiers
void clear();
pair<iterator, bool> insert(const value_type& value);
iterator insert(iterator hint, const value_type& value);
template < typename InputIt >
void insert(InputIt first, InputIt last);
void erase(iterator pos);
void erase(iterator first, iterator last);
size_type erase(const Key& key);
void swap(Bst& other);
// Lookup
iterator find(const Key& key);
const_iterator find(const Key& key) const;
size_type count(const Key& key) const;
private:
size_type _size;
node<value_type>* _root;
node_sentinel<value_type>* _sentinel;
Compare _comp;
Allocator _allocator;
// TODO : rebind syntaxe pas clair.
typename Allocator::template rebind< node<value_type> >::other _allocator_node; // Peu clair, verifier syntaxe
typename Allocator::template rebind< node_sentinel<value_type> >::other _allocator_node_sentinel; // Peu clair, verifier syntaxe
void _init_sentinel();
pair<iterator, bool> _insert(const value_type& value);
node<value_type>* _erase(iterator pos);
node<value_type>* _subtree_shift(node<value_type>* st_old, node<value_type>* st_new);
// AVL Balancing
void _insert_rebalancing(node<value_type>* n);
void _erase_rebalancing(node<value_type>* n);
short _compute_height(node<value_type>* n);
short _bf(node<value_type>* n); // balance factor
node<value_type>* _rotate_left(node<value_type>* n);
node<value_type>* _rotate_right(node<value_type>* n);
};
// Non-member functions
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator==(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator!=(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator<(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator>(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator<=(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
bool operator>=(const Bst<Key,T,Compare,Alloc>& lhs, const Bst<Key,T,Compare,Alloc>& rhs);
template < typename Key, typename T, typename Compare, typename Alloc >
void swap(Bst<Key,T,Compare,Alloc>& lhs, Bst<Key,T,Compare,Alloc>& rhs);
} // namespace ft
# include "bst.tpp"
// https://en.wikipedia.org/wiki/Binary_search_tree
// https://en.wikipedia.org/wiki/AVL_tree
// https://fr.wikipedia.org/wiki/Arbre_binaire_de_recherche
// https://fr.wikipedia.org/wiki/Arbre_AVL
// https://visualgo.net/en/bst
// https://visualgo.net/en/bst?slide=14-8 // --> to 14-13
#endif

207
headers/bst_iterator.hpp Normal file
View File

@@ -0,0 +1,207 @@
#ifndef BST_ITERATOR_HPP
# define BST_ITERATOR_HPP
# include <cstddef> // NULL, ptrdiff_t
# include <iterator> // iterator_tag
# include "pair.hpp"
# include "bst_node.hpp"
namespace ft {
template <
typename Key,
typename T,
typename Compare,
typename Allocator
> class Bst_iterator
{
private:
typedef Bst_iterator Self;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef pair<const Key, T> value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type* pointer;
typedef value_type& reference;
Bst_iterator() : _node(), _sentinel() {}
Bst_iterator(node<value_type>* n, node_sentinel<value_type>* sentinel) : _node(n), _sentinel(sentinel) {}
//Bst_iterator(const Bst_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
reference operator*() const
{ return _node->value; }
pointer operator->() const
{ return &_node->value; }
Self& operator++()
{
if (_node == NULL)
_node = _sentinel->child->min();
else if (_node->right)
_node = _node->right->min();
else
{
node<value_type>* up = _node->up;
while (up != NULL && _node == up->right)
{
_node = up;
up = up->up;
}
_node = up;
}
return *this;
}
Self& operator--()
{
if (_node == NULL)
_node = _sentinel->child->max();
else if (_node->left)
_node = _node->left->max();
else
{
node<value_type>* up = _node->up;
while (up != NULL && _node == up->left)
{
_node = up;
up = up->up;
}
_node = up;
}
return *this;
}
Self operator++(int)
{
//Self old(*this);
Self old = *this;
++(*this);
return old;
}
Self operator--(int)
{
//Self old(*this);
Self old = *this;
--(*this);
return old;
}
node<value_type>* getNode()
{ return _node; }
const node<value_type>* getNode() const
{ return _node; }
const node_sentinel<value_type>* getSentinel() const
{ return _sentinel; }
// TODO : friend Non-member functions syntaxe pas clair.
friend bool operator==(const Self &lhs, const Self &rhs)
{ return lhs._node == rhs._node; }
friend bool operator!=(const Self &lhs, const Self &rhs)
{ return !(lhs._node == rhs._node); }
private:
node<value_type>* _node;
node_sentinel<value_type>* _sentinel;
};
template <
typename Key,
typename T,
typename Compare,
typename Allocator
> class Bst_const_iterator
{
private:
typedef Bst_const_iterator Self;
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef pair<const Key, T> value_type;
typedef std::ptrdiff_t difference_type;
typedef const value_type* pointer;
typedef const value_type& reference;
Bst_const_iterator() : _node(), _sentinel() {}
Bst_const_iterator(const node<value_type>* node, const node_sentinel<value_type>* sentinel) : _node(node), _sentinel(sentinel) {}
//Bst_const_iterator(const Bst_const_iterator& src) : _node(src._node), _sentinel(src._sentinel) {} //implicit
Bst_const_iterator(const Bst_iterator<Key, T, Compare, Allocator>& src) : _node(src.getNode()), _sentinel(src.getSentinel()) {}
reference operator*() const
{ return _node->value; }
pointer operator->() const
{ return &_node->value; }
Self& operator++()
{
if (_node == NULL)
_node = _sentinel->child->min();
else if (_node->right)
_node = _node->right->min();
else
{
node<value_type>* up = _node->up;
while (up != NULL && _node == up->right)
{
_node = up;
up = up->up;
}
_node = up;
}
return *this;
}
Self& operator--()
{
if (_node == NULL)
_node = _sentinel->child->max();
else if (_node->left)
_node = _node->left->max();
else
{
node<value_type>* up = _node->up;
while (up != NULL && _node == up->left)
{
_node = up;
up = up->up;
}
_node = up;
}
return *this;
}
Self operator++(int)
{
//Self old(*this);
Self old = *this;
++(*this);
return old;
}
Self operator--(int)
{
//Self old(*this);
Self old = *this;
--(*this);
return old;
}
node<value_type>* getNode() const
{ return _node; }
friend bool operator==(const Self &lhs, const Self &rhs)
{ return lhs._node == rhs._node; }
friend bool operator!=(const Self &lhs, const Self &rhs)
{ return !(lhs._node == rhs._node); }
private:
const node<value_type>* _node;
const node_sentinel<value_type>* _sentinel;
};
} // namespace ft
#endif

50
headers/bst_node.hpp Normal file
View File

@@ -0,0 +1,50 @@
#ifndef BST_NODE_HPP
# define BST_NODE_HPP
# include <cstddef> // NULL
namespace ft {
template < typename ValueType >
struct node
{
ValueType value;
node *up;
node *left;
node *right;
short height;
node(const ValueType& val) : value(val), up(NULL), left(NULL), right(NULL), height(1) {}
node* min()
{
node* n = this;
while (n->left)
n = n->left;
return n;
}
node* max()
{
node* n = this;
while (n->right)
n = n->right;
return n;
}
};
template < typename ValueType >
struct node_sentinel
{
node<ValueType> *child;
node_sentinel() : child(NULL) {}
};
} // namespace ft
#endif

View File

@@ -8,7 +8,7 @@
# include <functional> // std::less, std::binary_function
# include "pair.hpp"
//# include "bst.hpp"
# include "bst.hpp"
namespace ft {
@@ -34,7 +34,7 @@ public:
// typedef typename allocator_type::pointer pointer;
// typedef typename allocator_type::const_pointer const_pointer;
// typedef Bst<Key,T,Compare,Alloc> bst_map;
typedef Bst<Key,T,Compare,Alloc> bst_map;
// typedef typename bst_map::iterator iterator;
// typedef typename bst_map::const_iterator const_iterator;
@@ -107,7 +107,7 @@ public:
* element access :
******************/
//// operator[] --------------------------------
// mapped_type & operator[] (const key_type& k);
mapped_type & operator[] (const key_type& k);
/*************
@@ -166,6 +166,7 @@ private:
allocator_type _allocator;
key_compare _comp;
bst_map _bst;
};