Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3.x Classes Binary Tree I am having trouble with my coding and after asking this question a few times, I don't think a lot

Python 3.x Classes Binary Tree

I am having trouble with my coding and after asking this question a few times, I don't think a lot of people know what a huffman tree is so I'll describe it below with pictures I do have the code for the Huffman tree, I'll have the question below that:

Huffman Tree:

A Huffman Tree is a binary tree node, with 4 fields:

char: A single character

freq: The number of times the char occurred.

left: A reference to another HuffmanTree.

right: A reference to another HuffmanTree.

To build a Huffman Tree:

1. Create a leafnode, for every character, with its frequency

image text in transcribed

2. Repeat until there is one huffman tree

> Pick the two lowest frequency trees

image text in transcribed

> Combine these as children to new node adding frequencies

image text in transcribed

Here is a completed Huffman Tree:

image text in transcribed

Here is the Question:

image text in transcribed

HuffmanHeap.py Needs to be completed:

# Defines a data structure that allows Huffman codes to # be computed efficiently. # # A Huffman Heap is a queue to store HuffmanTree objects. # The word "heap" implies that the dequeue operation will always # remove and return the HuffmanTree object with the lowest frequency. class HuffmanHeap(object): def __init__(self, leafs): """  Purpose:  Creates a new HuffmanHeap object.  Pre-conditions:  leafs: a list of HuffmanTree leaf nodes.  Post-conditions:  The heap is created and initialized.  Return:  None  """  pass def enqueue(self, tree): """  Purpose:  Adds the tree to the Heap.  This is an O(1) operation.  Pre-conditions:  tree is a HuffmanTree object  Post-conditions:  the heap increases in size by 1  Return:  None  """  return None def dequeue(self): """  Purpose:  Return the smallest value in the queue.  This is an O(1) operation.  Pre-conditions:  None  Post-conditions:  The heap decreases in size by 1  Return:  A HuffmanTree object, with the lowest frequency  """  return None def __len__(self): """  Purpose:  Return the number of data values stored in the heap.  This method allows Python scripts to call len(hh)  if hh is a HuffmanHeap object.  Pre-conditions:  None  Post-conditions:  None  Return:  The number of values stored in the heap  """  return 0 def display(self): """  Purpose:  Display the data for debugging.  Pre-conditions:  None  Post-conditions:  None  Return:  None  """  print('Add code to HuffmanHeap.display() to help you debug.') HuffmanTree.py:
# Defines the Huffman Tree data structure # # A Huffman tree-node has a character and a frequency. # some strings to avoid long lines later _INIT_ASSERT_MESSAGE_NONLEAF = 'Invalid Huffman tree construction attempted.' _GETCH_ASSERT_MESSAGE_NONLEAF = 'Method get_char() called on non-leaf node' class HuffmanTree(object): def __init__(self, freq=0, char=None, left=None, right=None): """  Purpose:  Initializes the HuffmanTree object.  To create a leaf node  aleaf = HuffmanTree(freq=10, char='A')  bleaf = HuffmanTree(freq=15, char='E')  To create an internal node:  node = HuffmanTree(left=aleaf,right=bleaf)   Pre-conditions:  :param freq: a positive integer  :param char: a character  :param left: another Huffman Tree  :param right: another HuffmanTree  """  self.__char = char self.left = left self.right = right if left is None and right is None: # leaf node: assign the frequency as given self.__freq = freq else: assert left is not None and right is not None, _INIT_ASSERT_MESSAGE_NONLEAF # non-leaf node: calculate the frequency from the given subtrees self.__freq = left.__freq + right.__freq def is_leaf(self): """  Purpose:  Check if the node is a leaf.  Simplifies some of the other methods with an abstraction.  Return:  True if the node is a leaf node  """  return self.left is None and self.right is None def get_freq(self): """  Purpose:  Return the frequency data stored in the node.  Return:  :return: the frequency  """  return self.__freq def get_char(self): """  Purpose:  Return the character stored at a leaf node.  Return:  :return: the character at a leaf node  """  assert self.is_leaf(), _GETCH_ASSERT_MESSAGE_NONLEAF return self.__char def display(self, level=0): """  Purpose:  For debugging, display the tree.  The structure of the tree is represented by indentation  No other real purpose.  Preconditions:  :param level: indentation amount for subtrees.  Return  :return: None  """  if self.is_leaf(): print(' '*level+'Leaf:', self.__char, self.__freq) else: print(' '*level+'Node:', self.__freq, 'Children:') self.left.display(level+1) self.right.display(level+1) def build_codec(self): """  Purpose:  Build a dictionary of char-code pairs from the Huffman tree.  Return:  :return: a dictionary with character as key, code as value  """  codes = {} def encoder(tree, code): if tree.is_leaf(): codes[tree.get_char()] = code else: encoder(tree.left, code+'0') encoder(tree.right, code+'1') if self.is_leaf(): codes[self.__char] = '0' else: encoder(self,'') return codes def __lt__(self, other): return self.__freq   data A freq 10 left right data freq|3 left right data D freq|4 left right data E freq |15 left right data G freq | 2 leii rigi ii igi dataI freq 6 left right left right  data A freq 10 left right data freq|3 left right data D freq|4 left right data E freq |15 left right data G freq | 2 leii rigi ii igi dataI freq 6 left right left right

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 Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students also viewed these Databases questions

Question

Does it use a maximum of two typefaces or fonts?

Answered: 1 week ago