Question

Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

Write a C program insertion and deletion functions for a max heap represented as a linked binary tree.

Assume that each node has a parent field as well as the usual left child, right child, and key fields.

-Condition : Do not use Array representation. Use the following structure.

typedef struct node *treePointer;

typedef struct node {

int key;

treePointer parent;

treePointer leftChild, rightChild;

};

- INPUT

i k : Insert the node with the key value of k in the heap.

d : Delete the node with the largest key in the heap.

q : End the program.

- OUTPUT

  Insert k : If i k is operating successfully

Exist number : If i k fails (if it is already a key value that exists)

Delete k : If d worked successfully

The heap is empty : if d fails (if the heap is empty)

- Exmple

input / output

i 4 / Insert 4

i 4 / Exist number

i 5 / Insert 5

d / Delete 5

d / Delete 4

d / The heap is empty

i 3 / Insert 3

q

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<math.h>
using namespace std;
typedef struct node * treePointer;

struct node {
int key;

treePointer parent;

treePointer leftChild, rightChild;
};
treePointer root=nullptr;
int heapSize=0;
treePointer lastParent=nullptr;
treePointer deleteParent=nullptr;
int index=0;
int check(treePointer root, int a)
{
   if(!root)
       return 0;
   if(root->key==a)
       return 1;
   else
   return check(root->leftChild,a)+check(root->rightChild,a);
}

void heapify(treePointer ptr)
{
   if(!ptr)
       return;
   //left is greater
   if(ptr->leftChild)
   {
   if((ptr->leftChild->key)>(ptr->key))
   {
   int i;
       i=ptr->leftChild->key;
       ptr->leftChild->key=ptr->key;
       ptr->key=i;
      
   }
   }
   if(ptr->rightChild){
   if(ptr->rightChild->key>ptr->key)
   {
   int i;
       i=ptr->rightChild->key;
       ptr->rightChild->key=ptr->key;
       ptr->key=i;
      
   }
   }
   heapify(ptr->parent);
}
void last_parent(treePointer pt,int lev)
{
  
   if(!pt || lev==0)
       return ;
   if(lev==1)
   {
       if(pt->leftChild==nullptr || pt->rightChild==nullptr)
       { index++;
       if(index==1)
       {
       lastParent=pt;
       return ;
       }
       return;
       }
   }
   else {
   last_parent(pt->leftChild,lev-1);
   last_parent(pt->rightChild,lev-1);
   }
   return;
}
treePointer last(treePointer temp)
{
   int level=ceil(log10(heapSize+1)/log10(2));
   index=0;
last_parent(temp,level-1);
   last_parent(temp,level);
   return lastParent;
}
bool insert(int a)
{
   if(root==nullptr)
   {
       treePointer temp=new node();
       temp->key=a;
       temp->parent=nullptr;
       temp->leftChild=nullptr;
       temp->rightChild=nullptr;
       root=temp;
       heapSize++;
       return true;
   }
   int n=check(root,a);
   if(n)
   {
       return false;
   }
   treePointer ptr=last(root);
   if(ptr!=nullptr)
   {
   //no left child
   if(ptr->leftChild==nullptr)
   {
       treePointer temp=new node();
       temp->key=a;
       temp->parent=ptr;
       temp->leftChild=nullptr;
       temp->rightChild=nullptr;
       ptr->leftChild=temp;
       heapSize++;
       heapify(ptr);
       return true;
   }
   //no right child
   treePointer temp=new node();
       temp->key=a;
       temp->parent=ptr;
       temp->leftChild=nullptr;
       temp->rightChild=nullptr;
       ptr->rightChild=temp;
       heapSize++;
       heapify(ptr);
       return true;
   }
   return false;
}
void lastDelete(treePointer pt, int level)
{
  
if (pt == nullptr)
return;
if (level == 1)
{
       deleteParent=pt;
       //cout<<pt->key<<endl;
   }
else if (level > 1)
{
lastDelete(pt->leftChild, level-1);
lastDelete(pt->rightChild, level-1);
}
}
void deleteHeapify(treePointer ptr)
{
if(!ptr)
return;
   if(ptr->leftChild)
   {
   if((ptr->leftChild->key)>(ptr->key))
   {
   int i;
       i=ptr->leftChild->key;
       ptr->leftChild->key=ptr->key;
   ptr->key=i;
      
   }
   }
   if(ptr->rightChild){
   if(ptr->rightChild->key>ptr->key)
   {
   int i;
       i=ptr->rightChild->key;
       ptr->rightChild->key=ptr->key;
       ptr->key=i;
      
   }
   }
   heapify(ptr->leftChild);
   heapify(ptr->rightChild);
}
  
