#ifndef BST_HPP # define BST_HPP # include // std::allocator # include // NULL, size_t, ptrdiff_t # include // max() # include // 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, typename Allocator = std::allocator< ft::pair > > class Bst { public: typedef Key key_type; typedef T mapped_type; typedef pair value_type; typedef Compare key_compare; typedef Allocator allocator_type; typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; typedef Bst_iterator iterator; typedef Bst_const_iterator const_iterator; typedef ft::reverse_iterator reverse_iterator; typedef ft::reverse_iterator const_reverse_iterator; /************ * copliens : ************/ // constructors ------------------------------ explicit Bst( const key_compare& comp = Compare(), const Allocator& alloc = Allocator() ); template < typename InputIt > Bst( InputIt first, InputIt last, const key_compare& comp = Compare(), const allocator_type& alloc = Allocator() ); Bst(const Bst& src); // destructor -------------------------------- ~Bst(); // operator= --------------------------------- Bst& operator=(const Bst& rhs); /************* * iterators : *************/ // begin ------------------------------------- iterator begin(); const_iterator begin() const; // end --------------------------------------- iterator end(); const_iterator end() const; // rbegin ------------------------------------ reverse_iterator rbegin(); const_reverse_iterator rbegin() const; // rend -------------------------------------- reverse_iterator rend(); const_reverse_iterator rend() const; /************ * capacity : ************/ // empty ------------------------------------- bool empty() const; // size -------------------------------------- size_type size() const; // max_size ---------------------------------- size_type max_size() const; /****************** * element access : ******************/ // operator[] -------------------------------- mapped_type& operator[](const key_type& key); /************* * modifiers : *************/ // insert ------------------------------------ pair insert(const value_type& value); iterator insert(iterator hint, const value_type& value); template < typename InputIt > void insert(InputIt first, InputIt last); // erase ------------------------------------- void erase(iterator pos); size_type erase(const key_type& key); void erase(iterator first, iterator last); // swap -------------------------------------- void swap(Bst& other); // clear ------------------------------------- void clear(); /************** * operations : **************/ // find -------------------------------------- iterator find(const key_type& key); const_iterator find(const key_type& key) const; // count ------------------------------------- size_type count(const key_type& key) const; private: size_type _size; node* _root; node_sentinel* _sentinel; Compare _comp; Allocator _allocator; // https://stackoverflow.com/questions/14148756/what-does-template-rebind-do typename Allocator::template rebind< node >::other _allocator_node; typename Allocator::template rebind< node_sentinel >::other _allocator_node_sentinel; void _init_sentinel(); pair _insert(const value_type& value); node* _erase(iterator pos); node* _subtree_shift( node* st_old, node* st_new ); // BBST // https://www.youtube.com/watch?v=vRwi_UcZGjU void _insert_rebalancing(node* n); void _erase_rebalancing(node* n); short _compute_height(node* n); short _balance_factor(node* n); node* _rotate_left(node* n); node* _rotate_right(node* n); }; // Non-member functions template < typename Key, typename T, typename Compare, typename Alloc > bool operator==(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > bool operator!=(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > bool operator<(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > bool operator>(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > bool operator<=(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > bool operator>=(const Bst& lhs, const Bst& rhs); template < typename Key, typename T, typename Compare, typename Alloc > void swap(Bst& lhs, Bst& 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