C++二叉搜索树(非递归算法)

#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;


template <typename Comparable>
class BinarySearchTree
{
public:
	BinarySearchTree()
	{
		root = nullptr;
	}
	BinarySearchTree(const BinarySearchTree& rhs) :root(nullptr)
	{
		root = clone(rhs.root);
	}
	BinarySearchTree(BinarySearchTree&& rhs)
	{
		root = rhs.root;
		rhs.root = nullptr;
	}
	BinarySearchTree& operator = (const BinarySearchTree& rhs)
	{
		BinarySearchTree copy = rhs;
		std::swap(*this, copy);
		return *this;

	}
	BinarySearchTree& operator = (BinarySearchTree&& rhs)
	{
		std::move(root, rhs.root);
		return *this;
	}
	~BinarySearchTree()
	{
		makeEmpty();
	}

	const Comparable& findMin() const
	{
		return findMin(root)->element;
	}
	const Comparable& findMax() const
	{
		return findMax(root)->element;
	}
	bool contains(const Comparable& x) const
	{
		return contains(x, root);
	}
	bool isEmpty() const
	{
		return root == nullptr;
	}
	void printTree(ostream& out = cout) const
	{
		if (root == nullptr)
		{
			out << "Empty tree!" << endl;
			return;
		}
		printTree(root, out);
	}

	void makeEmpty()
	{
		makeEmpty(root);
	}
	void insert(const Comparable& x)
	{
		insert(x, root);
	}
	void insert(Comparable&& x)
	{
		insert(std::move(x), root);
	}

	void remove(const Comparable& x)
	{
		remove(x, root);
	}

private:
	struct BinaryNode
	{
		Comparable element;
		BinaryNode* left;
		BinaryNode* right;
		BinaryNode(const Comparable& theElement, BinaryNode* lt, BinaryNode* rt) :element(theElement), left(lt), right(rt)
		{};
		BinaryNode(Comparable&& theElement, BinaryNode* lt, BinaryNode* rt) :element(std::move(theElement)), left(lt), right(rt)
		{};
	};
	BinaryNode* root;

	void insert(const Comparable& x, BinaryNode*& t)
	{
		if (t == nullptr)
		{
			t = new BinaryNode(x, nullptr, nullptr);
		}
		BinaryNode* p = t;
		while (x < p->element && p->left != nullptr || x > p->element && p->right != nullptr)
		{
			if (x < p->element && p->left != nullptr)
			{
				p = p->left;
			}
			else if (x > p->element && p->right != nullptr)
			{
				p = p->right;
			}
		}
		if (x < p->element)
		{
			p->left = new BinaryNode(x, nullptr, nullptr);
		}
		else if (x > p->element)
		{
			p->right = new BinaryNode(x, nullptr, nullptr);
		}

	}
	void insert(Comparable&& x, BinaryNode*& t)
	{
		if (t == nullptr)
		{
			t = new BinaryNode(std::move(x), nullptr, nullptr);
		}
		BinaryNode* p = t;
		while (x < p->element && p->left != nullptr || x > p->element && p->right != nullptr)
		{
			if (x < p->element && p->left != nullptr)
			{
				p = p->left;
			}
			else if (x > p->element && p->right != nullptr)
			{
				p = p->right;
			}
		}
		if (x < p->element)
		{
			p->left = new BinaryNode(std::move(x), nullptr, nullptr);
		}
		else if (x > p->element)
		{
			p->right = new BinaryNode(std::move(x), nullptr, nullptr);
		}
	}
	void remove(const Comparable& x, BinaryNode*& t)
	{
		if (t == nullptr)
		{
			t = nullptr;
			return;
		}
		BinaryNode* p = t, * pre = nullptr;
		while (p->left != nullptr && x < p->element || p->right != nullptr && x > p->element)
		{
			pre = p;
			if (p->left != nullptr && x < p->element)
			{
				p = p->left;
			}
			if (p->right != nullptr && x > p->element)
			{
				p = p->right;
			}
		}
		if (p->element != x)
		{
			return;
		}
		else if (p->left != nullptr && p->right != nullptr)
		{
			p->element = findMin(p->right)->element;
			remove(p->element, p->right);
		}
		else
		{
			BinaryNode* old = p;
			if (pre != nullptr)
			{
				(p == pre->right ? pre->right : pre->left) = ((p->left != nullptr) ? p->left : p->right);
			}
			else
			{
				t = (p->left == nullptr ? p->right : p->left);
			}
			delete old;
		}
	}
	BinaryNode* findMin(BinaryNode* t) const
	{
		if (t == nullptr)
		{
			return nullptr;
		}
		while (t->left != nullptr)
		{
			t = t->left;
		}
		return t;
	}
	BinaryNode* findMax(BinaryNode* t) const
	{
		if (t == nullptr)
		{
			return nullptr;
		}
		while (t->right != nullptr)
		{
			t = t->right;
		}
		return t;
	}
	bool contains(const Comparable& x, BinaryNode* t) const
	{
		if (t == nullptr)
		{
			return false;
		}
		while (t->left != nullptr && x < t->element || t->right != nullptr && x > t->element)
		{
			if (t->left != nullptr && x < t->element)
			{
				t = t->left;
			}
			else
			{
				t = t->right;
			}
		}
		return t->element == x;
	}
	void makeEmpty(BinaryNode*& t) //后续遍历,优先删除孩子结点
	{
		stack<BinaryNode*> sta;
		BinaryNode* p = t, * r = nullptr;
		while (p || !sta.empty())
		{
			if (p)
			{
				sta.push(p);
				p = p->left;
			}
			else
			{
				p = sta.top();
				if (p->right != nullptr && p->right != r)
				{
					p = p->right;
				}
				else
				{
					p = sta.top(); sta.pop();
					r = p;
					delete p;
					p = nullptr;
				}
			}
		}
		t = nullptr;
	}
	void printTree(BinaryNode* t, ostream& out) const //中序遍历,迭代法
	{
		stack<BinaryNode*> sta;
		BinaryNode* p = t;
		long long int i = 0;
		while (p || !sta.empty())
		{
			if (p)
			{
				sta.push(p);
				p = p->left;
			}
			else
			{
				p = sta.top();
				sta.pop();
				out << "#" << ++i << ": " << p->element << endl;
				p = p->right;
			}
		}
	}
	BinaryNode* clone(BinaryNode* t) const  //层次遍历复制
	{
		if (t == nullptr)
		{
			return nullptr;
		}

		queue<BinaryNode*> q1, q2;
		BinaryNode* p1, * p2, * newroot = new BinaryNode(t->element, nullptr, nullptr);
		q1.push(t), q2.push(newroot);
		while (!q1.empty())
		{
			p1 = q1.front(); q1.pop();
			p2 = q2.front(); q2.pop();
			if (p1->left)
			{
				q1.push(p1->left);
				p2->left = new BinaryNode(p1->left->element, nullptr, nullptr);
				q2.push(p2->left);

			}
			if (p1->right)
			{
				q1.push(p1->right);
				p2->right = new BinaryNode(p1->right->element, nullptr, nullptr);
				q2.push(p2->right);
			}
		}
		return newroot;

	}



};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值