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
#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
*/
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 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 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 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 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?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...
Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children. Don’t worry if the tree is unbalanced. Note that...
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...
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...
C++ There is a premade heap.h file. You will write a program that creates a heap, and then asks the user to enter values which you will put in the heap (at least 8), and then display the heap Then your program will display a menu with the following choices, and perform the appropriate actions Display the heap Add an item to the heap Remove the largest item Exit The program will continue until the user chooses to exit, at...