pythonbinary-heap

Can't print binary heap in python


I try to make a binary heap in python but I get trouble printing it. I make sure that the logic in the program is right but when I want to try printing it I get the wrong result. This is what I want for program output:

Input:

6
1 2
1 3
1 7
2
3
2

Output:

7
3

This is my program:

class BinaryHeap:
    def __init__(self):
        self.items = []
 
    def size(self):
        return len(self.items)
 
    def parent(self, i):
        return (i - 1)//2
 
    def left(self, i):
        return 2*i + 1
 
    def right(self, i):
        return 2*i + 2
 
    def get(self, i):
        return self.items[i]
 
    def get_max(self):
        if self.size() == 0:
            return None
        return self.items[0]
 
    def extract_max(self):
        if self.size() == 0:
            return None
        largest = self.get_max()
        self.items[0] = self.items[-1]
        del self.items[-1]
        self.max_heapify(0)
        return largest
 
    def max_heapify(self, i):
        l = self.left(i)
        r = self.right(i)
        if (l <= self.size() - 1 and self.get(l) > self.get(i)):
            largest = l
        else:
            largest = i
        if (r <= self.size() - 1 and self.get(r) > self.get(largest)):
            largest = r
        if (largest != i):
            self.swap(largest, i)
            self.max_heapify(largest)
 
    def swap(self, i, j):
        self.items[i], self.items[j] = self.items[j], self.items[i]
 
    def insert(self, key):
        index = self.size()
        self.items.append(key)
 
        while (index != 0):
            p = self.parent(index)
            if self.get(p) < self.get(index):
                self.swap(p, index)
            index = p
 
bheap = BinaryHeap()
 
n = int(input())
for i in range (n):
    operation= input('What would you like to do? ').split()
    if operation == 1:
        data = int(operation[1])
        bheap.insert(data)
    elif operation == 2:
        print('Maximum value: {}'.format(bheap.get_max()))
    elif operation  == 3:
        print('Maximum value removed: {}'.format(bheap.extract_max()))
    elif operation == 4:
        break

I need your opinion to fixed it.


Solution

  • operation is a list (you called split), but you compare it as an int in your if statements. Also, you should compare it against "1", "2", ... not 1, 2, ...

    So:

        operation = input('What would you like to do? ').split()
        if operation[0] == "1":
            data = int(operation[1])
            bheap.insert(data)
        elif operation[0] == "2":
            print('Maximum value: {}'.format(bheap.get_max()))
        elif operation[0]  == "3":
            print('Maximum value removed: {}'.format(bheap.extract_max()))
        elif operation[0] == "4":
            break
    

    If you just want 7 and 3 in the output, and only after the input has been completely processed, then you should remove those verbose print statement (where you output phrases), and instead collect the output -- for instance in a list:

    output = []
    n = int(input())
    for i in range(n):
        operation = input('What would you like to do? ').split()
        if operation[0] == "1":
            data = int(operation[1])
            bheap.insert(data)
        elif operation[0] == "2":
            output.append(bheap.get_max())
        elif operation[0]  == "3":
            bheap.extract_max()
        elif operation[0] == "4":
            break
    
    print("\n".join(map(str, output)))