i am building a max heap with class item which has (name ,price ,category) one time by comparing the name and one time by comparing the price but there is a problem with the name comparing
here is the class item
class Item
{
int Price;
public:
Item(string name, string category, int price);
string itemName;
string Category;
string get_category();
// operators to compare between prices
bool operator<(Item &another);
bool operator>(Item &another);
bool operator<=(Item &another);
bool operator>=(Item &another);
// operators to compare between names
bool operator>(string &another);
bool operator<(string &another);
bool operator<=(string &another_name);
bool operator>=(string &another_name);
void print();
bool operator==(Item &another);
};
and i have two functions, one to make the max heap by comparing prices which works fine
void Heap::Max_heapify(int size, int i)
{
int left = 2*i+1;
int right = 2*i+2;
int Max = i;
if (heap[Max] < heap[left] and left < size)
{
Max = left;
}
if (heap[Max] < heap[right] and right < size)
{
Max = right;
}
if (Max != i)
{
swap(heap[i], heap[Max]);
Max_heapify(size, Max);
}
void Heap::Build_Max()
{
for (int i = (heap.size() / 2 - 1); i >= 0; i--)
{
Max_heapify(heap.size(), i);
}
}
and another one to compare by names
void Heap::Max_heapify_By_Name(int size, int i)
{
int left = 2*i+1;
int right = 2*i+2;
int Max = i;
if ((heap[Max] < heap[left].itemName) and left <= size)
{
Max = left;
}
if ((heap[Max] < heap[right].itemName) and right <= size)
{
Max = right;
}
if (Max != i)
{
swap(heap[i], heap[Max]);
Max_heapify_By_Name(size, Max);
}
}
void Heap::Build_Max_By_Name()
{
int n = heap.size();
for (int i = floor(n / 2) - 1; i >= 0; i--)
{
cout << i << endl;
Max_heapify_By_Name(n, i);
}
}
here is the class heap
class Heap
{
vector<Item> heap;
public:
Heap(vector<Item>);
void insert_item(Item item);
void Delete_item(int index);
int Get_parent(int num);
int Get_Left_Chlid(int num);
int Get_Right_Child(int num);
void Insert_Item(Item item);
void Heap_sort();
void Heap_sort_Name();
void Max_heapify(int size ,int i );
void Min_heapify(int i);
void Build_Max();
void Build_Min();
void Max_heapify_By_Name(int size,int i);
void Min_heapify_By_Name(int i);
void Build_Max_By_Name();
void Build_Min_By_Name();
void print_heap();
};
the problem is that when i try to call the build_Max_by_name i just don’t get any output
here is the main.cpp
int main()
{
Item i1("item1", "food", 123);
Item i2("item5", "food", 120);
Item i3("item4", "food", 156);
Item i4("item6", "food", 1234);
Item i5("item3", "food", 122);
Item i6("item2", "food", 111);
Item i7("item7", "food", 161);
vector<Item> arr;
//
arr.push_back(i1);
arr.push_back(i2);
arr.push_back(i3);
arr.push_back(i4);
arr.push_back(i5);
arr.push_back(i6);
arr.push_back(i7);
//
Heap a(arr);
//
a.Build_Max_By_Name();
a.print_heap();
}
i thought that the problem was the overloaded operator but when i checked it was working fine
and here is its implementation
bool Item::operator<(Item &another)
{
// to compare prices
return (this->Price < another.Price);
}
bool Item::operator<(string &another_name)
{
return (this->itemName < another_name);
}
the max heap with price compare is working fine but the name one just doesn’t give any output i’d appreciate the help.
2