C++ Code ,start by designing a STUDENT structure which contains variables for storing a student’s id number, name and grade. Using the heap tree codes presented below; write a program which reads students’ information from the presented text file below into STUDENT structures and create a priority queue (min heap) according to the students’ grades then display as following:
0 - 1027 Nannie 4
1 - 1018 Leonardo 8 ...
29 - 1010 Taylor 98
txt file:
Heap tree code to implement:
1001 Mollie 21 1002 Marvin 94 1003 Aiden 84 1004 Elena 12 1005 Iona 60 1006 Lillie 44 1007 Philippa 70 1008 Herman 76 1009 Dewey 66 1010 Taylor 98 1011 Jak 38 1012 Eliza 26 1013 Tanya 29 1014 Tabitha 35 1015 Carla 74 1016 Jean 25 1017 Maggie 27 1018 Leonardo 8 1019 Rachel 36 1020 Juan 32 1021 Gerald 92 1022 Nina 43 1023 Verity 41 1024 Barbara 97 1025 Alfred 68 1026 Ruby 56 1027 Nannie 5 1028 Russell 30 1029 George 40 1030 Violet 8
#include<iostream> #include<cmath> using namespace std; #define SIZE 10 #define PARENT (i) ceil(i/2.0)-1 #define LCHILD(i) 2*i+1 #define RCHILD(i) 2*i+2 class MinHeap{ private: int data[SIZE]; int count; public: MinHeap():count (0){} bool isEmpty(){ return count == 0; } bool isFull(){ return count == SIZE; } void insert(int item){ if(isFull()) return; data[count] = item; heapUp (count); count++; } void heapUp (int ind) { if (ind == 0) return; int plndex = PARENT (ind); if(data[pIndex]>data[ind]){ int temp = data[pindex]; data[pIndex] = data[ind]; data[ind] = temp; heapUp (pindex); }
void heapDown (int ind){ int Ichild = LCHILD(ind); int rchild = RCHILD (ind); if (ind == count-1 || LCHILD (ind) >count) return; int minChildind; if (rchild < count){ minChildind = data[lchild]<data[rchild]?lchild:rchild; } else{ minChildlnd = Ichild; } if(data[minChildlnd] <data[ind]){ int temp = data[ind]; data[ind] = data[minChildlnd]; data[minChildind] = temp; heapDown (minChildind); data[0] = data[--count]; heapDown(0); return dt; } int extractMin(){ int dt = data[0]; } }; int main(){ MinHeap h; h.insert(3); h.insert(2); h.insert(15); h.insert(5); h.insert(4); h.insert(45); h.insert(55); h.insert(65); h.insert(75); h.insert(85); h.insert (95); while (!h.isEmpty()){ cout<<h.extractMin()<<" "; }
C++ Code ,start by designing a STUDENT structure which contains variables for storing a student’s id number, name and gr
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
C++ Code ,start by designing a STUDENT structure which contains variables for storing a student’s id number, name and gr
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!