二叉排序树(二叉查找树)

二叉排序树(二叉查找树)

删除插入操作

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值