C++BinarySearchTree

#include<iostream>
#include<string.h>
#include<string>
using namespace std;

class Data
{
public:
	Data() = default;
	Data(int key, string val):key(key),val(val){}
	void PrintData()
	{
		cout << this->key << "  " << this->val << endl;
	}
	int key;
	string val;
};
class TreeNode
{
public:
	TreeNode()
	{
		this->LChild = nullptr;
		this->RChild = nullptr;
	}
	TreeNode(Data data) :data(data)
	{
		this->LChild = nullptr;
		this->RChild = nullptr;
	}
	TreeNode(Data data,TreeNode* LChild,TreeNode* RChild) :data(data)
	{
		this->LChild = LChild;
		this->RChild = RChild;
	}
	~TreeNode()
	{
		if (this->LChild != nullptr)
		{
			this->LChild = nullptr;
			delete this->LChild;
		}
		if (this->RChild != nullptr)
		{
			this->RChild = nullptr;
			delete this->RChild;
		}
	}
	Data data;
	TreeNode* LChild;
	TreeNode* RChild;
};
class BinarySearchTree
{
public:
	BinarySearchTree()
	{
		this->root = nullptr;
		this->cursize = 0;
	}
	BinarySearchTree(TreeNode* root)
	{
		this->root = root;
		this->cursize = 0;
	}
	void Push(Data data)
	{
		TreeNode* NewNode = new TreeNode(data);
		TreeNode* pmove = this->root;
		TreeNode* pmovefather = nullptr;
		while (pmove != nullptr && pmove->data.key != NewNode->data.key)
		{
			pmovefather = pmove;
			if (pmove->data.key > NewNode->data.key)
			{
				pmove = pmove->LChild;
			}
			else if (pmove->data.key < NewNode->data.key)
			{
				pmove = pmove->RChild;
			}
			else
			{
				char* c = const_cast<char*>(pmove->data.val.c_str());
				strcpy(c, NewNode->data.val.c_str());
			}
		}
		if (pmovefather == nullptr)
		{
			this->root = NewNode;
			return;
		}
		if (pmovefather->data.key > NewNode->data.key)
		{
			pmovefather->LChild = NewNode;
		}
		else
		{
			pmovefather->RChild = NewNode;
		}
		this->cursize++;
	}
	Data Seach(int key)
	{
		TreeNode* pmove = this->root;
		while (pmove != nullptr && pmove->data.key != key)
		{
			if (pmove->data.key > key)
			{
				pmove = pmove->LChild;
			}
			else
			{
				pmove = pmove->RChild;
			}
		}
		Data data;
		data = pmove->data;
		return data;
	}
	TreeNode* SeachFatherNode(int key)
	{
		TreeNode* pmove = this->root;
		TreeNode* pfather = nullptr;
		while (pmove != nullptr && pmove->data.key != key)
		{
			pfather = pmove;
			if (pmove->data.key > key)
			{
				pmove = pmove->LChild;
			}
			else
			{
				pmove = pmove->RChild;
			}
		}
		return pfather;
	}
	TreeNode* SeachNode(int key)
	{
		TreeNode* pmove = this->root;
		while (pmove != nullptr && pmove->data.key != key)
		{
			if (pmove->data.key > key)
			{
				pmove = pmove->LChild;
			}
			else
			{
				pmove = pmove->RChild;
			}
		}
		return pmove;
	}
	void DeleteNode(int key)
	{
		TreeNode* pmove = this->SeachNode(key);
		TreeNode* pmoveparent = this->SeachFatherNode(key);
		if (pmove == nullptr)
		{
			cout << "结点为空,无法删除";
			return;
		}
		if (pmove->LChild != nullptr && pmove->RChild != nullptr)
		{
			TreeNode* MoveNode = pmove->LChild;
			TreeNode* MoveNodeParent = nullptr;
			while (MoveNode->RChild!= nullptr)
			{
				MoveNodeParent - MoveNode;
				MoveNode = MoveNode->RChild;
			} 
			TreeNode* newNode = new TreeNode(MoveNode->data);
			newNode->LChild = pmove->LChild;
			newNode->RChild = pmove->RChild;
			if (pmoveparent == nullptr)
			{
				this->root = newNode;
			}
			else if (pmoveparent->LChild == pmove)
			{
				pmoveparent->LChild = newNode;
			}
			else
			{
				pmoveparent->RChild = newNode;
			}
			if (pmoveparent == pmove)
			{
				pmoveparent = newNode;
			}
			else
			{
				pmoveparent = MoveNodeParent;
			}
			delete pmove;
			pmove = MoveNode;
			return;
			this->cursize--;
		}
		TreeNode* snode = nullptr;
		if (pmove->LChild != nullptr)
		{
				//有左边
			snode = pmove->LChild;
		}
		else
		{
				//有右边
			snode = pmove->RChild;
		}
		if (this->root == pmove)
		{
			root = snode;
		}
		else
		{
			if (pmoveparent->LChild == pmove)
			{
				pmoveparent->LChild = snode;
			}
			else
			{
				pmoveparent->RChild = snode;
			}
		}
		delete pmove;
		this->cursize--;
		return;
	}
	void PrintTree(TreeNode* Node)
	{
		if (Node != nullptr)
		{
			PrintTree(Node->LChild);
			Node->data.PrintData();
			PrintTree(Node->RChild);
		}
	}
	~BinarySearchTree()
	{
		if (this->root != nullptr)
		{
			this->root = nullptr;
			delete this->root;
			this->root = 0;
		}
	}
	TreeNode* root;
	int cursize;
};

int main()
{
	BinarySearchTree BST;
	BST.Push(Data(6,"A"));
	BST.Push(Data(7, "B"));
	BST.Push(Data(8, "C"));
	BST.Push(Data(9, "D"));
	BST.Push(Data(10, "E"));
	BST.DeleteNode(9);
	BST.PrintTree(BST.root);


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值