Last active
September 2, 2017 00:51
-
-
Save Aegean-Homines/f24e0a56aad77a1ef1a1bb317a4cb642 to your computer and use it in GitHub Desktop.
General tree structure using template of template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef BINARY_TREE_CPP | |
#define BINARY_TREE_CPP | |
#include "BT.h" | |
template<typename T> | |
BT<T>::BT(T const & data): NodeInterface<T>(data) { | |
} | |
template < typename T > | |
BT<T>::~BT() { | |
} | |
template<typename T> | |
void BT<T>::insert(T const & data) | |
{ | |
// Insert function here | |
} | |
template<typename T> | |
void BT<T>::remove(T const & data) | |
{ | |
} | |
template<typename T> | |
std::unique_ptr<NodeInterface<T>> BT<T>::find(T const & data) const | |
{ | |
return std::unique_ptr<NodeInterface<T>>(); | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "NodeInterface.h" | |
template<typename T> | |
class BT : public NodeInterface<T> | |
{ | |
public: | |
explicit BT(T const & data); | |
~BT(); | |
void insert(T const& data) override; | |
void remove(T const& data) override; | |
std::unique_ptr<NodeInterface<T>> find(T const& data) const override; | |
}; | |
#include "BT.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tree<BT, int> tree; | |
tree.insert(5); | |
tree.insert(6); | |
Tree<Node, float> regularTree; | |
regularTree.insert(3.4f); | |
regularTree.insert(3.0f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <memory> | |
#include <vector> | |
template<typename T> | |
class NodeInterface { | |
public: | |
explicit NodeInterface(T const & data) : data(data) { | |
}; | |
virtual ~NodeInterface() { | |
}; | |
protected: | |
T data; | |
std::vector<std::unique_ptr<NodeInterface<T>>> children; | |
virtual void insert(T const & data) = 0; | |
virtual void remove(T const & data) = 0; | |
virtual std::unique_ptr<NodeInterface<T>> find(T const & data) const = 0; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <vector> | |
#include <memory> | |
#include <iostream> | |
template<template <typename DataType> class NodeType, typename DataType> | |
class Tree | |
{ | |
public: | |
Tree() : treeSize(0), root(nullptr){} | |
void insert(DataType const & data); | |
void remove(DataType const & data); | |
bool has(DataType const & data) const; | |
int size() const { return treeSize; } | |
protected: | |
int treeSize; | |
std::unique_ptr<NodeType<DataType>> root; | |
}; | |
template < template <typename DataType> class NodeType, typename DataType > | |
void Tree<NodeType, DataType>::insert ( DataType const & data ) { | |
if(!root) { | |
root = std::unique_ptr<NodeType<DataType>>(new NodeType<DataType>(data)); | |
}else { | |
root->insert(data); | |
} | |
++treeSize; | |
} | |
template < template <typename DataType> class NodeType, typename DataType > | |
void Tree<NodeType, DataType>::remove ( DataType const& data ) {} | |
template < template <typename DataType> class NodeType, typename DataType > | |
bool Tree<NodeType, DataType>::has ( DataType const& data ) const {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment