diff --git a/dyn_huffman.py b/dyn_huffman.py index 8e696334a50bcef2aafee26ba8e66a5394af5074..eb04d1ea08a51543bc6334c18e27ad48c1d1aef8 100644 --- a/dyn_huffman.py +++ b/dyn_huffman.py @@ -13,21 +13,8 @@ class Node: right: Node | None = None -def char_in_tree(tree: Node, char: str) -> Node | None: - if tree.left != None: - char_in_tree(tree.left, char) - elif tree.right != None: - char_in_tree(tree.right, char) - - if tree.value == char: - return tree - else: - return None - - def swap(tree: Node): if tree.left and tree.right: - # print("weeeeeeeeeeeeeeesh") if tree.left.weight > tree.right.weight: print(f'({tree.left.weight}, {tree.right.weight})') tmp = tree.right @@ -35,13 +22,19 @@ def swap(tree: Node): tree.left = tmp def insert_char(tree: Node, char: str): + if tree.left: + if tree.left.value == char: + tree.left.weight += 1 + tree.weight += 1 + return + # Leaf case if tree.left == None and tree.right == None: tree.left = Node() tree.right = Node(value=char, weight=1) tree.weight += 1 return - + if tree.left: if tree.left.value == 'NYT': insert_char(tree.left, char) @@ -49,10 +42,10 @@ def insert_char(tree: Node, char: str): if tree.right: insert_char(tree.right, char) - tree.weight += 1 swap(tree) + def compute_vitter_params(m: int) -> (int, int): e = math.floor(np.log2(m)) r = m - (2 ** e)