import'dart:io';classNode{int_data;Node(intthis._data){}intgetdata_key=>_data;setdata_key(intid)=>_data=id;}classHeap{List<Node>heapArray;intmaxSize;intcurrentSize;Heap(intthis.maxSize){currentSize=0;heapArray=newList<Node>(maxSize);}boolisEmpty()=>currentSize==0;boolinsert(intkey){if(currentSize==maxSize){returnfalse;}NodenewNode=newNode(key);heapArray[currentSize]=newNode;trickleUp(currentSize++);returntrue;}voidtrickleUp(intindex){intparent=(index-1)~/2;Nodebottom=heapArray[index];while(index>0&&heapArray[parent].data_key<bottom.data_key){heapArray[index]=heapArray[parent];index=parent;parent=(parent-1)~/2;}heapArray[index]=bottom;}Noderemove(){Noderoot=heapArray[0];heapArray[0]=heapArray[--currentSize];trickleDown(0);returnroot;}voidtrickleDown(intindex){intlargerChild;Nodetop=heapArray[index];while(index<currentSize~/2){intleftChild=2*index+1;intrightChild=leftChild+1;if(rightChild<currentSize&&heapArray[leftChild].data_key<heapArray[rightChild].data_key){largerChild=rightChild;}else{largerChild=leftChild;}if(top.data_key>=heapArray[largerChild].data_key){break;}heapArray[index]=heapArray[largerChild];index=largerChild;}heapArray[index]=top;}boolchange(intindex,intnewValue){if(index<0||index>=currentSize){returnfalse;}intoldValue=heapArray[index].data_key;heapArray[index].data_key=newValue;if(oldValue<newValue){trickleUp(index);}else{trickleDown(index);}returntrue;}voiddisplayHeap(){stdout.write('Heap Array: ');for(inti=0;i<currentSize;i++){if(heapArray[i]!=null){stdout.write('${heapArray[i].data_key} ');}else{stdout.write('--');}}stdout.writeln();intnBlanks=32;intitemsPerRow=1;intcolumn=0;intj=0;Stringdots='............................';stdout.writeln(dots+dots);while(currentSize>0){if(column==0){for(intk=0;k<nBlanks;k++){stdout.write(' ');}}stdout.write(heapArray[j].data_key);if(++j==currentSize){break;}if(++column==itemsPerRow){nBlanks~/=2;itemsPerRow*=2;column=0;stdout.writeln();}else{for(intk=0;k<nBlanks*2-2;k++){stdout.write(' ');}}}stdout.writeln("\n${dots + dots}");}}voidmain(){intvalue,value2;Heapheap=newHeap(31);boolsuccess;heap.insert(70);heap.insert(40);heap.insert(50);heap.insert(20);heap.insert(60);heap.insert(100);heap.insert(80);heap.insert(30);heap.insert(10);heap.insert(90);while(true){stdout.writeln('Enter first letter of show, insert, remove, change: ');Stringchoice=stdin.readLineSync();switch(choice){case's':heap.displayHeap();break;case'i':stdout.write('Enter value to insert: ');value=int.parse(stdin.readLineSync());success=heap.insert(value);if(!success){stdout.writeln("Can't insert; heap full");}break;case'r':if(!heap.isEmpty()){heap.remove();}else{stdout.writeln("Can't remove; heap is empty");}break;case'c':stdout.write('Enter current index of item: ');value=int.parse(stdin.readLineSync());stdout.write('Enter new key: ');value2=int.parse(stdin.readLineSync());success=heap.change(value,value2);if(!success){stdout.writeln('Invalid index');}break;default:stdout.writeln('Invalid entry \n');}}}