【数据结构】二叉搜索树

#include<windows.h>
#include<assert.h>
#include<limits.h>
#include<iostream>
#include<vector>
#include<time.h>
#include<stack>
#include<queue>
using namespace std;
/*
typedef int KeyType;
typedef struct BstNode
{
	BstNode *leftchild;
	BstNode *parent;
	BstNode *rightchild;
	KeyType key;
}BstNode;

typedef struct
{
	BstNode *head;  //一定要带头结点,不然就要用二级指针!
	int cursize;
}BSTree;

BstNode * Buynode(BstNode *pa = NULL)
{
	BstNode *s = (BstNode *)malloc(sizeof(BstNode));
	if(NULL == s) exit(1);
	memset(s,0,sizeof(BstNode));
	s->parent = pa;
	return s;
}
void Freenode(BstNode *p)
{
	free(p);
}
void InitBSTree(BSTree &myt)
{
	myt.head = Buynode();
	myt.cursize = 0;
}
void DestroyBSTree(BSTree &myt)
{
}

BstNode * FindValue(BSTree &myt,KeyType kx)  //查找某结点  //循环
{
	BstNode * p = myt.head->parent; // root;
	while(p != NULL && p->key != kx)
	{
		p = kx < p->key? p->leftchild:p->rightchild;
	}
	return p;
}
BstNode * Search(BstNode *p,KeyType kx)   //查找某结点  //递归查找
{
	if(p == NULL || p->key == kx) 
		return ptr;
	else if(kx < p->key)
		return Search(p->leftchild,kx);
	else 
		return Search(p->rightchild,kx);
}
BstNode * SearchValue(BSTree &myt,KeyType kx)
{
	return Search(myt.head->parent,kx);
}

BstNode * First(BstNode *ptr) // Min;
{
	while(ptr != NULL && ptr->leftchild != NULL)
	{
		ptr = ptr->leftchild;
	}
	return ptr;
}
BstNode * Next(BSTree &myt,BstNode *ptr)
{
	if(ptr == NULL || ptr == myt.head) return NULL;
	if(ptr->rightchild != NULL)
	{
		return First(ptr->rightchild);
	}
	else
	{
		BstNode *pa = ptr->parent;
		while(pa != myt.head && pa->leftchild != ptr)
		{
			ptr = pa;
			pa = pa->parent;
		}
		if(pa == myt.head)
		{
			pa = NULL;
		}
		return pa;
	}
}
BstNode *Last(BstNode *ptr) // Max  //从大输出
{
	while(ptr != NULL && ptr->rightchild != NULL)
	{
		ptr = ptr->rightchild;
	}
	return ptr;
}
BstNode * Prev(BSTree &myt,BstNode *ptr)
{
	if(ptr == NULL || ptr == myt.head) return NULL;
	if(ptr->leftchild != NULL)
	{
		return Last(ptr->leftchild);
	}
	else
	{
		BstNode *pa = ptr->parent;
		while(pa != myt.head && pa->rightchild != ptr)
		{
			ptr = pa;
			pa = pa->parent;
		}
		if(pa == myt.head)
		{
			pa = NULL;
		}
		return pa;
	}
}
void NiceInOrder(BSTree &myt) 
{
	for(BstNode *p = First(myt.head->parent);
		p !=NULL; p = Next(myt,p))
	{
		cout<<p->key<<" ";
	}
	cout<<endl;
}
void ResNiceInOrder(BSTree &myt)  //中序的逆序输出  //从大到小
{
	for(BstNode *p = Last(myt.head->parent); 
		p != NULL; p = Prev(myt,p))
	{
		cout<<p->key<<" ";
	}
	cout<<endl;
}
bool InsertBST(BSTree &myt,KeyType kx)  //插入数据
{
	BstNode *pa = myt.head ; // head;
	BstNode *p = myt.head->parent;// root;
	while(p != NULL && p->key != kx)
	{
		pa = p;
		p = kx < p->key? p->leftchild:p->rightchild;
	}
	if(p != NULL)  return false;
	p = Buynode(pa);
	p->key = kx;
	if(pa == myt.head)
	{
		myt.head->leftchild = p;
		myt.head->rightchild = p;
		myt.head->parent = p;
	}
	else
	{
		if(p->key < pa->key)
		{
			pa->leftchild = p;
			if(p->key < myt.head->leftchild->key)
			{
				myt.head->leftchild = p;
			}
		}
		else
		{
			pa->rightchild = p;
			if(p->key > myt.head->rightchild->key)
			{
				myt.head->rightchild = p;
			}
		}
	}
	myt.cursize+=1;
	return true;
}
bool RemoveBST(BSTree &myt,KeyType kx)  //删除数据
{
	BstNode *pa = myt.head; // head
	BstNode *p = myt.head->parent; // root;
	while(p != NULL && p->key != kx)
	{
		pa = p;
		p = kx < p->key? p->leftchild:p->rightchild;
	}
	if(p == NULL) return false;
	if(p->leftchild != NULL && p->rightchild != NULL)
	{
		BstNode *nt = Next(myt,p->rightchild);
		p->key = nt->key;
		p = nt;
	}
	BstNode * child = p->leftchild != NULL? p->leftchild:p->rightchild;
	if(child != NULL) child->parent = pa;
	if(pa == myt.head)
	{
		myt.head->parent = child;
	}
	else
	{
		if(p == pa->leftchild)
		{
			pa->leftchild = child;
		}
		else
		{
			pa->rightchild = child;
		}
	}
	Freenode(p);
	myt.cursize-=1;
	return true;
}
int main()
{
	int ar[]={53,17,78,9,45,65,87,23,81,94,88};
	int n = sizeof(ar)/sizeof(ar[0]);
	BSTree myt;
	InitBSTree(myt);
	for(int i = 0;i<n;++i)
	{
		InsertBST(myt,ar[i]);
	}
	NiceInOrder(myt);
	ResNiceInOrder(myt);
	int kx;
	while(cin>>kx, kx != -1)
	{
		RemoveBST(myt,kx);
		NiceInOrder(myt);
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值