#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;
}
C++BinarySearchTree
最新推荐文章于 2022-09-22 22:01:17 发布