-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempCodeRunnerFile.c
129 lines (118 loc) · 2.67 KB
/
tempCodeRunnerFile.c
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
#include <stdio.h>
#include <stdlib.h>
// Heap :consist of a pointer to the array and variable size which will tell the size of the array
typedef struct Heap
{
int *arr;
int size;
} maxHeap;
// Tree Node : consist of data and two pointers->left,right
typedef struct Tnode
{
int data int maxIndex;
struct Tnode *left;
struct Tnode *right;
} node;
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
// creating Node
node *createNode(int data)
{
node *temp = (node *)malloc(sizeof(node));
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// creating Heap
maxHeap *createHeap(int n)
{
printf("1\n");
maxHeap *temp = (maxHeap *)malloc(sizeof(maxHeap));
printf("2\n");
temp->arr = (int *)malloc(sizeof(int) * n);
printf("3\n");
temp->size = 0;
printf("4\n");
return temp;
}
// Heapify
void heapify(int a[], int size, int i)
{
if (size == 0)
return;
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
maxIndex = max( a[i], left, right)
if(i != maxIndex)
swap(a[i], a[maxIndex])
}
// inserting data in the MaxHeap
void insert(maxHeap **heap, int data)
{
printf("Called\n");
if ((*heap)->size == 0)
{
(*heap)->arr[0] = data;
(*heap)->size++;
printf("Here goes 1\n");
}
else
{
printf("Here goes 2\n");
(*heap)->arr[(*heap)->size++] = data;
for (int i = (*heap)->size / 2 - 1; i >= 0; i--)
{
heapify((*heap)->arr, (*heap)->size, i);
}
}
}
// printing Heap
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
}
// deleting Node
void deleteNode(maxHeap *heap, int data)
{
int i;
for (i = 0; i < heap->size; i++)
{
if (heap->arr[i] == data)
break;
}
// swap the node with the last node of the heap
swap(&(heap->arr[i]), &(heap->arr[heap->size - 1]));
heap->size--;
for (int i = heap->size / 2 - 1; i >= 0; i--)
{
heapify(heap->arr, heap->size, i);
}
}
int main()
{
int n;
printf("Enter the size of Max Heap\n");
scanf("%d ", &n);
printf("hui\n");
maxHeap *heap = createHeap(n);
insert(&heap, 1);
insert(&heap, 2);
insert(&heap, 3);
insert(&heap, 4);
insert(&heap, 5);
insert(&heap, 7);
insert(&heap, 6);
insert(&heap, 8);
printArray(heap->arr, heap->size);
deleteNode(heap, 5);
printArray(heap->arr, heap->size);
return 0;
}