Skip to content
Snippets Groups Projects
Verified Commit dbfdf7df authored by iliya.saroukha's avatar iliya.saroukha :first_quarter_moon:
Browse files

wip: find_char

parent c701791c
No related branches found
No related tags found
No related merge requests found
from __future__ import annotations
import code
from dataclasses import dataclass
import numpy as np
import pprint
......@@ -12,6 +13,7 @@ DICT_ALPHABET = {item: index + 1 for index, item in enumerate(LIST_ALPHABET)}
@dataclass
class Node:
weight: int = 0
nyt_code: str | None = None
value: str | None = "NYT"
left: Node | None = None
right: Node | None = None
......@@ -25,7 +27,24 @@ def swap(tree: Node):
tree.right = tree.left
tree.left = tmp
def insert_char(tree: Node, char: str):
def find_char(tree: Node, char: str) -> Node | None:
if tree:
if tree.right:
_ = find_char(tree.right, char)
if tree.left:
_ = find_char(tree.left, char)
if tree.value == char:
print(tree)
return tree
else:
return None
def insert_char(tree: Node, char: str, nyt_code = ''):
if tree.left:
if tree.left.value == char:
tree.left.weight += 1
......@@ -35,16 +54,16 @@ def insert_char(tree: Node, char: str):
# Leaf case
if tree.left == None and tree.right == None:
tree.left = Node()
tree.right = Node(value=char, weight=1)
tree.right = Node(value=char, weight=1, nyt_code=nyt_code)
tree.weight += 1
return
if tree.left:
if tree.left.value == 'NYT':
insert_char(tree.left, char)
insert_char(tree.left, char, nyt_code + '0')
else:
if tree.right:
insert_char(tree.right, char)
insert_char(tree.right, char, nyt_code + '1')
tree.weight += 1
swap(tree)
......@@ -75,16 +94,23 @@ def compute_code(char: str):
def main(string: str) -> None:
tree = Node()
enc = []
fixed_code = []
for c in string:
fixed_code.append(compute_code(c))
pprint.pp(fixed_code)
for c in string:
pprint.pp(tree)
print(f'\n====== Inserting \'{c}\' ======\n')
insert_char(tree, c)
enc.append(compute_code(c))
pprint.pp(tree)
pprint.pp(enc)
n = find_char(tree, 'k')
pprint.pp(n)
if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment