Skip to content

Instantly share code, notes, and snippets.

@Aegean-Homines
Last active Sep 2, 2017
Embed
What would you like to do?
General tree structure using template of template
#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
#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"
Tree<BT, int> tree;
tree.insert(5);
tree.insert(6);
Tree<Node, float> regularTree;
regularTree.insert(3.4f);
regularTree.insert(3.0f);
#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;
};
#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