LIPH's C++ Codes
avl.h
Go to the documentation of this file.
1#ifndef LIPH_AVL_H_
2#define LIPH_AVL_H_
3
4#include <cstddef> // size_t, ptrdiff_t
5#include <functional> // less
6
7namespace liph {
8
9template <class T, class Compare = std::less<T>>
10class avl {
11public:
12 typedef T value_type;
13 typedef std::size_t size_type;
14 typedef std::ptrdiff_t difference_type;
15
16 avl() : tree(nullptr), node_num(0), comp(Compare()) {}
17
18 ~avl() { clear(); }
19
20 bool empty() const { return node_num == 0; }
21
22 size_type size() const { return node_num; }
23
24 bool find(const value_type& value) const {
25 if (tree == nullptr) return false;
26 return tree->find(value, comp);
27 }
28
29 void insert(const value_type& value) {
30 if (tree == nullptr)
31 tree = new node(value);
32 else
33 tree->insert(value, comp);
34
35 node_num++;
36 }
37
38 void erase(const value_type& x) {
39 if (tree) tree->erase(x, comp);
40 }
41
42 void clear() { delete tree; }
43
44private:
45 struct node {
46 value_type value;
47 node *left;
48 node *right;
49 int bf;
50
51 node(const value_type& x) : value(x), left(nullptr), right(nullptr), bf(0) {}
52 bool find(const value_type& x, const Compare& comp) const;
53 void insert(const value_type& x, const Compare& comp);
54 void erase(const value_type& x, const Compare& comp);
55 void update();
56 ~node() {
57 delete left;
58 delete right;
59 }
60 };
61
62 node *tree;
63 size_type node_num;
64 Compare comp;
65};
66
67template <class T, class Compare>
68bool avl<T, Compare>::node::find(const value_type& x, const Compare& comp) const {
69 if (!comp(value, x) && !comp(x, value)) return true;
70 if (comp(x, value) && left != nullptr) return left->find(x, comp);
71 if (comp(value, x) && right != nullptr) return right->find(x, comp);
72 return false;
73}
74
75template <class T, class Compare>
76void avl<T, Compare>::node::insert(const value_type& x, const Compare& comp) {
77 if (comp(x, value)) {
78 if (left == nullptr) {
79 left = new node(x);
80 bf++;
81 } else {
82 left->insert(x, comp);
83 }
84 } else {
85 if (right == nullptr) {
86 right = new node(x);
87 bf--;
88 } else {
89 right->insert(x, comp);
90 }
91 }
92
93 update();
94}
95
96template <class T, class Compare>
97void avl<T, Compare>::node::erase(const value_type& x, const Compare& comp) {}
98
99template <class T, class Compare>
100void avl<T, Compare>::node::update() {}
101
102} // namespace liph
103
104#endif // LIPH_AVL_H_
Definition: avl.h:10
size_type size() const
Definition: avl.h:22
void insert(const value_type &value)
Definition: avl.h:29
void clear()
Definition: avl.h:42
T value_type
Definition: avl.h:12
void erase(const value_type &x)
Definition: avl.h:38
std::size_t size_type
Definition: avl.h:13
avl()
Definition: avl.h:16
std::ptrdiff_t difference_type
Definition: avl.h:14
bool empty() const
Definition: avl.h:20
bool find(const value_type &value) const
Definition: avl.h:24
~avl()
Definition: avl.h:18
Definition: algorithm.h:10
void erase(std::vector< T > &v, const std::set< size_t > &idx)
Erases the specified elements from v, according to index in idx.
Definition: algorithm.h:14