二叉排序树(二叉查找树)
删除插入操作
#include<iostream>
using namespace std;
typedef struct SortTree
{
int value;
int leftLink, rightLink;
struct SortTree *leftChild, *rightChild, *parent;
}SortTree;
SortTree *head;
void initSortTree()
{
head = (SortTree *)malloc(sizeof(SortTree *));
head->value = 1000;
head->leftChild = NULL;
head->rightChild = NULL;
head->leftLink = -1;
head->rightLink = -1;
}
SortTree* Search(SortTree *node, int value, SortTree *parent)
{
if (!node)return parent;
if (node->value == value)return node;
else if (node->value > value)
return Search(node->leftChild, value, node);
else if (node->value < value)
return Search(node->rightChild, value, node);
}
bool Insert(int value)
{
SortTree *temporary = NULL;
temporary = Search(head, value, head);
if (temporary->value == value)return false;
else
{
SortTree *node = (SortTree *)malloc(sizeof(SortTree));
node->value = value;
node->leftChild = NULL;
node->rightChild = NULL;
node->leftLink = -1;
node->rightLink = -1;
if (temporary->value > node->value)
temporary->leftChild = node;
else if (temporary->value < node->value)
temporary->rightChild = node;
node->parent = temporary;
return true;
}
}
void print(SortTree *node)
{
if (!node)return;
print(node->leftChild);
if (node != head)cout << node->value << " " << endl;
print(node->rightChild);
}
void Delete(int value)
{
SortTree *temporary = NULL;
temporary = Search(head, value, head);
if (!temporary->leftChild && !temporary->rightChild)
{
if (temporary->parent->rightChild == temporary)
temporary->parent->rightChild = NULL;
else if (temporary->parent->leftChild == temporary)
temporary->parent->leftChild = NULL;
free(temporary);
}
else if (!temporary->leftChild)
{
if (temporary->parent->rightChild == temporary)
{
temporary->parent->rightChild = temporary->rightChild;
temporary->rightChild->parent = temporary->parent;
}
else if (temporary->parent->leftChild == temporary)
{
temporary->parent->leftChild = temporary->rightChild;
temporary->rightChild->parent = temporary->parent;
}
free(temporary);
}
else if (!temporary->rightChild)
{
if (temporary->parent->rightChild == temporary)
{
temporary->parent->rightChild = temporary->leftChild;
temporary->leftChild->parent = temporary->parent;
}
else if (temporary->parent->leftChild == temporary)
{
temporary->parent->leftChild = temporary->leftChild;
temporary->leftChild->parent = temporary->parent;
}
free(temporary);
}
else
{
SortTree *p = NULL;
p = temporary->leftChild;
if (p)
{
while (p->rightChild)
p = p->rightChild;
}
temporary->rightChild->parent = p;
p->rightChild = temporary->rightChild;
if (temporary->parent->rightChild == temporary)
{
temporary->parent->rightChild = temporary->leftChild;
temporary->leftChild->parent = temporary->parent;
}
else if (temporary->parent->leftChild == temporary)
{
temporary->parent->leftChild = temporary->leftChild;
temporary->leftChild->parent = temporary->parent;
}
free(temporary);
}
}
int main()
{
initSortTree();
Insert(10);
Insert(5);
Insert(11);
Insert(2);
Insert(6);
Insert(12);
Delete(12);
print(head);
return 0;
}