#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;
}