buct数据结构——平衡树和hash表

问题 A: 案例4-1.3:平衡二叉树的根

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
struct node {
	int balanced_factor;
	int height;
	int val;
	node *lchild,*rchild,*fa;
	node(){
		lchild=NULL,rchild=NULL,fa=NULL;
		balanced_factor=0,height=1,val=0;
	}
};

void pushup( node * rt){
	//通过左右儿子的信息更新根节点的信息
	int hl=0,hr=0;
	if(rt->lchild!=NULL) hl=rt->lchild->height;
	if(rt->rchild!=NULL) hr=rt->rchild->height;
	rt->height=max(hl,hr)+1;
	rt->balanced_factor=hl-hr;
}
node * rotate( node *son,node *rt){
	if(rt->rchild==son){//此时rt需要左旋
		rt->rchild=son->lchild;
		if(son->lchild!=NULL)
		son->lchild->fa=rt;
		son->lchild=rt;
		rt->fa=son;
	}
	else{
		rt->lchild=son->rchild;
		if(son->rchild!=NULL)
		son->rchild->fa=rt;
		son->rchild=rt;
		rt->fa=son;
	}
	pushup(rt);
	pushup(son);
	return son;
}
int get_height(node *rt){
	//防止查询时指针悬空
	if(rt==NULL) return 0;
	else return rt->height;
}
node * adjust( node *rt){
	if(get_height(rt->lchild)>get_height(rt->rchild)){
		node *son=rt->lchild;
		if(get_height(son->lchild)>get_height(son->rchild)){
			//LL型
			return rotate(son,rt);
		}
		else{
			//LR型
			rt->lchild=rotate(son->rchild,son);
			return rotate(rt->lchild,rt);
		}
	}
	else {
		node * son=rt->rchild;
		if(get_height(son->lchild)>get_height(son->rchild)){
			//RL型
			rt->rchild=rotate(son->lchild,son);
			return rotate(rt->rchild,rt);
		}
		else{
			//RR型
			return rotate(son,rt);
		}

	}
}
node * insert(int val,node * rt){
	if(val>rt->val){
		//插在右子树上
		if(rt->rchild==NULL){
			node *p=new node;
			p->fa=rt;
			p->val=val;
			rt->rchild=p;
		}
		else
		rt->rchild=insert(val,rt->rchild);
	}
	else {
		//插在左子树上
		if(rt->lchild==NULL){
			node *p=new node ;
			p->fa=rt;
			p->val=val;
			rt->lchild=p;
		}
		else 
		rt->lchild=insert(val,rt->lchild);
	}
	pushup(rt);
	if(abs(rt->balanced_factor)>1)//不平衡,需要调整
	rt=adjust(rt);
	return rt;
}
// void debug( node * root){
// 	if(root==NULL) return ;
// 	cout<<root->val<<root->balanced_factor<<endl;
// 	if(root->lchild!=NULL){
// 		debug(root->lchild);

// 	}
// 	if(root->rchild!=NULL){
// 		debug(root->rchild);
// 	}
// 	return ;
// }
int main(){
	int n;
	cin>>n;
	node *root=new node;
	cin>>root->val;
	for( int i=2;i<=n;i++){
		int x;
		cin>>x;
		root=insert(x,root);
	}
	printf("%d",root->val);
	return 0;
}

问题 B: 二叉排序树 - 文本输出

见F题

问题 C: 销售排行榜

问题 D: 算法9-2:有序表的折半查找

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int main(){
	int n,m;
	cin>>n>>m;
	vector<int>v;
	for( int i=0;i<n;i++){
		int x;
		cin>>x;
		v.push_back(x);
	}
	for( int i=0;i<m;i++){
		int x,l=0,r=v.size();
		cin>>x;
		while(l<r){
			int mid=(l+r)>>1;
			if(v[mid]>=x) r=mid;
			else l=mid+1;
		}
		if(v[l]!=x) cout<<-1<<" ";
		else cout<<l<<" ";
	}
	return 0;
}

问题 E: Hash表-线性探测法解决冲突

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=11;
struct Hash_map{
	int next_pos[N];
	string real_val[N];
	void init(){
		// memset(Hash_val,0,sizeof(Hash_val));
		for( int i=0;i<11;i++) real_val[i]="#";
		memset(next_pos,-1,sizeof(next_pos));
	}
	int get_hash_val(string s){
		int res=0;
		for( int i=s.size()-4;i<=s.size()-1;i++) res+=s[i]-'0';
		return res%10;
	}
	void insert(string s){
		int x=get_hash_val(s);
		int shift=0;
		while(1){
			if(x+shift<=9&&real_val[x+shift]=="#"){
				real_val[x+shift]=s;
				next_pos[x]=x+shift;
				break;
			}
			if(x-shift>=0&&real_val[x-shift]=="#"){
				real_val[x-shift]=s;
				next_pos[x]=x-shift;
				break;
			}
			shift++;

		}
	}

};
int main(){
	Hash_map H;
	H.init();
	int n;
	cin>>n;
	for( int i=0;i<n;i++){
		string s;
		cin>>s;
		H.insert(s);
	}
	for( int i=0;i<10;i++){
		cout<<H.real_val[i]<<endl;
	}
	return 0;
}

问题 F: 二叉排序树-平衡因子

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=200100;
const ll mod=998224353;
int tol[N],tor[N],w[N];
int cnt=0;
void insert(int rt,int val){
    int l=tol[rt],r=tor[rt];
    if(val>w[rt]){
        if(r==-1){
            tor[rt]=++cnt;
            w[cnt]=val;
        }
        else {
            insert(r,val);
        }
        return ;
    }
    else{
        if(l==-1){
            tol[rt]=++cnt;
            w[cnt]=val;
        }
        else{
            insert(l,val);
        }
        return ;
    }
}
int get_h(int rt){
    if(rt==-1){
        return 0;
    }
    int l=tol[rt],r=tor[rt];
    int hl=0,hr=0;
    if(l!=-1){
        hl=get_h(l)+1;
    }
    if(r!=-1){
        hr=get_h(r)+1;
    }
    return max(hl,max(1,hr));
}
void dfs( int h,int rt){
    int l=tol[rt],r=tor[rt];
    // h=get_h(rt);
    for( int i=1;i<=h;i++){
        printf("    ");
    }
    if(rt==-1){
        cout<<'#'<<endl;
        return ;
    }
    cout<<w[rt];
    printf("(%d)\n",get_h(l)-get_h(r));
    if(l==-1&&r==-1){
        return ;
    }
    if(l!=-1||r!=-1){
        dfs(h+1,l);
        dfs(h+1,r);
        return ;
    }
    if(l!=-1)
    dfs(h+1,l);
    if(r!=-1)
    dfs(h+1,r);
    return ;
}
int main(){
    int n;
    cin>>n;
    memset(tol,-1,sizeof(tol));
    memset(tor,-1,sizeof(tor));
    int y;
    cin>>y;
    w[0]=y;
    for( int i=2;i<=n;i++){
        int x;
        cin>>x;
        insert(0,x);
    }
    dfs(0,0);
    return 0;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值