mirror of https://github.com/xfarrow/lasd.git
parent
2b6dcf6700
commit
8e56f33808
|
@ -2,21 +2,27 @@
|
|||
#include "../queue/queue.hpp"
|
||||
#include "../queue/vec/queuevec.hpp"
|
||||
#include "../queue/lst/queuelst.hpp"
|
||||
|
||||
#include "../stack/stack.hpp"
|
||||
#include "../stack/lst/stacklst.hpp"
|
||||
#include "../stack/vec/stackvec.hpp"
|
||||
#include<iostream>
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* ----- begin of class BinaryTree ----- */
|
||||
|
||||
/* ----- begin of struct Node ----- */
|
||||
|
||||
// checks if two trees which have the same root node are identical
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::Node::operator==(const Node& toEvaluate) const noexcept{
|
||||
return EqualNodes(*this, toEvaluate);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate) const noexcept{
|
||||
return !(*this == toEvaluate);
|
||||
}
|
||||
|
||||
/* given two nodes, checks if the subtree is the same */
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::Node::EqualNodes(const Node& n1, const Node& n2) const{
|
||||
|
@ -42,11 +48,6 @@ bool BinaryTree<Data>::Node::EqualNodes(const Node& n1, const Node& n2) const{
|
|||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::Node::operator!=(const Node& toEvaluate) const noexcept{
|
||||
return !(*this == toEvaluate);
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
Data& BinaryTree<Data>::Node::Element(){
|
||||
return this->data;
|
||||
|
@ -57,6 +58,8 @@ const Data& BinaryTree<Data>::Node::Element() const{
|
|||
return this->data;
|
||||
}
|
||||
|
||||
/* ----- end of struct Node ----- */
|
||||
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
|
||||
if(size!=toCompare.size) return false;
|
||||
|
@ -65,7 +68,7 @@ bool BinaryTree<Data>::operator==(const BinaryTree& toCompare) const noexcept{
|
|||
|
||||
template <typename Data>
|
||||
bool BinaryTree<Data>::operator!=(const BinaryTree& toCompare) const noexcept{
|
||||
return(Root() != toCompare.Root());
|
||||
return !(*this == toCompare);
|
||||
}
|
||||
|
||||
/* ----- Map and fold functions ----- */
|
||||
|
@ -129,15 +132,47 @@ template <typename Data>
|
|||
void BinaryTree<Data>::MapPostOrder(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
|
||||
if(node != nullptr){
|
||||
if(node->HasLeftChild()){
|
||||
MapPreOrder(function, par, &(node->LeftChild()));
|
||||
MapPostOrder(function, par, &(node->LeftChild()));
|
||||
}
|
||||
if(node->HasRightChild()){
|
||||
MapPreOrder(function, par, &(node->RightChild()));
|
||||
MapPostOrder(function, par, &(node->RightChild()));
|
||||
}
|
||||
function(node->Element(), par);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
|
||||
if(node != nullptr){
|
||||
if(node->HasLeftChild()){
|
||||
MapInOrder(function, par, &(node->LeftChild()));
|
||||
}
|
||||
function(node->Element(), par);
|
||||
if(node->HasRightChild()){
|
||||
MapInOrder(function, par, &(node->RightChild()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
|
||||
QueueLst<struct Node*> toVisit;
|
||||
if(node != nullptr){
|
||||
toVisit.Enqueue(node);
|
||||
while(!toVisit.Empty()){
|
||||
function(toVisit.Head()->Element(), par);
|
||||
|
||||
if(toVisit.Head()->HasLeftChild()){
|
||||
toVisit.Enqueue(&(toVisit.Head()->LeftChild()));
|
||||
}
|
||||
if(toVisit.Head()->HasRightChild()){
|
||||
toVisit.Enqueue(&(toVisit.Head()->RightChild()));
|
||||
}
|
||||
toVisit.Dequeue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::FoldPreOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{
|
||||
if(node != nullptr){
|
||||
|
@ -164,19 +199,6 @@ void BinaryTree<Data>::FoldPostOrder(const typename FoldableContainer<Data>::Fol
|
|||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::MapInOrder(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
|
||||
if(node != nullptr){
|
||||
if(node->HasLeftChild()){
|
||||
MapInOrder(function, par, &(node->LeftChild()));
|
||||
}
|
||||
function(node->Element(), par);
|
||||
if(node->HasRightChild()){
|
||||
MapInOrder(function, par, &(node->RightChild()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor function, const void* par, void* acc, const Node* node) const{
|
||||
if(node != nullptr){
|
||||
|
@ -185,26 +207,7 @@ void BinaryTree<Data>::FoldInOrder(const typename FoldableContainer<Data>::FoldF
|
|||
}
|
||||
function(node->Element(), par, acc);
|
||||
if(node->HasRightChild()){
|
||||
FoldPreOrder(function, par, acc, &(node->RightChild()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Data>
|
||||
void BinaryTree<Data>::MapBreadth(const typename MappableContainer<Data>::MapFunctor function, void* par, Node* node){
|
||||
QueueLst<struct Node*> toVisit;
|
||||
if(node != nullptr){
|
||||
toVisit.Enqueue(node);
|
||||
while(!toVisit.Empty()){
|
||||
function(toVisit.Head()->Element(), par);
|
||||
|
||||
if(toVisit.Head()->HasLeftChild()){
|
||||
toVisit.Enqueue(&(toVisit.Head()->LeftChild()));
|
||||
}
|
||||
if(toVisit.Head()->HasRightChild()){
|
||||
toVisit.Enqueue(&(toVisit.Head()->RightChild()));
|
||||
}
|
||||
toVisit.Dequeue();
|
||||
FoldInOrder(function, par, acc, &(node->RightChild()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -228,6 +231,10 @@ void BinaryTree<Data>::FoldBreadth(const typename FoldableContainer<Data>::FoldF
|
|||
}
|
||||
}
|
||||
|
||||
/* ----- end of class BinaryTree ----- */
|
||||
|
||||
/* ----- begin of class BTPreOrderIterator ----- */
|
||||
|
||||
template <typename Data>
|
||||
BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
|
||||
curr = &tree.Root();
|
||||
|
@ -235,7 +242,6 @@ BTPreOrderIterator<Data>::BTPreOrderIterator(const BinaryTree<Data>& tree){
|
|||
|
||||
template <typename Data>
|
||||
BTPreOrderIterator<Data>::BTPreOrderIterator(const BTPreOrderIterator& itr){
|
||||
//curr = &(*itr);
|
||||
curr = itr.curr;
|
||||
stack = itr.stack;
|
||||
}
|
||||
|
@ -254,7 +260,6 @@ BTPreOrderIterator<Data>::~BTPreOrderIterator(){
|
|||
|
||||
template <typename Data>
|
||||
BTPreOrderIterator<Data>& BTPreOrderIterator<Data>::operator=(const BTPreOrderIterator& itr){
|
||||
//curr = &(*itr);
|
||||
curr = itr.curr;
|
||||
stack = itr.stack;
|
||||
return *this;
|
||||
|
@ -279,6 +284,7 @@ bool BTPreOrderIterator<Data>::operator!=(const BTPreOrderIterator& itr) const n
|
|||
|
||||
template <typename Data>
|
||||
Data& BTPreOrderIterator<Data>::operator*() const{
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
return curr->Element();
|
||||
}
|
||||
|
||||
|
@ -311,6 +317,10 @@ void BTPreOrderIterator<Data>::operator++(){
|
|||
}
|
||||
}
|
||||
|
||||
/* ----- end of class BTPreOrderIterator ----- */
|
||||
|
||||
/* ----- begin of class BTPostOrderIterator ----- */
|
||||
|
||||
template <typename Data>
|
||||
struct BinaryTree<Data>::Node* BTPostOrderIterator<Data>::DeepestLeftLeaf(struct BinaryTree<Data>::Node* node){
|
||||
if(node->HasLeftChild()){
|
||||
|
@ -374,6 +384,7 @@ bool BTPostOrderIterator<Data>::operator!=(const BTPostOrderIterator& itr) const
|
|||
|
||||
template <typename Data>
|
||||
Data& BTPostOrderIterator<Data>::operator*() const{
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
return curr->Element();
|
||||
}
|
||||
|
||||
|
@ -384,8 +395,16 @@ bool BTPostOrderIterator<Data>::Terminated() const noexcept{
|
|||
|
||||
template <typename Data>
|
||||
void BTPostOrderIterator<Data>::operator++(){
|
||||
/*
|
||||
* If we're coming from the left then we have to analyze the tree on the right
|
||||
* (if existent). Otherwise we just top 'n' pop.
|
||||
*/
|
||||
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
try{
|
||||
|
||||
if(stack.Empty()){
|
||||
curr = nullptr;
|
||||
}else{
|
||||
if( curr == &((stack.Top())->LeftChild()) ){
|
||||
if( (stack.Top())->HasRightChild() ){
|
||||
curr = DeepestLeftLeaf(&((stack.Top())->RightChild()));
|
||||
|
@ -395,11 +414,13 @@ void BTPostOrderIterator<Data>::operator++(){
|
|||
}else{
|
||||
curr = stack.TopNPop();
|
||||
}
|
||||
}catch(std::length_error exc){
|
||||
curr = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/* ----- end of class BTPostOrderIterator ----- */
|
||||
|
||||
/* ----- begin of class BTInOrderIterator ----- */
|
||||
|
||||
template <typename Data>
|
||||
struct BinaryTree<Data>::Node* BTInOrderIterator<Data>::MostLeftNode(struct BinaryTree<Data>::Node& root){
|
||||
if(root.HasLeftChild()){
|
||||
|
@ -459,6 +480,7 @@ bool BTInOrderIterator<Data>::operator!=(const BTInOrderIterator& itr) const noe
|
|||
|
||||
template <typename Data>
|
||||
Data& BTInOrderIterator<Data>::operator*() const{
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
return curr->Element();
|
||||
}
|
||||
|
||||
|
@ -474,14 +496,18 @@ void BTInOrderIterator<Data>::operator++(){
|
|||
if(curr->HasRightChild()){
|
||||
curr = MostLeftNode(curr->RightChild());
|
||||
}else{
|
||||
try{
|
||||
curr = stack.TopNPop();
|
||||
}catch(std::length_error exc){
|
||||
if(stack.Empty()){
|
||||
curr = nullptr;
|
||||
}else{
|
||||
curr = stack.TopNPop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ----- end of class BTInOrderIterator ----- */
|
||||
|
||||
/* ----- begin of class BTBreadthIteratorOrderIterator ----- */
|
||||
|
||||
template <typename Data>
|
||||
BTBreadthIterator<Data>::BTBreadthIterator(const BinaryTree<Data>& tree){
|
||||
curr = &(tree.Root());
|
||||
|
@ -531,6 +557,7 @@ bool BTBreadthIterator<Data>::operator!=(const BTBreadthIterator& itr) const noe
|
|||
|
||||
template <typename Data>
|
||||
Data& BTBreadthIterator<Data>::operator*() const{
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
return curr->Element();
|
||||
}
|
||||
|
||||
|
@ -542,6 +569,7 @@ bool BTBreadthIterator<Data>::Terminated() const noexcept{
|
|||
template <typename Data>
|
||||
void BTBreadthIterator<Data>::operator++(){
|
||||
if(Terminated()) throw std::out_of_range("Iterator is terminated!");
|
||||
|
||||
if(curr->HasLeftChild()){
|
||||
queue.Enqueue(&(curr->LeftChild()));
|
||||
}
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
#ifndef BINARYTREE_HPP
|
||||
#define BINARYTREE_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../container/container.hpp"
|
||||
|
||||
#include "../iterator/iterator.hpp"
|
||||
|
@ -16,24 +14,14 @@
|
|||
#include "../stack/lst/stacklst.hpp"
|
||||
#include "../stack/vec/stackvec.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BinaryTree : virtual public InOrderMappableContainer<Data>,
|
||||
virtual public BreadthMappableContainer<Data>,
|
||||
virtual public InOrderFoldableContainer<Data>,
|
||||
virtual public BreadthFoldableContainer<Data>{ // Must extend InOrder/BreadthMappableContainer<Data> and InOrder/BreadthFoldableContainer<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using BreadthMappableContainer<Data>::size;
|
||||
|
||||
public:
|
||||
|
||||
struct Node {
|
||||
|
@ -76,21 +64,14 @@ public:
|
|||
|
||||
};
|
||||
|
||||
// Destructor
|
||||
virtual ~BinaryTree() = default;
|
||||
|
||||
// Copy assignment
|
||||
BinaryTree& operator=(const BinaryTree&) = delete; // Copy assignment of abstract types should not be possible.
|
||||
|
||||
// Move assignment
|
||||
BinaryTree& operator=(BinaryTree&&) noexcept = delete; // Move assignment of abstract types should not be possible.
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible.
|
||||
bool operator!=(const BinaryTree&) const noexcept; // Comparison of abstract binary tree is possible.
|
||||
|
||||
/* ----- Specific member functions ----- */
|
||||
|
||||
virtual Node& Root() const = 0; // (concrete function must throw std::length_error when empty)
|
||||
|
||||
/* ----- Map and fold functions ----- */
|
||||
|
@ -105,9 +86,10 @@ public:
|
|||
virtual void FoldInOrder(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override InOrderFoldableContainer member
|
||||
virtual void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*) const override; // Override BreadthFoldableContainer member
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
using BreadthMappableContainer<Data>::size;
|
||||
|
||||
/* ----- Auxiliary map and fold functions ----- */
|
||||
|
||||
void MapPreOrder(const typename MappableContainer<Data>::MapFunctor, void*, Node*); // Accessory function executing from one node of the tree
|
||||
|
@ -121,59 +103,34 @@ protected:
|
|||
void FoldBreadth(const typename FoldableContainer<Data>::FoldFunctor, const void*, void*, Node*) const; // Accessory function executing from one node of the tree
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BTPreOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
struct BinaryTree<Data>::Node* curr;
|
||||
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
StackLst<struct BinaryTree<Data>::Node*> stack;
|
||||
|
||||
public:
|
||||
|
||||
// Specific constructors
|
||||
BTPreOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BTPreOrderIterator(const BTPreOrderIterator&);
|
||||
|
||||
// Move constructor
|
||||
BTPreOrderIterator(BTPreOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BTPreOrderIterator();
|
||||
|
||||
/* ************************************************************************ */
|
||||
BTPreOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
BTPreOrderIterator(const BTPreOrderIterator&);
|
||||
BTPreOrderIterator(BTPreOrderIterator&&) noexcept;
|
||||
|
||||
// Copy assignment
|
||||
BTPreOrderIterator& operator=(const BTPreOrderIterator&);
|
||||
|
||||
// Move assignment
|
||||
BTPreOrderIterator& operator=(BTPreOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BTPreOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTPreOrderIterator&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
@ -185,54 +142,33 @@ public:
|
|||
template <typename Data>
|
||||
class BTPostOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
struct BinaryTree<Data>::Node* curr;
|
||||
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
StackLst<struct BinaryTree<Data>::Node*> stack;
|
||||
|
||||
struct BinaryTree<Data>::Node* DeepestLeftLeaf(struct BinaryTree<Data>::Node*);
|
||||
|
||||
public:
|
||||
|
||||
// Specific constructors
|
||||
BTPostOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BTPostOrderIterator(const BTPostOrderIterator&);
|
||||
|
||||
// Move constructor
|
||||
BTPostOrderIterator(BTPostOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BTPostOrderIterator();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BTPostOrderIterator& operator=(const BTPostOrderIterator&);
|
||||
|
||||
// Move assignment
|
||||
BTPostOrderIterator& operator=(BTPostOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BTPostOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTPostOrderIterator&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
@ -244,122 +180,71 @@ public:
|
|||
template <typename Data>
|
||||
class BTInOrderIterator : virtual public ForwardIterator<Data> { // Must extend ForwardIterator<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
struct BinaryTree<Data>::Node* curr;
|
||||
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
StackLst<struct BinaryTree<Data>::Node*> stack;
|
||||
|
||||
struct BinaryTree<Data>::Node* MostLeftNode(struct BinaryTree<Data>::Node&);
|
||||
|
||||
public:
|
||||
|
||||
// Specific constructors
|
||||
BTInOrderIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BTInOrderIterator(const BTInOrderIterator&);
|
||||
|
||||
// Move constructor
|
||||
BTInOrderIterator(BTInOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BTInOrderIterator();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BTInOrderIterator& operator=(const BTInOrderIterator&);
|
||||
|
||||
// Move assignment
|
||||
BTInOrderIterator& operator=(BTInOrderIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BTInOrderIterator&) const noexcept;
|
||||
bool operator!=(const BTInOrderIterator&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BTBreadthIterator : virtual public ForwardIterator<Data>{ // Must extend ForwardIterator<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
struct BinaryTree<Data>::Node* curr;
|
||||
struct BinaryTree<Data>::Node* curr = nullptr;
|
||||
QueueVec<struct BinaryTree<Data>::Node*> queue;
|
||||
|
||||
public:
|
||||
|
||||
// Specific constructors
|
||||
BTBreadthIterator(const BinaryTree<Data>&); // An iterator over a given binary tree
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BTBreadthIterator(const BTBreadthIterator&);
|
||||
|
||||
// Move constructor
|
||||
BTBreadthIterator(BTBreadthIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BTBreadthIterator();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BTBreadthIterator& operator=(const BTBreadthIterator&);
|
||||
|
||||
// Move assignment
|
||||
BTBreadthIterator& operator=(BTBreadthIterator&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BTBreadthIterator&) const noexcept;
|
||||
bool operator!=(const BTBreadthIterator&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Iterator)
|
||||
|
||||
Data& operator*() const; // (throw std::out_of_range when terminated)
|
||||
|
||||
bool Terminated() const noexcept; // (should not throw exceptions)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from ForwardIterator)
|
||||
|
||||
void operator++(); // (throw std::out_of_range when terminated)
|
||||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "binarytree.cpp"
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* ----- begin of struct NodeLnk ----- */
|
||||
|
||||
template <typename Data>
|
||||
struct BinaryTreeLnk<Data>::NodeLnk& BinaryTreeLnk<Data>::NodeLnk::operator=(const BinaryTreeLnk<Data>::NodeLnk& node){
|
||||
data = node.data;
|
||||
|
@ -43,6 +44,10 @@ struct BinaryTree<Data>::Node& BinaryTreeLnk<Data>::NodeLnk::RightChild() const{
|
|||
return *right;
|
||||
}
|
||||
|
||||
/* ----- end of struct NodeLnk ----- */
|
||||
|
||||
/* ----- begin of class BinaryTreeLnk ----- */
|
||||
|
||||
// creates a tree from a linear container in breadth
|
||||
template <typename Data>
|
||||
BinaryTreeLnk<Data>::BinaryTreeLnk(const LinearContainer<Data>& lc){
|
||||
|
@ -55,7 +60,6 @@ struct BinaryTreeLnk<Data>::NodeLnk* BinaryTreeLnk<Data>::CreateTreeFromLinearC
|
|||
if(position >= lc.Size()) return nullptr;
|
||||
|
||||
struct BinaryTreeLnk<Data>::NodeLnk* tmp = CreateNode(lc[position]);
|
||||
// if(position == 0) root = tmp;
|
||||
|
||||
tmp->left = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+1);
|
||||
tmp->right = CreateTreeFromLinearContainerInBreadth(lc, (2*position)+2);
|
||||
|
@ -130,6 +134,7 @@ BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(const BinaryTreeLnk<Data>& t
|
|||
|
||||
template <typename Data>
|
||||
BinaryTreeLnk<Data>& BinaryTreeLnk<Data>::operator=(BinaryTreeLnk<Data>&& tree) noexcept{
|
||||
Clear();
|
||||
std::swap(size, tree.size);
|
||||
std::swap(root, tree.root);
|
||||
return *this;
|
||||
|
|
|
@ -1,30 +1,17 @@
|
|||
|
||||
#ifndef BINARYTREELNK_HPP
|
||||
#define BINARYTREELNK_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../binarytree.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BinaryTreeLnk : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using BinaryTree<Data>::size;
|
||||
|
||||
struct NodeLnk : virtual protected BinaryTree<Data>::Node { // Must extend Node
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using BinaryTree<Data>::Node::data;
|
||||
|
@ -46,64 +33,42 @@ protected:
|
|||
};
|
||||
|
||||
protected:
|
||||
using BinaryTree<Data>::size;
|
||||
|
||||
struct BinaryTreeLnk<Data>::NodeLnk* root = nullptr;
|
||||
struct BinaryTreeLnk<Data>::NodeLnk* CreateNode(const Data& data);
|
||||
|
||||
public:
|
||||
|
||||
// Default constructor
|
||||
BinaryTreeLnk() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructors
|
||||
BinaryTreeLnk(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BinaryTreeLnk(const BinaryTreeLnk<Data>&);
|
||||
|
||||
// Move constructor
|
||||
BinaryTreeLnk(BinaryTreeLnk<Data>&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BinaryTreeLnk();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BinaryTreeLnk& operator=(const BinaryTreeLnk<Data>&);
|
||||
|
||||
// Move assignment
|
||||
BinaryTreeLnk& operator=(BinaryTreeLnk<Data>&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BinaryTreeLnk<Data>&) const noexcept;
|
||||
bool operator!=(const BinaryTreeLnk<Data>&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from BinaryTree)
|
||||
|
||||
struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
// Specific functions
|
||||
|
||||
struct BinaryTreeLnk<Data>::NodeLnk* CreateTreeFromLinearContainerInBreadth(const LinearContainer<Data>&,ulong);
|
||||
struct BinaryTreeLnk<Data>::NodeLnk* CopyTree(struct BinaryTreeLnk<Data>::Node*);
|
||||
void DeleteTree(BinaryTreeLnk<Data>::NodeLnk* node);
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "binarytreelnk.cpp"
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
/* ----- begin of struct NodeVec ----- */
|
||||
|
||||
template <typename Data>
|
||||
BinaryTreeVec<Data>::NodeVec::NodeVec(Data& dat, ulong idx, BinaryTreeVec<Data>* ref){
|
||||
data = dat;
|
||||
|
@ -64,6 +65,10 @@ struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::NodeVec::RightChild() const{
|
|||
throw std::out_of_range("Right child does not exist!");
|
||||
}
|
||||
|
||||
/* ----- end of struct NodeVec ----- */
|
||||
|
||||
/* ----- begin of class BinaryTreeVec ----- */
|
||||
|
||||
template <typename Data>
|
||||
BinaryTreeVec<Data>::BinaryTreeVec(const LinearContainer<Data>& lc){
|
||||
tree.Resize(lc.Size());
|
||||
|
@ -137,6 +142,7 @@ bool BinaryTreeVec<Data>::operator!=(const BinaryTreeVec& bt) const noexcept{
|
|||
|
||||
template <typename Data>
|
||||
struct BinaryTree<Data>::Node& BinaryTreeVec<Data>::Root() const{
|
||||
if(size==0) throw std::length_error("Empty tree!");
|
||||
return *(tree.Front());
|
||||
}
|
||||
|
||||
|
|
|
@ -2,31 +2,18 @@
|
|||
#ifndef BINARYTREEVEC_HPP
|
||||
#define BINARYTREEVEC_HPP
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../binarytree.hpp"
|
||||
#include "../../vector/vector.hpp"
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
namespace lasd {
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
template <typename Data>
|
||||
class BinaryTreeVec : virtual public BinaryTree<Data>{ // Must extend BinaryTree<Data>
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
|
||||
using BinaryTree<Data>::size;
|
||||
// using BinaryTree<Data>::Node;
|
||||
|
||||
struct NodeVec : virtual protected BinaryTree<Data>::Node { // Must extend Node
|
||||
|
||||
private:
|
||||
|
||||
protected:
|
||||
using BinaryTree<Data>::Node::data;
|
||||
ulong index;
|
||||
|
@ -46,59 +33,35 @@ protected:
|
|||
};
|
||||
|
||||
protected:
|
||||
|
||||
using BinaryTree<Data>::size;
|
||||
Vector<struct BinaryTreeVec<Data>::NodeVec*> tree;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
// Default constructor
|
||||
BinaryTreeVec() = default;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific constructors
|
||||
BinaryTreeVec(const LinearContainer<Data>&); // A binary tree obtained from a LinearContainer
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy constructor
|
||||
BinaryTreeVec(const BinaryTreeVec<Data>&);
|
||||
|
||||
// Move constructor
|
||||
BinaryTreeVec(BinaryTreeVec<Data>&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Destructor
|
||||
virtual ~BinaryTreeVec();
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Copy assignment
|
||||
BinaryTreeVec& operator=(const BinaryTreeVec<Data>&);
|
||||
|
||||
// Move assignment
|
||||
BinaryTreeVec& operator=(BinaryTreeVec<Data>&&) noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const BinaryTreeVec&) const noexcept;
|
||||
bool operator!=(const BinaryTreeVec&) const noexcept;
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from BinaryTree)
|
||||
|
||||
struct BinaryTree<Data>::Node& Root() const override; // Override BinaryTree member (throw std::length_error when empty)
|
||||
|
||||
/* ************************************************************************ */
|
||||
|
||||
// Specific member functions (inherited from Container)
|
||||
|
||||
void Clear() override; // Override Container member
|
||||
|
||||
/* ----- override of map and fold in breadth ----- */
|
||||
|
||||
using typename BinaryTree<Data>::MapFunctor;
|
||||
using typename BinaryTree<Data>::FoldFunctor;
|
||||
void MapBreadth(const MapFunctor, void*) override;
|
||||
|
@ -106,8 +69,6 @@ public:
|
|||
|
||||
};
|
||||
|
||||
/* ************************************************************************** */
|
||||
|
||||
}
|
||||
|
||||
#include "binarytreevec.cpp"
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
int main() {
|
||||
std::cout << "Lasd Libraries 2020" << std::endl;
|
||||
//menu();
|
||||
menu();
|
||||
lasdtest(); // To call in the menu of your library test!
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -17,10 +17,27 @@
|
|||
#include"../binarytree/vec/binarytreevec.hpp"
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
using namespace lasd;
|
||||
/* ************************************************************************** */
|
||||
|
||||
void menu(){
|
||||
std::cout<<"MYTESTS\n";
|
||||
cout<<"MY TESTS\n";
|
||||
Vector<string> vec(10);
|
||||
vec[0] = "A";
|
||||
vec[1] = "B";
|
||||
vec[2] = "C";
|
||||
vec[3] = "D";
|
||||
vec[4] = "E";
|
||||
vec[5] = "F";
|
||||
vec[6] = "G";
|
||||
vec[7] = "H";
|
||||
vec[8] = "I";
|
||||
vec[9] = "L";
|
||||
|
||||
BinaryTreeLnk<string> bt1(vec);
|
||||
BinaryTreeLnk<string> bt2(vec);
|
||||
// if(bt1 == bt2) cout<<"uguali";
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue