CPPX 1.1.2
A Modern C++ Utility Library
Loading...
Searching...
No Matches
avl.tpp
Go to the documentation of this file.
1#include <algorithm>
2
3namespace stl_ext
4{
5
6template <typename T> int AVLTree<T>::get_height(const Node<T> *node) const
7{
8 if (node == nullptr)
9 {
10 return 0;
11 }
12
13 return node->get_height_val();
14}
15
16template <typename T> int AVLTree<T>::get_balance_factor(const Node<T> *node) const
17{
18 if (node == nullptr)
19 {
20 return 0;
21 }
22
23 return get_height(node->get_left()) - get_height(node->get_right());
24}
25
26template <typename T> void AVLTree<T>::update_height(Node<T> *node)
27{
28 if (node == nullptr)
29 {
30 return;
31 }
32
33 int left_h = get_height(node->get_left());
34 int right_h = get_height(node->get_right());
35
36 node->set_height_val(1 + std::max(left_h, right_h));
37}
38
39template <typename T> std::unique_ptr<Node<T>> AVLTree<T>::rotate_left(std::unique_ptr<Node<T>> node)
40{
41 std::unique_ptr<Node<T>> new_root = node->detach_right();
42
43 node->set_right(new_root->detach_left());
44
45 new_root->set_left(std::move(node));
46
47 update_height(new_root->get_left());
48 update_height(new_root.get());
49
50 return new_root;
51}
52
53template <typename T> std::unique_ptr<Node<T>> AVLTree<T>::rotate_right(std::unique_ptr<Node<T>> node)
54{
55 std::unique_ptr<Node<T>> new_root = node->detach_left();
56
57 node->set_left(new_root->detach_right());
58
59 new_root->set_right(std::move(node));
60
61 update_height(new_root->get_right());
62 update_height(new_root.get());
63
64 return new_root;
65}
66
67template <typename T> std::unique_ptr<Node<T>> AVLTree<T>::rebalance(std::unique_ptr<Node<T>> node)
68{
69 update_height(node.get());
70
71 int balance = get_balance_factor(node.get());
72
73 if (balance > 1)
74 {
75 if (get_balance_factor(node->get_left()) < 0)
76 {
77 node->set_left(rotate_left(node->detach_left()));
78 }
79 return rotate_right(std::move(node));
80 }
81
82 if (balance < -1)
83 {
84 if (get_balance_factor(node->get_right()) > 0)
85 {
86 node->set_right(rotate_right(node->detach_right()));
87 }
88 return rotate_left(std::move(node));
89 }
90
91 return node;
92}
93
94template <typename T> void AVLTree<T>::insert(const T &val)
95{
96 this->p_head = insert_helper(std::move(this->p_head), val);
97}
98
99template <typename T> std::unique_ptr<Node<T>> AVLTree<T>::insert_helper(std::unique_ptr<Node<T>> node, const T &val)
100{
101 if (node == nullptr)
102 {
104 }
105
106 if (val < node->get_data())
107 {
108 node->set_left(insert_helper(node->detach_left(), val));
109 }
110 else if (val > node->get_data())
111 {
112 node->set_right(insert_helper(node->detach_right(), val));
113 }
114 else
115 {
116 return node;
117 }
118
119 return rebalance(std::move(node));
120}
121
122template <typename T> void AVLTree<T>::remove(const T &val)
123{
124 this->p_head = remove_helper(std::move(this->p_head), val);
125}
126
127template <typename T> std::unique_ptr<Node<T>> AVLTree<T>::remove_helper(std::unique_ptr<Node<T>> node, const T &val)
128{
129 if (node == nullptr)
130 {
131 return nullptr;
132 }
133
134 if (val < node->get_data())
135 {
136 node->set_left(remove_helper(node->detach_left(), val));
137 }
138 else if (val > node->get_data())
139 {
140 node->set_right(remove_helper(node->detach_right(), val));
141 }
142 else
143 {
144 if (node->get_left() == nullptr)
145 {
146 return node->detach_right();
147 }
148 else if (node->get_right() == nullptr)
149 {
150 return node->detach_left();
151 }
152 Node<T> *temp = node->get_right();
153 while (temp->get_left() != nullptr)
154 {
155 temp = temp->get_left();
156 }
157
158 node->set_data(temp->get_data());
159
160 node->set_right(remove_helper(node->detach_right(), temp->get_data()));
161 }
162
163 return rebalance(std::move(node));
164}
165
166} // namespace stl_ext
void remove(const T &val) override
Definition avl.tpp:122
void insert(const T &val) override
Definition avl.tpp:94
static std::unique_ptr< Node< T > > make_node(const T &val)
void set_left(Node< T > *parent, std::unique_ptr< Node< T > > left_child)
std::unique_ptr< Node< T > > detach_right()
Definition node.tpp:40
const T & get_data() const
Definition node.tpp:45
void set_right(std::unique_ptr< Node< T > > node)
Definition node.tpp:70
Definition cppx.h:12