Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Heap Implementation is not correctly outputting the 'inorder traversal'. I have created a heap that takes input w/a menu. When I call inorder, the

Python Heap Implementation is not correctly outputting the 'inorder traversal'. I have created a heap that takes input w/a menu. When I call inorder, the heap contains the previous element that was deleted with the makenull call. I can't find the bug that is causing this element to persist. Everything seems to be fine other than that one issue. I have attached the heap.py class a test file and my output. Do help find the solution to the issue presented.

heap.py

import math class heap: def __init__(self): self.data = [] def __str__(self): res="" for x in self.data: res=res+str(x)+" " return res def makenull(self): self.data = [] def is_null(self): return len(self.data) == 0 def insert(self,x): self.data.append(x) self.upheap(len(self.data)-1) def parent(self,index): return math.floor((index-1)/2) def left(self,index): return int((index + 1) * 2 - 1) def right(self,index): return int((index + 1) * 2) def swap(self,a,b): self.data[a], self.data[b] = self.data[b], self.data[a] return def upheap(self,index): p_i = self.parent(index) if p_i

def preorder(self,index, tree=[]): if self.in_bounds(index): tree.append(str(self.data[index]) + " ") self.preorder(self.left(index), tree) self.preorder(self.right(index), tree) return ''.join(tree)

def postorder(self,index, tree=[]): if self.in_bounds(index): self.postorder(self.left(index), tree) self.postorder(self.right(index), tree) tree.append(str(self.data[index]) + " ") return ''.join(tree)

def min(self): return self.data[0] def last(self): return self.data[-1] def rm_last(self): return self.data.pop(-1) def deletemin(self): if self.is_null(): return False self.swap(0, -1) min = self.rm_last() self.downheap(0) return min def has_child(self): return len(self.data) > 0 def in_bounds(self, index): upper_bound = len(self.data) return 0

test_heap.py

from heap import *

#Read In input and Act on it

def get_input(): command="" try: command=input("> ") except(EOFError): return "exit" return command

def print_help(): print("help - Prints this list") print("makenull - Clears the heap") print("insert - Inserts the number into the heap") print("min - Prints the current min on the heap") print("inorder - Prints heap in inorder") print("preorder - Prints heap in preorder") print("postorder - Prints heap in postorder") print("deletemin - Removes min from the heap") print("sort - Calls deletemin repeatedly to print out sorted numbers") print("exit - Exits the program (also Crtl-D exits)")

def parse_command(command,H): vals = command.split() if len(vals) 2: print("Bad Command - type help for commands") return True if vals[0]=="exit": return False if vals[0]=="makenull": H.makenull() return True if vals[0]=="insert" and len(vals)==2: num = int(vals[1]) H.insert(num) return True if vals[0]=="min": print(str(H.min())) return True if vals[0]=="inorder": print(H.inorder(0)) return True if vals[0]=="preorder": print(H.preorder(0)) return True if vals[0]=="postorder": print(H.postorder(0)) return True if vals[0]=="deletemin": H.deletemin() return True if vals[0]=="sort": H.sort() return True if vals[0]=="help": print_help() return True

print("Bad Command - type help for commands") return True

if __name__=="__main__": go=True; H = heap()

print("Welcome to the Heap") print("The List of Commands is below, type help to see them again.") print_help()

command = get_input() go = parse_command(command,H) while go: command = get_input() go = parse_command(command,H)

output:image text in transcribed

Welcome to the Heap The List of Commands is below, type help to see them again. help - Prints this list makenull - Clears the heap insert integer> - Inserts the number into the heap min - Prints the current min on the heap inorder - Prints heap in inorder preorder - Prints heap in preorder postorder - Prints heap in postorder deletemin - Removes min from the heap sort - Calls deletemin repeatedly to print out sorted numbers exit - Exits the program (also Crtl-D exits) makenull inorder insert 7 inorder makenull preorder insert 95 insert 36 insert 11 insert 92 insert 88 insert 57 insert 53 insert 21 insert 35 insert 86 inorder 7 95 35 88 21 92 86 11 57 36 53

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions