huffman

.py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

3

Uploaded by CommodoreFire8688

Report
""" name: huffman.py author: Launa sigars purpose: we are creating a binary tree with parents and children and leaves\ with that we start to decode it to where the left side is called by typing\ the binary number 1 and the right side is called by typing in the binary\ number 0 there for we no longer have to write out left or right each time\ it just simplizes it for us. class: csc 120 """ class BinaryTree: def __init__(self): '''Initialize tree's value, left, right to None ''' self._value = None self._left = None self._right = None def build_tree(self,pre_order,in_order): '''This my recursive method that takes two lists of integers; the preorder traversal and the inorder traversal of the tree and returns the tree parameter: pre_order: list of int in pre order in_order: list of int in in order ''' if (pre_order == [] or in_order == []): return None else: head = pre_order[0] head_index = in_order.index(head) self._value = head if len(pre_order[1:1+head_index]) > 0: self._left = BinaryTree() self._left.build_tree(pre_order[1:1+head_index],\ in_order[:head_index]) if len(pre_order[head_index+1:]) > 0: self._right = BinaryTree() self._right.build_tree(pre_order[head_index+1:],\ in_order[head_index+1:]) def print_post(self,post): '''This my recursive method print_post that adds the postorder traversal of the tree you build from the first two lines of the data file to post list parameter: post: an empty list post-condition: post: list of integers in postorder ''' if self._value == None: return if self._left != None: self._left.print_post(post) if self._right != None: self._right.print_post(post) post.append(self._value)
def __str__(self): '''Returns a string representation of the tree.''' if self._value == "None": return None return "({:} {} {})".format(self._value, \ str(self._left), str(self._right)) def read_file(info): '''this is my read_file function that takes file info\ and returns preorder inorder and sequence lists parameter: info: information in file in list return values: pre_order: list of integers in preorder in_order: list of integers in inorder sequence: list with string in huffman sequence ''' file_info = [] for line in info: line = line.split() file_info.append(line) pre_order = file_info[0] in_order = file_info[1] sequence = file_info[2] return pre_order,in_order,sequence def decode(Tree,Root,seq): '''this is my decode function that takes decodes the Tree with Huffman Coding and returns a string parameter: Tree: BinaryTree object seq: string of encoded sequence of values Root: BinaryTree object representing the root of \ the BinaryTree ''' if len(seq) > 0: if Tree._left == None and Tree._right == None: return (str(Tree._value) + decode(Root,Root,seq)) elif seq[0] == '0': if Tree._left == None: return decode(Root,Root,seq[1:]) else: return decode(Tree._left,Root,seq[1:]) elif seq[0] == '1': if Tree._right == None: return decode(Root,Root,seq[1:]) else: return decode(Tree._right,Root,seq[1:]) elif Tree._left == None and Tree._right == None: return str(Tree._value) else: return '' def main(): T = BinaryTree() file = input('Input file: ') info = open(file) pre_order,in_order,sequence = read_file(info) T.build_tree(pre_order,in_order) post = [] T.print_post(post) print(' '.join(post))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help