【数据结构】二叉查找树

本文详细介绍了如何使用C++编程语言实现二叉排序树的数据结构,包括创建树、插入节点、查找节点以及删除节点的操作,并展示了如何使用LCA算法在特定条件下寻找最近公共祖先。
摘要由CSDN通过智能技术生成

问题 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(){
//	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 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;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值