Question: / / myAVLtree.hpp #ifndef MY _ AVLTREE _ HPP #define MY _ AVLTREE _ HPP #include #include #include struct node { int data; int height;
myAVLtree.hpp
#ifndef MYAVLTREEHPP
#define MYAVLTREEHPP
#include
#include
#include
struct node
int data;
int height;
node left;
node right;
nodeint data : datadata height leftnullptr rightnullptr
;
extern node root;
int heightnode t;
void insertint x node & t;
void balancenode & t;
void rotateWithLeftChildnode & k;
void rotateWithRightChildnode & k;
void doubleWithLeftChildnode & k;
void doubleWithRightChildnode & k;
void removeint data, node & t;
void treeMedianconst std::vector instructions;
#endif
void treeMedianconst std::vector instructions
I just need implementation for avl treeMedian
void heapMedianconst std::vector instructions
Initialize two heaps: one for the smaller half max heap and one for the larger half min heap
std::priorityqueue, std::less small; Max heap for the smaller half
std::priorityqueue, std::greater large; Min heap for the larger half
Iterate through the instructions
for int instruction : instructions
Check if the instruction is a pop median operation
if instruction
Print and remove the median element from the smaller heap
std::cout small.top;
small.pop;
Balance the heaps if the sizes are not equal
if smallsize large.size
small.pushlargetop;
large.pop;
else Insert operation
Determine where to insert the element based on the current median
if smallempty instruction small.top
small.pushinstruction; Insert into the smaller heap
else
large.pushinstruction; Insert into the larger heap
Balance the heaps if necessary
if smallsize large.size
large.pushsmalltop; Move the top element of the smaller heap to the larger heap
small.pop;
else if largesize small.size
small.pushlargetop; Move the top element of the larger heap to the smaller heap
large.pop;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
