c++堆调整

代码实现了一个堆数据结构的调整函数heap_ajust,通过对节点的权值与父节点或孩子节点进行比较和交换,保持堆的性质。同时,类tree定义了节点的左孩子、右孩子和父节点的关系。此外,还有交换两个数的函数swap和找到最大值的函数max。
摘要由CSDN通过智能技术生成
#include<iostream>
using namespace std;
class tree{//最重要的!!!
	public://表示他是公开的,在类外也能用
		int l_kid=data*2/*某节点左孩子的数据域 = 该节点数据域 x 2*/,r_kid=data*2+1/*右孩子数据域 = 左孩子 + 1*/;
		int father=(data-(data%2))/2;//父节点数据域 = ( 该节点数据域 - 该节点数据域 % 2 ) / 2
		int data,n;//数据域与值
};
void swap(int a/*酱油*/,int b/*醋*/){//交换二数(重点:酱油、醋、瓶子)
	//我们要把酱油和醋交换位置
	int c;//>空<瓶子
	c=a;//将酱油倒进空瓶子里
	a=b;//将醋倒进酱油瓶子里
	b=c;//将装满酱油的《空》瓶子里的酱油倒进醋瓶里
	return;
}
int max(int a,int b){//返回最大
	if(a>=b){return a;}
	if(a<b){return b;}
}
int heap_ajust(tree heap[],int data,int n){//堆调整,返回位置
	heap[data].n=n;
	heap[data].data=data;
	//初始化
	for(;;){
		if(heap[data].n>heap[heap[data].father].n){//如果该数权值大于其父
			swap(heap[data].n,heap[heap[data].father].n);//交换
			data=heap[data].father;//数据域改变
		}
		else if((heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){//如果该数权值小于其所有孩子
			swap(heap[data].n,max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n));//与最大的孩子交换
			data=max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n);//数据域改变
		}
		else if(heap[data].n<heap[heap[data].l_kid].n){
			swap(heap[data].n,heap[heap[data].l_kid].n);
			data=heap[data].l_kid;
		}
		else if(heap[data].n<heap[heap[data].r_kid].n){
			swap(heap[data].n,heap[heap[data].r_kid].n);
			data=heap[data].r_kid;
		}
		else if((heap[data].n<heap[heap[data].father].n)&&(heap[data].n>heap[heap[data].l_kid].n)&&(heap[data].n>heap[heap[data].r_kid].n)){//如果都不是,打破;这一步可以写到for(;jntm;)jntm处
			break;
		}
	}
	return data;
}
int main(){
	
}

无注释:

#include<iostream>
using namespace std;
class tree{
	public:
		int l_kid=data*2,r_kid=data*2+1;
		int father=(data-(data%2))/2;
		int data,n;
};
void swap(int a,int b){
	int c;
	c=a;
	a=b;
	b=a;
}
int max(int a,int b){
	if(a>=b){return a;}
	if(a<b){return b;}
}
int heap_ajust(tree heap[],int data,int n){
	heap[data].n=n;
	heap[data].data=data;
	for(;;){
		if(heap[data].n>heap[heap[data].father].n){
			swap(heap[data].n,heap[heap[data].father].n);
			data=heap[data].father;
		}
		else if((heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){
			swap(heap[data].n,max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n));
			data=max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n);
		}
		else if(heap[data].n<heap[heap[data].l_kid].n){
			swap(heap[data].n,heap[heap[data].l_kid].n);
			data=heap[data].l_kid;
		}
		else if(heap[data].n<heap[heap[data].r_kid].n){
			swap(heap[data].n,heap[heap[data].r_kid].n);
			data=heap[data].r_kid;
		}
		else if((heap[data].n>heap[heap[data].father].n)&&(heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){
			break;
		}
	}
	return data;
}

@没登录的:

注释:

#include<iostream>
using namespace std;
class tree{//最重要的!!!
    public://表示他是公开的,在类外也能用
        int l_kid=data*2/*某节点左孩子的数据域 = 该节点数据域 x 2*/,r_kid=data*2+1/*右孩子数据域 = 左孩子 + 1*/;
        int father=(data-(data%2))/2;//父节点数据域 = ( 该节点数据域 - 该节点数据域 % 2 ) / 2
        int data,n;//数据域与值
};
void swap(int a/*酱油*/,int b/*醋*/){//交换二数(重点:酱油、醋、瓶子)
    //我们要把酱油和醋交换位置
    int c;//>空<瓶子
    c=a;//将酱油倒进空瓶子里
    a=b;//将醋倒进酱油瓶子里
    b=c;//将装满酱油的《空》瓶子里的酱油倒进醋瓶里
    return;
}
int max(int a,int b){//返回最大
    if(a>=b){return a;}
    if(a<b){return b;}
}
int heap_ajust(tree heap[],int data,int n){//堆调整,返回位置
    heap[data].n=n;
    heap[data].data=data;
    //初始化
    for(;;){
        if(heap[data].n>heap[heap[data].father].n){//如果该数权值大于其父
            swap(heap[data].n,heap[heap[data].father].n);//交换
            data=heap[data].father;//数据域改变
        }
        else if((heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){//如果该数权值小于其所有孩子
            swap(heap[data].n,max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n));//与最大的孩子交换
            data=max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n);//数据域改变
        }
        else if(heap[data].n<heap[heap[data].l_kid].n){
            swap(heap[data].n,heap[heap[data].l_kid].n);
            data=heap[data].l_kid;
        }
        else if(heap[data].n<heap[heap[data].r_kid].n){
            swap(heap[data].n,heap[heap[data].r_kid].n);
            data=heap[data].r_kid;
        }
        else if((heap[data].n<heap[heap[data].father].n)&&(heap[data].n>heap[heap[data].l_kid].n)&&(heap[data].n>heap[heap[data].r_kid].n)){//如果都不是,打破;这一步可以写到for(;jntm;)jntm处
            break;
        }
    }
    return data;
}
int main(){
    
}

无注释:

#include<iostream>
using namespace std;
class tree{
    public:
        int l_kid=data*2,r_kid=data*2+1;
        int father=(data-(data%2))/2;
        int data,n;
};
void swap(int a,int b){
    int c;
    c=a;
    a=b;
    b=a;
}
int max(int a,int b){
    if(a>=b){return a;}
    if(a<b){return b;}
}
int heap_ajust(tree heap[],int data,int n){
    heap[data].n=n;
    heap[data].data=data;
    for(;;){
        if(heap[data].n>heap[heap[data].father].n){
            swap(heap[data].n,heap[heap[data].father].n);
            data=heap[data].father;
        }
        else if((heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){
            swap(heap[data].n,max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n));
            data=max(heap[heap[data].l_kid].n,heap[heap[data].r_kid].n);
        }
        else if(heap[data].n<heap[heap[data].l_kid].n){
            swap(heap[data].n,heap[heap[data].l_kid].n);
            data=heap[data].l_kid;
        }
        else if(heap[data].n<heap[heap[data].r_kid].n){
            swap(heap[data].n,heap[heap[data].r_kid].n);
            data=heap[data].r_kid;
        }
        else if((heap[data].n>heap[heap[data].father].n)&&(heap[data].n<heap[heap[data].l_kid].n)&&(heap[data].n<heap[heap[data].r_kid].n)){
            break;
        }
    }
    return data;
}

给个赞,好吗?

没登录的登录之后再点赞,好吗?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值