CPPX 1.1.2
A Modern C++ Utility Library
Loading...
Searching...
No Matches
bst.tpp
Go to the documentation of this file.
1#include <algorithm>
2#include <vector>
3
4namespace stl_ext
5{
6template <typename T> void BST<T>::insert(const T &val)
7{
8 if (!this->p_head)
9 {
10 this->p_head = BinaryTree<T>::make_node(val);
11 return;
12 }
13 insert_iterative(this->p_head.get(), val);
14}
15
16template <typename T> void BST<T>::insert_iterative(Node<T> *node, const T &val)
17{
18 Node<T> *curr = node;
19 std::vector<Node<T> *> path;
20
21 while (true)
22 {
23 path.push_back(curr);
24
26 {
27 if (!curr->get_left())
28 {
30 break;
31 }
32 curr = curr->get_left();
33 }
34 else
35 {
36 if (!curr->get_right())
37 {
39 break;
40 }
41 curr = curr->get_right();
42 }
43 }
44 for (auto it = path.rbegin(); it != path.rend(); ++it)
45 {
46 Node<T> *ancestor = *it;
47 int h_left = ancestor->get_left() ? ancestor->get_left()->get_height_val() : 0;
48 int h_right = ancestor->get_right() ? ancestor->get_right()->get_height_val() : 0;
49
50 ancestor->set_height_val(1 + std::max(h_left, h_right));
51 }
52}
53
54template <typename T> inline std::unique_ptr<Node<T>> BST<T>::remove_impl(std::unique_ptr<Node<T>> node, const T &val)
55{
56 if (!node)
57 return nullptr;
58
59 if (val < node->m_data)
60 {
61 node->p_left = remove_impl(std::move(node->p_left), val);
62 }
63 else if (val > node->m_data)
64 {
65 node->p_right = remove_impl(std::move(node->p_right), val);
66 }
67 else
68 {
69 if (!node->p_left)
70 return std::move(node->p_right);
71
72 if (!node->p_right)
73 return std::move(node->p_left);
74
75 Node<T> *temp = node->p_right.get();
76 while (temp->p_left)
77 {
78 temp = temp->p_left.get();
79 }
80
81 node->m_data = temp->m_data;
82
83 node->p_right = remove_impl(std::move(node->p_right), temp->m_data);
84 }
85 return node;
86}
87
88template <typename T> bool BST<T>::contains(const T &val) const
89{
90 Node<T> *curr = this->p_head.get();
91 while (curr)
92 {
93 if (val == curr->get_data())
94 return true;
96 curr = curr->get_left();
97 else
98 curr = curr->get_right();
99 }
100 return false;
101}
102
103template <typename T> void BST<T>::remove(const T &val)
104{
105 this->p_head = remove_impl(std::move(this->p_head), val);
106}
107
108template <typename T> T BST<T>::get_min() const
109{
110 Node<T> *r = this->p_head.get();
111 if (!r)
112 throw std::runtime_error("Empty");
113 while (r->get_left())
114 r = r->get_left();
115 return r->get_data();
116}
117
118template <typename T> T BST<T>::get_max() const
119{
120 Node<T> *r = this->p_head.get();
121 if (!r)
122 throw std::runtime_error("Empty");
123 while (r->get_right())
124 r = r->get_right();
125 return r->get_data();
126}
127
128template <typename T> T BST<T>::get_successor(const T &val) const
129{
130 Node<T> *curr = this->get_root();
131 Node<T> *ancestor_succ = nullptr;
132
133 while (curr)
134 {
136 {
138 curr = curr->get_left();
139 }
140 else if (val > curr->get_data())
141 {
142 curr = curr->get_right();
143 }
144 else
145 {
146 if (curr->get_right())
147 {
149 while (temp->get_left())
150 temp = temp->get_left();
151 return temp->get_data();
152 }
153
154 if (ancestor_succ)
155 return ancestor_succ->get_data();
156
157 throw std::runtime_error("No successor");
158 }
159 }
160
161 throw std::runtime_error("Value not found");
162}
163
164template <typename T> T BST<T>::get_predecessor(const T &val) const
165{
166 Node<T> *curr = this->get_root();
167 Node<T> *ancestor_pred = nullptr;
168
169 while (curr)
170 {
172 {
173 curr = curr->get_left();
174 }
175 else if (val > curr->get_data())
176 {
178 curr = curr->get_right();
179 }
180 else
181 {
182 if (curr->get_left())
183 {
185 while (temp->get_right())
186 temp = temp->get_right();
187 return temp->get_data();
188 }
189
190 if (ancestor_pred)
191 return ancestor_pred->get_data();
192
193 throw std::runtime_error("No predecessor");
194 }
195 }
196 throw std::runtime_error("Value not found");
197}
198
199} // namespace stl_ext
T get_predecessor(const T &val) const
Definition bst.tpp:164
virtual void insert(const T &val)
Definition bst.tpp:6
T get_max() const
Definition bst.tpp:118
T get_successor(const T &val) const
Definition bst.tpp:128
virtual void remove(const T &val)
Definition bst.tpp:103
T get_min() const
Definition bst.tpp:108
bool contains(const T &val) const
Definition bst.tpp:88
static std::unique_ptr< Node< T > > make_node(const T &val)
int get_height_val() const
Definition node.tpp:25
void set_left(std::unique_ptr< Node< T > > node)
Definition node.tpp:60
const T & get_data() const
Definition node.tpp:45
Node< T > * get_right() const
Definition node.tpp:65
void set_right(std::unique_ptr< Node< T > > node)
Definition node.tpp:70
Node< T > * get_left() const
Definition node.tpp:55
void set_height_val(int h)
Definition node.tpp:30
Definition cppx.h:12