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