int deleteMax()
{
if(!root && heapSize==0)
return -1;
int level=ceil(log10(heapSize+1)/log10(2));
lastDelete(root,level);
treePointer ptr=deleteParent;
if(ptr==root)
{ int i=root->key;
delete root;
heapSize--;
root=nullptr;
return i;
}
if(ptr)
{
       int i;
       i=ptr->key;
       ptr->key=root->key;
       root->key=i;
       i=ptr->key;
       if(ptr->parent->rightChild==ptr)
       ptr->parent->rightChild=nullptr;
       else if(ptr->parent->leftChild==ptr)
       ptr->parent->leftChild=nullptr;
       delete ptr;
       deleteHeapify(root);
       heapSize--;
       return i;
   }
}
int main()
{
   char choice;
   int num;
   int max;
   while(1){
   cin>>choice;
   switch(choice)
   {
   case 'i' : cin>>num;
   if(insert(num))
   cout<<"Insert "<<num<<endl;
   else
   cout<<"Exist number "<<num<<endl;
   // cout<<heapSize<<endl;
   break;
   case 'd' : max=deleteMax();
   if(max<0)
   cout<<"The heap is empty"<<endl;
   else
   cout<<"Delete "<<max<<endl;
   // cout<<heapSize<<endl;
   break;
   case 'q' : deleteMax();
   cout<<root->key<<endl;
   return 0;
   }
   }
}
  
      

/*

Output

Insert 4
Exist number 4
Insert 5
Delete 5
Delete 4
The heap is empty
Insert 3

Input

i 4
i 4
i 5
d
d
d 
i 3
q

*/

Add a comment
Know the answer?
Add Answer to:
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • add/ remove any methods to the program. please post new code and output Min Heap: public...

    add/ remove any methods to the program. please post new code and output Min Heap: public class MinHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MinHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) {...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • 1. Which of the following is a proper array representation a binary min heap?

    1. Which of the following is a proper array representation a binary min heap?2. A heap is implemented using an array. At what index will the right child of node at index i be found? Note, the Oth position of the array is not used.Select one:a. i/2b. 2 i+1c. i-1d. 2 i3. Consider the following array of length 6. Elements from the array are added, in the given order, to a max heap. The heap is initially empty and stored as an array.A={18,5,37,44,27,53}What...

  • SECTION B (4 QUESTIONS, 90 MARKS) Question 4. [32 marks in total] For this question, assume the definition of Java clas...

    SECTION B (4 QUESTIONS, 90 MARKS) Question 4. [32 marks in total] For this question, assume the definition of Java class Heap given in the Appendix. The heaps referred to in this question are all maxHeaps. a. (5 marks) Insert into an empty (binary) heap the values 35, 4, 72, 36, 49, 50. Draw all intermediate steps b. (3 marks) Carry out one deletion operation on the final heap in (a.) above. c. (2 marks) Give the worst-case time complexity...

  • Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C. Then, use reheapup algorithm and print out...

    Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C. Then, use reheapup algorithm and print out the result. Need to use this algorithm. ReheapUp ( int root, int bottom ) // Pre: bottom is the index of the node that may violate the heap // order property. The order property is satisfied from root to // next-to-last node. // Post: Heap order property is restored between root and bottom {     int parent ;     if ( bottom > root...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT