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