Skip to content

Commit

Permalink
Реализация на голяма шестица за двоичното дърво
Browse files Browse the repository at this point in the history
  • Loading branch information
triffon committed Dec 16, 2019
1 parent 2e9129f commit 2b1170b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions trees/bintree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ class BinTree
BinTree (const T&);
// BinTree (const T&, const BinTree<T>&, const BinTree<T>&);

// O(n) по памет и време
BinTree(BinTree& bt) : root(nullptr) {
copy(bt.rootPos());
}

// O(1) по памет и време
BinTree(BinTree&& bt) : root(nullptr) {
assignFrom(rootPos(), bt.rootPos());
}

BinTree& operator=(BinTree& bt) {
if (this != &bt) {
erase(rootPos());
copy(bt.rootPos());
}
return *this;
}

BinTree& operator=(BinTree&& bt) {
if (this != &bt)
assignFrom(rootPos(), bt.rootPos());
return *this;
}


~BinTree() { erase(rootPos()); }

P rootPos() { return P(root); }
bool empty() const { return root == nullptr; }

Expand Down Expand Up @@ -141,6 +168,7 @@ class BinTree
T reduceHelp (T (*op)(const T&, const T&), const T& null_val, BinTreeNode<T> *current);

void erase(P pos);
P copy(P pos);
};

template <class T>
Expand Down Expand Up @@ -356,4 +384,12 @@ void BinTree<T>::erase(P pos) {
}
}

template <typename T>
BinTreePosition<T> BinTree<T>::copy(P pos) {
// допускаме, че дървото е празно
if (pos)
root = new BTN(*pos, copy(-pos).ptr(), copy(+pos).ptr());
return rootPos();
}

#endif

0 comments on commit 2b1170b

Please sign in to comment.