Advertisement
darkjessy94

max_diff_heap

Nov 28th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int Left(int i){
  5. return 2*i+1;
  6. }
  7. int Right(int i){
  8. return 2*i+2;
  9. }
  10. int MaxDifHeap(vector <int> H,int index){
  11. int max=-1,n=H.size();
  12. if(index<n){
  13.    int lChild=Left(index);
  14.    if(lChild<n){
  15.       int Lmax=H[index]-H[lChild];
  16.    if(Lmax>max)
  17.       max=Lmax;
  18.    int MaxSottalberoLeft=MaxDifHeap(H,lChild);
  19.    if(max<MaxSottalberoLeft)
  20.       max=MaxSottalberoLeft;
  21.    }
  22.    int rChild=Right(index);
  23.    if(rChild<n){
  24.       int Rmax=H[index]-H[rChild];
  25.    if(Rmax>max)
  26.       max=Rmax;
  27.    int MaxSottalberoRight=MaxDifHeap(H,rChild);
  28.    if(max<MaxSottalberoRight)
  29.      max=MaxSottalberoRight;
  30.    }
  31. }
  32. return max;
  33. }
  34. /*int MaxNodoDifHeap(vector <int> H,int index,int &maxIndex){
  35. int max=-1,n=H.size();
  36. if(index<n){
  37.    int lChild=Left(index);
  38.    if(lChild<n){
  39.       int Lmax=H[index]-H[lChild];
  40.    if(Lmax>max){
  41.       max=Lmax;
  42.       maxIndex=index;
  43.    }
  44.    int MaxSottalberoLeft=MaxNodoDifHeap(H,lChild,maxIndex);
  45.    if(max<MaxSottalberoLeft)
  46.       max=MaxSottalberoLeft;
  47.    }
  48.    int rChild=Right(index);
  49.    if(rChild<n){
  50.       int Rmax=H[index]-H[rChild];
  51.    if(Rmax>max){
  52.       max=Rmax;
  53.       maxIndex=index;
  54.    }
  55.    int MaxSottalberoRight=MaxNodoDifHeap(H,rChild,maxIndex);
  56.    if(max<MaxSottalberoRight)
  57.      max=MaxSottalberoRight;
  58.    }
  59. }
  60. return max;
  61. }*/
  62. int MaxNodoDifHeap(vector <int> H,int index,int &max){
  63. int maxIndex=-1,n=H.size();
  64. if(index<n){
  65.    int lChild=Left(index);
  66.    if(lChild<n){
  67.       int LmaxDiff=H[index]-H[lChild];
  68.    if(LmaxDiff>max){
  69.       max=LmaxDiff;
  70.       maxIndex=index;
  71.    }
  72.    int MaxSottalberoLeft=-1,MaxLIndex;
  73.    MaxLIndex=MaxNodoDifHeap(H,lChild,MaxSottalberoLeft);
  74.    if(max<MaxSottalberoLeft){
  75.       max=MaxSottalberoLeft;
  76.       maxIndex=MaxLIndex;
  77.    }
  78.    }
  79.    int rChild=Right(index);
  80.    if(rChild<n){
  81.       int RmaxDiff=H[index]-H[rChild];
  82.    if(RmaxDiff>max){
  83.       max=RmaxDiff;
  84.       maxIndex=index;
  85.    }
  86.    int MaxSottalberoRight=-1,MaxRIndex;
  87.    MaxRIndex=MaxNodoDifHeap(H,rChild,MaxSottalberoRight);
  88.    if(max<MaxSottalberoRight){
  89.      max=MaxSottalberoRight;
  90.      maxIndex=MaxRIndex;
  91.    }
  92. }
  93. }
  94. return maxIndex;
  95. }
  96. int main(){
  97. vector <int> H{10,9,8,4,6,5,1};
  98. ///int maxNodo=-1;
  99. int maxDif=-1;
  100. cout<<"Differenza massima: "<<MaxDifHeap(H,0)<<endl;
  101. cout<<"Massima differenza fra nodo e figlio si verifica all'indice: "<<MaxNodoDifHeap(H,0,maxDif)<<endl;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement