delete
This commit is contained in:
parent
c32200907a
commit
8fb5f13336
60
tree.cpp
60
tree.cpp
|
@ -3,27 +3,64 @@
|
||||||
#include<vector>
|
#include<vector>
|
||||||
#include<memory>
|
#include<memory>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Node;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using Node_ptr = std::unique_ptr<Node<T>>;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct Node{
|
struct Node{
|
||||||
T value;
|
T value;
|
||||||
std::unique_ptr<Node<T>> small;
|
Node_ptr<T> small;
|
||||||
std::unique_ptr<Node<T>> big;
|
Node_ptr<T> big;
|
||||||
void dump(std::ofstream& ofs) const;
|
void dump(std::ofstream& ofs) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Tree{
|
class Tree{
|
||||||
std::unique_ptr<Node<T>> root;
|
Node_ptr<T> root;
|
||||||
|
Node_ptr<T>* find(T val);
|
||||||
|
Node_ptr<T>* find_to_swap(Node_ptr<T>* unwanted);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool insert(T val);
|
bool insert(T val);
|
||||||
void del(T val);
|
bool del(T val);
|
||||||
|
|
||||||
void print() const;
|
void print() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Node_ptr<T>* Tree<T>::find(T val){
|
||||||
|
for(Node_ptr<T>* n = &root; *n;){
|
||||||
|
if(val == (*n)->value) return n;
|
||||||
|
else if(val < (*n)->value) n = &(*n)->small;
|
||||||
|
else n = &(*n)->big;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void del(T val);
|
Node_ptr<T>* Tree<T>::find_to_swap(Node_ptr<T>* unwanted){
|
||||||
|
if((*unwanted)->small) unwanted = &(*unwanted)->small;
|
||||||
|
else return nullptr;
|
||||||
|
while((*unwanted)->big) unwanted = &(*unwanted)->big;
|
||||||
|
return unwanted;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool Tree<T>::del(T val){
|
||||||
|
Node_ptr<T>* unwanted = find(val);
|
||||||
|
if(!unwanted) return false;
|
||||||
|
Node_ptr<T>* to_swap = find_to_swap(unwanted);
|
||||||
|
if(!to_swap){
|
||||||
|
(*unwanted).reset((*unwanted)->big.release());
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
std::swap((*unwanted)->value, (*to_swap)->value);
|
||||||
|
(*to_swap).reset((*to_swap)->small.release());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool Tree<T>::insert(T val){
|
bool Tree<T>::insert(T val){
|
||||||
|
@ -83,12 +120,15 @@ void Tree<T>::print() const {
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
Tree<int> t;
|
Tree<int> t;
|
||||||
std::vector<int> v = {3, 6, 8, 0, -1, 1, 4, 2};
|
std::vector<int> v = {3, 6, 8, -1, 7, 0, 9, 2, -11, 5};
|
||||||
for(auto i : v){
|
for(auto i : v){
|
||||||
if(t.insert(i)) std::cout << "added " << i << "\n";
|
if(t.insert(i)) std::cout << "added " << i << "\n";
|
||||||
else std::cout << "failed at " << i << "\n";
|
else std::cout << "failed at adding " << i << "\n";
|
||||||
|
t.print();
|
||||||
|
}
|
||||||
|
for(auto i : v){
|
||||||
|
if(t.del(i)) std::cout << "deleted " << i << "\n";
|
||||||
|
else std::cout << "failed at deleting " << i << "\n";
|
||||||
t.print();
|
t.print();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue