題目描述
給出一個數據序列,建立二叉排序樹,並實現刪除功能
對二叉排序樹進行中序遍歷,可以得到有序的數據序列
輸入
第一行輸入t,表示有t個數據序列
第二行輸入n,表示首個序列包含n個數據
第三行輸入n個數據,都是自然數且互不相同,數據之間用空格隔開
第四行輸入m,表示要刪除m個數據
從第五行起,輸入m行,每行一個要刪除的數據,都是自然數
以此類推輸入下一個示例
輸出
第一行輸出有序的數據序列,對二叉排序樹進行中序遍歷可以得到
從第二行起,輸出刪除第m個數據后的有序序列,輸出m行
以此類推輸出下一個示例的結果
樣例輸入
1 6 22 33 55 66 11 44 3 66 22 77
樣例輸出
11 22 33 44 55 66 11 22 33 44 55 11 33 44 55 11 33 44 55
提示
當刪除數據不在序列中,那么刪除操作等於不執行,所以輸出序列不變化
#include<iostream> using namespace std; class CNode { public: int data; CNode *left; CNode *right; CNode() { left=right=NULL; } }; class BStree { public: CNode *root; BStree() { root=NULL; } void addTree(int number) { CNode *t=root; while(true) { if(number<t->data) { if(t->left==NULL) { t->left=new CNode(); t->left->data=number; return; } else { t=t->left; } } else { if(t->right==NULL) { t->right=new CNode(); t->right->data=number; return; } else { t=t->right; } } } } int findtimes(int number) { CNode *t=root; int times=1; while(true) { if(t==NULL) return -1; if(number==t->data) return times; else if(number<t->data) { t=t->left; times++; } else { t=t->right; times++; } } return times; } CNode *findptr(int number) { CNode *t=root; while(true) { if(t==NULL) return NULL; if(number==t->data) return t; else if(number<t->data) t=t->left; else t=t->right; } return NULL; } CNode *findptrparent(int number) { CNode *t=root; CNode *parent=NULL; while(true) { if(t==NULL) return NULL; if(number==t->data) return parent; else if(number<t->data) { parent=t; t=t->left; } else { parent=t; t=t->right; } } return NULL; } int Delete(int number) { CNode *t=findptr(number); CNode *parent=NULL; if(t!=NULL) { parent=findptrparent(number); } while(true) { if(t==NULL)///t空,沒找到,返回0 { return 0; } if(t!=NULL&&parent==NULL)///t不空,parent空,是根 { if(t->left==NULL&&t->right!=NULL) { root=t->right; return 0; } else if(t->right==NULL&&t->left!=NULL) { root=t->left; return 0; } else if(t->left==NULL&&t->right==NULL) { root=NULL; return 0; } } if(number==t->data) { if(t->left==NULL&&t->right==NULL) { if(parent->left==t) parent->left=NULL; else if(parent->right==t) parent->right=NULL; delete t; return 1; } else if(t->left==NULL&&t->right!=NULL) { CNode *temp=t->right; if(parent->left==t) parent->left=temp; else if(parent->right==t) parent->right=temp; delete t; return 1; } else if(t->left!=NULL&&t->right==NULL) { CNode *temp=t->left; if(parent->left==t) parent->left=temp; else if(parent->right==t) parent->right=temp; delete t; return 1; } else///左右都不空 { CNode *S=t->left; CNode *Q=t; if(S->right==NULL) { t->data=S->data; t->left=S->left; delete S; return 1; } while(S->right!=NULL) { Q=S; S=S->right; } t->data=S->data; Q->right=S->left; delete S; return 1; } } return 0; } } void printTree(CNode *p) { if(p) { printTree(p->left); cout<<p->data<<" "; printTree(p->right); } } }; int main() { int T; cin>>T; while(T--) { int n; cin>>n; BStree Tree; for(int i=0;i<n;i++) { int c; cin>>c; if(i==0) { Tree.root=new CNode(); Tree.root->data=c; } else { Tree.addTree(c); } } Tree.printTree(Tree.root); cout<<endl; int m; cin>>m; while(m--) { int ch; cin>>ch; Tree.Delete(ch); Tree.printTree(Tree.root); cout<<endl; } } return 0; }