-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapBottomUp.cpp
122 lines (103 loc) · 2.98 KB
/
HeapBottomUp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Bottom-Up Heap Construction
// heapify procedure calls itself recursively to build heap in top down manner.
//
// Created by @altanai on 31/03/21.
//
#include <iostream>
using namespace std;
//Constructs a heap from elements of a given array by the bottom-up algorithm
//Input: An array H [1..n] of orderable items
//Output: A heap H [1..n]
// void heapbottomup(int arr[] , int n){
// for (int i =0; i<=n/2; i++){
// int k = i;
// int v = arr[k];
// bool heap = false;
// while( !heap && 2*k <= n ){
// int j = 2*k;
// if(arr[j]< arr[j+1]) j++;
// if(v >= arr[j]) {
// heap = true;
// }else {
// arr[k] = arr[j];
// k = j;
// }
// }
// arr[k]=v;
// }
// }
void printarray(int A[], int size){
for (int i = 0; i < size; i++)
cout << A[i] << " ";
cout<< endl;
}
// approach 1 : bottom-up order.
// Heapify procedure applied to a node only if its children nodes are heapified
// 2(n − log 2 (n + 1))
void heapify(int arr[], int n, int i){
int largest = i;
int l = 2*i +1 ;
int r = 2*i +2 ;
if( l<n && arr[l]>arr[largest]) largest=l;
if( r<n && arr[r]>arr[largest]) largest=r;
if(largest !=i ) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapbottomup(int arr[] , int n){
int nonleftindex = n/2 -1;
for(int i = nonleftindex; i>=0; i--){
heapify(arr, n,i);
printarray(arr, n);
}
}
// approach 2 : Top Down heap order
// constructs a heap by successive insertions of a new key into a previously constructed heap
// height of a heap with n nodes is about log2 n, the time efficiency of insertion is in O(log n).
void heaptopdown(int arr[], int n){
}
// ----------------------- utility function
struct node{
int data;
struct node* left;
struct node* right;
node(int value){
data=value;
left=NULL;
right=NULL;
}
};
#include</home/altanai/Documents/algorithms/algorithmscpp/binarytree_algorithms/printBTvertical.h>
node* insertarr_bt(int arr[], node* root, int i, int n){
if(i<n){
root = new node(arr[i]);
root->left = insertarr_bt(arr, root->left, 2*i+1, n);
root->right = insertarr_bt(arr, root->right, 2*i+2, n);
}
return root;
}
int main(){
int arr[] = {12,15,19,10,8,16,5}; //{2,9,7,6,5,8}; //{3,5,1,7,2,8};
int arr_size = sizeof(arr)/sizeof(arr[0]);
heapbottomup(arr, arr_size);
printarray(arr, arr_size);
int n = sizeof(arr)/sizeof(arr[0]);
struct node* root = insertarr_bt(arr, root, 0, n);
printBT(root);
return 0;
}
// g++ HeapBottomUp.cpp -o HeapBottomUp.out
// ./HeapBottomUp.out
// Outpupt
// 12 15 19 10 8 16 5
// 12 15 19 10 8 16 5
// 19 15 16 10 8 12 5
// 19 15 16 10 8 12 5
// └──19
// ├──15
// │ ├──10
// │ └──8
// └──16
// ├──12
// └──5