stuff-from-scratch/src/base/core/data_structures/Tree.h
2023-12-18 10:16:31 +00:00

76 lines
1 KiB
C++

#pragma once
#include "Pointer.h"
template<typename T>
class Node
{
public:
Node(T data)
: mData(data)
{}
void addChild(Ptr<Node> child)
{
if(!mLeftChild)
{
mLeftChild = std::move(child);
}
else
{
mRightChild = std::move(child);
}
}
bool isLeaf() const
{
return !mLeftChild && !mRightChild;
}
T getData() const
{
return mData;
}
Node* getLeftChild() const
{
return mLeftChild.get();
}
Node* getRightChild() const
{
return mRightChild.get();
}
private:
T mData;
unsigned char mTag{0};
Ptr<Node> mLeftChild;
Ptr<Node> mRightChild;
};
template<typename T>
using NodePtr = Ptr<Node<T> >;
template<typename T>
class Tree
{
public:
Tree()
{
}
void addRootNode(NodePtr<T> root)
{
mRootNode = std::move(root);
}
Node<T>* getRootNode() const
{
return mRootNode.get();
}
private:
NodePtr<T> mRootNode;
};