问题 A: DS二叉排序树之创建和插入
#include<iostream>
using namespace std;
struct node{
node* left;
node* right;
int data;
};
class BST{
public:
node* root;
BST(){
root=NULL;
int t;
cin>>t;
while(t--){
int T;
cin>>T;
InsertNode(root,T);
}
}
void InsertNode(node* &p,int x){
if(p==NULL){
p=new node;
p->left=NULL;
p->right=NULL;
p->data=x;
return;
}
if(p->data>x) InsertNode(p->left,x);
else InsertNode(p->right,x);
}
void FindNode(node* &p,int x,int cnt){
if(p==NULL){
cout<<-1<<endl;
return;
}
if(p->data==x){
cout<<cnt<<endl;
return;
}
if(x<p->data) FindNode(p->left,x,cnt+1);
else FindNode(p->right,x,cnt+1);
}
void Inorder(node* &p){
if(p==NULL) return;
Inorder(p->left);
cout<<p->data<<" ";
Inorder(p->right);
}
void Inorder(){
Inorder(root);
cout<<endl;
}
};
void solve(){
BST test;
int t;
cin>>t;
test.Inorder();
while(t--){
int n;
cin>>n;
test.InsertNode(test.root,n);
test.Inorder();
}
}
int main(){
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
DS二叉排序树之查找
#include<iostream>
using namespace std;
struct node{
node* left;
node* right;
int data;
};
class BST{
public:
node* root;
BST(){
root=NULL;
int t;
cin>>t;
while(t--){
int T;
cin>>T;
InsertNode(root,T);
}
}
void InsertNode(node* &p,int x){
if(p==NULL){
p=new node;
p->left=NULL;
p->right=NULL;
p->data=x;
return;
}
if(p->data>x) InsertNode(p->left,x);
else InsertNode(p->right,x);
}
void FindNode(node* &p,int x,int cnt){
if(p==NULL){
cout<<-1<<endl;
return;
}
if(p->data==x){
cout<<cnt<<endl;
return;
}
if(x<p->data) FindNode(p->left,x,cnt+1);
else FindNode(p->right,x,cnt+1);
}
void Inorder(node* &p){
if(p==NULL) return;
Inorder(p->left);
cout<<p->data<<" ";
Inorder(p->right);
}
void Inorder(){
Inorder(root);
cout<<endl;
}
};
void solve(){
BST test;
int t;
cin>>t;
test.Inorder();
while(t--){
int n;
cin>>n;
test.FindNode(test.root,n,1);
}
}
int main(){
freopen("d:\\in.txt","r",stdin);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
DS二叉排序树之删除
#include<iostream>
using namespace std;
struct node{
node* left;
node* right;
int data;
};
class BST{
public:
node* root;
BST(){
root=NULL;
int t;
cin>>t;
while(t--){
int T;
cin>>T;
InsertNode(root,T);
}
}
void InsertNode(node* &p,int x){
if(p==NULL){
p=new node;
p->left=NULL;
p->right=NULL;
p->data=x;
return;
}
if(p->data>x) InsertNode(p->left,x);
else InsertNode(p->right,x);
}
void FindNode(node* &p,int x,int cnt){
if(p==NULL){
cout<<-1<<endl;
return;
}
if(p->data==x){
cout<<cnt<<endl;
return;
}
if(x<p->data) FindNode(p->left,x,cnt+1);
else FindNode(p->right,x,cnt+1);
}
void Inorder(node* &p){
if(p==NULL) return;
Inorder(p->left);
cout<<p->data<<" ";
Inorder(p->right);
}
void Inorder(){
Inorder(root);
cout<<endl;
}
void DeleteNode(node* &p){
if(p->left==NULL&&p->right==NULL){
p=NULL;
}
else if(p->left==NULL){
p=p->right;
}
else if(p->right==NULL){
p=p->left;
}
else{
node* pre=p->left;
node* parent=p;
while(pre->right){
parent=pre;
pre=pre->right;
}
p->data=pre->data;
if(p!=parent){
parent->right=pre->left;
}
else{
parent->left=pre->left;
}
}
}
void DeleteTree(node* &p,int x){
if(p==NULL) return;
if(p->data==x){
DeleteNode(p);
return ;
}
if(p->data<x) DeleteTree(p->right,x);
else DeleteTree(p->left,x);
}
};
void solve(){
BST test;
int t;
cin>>t;
test.Inorder();
while(t--){
int n;
cin>>n;
test.DeleteTree(test.root,n);
test.Inorder();
}
}
int main(){
freopen("d:\\in.txt","r",stdin);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N];
int n,m,root;
bool st[N];
int head[N],nxt[N],to[N],idx;
int depth[N],fa[N][31];
unordered_map<int,int>bk;
void add(int u,int v){
to[++idx]=v,nxt[idx]=head[u],head[u]=idx;
}
void init(){
queue<int>q;
memset(depth,0x3f,sizeof depth);
depth[0]=0,depth[root]=1;
q.push(root);
while(q.size()){
int t=q.front();
q.pop();
for(int i=head[t];i;i=nxt[i]){
int j=to[i];
if(depth[j]>depth[t]+1){
depth[j]=depth[t]+1;
fa[j][0]=t;
q.push(j);
for(int k=1;k<31;k++)
fa[j][k]=fa[fa[j][k-1]][k-1];
}
}
}
}
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
for(int i=30;i>=0;i--)
if(depth[fa[a][i]]>=depth[b])
a=fa[a][i];
if(a==b) return a;
for(int i=30;i>=0;i--)
if(fa[a][i]!=fa[b][i]){
a=fa[a][i];
b=fa[b][i];
}
return fa[a][0];
}
void build(int u){
st[u]=true;
int l,r;
bool flagl=false,flagr=false;
for(int i=1;i<=n;i++){
if(!st[i]&&a[u]>a[i]){
flagl=true;
l=i;
break;
}
}
if(flagl){
add(u,l);
build(l);
}
for(int i=1;i<=n;i++){
if(!st[i]&&a[u]<a[i]){
flagr=true;
r=i;
break;
}
}
if(flagr){
add(u,r);
build(r);
}
}
void solve(){
cin>>m>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
bk[a[i]]++;
}
root=a[1];
build(1);
init();
while(m--){
int u,v;
cin>>u>>v;
if(!bk[u]&&!bk[v]){
printf("ERROR: %d and %d are not found.\n",u,v);
continue;
}
else if(!bk[u]){
printf("ERROR: %d is not found.\n",u);
continue;
}
else if(!bk[v]){
printf("ERROR: %d is not found.\n",v);
continue;
}
int p=lca(u,v);
if(p==u){
printf("%d is an ancestor of %d.\n",p,v);
continue;
}
if(p==v){
printf("%d is an ancestor of %d.\n",p,u);
continue;
}
printf("LCA of %d and %d is %d.\n",u,v,p);
}
}
int main(){
freopen("d:\\in.txt","r",stdin);
int t=1;
while(t--){
solve();
}
return 0;
}