-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex-3-2-heap.cpp
More file actions
131 lines (127 loc) · 3.21 KB
/
ex-3-2-heap.cpp
File metadata and controls
131 lines (127 loc) · 3.21 KB
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
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define PARENT(i) ((i-1) / 2)
using namespace std;
int * createTable(int &);
void createHeap(int * , int );
void insertheap(int *, int &, int );
void displayHeap(int *, int);
void displayTreeHeap(int *, int);
void heapSort(int *, int);
void fixheap(int *, int , int );
int main() {
int N;
int *A;
int random=0;
int choice=0;
while(true) {
cout << "1. Create Table.\n";
cout << "2. Create heap from table.\n";
cout << "3. New item in heap.\n";
cout << "4. Show heap as table.\n";
cout << "5. Show heap as tree.\n";
cout << "6. Sort and show table.\n";
cout << "7. Exit.\n";
cin >> choice;
if(choice==1)
A=createTable(N);
else if(choice==2)
createHeap(A,N);
else if(choice==3) {
int num;
cout << "Insert number to insert in heap: ";
cin >> num;
insertheap(A,N,num);
}
else if(choice==4)
displayHeap(A,N);
else if(choice==5)
displayTreeHeap(A,N);
else if(choice==6)
heapSort(A,N);
else if(choice==7)
break;
}
return 0;
}
int * createTable(int &N) {
N=20;
int * A = new int[N+1];
srand(time(NULL));
for(int i=1; i<=N; i++)
A[i]=(rand()%100)+1;
return A;
}
void createHeap(int * A, int N) {
for(int i=1; i<=N; i++)
while(i>1 && A[i]<A[i/2]){
int temp = A[i];
A[i] = A[i/2];
A[i/2]=temp;
i/=2;
}
}
void insertheap(int * A, int &N, int num){
N++;
int *tmp = A;
A = new int[N+1];
A=tmp;
A[N]=num;
int i=N;
while(i>1 && A[i]<A[i/2]){
int temp = A[i];
A[i] = A[i/2];
A[i/2]=temp;
i/=2;
}
}
void displayHeap(int * A, int N) {
for (int i=1;i<=N;i++)
cout << A[i] << " ";
cout << endl;
}
void displayTreeHeap(int * A, int N) {
int width=N*4;
int position[N-1];
int i, j, k, pos, x=1, level=0;
for(i=0,j=1; i<N; i++,j++) {
pos = position[PARENT(i)] + (i%2?-1:1)*(width/(pow(2,level+1))+1);
for (k=1; k<pos-x; k++) {
if(i==0 || i%2) cout<< " ";
else cout<<"-";
}
cout << A[i+1];
position[i] = x = pos+1;
if (j==pow(2,level)) {
cout << endl;
level++;
x = 1;
j = 0;
}
}
cout << endl;
}
void heapSort(int * A, int N) {
for (int i=N/2-1;i>0;i--)
fixheap(A,N,i);
for (int i=N;i>0;i--) {
swap(A[i], A[1]);
fixheap(A,i,1);
}
displayHeap(A,N);
}
void fixheap(int * A, int N, int i) {
int largest=i;
int left=2*i;
int right=2*i+1;
if(left<N && A[left]>A[largest] && left!=0)
largest=left;
if(right<N && A[right]>A[largest])
largest = right;
if(largest!=i) {
swap(A[i],A[largest]);
fixheap(A,N,largest);
}
}