diff --git a/huffman_py/functions/calculate_code.py b/huffman_py/functions/calculate_code.py
new file mode 100644
index 0000000..3841a3f
--- /dev/null
+++ b/huffman_py/functions/calculate_code.py
@@ -0,0 +1,39 @@
+#
+# Sam Hadow - Huffman-py
+# Copyright (C) 2023
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+#
+def calculate_code(node, value = '', the_codes = None):
+ # Create code Dict if it doesn't exist yet (before recursion)
+ if the_codes == None:
+ the_codes = dict()
+
+ # handle the case with a single node (0 as default value)
+ if(not node.left and not node.right):
+ the_codes[node.char] = "0"
+ return the_codes
+
+ # actual node code
+ newValue = value + str(node.code)
+
+ if(node.left):
+ calculate_code(node.left, newValue, the_codes)
+ if(node.right):
+ calculate_code(node.right, newValue, the_codes)
+
+ if(not node.left and not node.right):
+ the_codes[node.char] = newValue
+
+ return the_codes
diff --git a/huffman_py/functions/decode.py b/huffman_py/functions/decode.py
new file mode 100644
index 0000000..15eb82f
--- /dev/null
+++ b/huffman_py/functions/decode.py
@@ -0,0 +1,81 @@
+#
+# Sam Hadow - Huffman-py
+# Copyright (C) 2023
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+#
+import re
+
+def huffman_decode(encodedData, current_node):
+ root = current_node
+ decodedOutput = []
+
+ # if single node in source tree (unique char in the text)
+ if (not root.left and not root.right):
+ string = ''.join([root.char for _ in encodedData])
+ return string
+
+ # else
+ for x in encodedData:
+ if x == '1':
+ current_node = current_node.right
+ elif x == '0':
+ current_node = current_node.left
+
+ # If internal node we keep going down, else (a leaf) we can decode a part of the binary.
+ try:
+ # internal node
+ if current_node.left.char == None and current_node.right.char == None:
+ pass
+ except AttributeError:
+ # leaf
+ decodedOutput.append(current_node.char)
+ current_node = root
+
+ if current_node != root and (current_node.right !=None or current_node.left != None):
+ # If we end on an internal node then source tree wasn't the correct tree.
+ raise ValueError ("Tree and binary don't correspond.")
+
+ string = ''.join([str(item) for item in decodedOutput])
+ return string
+
+def decode_from_dict(encodedData, dict_):
+ # we have a Dict like this: {char:code}
+ # we convert it to a Dict like that: {code:char} (both codes and chars are unique)
+ dict_ = {value:key for key,value in dict_.items()}
+ text = str(encodedData)
+
+ # we check if we have a binary
+ invalid_char = re.compile('[^01]')
+ if invalid_char.search(text):
+ raise TypeError ('Input text must be a binary.')
+
+ decoded = ''
+ sorted_dict = dict(sorted(dict_.items(), key=lambda x: len(x[0]), reverse=False))
+ while len(text) > 0:
+ for i,binary in enumerate(sorted_dict.keys()):
+ if binary == text[0:len(binary)]:
+ # If we have this binary part in our Dict we can decode a char
+ decoded += sorted_dict[binary]
+ # delete decoded part
+ text = text[len(binary):]
+ # next while iteration
+ break
+
+ # if nothing corresponds then it's not the correct Dict
+ elif i == len(sorted_dict.keys())-1:
+ raise ValueError ("Can't convert text with current Dict.")
+ return decoded
+
+
diff --git a/huffman_py/functions/encode.py b/huffman_py/functions/encode.py
new file mode 100644
index 0000000..d4b2047
--- /dev/null
+++ b/huffman_py/functions/encode.py
@@ -0,0 +1,62 @@
+#
+# Sam Hadow - Huffman-py
+# Copyright (C) 2023
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+#
+from huffman_py.functions.occurence import *
+from huffman_py.functions.calculate_code import *
+from huffman_py.functions.print_binary import *
+from huffman_py.Node import Node
+from huffman_py.Tree import Tree
+
+def huffman_encode(the_data):
+ char_and_occurences = calcul_occurence(the_data)
+
+ # log char and occurences
+ the_char = char_and_occurences.keys()
+ the_occurences = char_and_occurences.values()
+ print("char: ", the_char)
+ print("occurences: ", the_occurences)
+
+ the_nodes = []
+
+ # convert char and occurences in nodes
+ for char in the_char:
+ the_nodes.append(Node(char_and_occurences.get(char), char))
+
+ while len(the_nodes) > 1:
+ # sort nodes
+ the_nodes = sorted(the_nodes, key = lambda x: x.occurrence)
+
+ # get the 2 smallest nodes
+ left = the_nodes[0]
+ right = the_nodes[1]
+
+ left.code = 0
+ right.code = 1
+
+ # Merge these 2 nodes
+ new_node = Node(left.occurrence + right.occurrence, left.char + right.char, left, right)
+
+ the_nodes.remove(left)
+ the_nodes.remove(right)
+ the_nodes.append(new_node)
+
+ my_tree = Tree(the_nodes[0])
+
+ huffmanEncoding = calculate_code(the_nodes[0])
+ print("char avec codes", huffmanEncoding)
+ encodedOutput = print_binary(the_data,huffmanEncoding)
+ return encodedOutput, the_nodes[0], huffmanEncoding
diff --git a/huffman_py/functions/ifFileGetContent.py b/huffman_py/functions/ifFileGetContent.py
new file mode 100644
index 0000000..c7b87d8
--- /dev/null
+++ b/huffman_py/functions/ifFileGetContent.py
@@ -0,0 +1,27 @@
+#
+# Sam Hadow - Huffman-py
+# Copyright (C) 2023
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+#
+import os
+
+def ifFileGetContent(string):
+ '''input: str, return file content if str is a path, else str itself'''
+ if os.path.isfile(string):
+ with open(string, 'r') as fd:
+ content = fd.read()
+ return content
+ else:
+ return string
diff --git a/huffman_py/functions/occurence.py b/huffman_py/functions/occurence.py
new file mode 100644
index 0000000..3e76454
--- /dev/null
+++ b/huffman_py/functions/occurence.py
@@ -0,0 +1,25 @@
+#
+# Sam Hadow - Huffman-py
+# Copyright (C) 2023
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+#
+def calcul_occurence(the_data):
+ the_char = dict()
+ for item in the_data:
+ if the_char.get(item) == None:
+ the_char[item] = 1
+ else:
+ the_char[item] += 1
+ return the_char