Data-Structures-and-Algorithms / Binary Tree / ExamTests / NapTree.h
NapTree.h
Raw
#pragma once

/*
 *  NOTE:
 *  Please keep all your code in the header file for the exam (no .cpp required)
 */

/*	WARNING:
 *	IF you are having issues with missing headers / libraries
 *	follow the steps below to fix
 *		- Tools->NuGet Package Manager->Manage NuGet Packages For Solution
 *		- Uninstall Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn
 *		- Install Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn
 *
 *	If you cannot find Windows SDK version or Platform Toolset
 *		- Right click project->Properties
 *		- General Tab
 *		- Update Windows SDK / Platform Toolset to one on your machine
 */

 /*
  * Params (do not change parameter names)
  *
  * All the parameters for the problem should be stored in
  * this struct prior to calling process on the tree
  */
struct Params
{
	bool want_to_nap;
	bool trouble_sleeping_at_night;
	bool after_4_pm;
	bool at_work;
	bool will_boss_be_mad;
	bool have_30_minutes;
	bool have_10_minutes;

	Params() : want_to_nap(false), trouble_sleeping_at_night(false),
		after_4_pm(false), at_work(false),
		will_boss_be_mad(false), have_30_minutes(false),
		have_10_minutes(false)
	{
	}
};

/*
 * TreeNode
 *
 * This is the base structure for all the tree nodes (no need to modify)
 * All your nodes should inherit from this struct
 * and override the process method
 *
 */
struct TreeNode
{
	TreeNode* true_;
	TreeNode* false_;

	explicit TreeNode(TreeNode* true_ = nullptr, TreeNode* false_ = nullptr) :true_(true_), false_(false_) {}
	virtual ~TreeNode() = default;

	virtual bool process(const Params& params) const = 0;
};

/*
 * Leaf Nodes:
 *	Yes/No - These are the terminal/leaf nodes and
 *	will return the final result of the tree
 */
struct No final : public TreeNode
{
	bool process(const Params& params) const override
	{
		return false;
	}
};

struct Yes final : public TreeNode
{
	bool process(const Params& params) const override
	{
		return true;
	}
};

/*
 * Place all your node class/struct below this comment.
 */

struct WantToNap final : public TreeNode
{
	explicit WantToNap(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.want_to_nap ? true_->process(p) : false_->process(p);
	}
};

struct HaveTroubleSleeping final : public TreeNode
{
	explicit HaveTroubleSleeping(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.trouble_sleeping_at_night ? true_->process(p) : false_->process(p);
	}
};

struct After4Pm final : public TreeNode
{
	explicit After4Pm(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.after_4_pm ? true_->process(p) : false_->process(p);
	}
};

struct AtWork final : public TreeNode
{
	explicit AtWork(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.at_work ? true_->process(p) : false_->process(p);
	}
};

struct WillBossBeMad final : public TreeNode
{
	explicit WillBossBeMad(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.will_boss_be_mad ? true_->process(p) : false_->process(p);
	}
};

struct Have30Minutes final : public TreeNode
{
	explicit Have30Minutes(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.have_30_minutes ? true_->process(p) : false_->process(p);
	}
};

struct Have10Minutes final : public TreeNode
{
	explicit Have10Minutes(TreeNode* trueBranch, TreeNode* falseBranch) : TreeNode(trueBranch, falseBranch) {}

	bool process(const Params& p) const override
	{
		return p.have_10_minutes ? true_->process(p) : false_->process(p);
	}
};

/**
 * \brief use this function to build your tree structure
 * \return the root node of the built tree
 */
inline TreeNode* create_tree()
{
	auto* yes = new Yes();
	auto* no = new No();

	auto* have10Minutes = new Have10Minutes(yes, no);
	auto* have30Minutes = new Have30Minutes(yes, have10Minutes);
	auto* bossMad = new WillBossBeMad(no, have30Minutes);
	auto* atWork = new AtWork(bossMad, have30Minutes);
	auto* after4Pm = new After4Pm(no, atWork);
	auto* troubleSleeping = new HaveTroubleSleeping(no, after4Pm);
	auto* takeNap = new WantToNap(troubleSleeping, after4Pm);


	return takeNap;
}