#pragma once
#include "Leaf.h"
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

template< class T >
class Node
{
public:
	T data;
	Node<T>* left;
	Node<T>* right;
	int Frequency;

	Node()
	{
		this->data = NULL;
		this->left = NULL;
		this->right = NULL;
		this->Frequency = 0; 
	}

	Node(const Node& node)
	{
		this->data = node.data;
		this->left = node.left;
		this->right = node.right;
		this->Frequency = node.Frequency; 
	}

	Node(const T& data, int frequency)
	{
		this->data = data;
		this->left = NULL;
		this->right = NULL;
		this->Frequency = frequency; 
	}

	void Merge(Node<T> *Left, Node<T> *Right)
	{
		this->left = Left;
		this->right = Right;
		this->Frequency = left->Frequency + right->Frequency; 
	}
	
	bool operator>(const Node<T>& node) const
	{
		return this->Frequency > node.Frequency;
	}

	void Traverse(string ByteCode, vector<Leaf*> *leafs)
	{
		if (left)
		{
			left->Traverse(ByteCode + "0", leafs);
			right->Traverse(ByteCode +  "1", leafs);
		}
		else
		{
			Leaf *l = new Leaf(this->data, ByteCode);
			leafs->push_back(l);
		}
	}

	~Node()
	{
	}
};

