BST树相关函数

#include<stdio.h>
#include<assert.h>
#include<iostream>
#include<Windows.h>
#include<queue>
using namespace std;
// map rb_tree;
//树的结构
typedef int KeyType;
typedef struct BstNode
{
	KeyType key;
	BstNode* leftchild;
	BstNode* parent;
	BstNode* rightchild;
}BstNode, * BSTree;

BstNode* Buynode()
{
	BstNode* s = (BstNode*)malloc(sizeof(BstNode));
	if (NULL == s) exit(1);
	memset(s, 0, sizeof(BstNode));
	return s;
}
//创建头结点
BstNode* MakeRoot(KeyType kx)
{
	BstNode* s = Buynode();
	s->key = kx;
	return s;
}
//插入新结点左子树的值比本身小右子树相反
bool Insert(BstNode*&ptr, KeyType kx)
{
	BstNode* pa = NULL;
	BstNode* p = ptr;
	while (p != NULL && p->key != kx)
	{
		pa = p;
		p = kx < p->key ? p->leftchild : p->rightchild;
	}
	if (p != NULL && p->key == kx) return false;

	p = Buynode();
	p->key = kx;
	p->parent = pa;
	if (pa == NULL)
	{
		ptr = p;
	}
	else
	{

		if (p->key < pa->key)
		{
			pa->leftchild = p;
		}
		else
		{
			pa->rightchild = p;
		}
	}
	return true;
}
//前序遍历
void InOrder(BstNode* ptr)
{
	if (ptr != NULL)
	{
		InOrder(ptr->leftchild);
		cout << ptr->key << " ";
		InOrder(ptr->rightchild);
	}
}
//寻找最小值
BstNode* First(BstNode* ptr) // Min
{
	while (ptr != NULL && ptr->leftchild != NULL)
	{
		ptr = ptr->leftchild;
	}
	return ptr;
}
//寻找下一个大的值
BstNode* Next(BstNode* ptr)
{
	if (ptr == NULL) return ptr;
	if (ptr->rightchild != NULL)
	{
		return First(ptr->rightchild);
	}
	else
	{
		BstNode* pa = ptr->parent;
		while (pa != NULL && pa->leftchild != ptr) // pa->rightchild == ptr
		{
			ptr = pa;
			pa = ptr->parent;
		}
		return pa;
	}
}
//寻找最大值
BstNode* Last(BstNode* ptr)	 //  MAX;
{
	while (ptr != NULL && ptr->rightchild != NULL)
	{
		ptr = ptr->rightchild;
	}
	return ptr;
}
//寻找这个值的次小值
BstNode* Prev(BstNode* ptr)
{
	if (ptr == NULL) return NULL;
	if (ptr->leftchild != NULL)
	{
		return Last(ptr->leftchild);
	}
	else
	{
		BstNode* pa = ptr->parent;
		while (pa != NULL && pa->rightchild != ptr)
		{
			ptr = pa;
			pa = ptr->parent;
		}
		return pa;
	}
}
//链式思想的中序遍历
void NiceInOrder(BstNode* ptr)
{
	for (BstNode* p = First(ptr); p != NULL; p = Next(p))
	{
		cout << p->key << " ";
	}
	cout << endl;
}
//
void RevNiceInOrder(BstNode* ptr)
{
	for (BstNode* p = Last(ptr); p != NULL; p = Prev(p))
	{
		cout << p->key << " ";
	}
	cout << endl;
}

//删除一个节点     分删叶子结点,删只有一个孩子的结点,删两个叶子的节点,
bool Remove(BstNode*& ptr, KeyType kx)
{
	if (ptr == NULL) return false;
	BstNode* p = ptr;
	while (p != NULL && p->key != kx)
	{
		p = kx < p->key ? p->leftchild : p->rightchild;
	}
	if (p == NULL) return false;

	if (p->leftchild != NULL && p->rightchild != NULL)
	{
		BstNode* last = First(p->rightchild);
		p->key = last->key;	
		p = last;
	}
	BstNode* pa = p->parent;
	BstNode* child = p->leftchild != NULL ? p->leftchild : p->rightchild;
	if (child != NULL) child->parent = pa;
	if (pa == NULL)
	{
		ptr = child;
	}
	else
	{
		if (pa->leftchild == p)
		{
			pa->leftchild = child;
		}
		else
		{
			pa->rightchild = child;
		}
	}
	free(p);
	return true;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值