typedef struct Song {
int key;
char title[50];
char plays[20];
char artist[50];
char duration[5];
struct Song *next;
} Song;
typedef struct {
Song *front;
Song *rear;
} Queue;
typedef struct {
char username[24];
char password[24];
} Profile;
typedef struct Node {
int key;
struct Node *left;
struct Node *right;
} Node;
typedef struct {
Node *root;
int size;
} BinaryHeap;
Node* createNode(int key) {
Node* node = (Node*)malloc(sizeof(Node));
node->key = key;
node->left = node->right = NULL;
return node;
}
// Function to insert an element into the binary heap
void insert(BinaryHeap* heap, int key) {
Node* node = createNode(key);
if (heap->root == NULL) {
heap->root = node;
} else {
Node* current = heap->root;
Node* parent = NULL;
while (current!= NULL) {
parent = current;
if (key < current->key) {
current = current->left;
} else {
current = current->right;
}
}
if (key < parent->key) {
parent->left = node;
} else {
parent->right = node;
}
}
heap->size++;
}
void heapify(BinaryHeap* heap) {
Node* current = heap->root;
while (current!= NULL) {
Node* left = current->left;
Node* right = current->right;
Node* largest = current;
if (left!= NULL && left->key > largest->key) {
largest = left;
}
if (right!= NULL && right->key > largest->key) {
largest = right;
}
if (largest!= current) {
int temp = current->key;
current->key = largest->key;
largest->key = temp;
current = largest;
} else {
break;
}
}
}
// Function to convert a binary search tree to a binary heap
void bstToHeap(Node* bst, BinaryHeap* heap) {
if (bst == NULL) return;
insert(heap, bst->key);
bstToHeap(bst->left, heap);
bstToHeap(bst->right, heap);
}
void deleteElement(BinaryHeap* heap, Song* song) {
Node* current = heap->root;
Node* parent = NULL;
while (current!= NULL) {
if (current->key == song->key) {
break;
}
parent = current;
if (song->key < current->key) {
current = current->left;
} else {
current = current->right;
}
}
if (current == NULL) return;
if (parent == NULL) {
heap->root = NULL;
} else if (parent->left == current) {
parent->left = NULL;
} else {
parent->right = NULL;
}
free(current);
heap->size--;
heapify(heap);
}
void read_Favorites(Queue* queue) {
FILE *fpFav = fopen("favorites.txt", "r");
if (!fpFav) {
printf("Error! Unable to open the filen");
return;
}
BinaryHeap heap;
heap.root = NULL;
heap.size = 0;
Song songs[100];
int i = 0;
char title[50];
char plays[20];
char artist[50];
char duration[5];
while (fscanf(fpFav, "%[^#]#%[^#]#%[^#]#%[^n]n", title, plays, artist, duration) == 4) {
Song* song = &songs[i];
strcpy(song->title, title);
strcpy(song->plays, plays);
strcpy(song->artist, artist);
strcpy(song->duration, duration);
i++;
}
heap.root = &songs[0];
heap.size = i;
heapify(&heap);
fclose(fpFav);
}
Here is some error that i can’t understand
‘heap’ undeclared (first use in this function); did you mean ‘heapify’? gcc [Ln 777, Col 55]
A assignment to ‘Node ” (aka ‘struct Node *”) from incompatible pointer type ‘Song * (aka ‘struct Song **}
▲ assignment to ‘struct Node *” from incompatible pointer type ‘Song *’ (aka ‘struct Song *”} [-Wincompatible-pointer-types]
A assignment to ‘struct Node *” from incompatible pointer type ‘Song *” (aka ‘struct Song *} [-Wincompatible-pointer-types]
this heap tree is complicated if i’m not wrong
Jonathan C G is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.