-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleftistheap.c
More file actions
177 lines (148 loc) · 3.22 KB
/
leftistheap.c
File metadata and controls
177 lines (148 loc) · 3.22 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Node
{
int element, npl;
struct Node *left;
struct Node *right;
};
struct LeftHeap
{
struct Node *head;
};
struct Node *createNode(int element)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
newNode->element = element;
newNode->left = NULL;
newNode->right = NULL;
newNode->npl = 0;
return newNode;
}
struct LeftHeap *createLeftHeap()
{
struct LeftHeap *heap = (struct LeftHeap *)malloc(sizeof(struct LeftHeap));
if (heap == NULL)
{
printf("Memory allocation failed\n");
exit(1);
}
heap->head = NULL;
return heap;
}
bool isEmpty(struct LeftHeap *heap)
{
return heap->head == NULL;
}
void clear(struct LeftHeap *heap)
{
heap->head = NULL;
}
struct Node *merge(struct Node *a, struct Node *b)
{
if (a == NULL)
return b;
if (b == NULL)
return a;
if (a->element > b->element)
{
struct Node *temp = a;
a = b;
b = temp;
}
a->right = merge(a->right, b);
if (a->left == NULL)
{
a->left = a->right;
a->right = NULL;
}
else
{
if (a->left->npl < a->right->npl)
{
struct Node *temp = a->left;
a->left = a->right;
a->right = temp;
}
a->npl = a->right->npl + 1;
}
return a;
}
void mergeHeaps(struct LeftHeap *heap1, struct LeftHeap *heap2)
{
if (heap1 == heap2)
return;
heap1->head = merge(heap1->head, heap2->head);
heap2->head = NULL;
}
void insert(struct LeftHeap *heap, int element)
{
heap->head = merge(createNode(element), heap->head);
}
int deleteMin(struct LeftHeap *heap)
{
if (isEmpty(heap))
return -1;
int min = heap->head->element;
heap->head = merge(heap->head->left, heap->head->right);
return min;
}
void inorder(struct Node *root)
{
if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->element);
inorder(root->right);
}
int main()
{
printf("LEFTIST HEAP\n");
struct LeftHeap *h = createLeftHeap();
bool askForAnother = true;
char ch;
while (askForAnother)
{
printf("Functions to do:\n1.Insert\n2.Delete minimum\n3.Checkn Empty\n4.Clear\n5.Exit\nEnter your choice:");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter integer element to insert:");
int element;
scanf("%d", &element);
insert(h, element);
break;
case 2:
deleteMin(h);
break;
case 3:
printf("Empty status = %s\n", isEmpty(h) ? "true" : "false");
break;
case 4:
clear(h);
break;
case 5:
askForAnother = false;
break;
default:
printf("Wrong Entry\n");
break;
}
if (choice != 5)
{
printf("Inorder Traversal:");
inorder(h->head);
printf("\n");
}
}
free(h);
return 0;
}