Upload files to 'huffman_py'
This commit is contained in:
parent
e2dab0a87a
commit
f76fa67d93
76
huffman_py/Node.py
Normal file
76
huffman_py/Node.py
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
class Node(object):
|
||||||
|
def __init__(self, occurrence, char, left = None, right = None):
|
||||||
|
# nombre d'occurence de la lettre
|
||||||
|
self.occurrence = occurrence
|
||||||
|
|
||||||
|
# the character
|
||||||
|
self.char = char
|
||||||
|
|
||||||
|
# left child
|
||||||
|
self.left = left
|
||||||
|
|
||||||
|
# right child
|
||||||
|
self.right = right
|
||||||
|
|
||||||
|
# orientation from father (0: left, 1: right)
|
||||||
|
self.code = ''
|
||||||
|
|
||||||
|
# node identifier (unique in a tree)
|
||||||
|
self.identifier = None
|
||||||
|
|
||||||
|
|
||||||
|
# getters and setters (private attributes)
|
||||||
|
|
||||||
|
def get_occurence(self):
|
||||||
|
return self.__occurence
|
||||||
|
def set_occurence(self, valeur):
|
||||||
|
self.__occurence = valeur
|
||||||
|
occurence = property(fget=get_occurence, fset=set_occurence, doc="Character occurences in a text")
|
||||||
|
|
||||||
|
def get_char(self):
|
||||||
|
return self.__char
|
||||||
|
def set_char(self, valeur):
|
||||||
|
self.__char = valeur
|
||||||
|
char = property(fget=get_char, fset=set_char, doc="corresponding character")
|
||||||
|
|
||||||
|
def get_left(self):
|
||||||
|
return self.__left
|
||||||
|
def set_left(self, valeur):
|
||||||
|
self.__left = valeur
|
||||||
|
left = property(fget=get_left, fset=set_left, doc="left child")
|
||||||
|
|
||||||
|
def get_right(self):
|
||||||
|
return self.__right
|
||||||
|
def set_right(self, valeur):
|
||||||
|
self.__right = valeur
|
||||||
|
right = property(fget=get_right, fset=set_right, doc="right child")
|
||||||
|
|
||||||
|
def get_code(self):
|
||||||
|
return self.__code
|
||||||
|
def set_code(self, valeur):
|
||||||
|
self.__code = valeur
|
||||||
|
code = property(fget=get_code, fset=set_code, doc="orientation from father (0: left, 1: right)")
|
||||||
|
|
||||||
|
def get_id(self):
|
||||||
|
return self.__identifier
|
||||||
|
def set_id(self, valeur):
|
||||||
|
self.__identifier = valeur
|
||||||
|
identifier = property(fget=get_id, fset=set_id, doc="node identifier")
|
101
huffman_py/Tree.py
Normal file
101
huffman_py/Tree.py
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
from huffman_py.Node import *
|
||||||
|
|
||||||
|
class Tree(object):
|
||||||
|
def __init__(self, root = None):
|
||||||
|
# tree root (Node object)
|
||||||
|
self.root = root
|
||||||
|
|
||||||
|
# tree nodes (list of Node object)
|
||||||
|
self.nodes = []
|
||||||
|
self.gen_tree()
|
||||||
|
|
||||||
|
self.set_id()
|
||||||
|
|
||||||
|
|
||||||
|
# getters and setters (private attributes)
|
||||||
|
|
||||||
|
def get_root(self):
|
||||||
|
return self.__root
|
||||||
|
def set_root(self, valeur):
|
||||||
|
self.__root = valeur
|
||||||
|
root = property(fget=get_root, fset=set_root, doc="tree root")
|
||||||
|
|
||||||
|
def get_nodes(self):
|
||||||
|
return self.__nodes
|
||||||
|
def set_nodes(self, valeur):
|
||||||
|
self.__nodes = valeur
|
||||||
|
nodes = property(fget=get_nodes, fset=set_nodes, doc="nodes list")
|
||||||
|
|
||||||
|
def gen_tree(self):
|
||||||
|
# generate nodes list from a tree root
|
||||||
|
L = list()
|
||||||
|
L.append(self.root)
|
||||||
|
while len(L) > 0:
|
||||||
|
traitement = L.pop(0)
|
||||||
|
self.nodes.append(traitement)
|
||||||
|
if traitement.left:
|
||||||
|
L.append(traitement.left)
|
||||||
|
if traitement.right:
|
||||||
|
L.append(traitement.right)
|
||||||
|
|
||||||
|
def set_id(self):
|
||||||
|
# Give unique identifiers to every node in a tree
|
||||||
|
for i,elem in enumerate(self.nodes):
|
||||||
|
elem.identifier = i
|
||||||
|
|
||||||
|
def __isub__(self, node):
|
||||||
|
# delete node (overload -=)
|
||||||
|
if self.seek(node) != None:
|
||||||
|
self.nodes.remove(node)
|
||||||
|
# recursively delete children
|
||||||
|
if node.right != None:
|
||||||
|
self.__isub__(node.right)
|
||||||
|
if node.left != None:
|
||||||
|
self.__isub__(node.left)
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __iadd__(self, tree2):
|
||||||
|
# Tree merge
|
||||||
|
# new root
|
||||||
|
occurrence = self.root.occurrence
|
||||||
|
old_root = self.root
|
||||||
|
if occurrence > tree2.root.occurrence:
|
||||||
|
self.root = Node(occurrence, '', left = tree2.root, right = old_root)
|
||||||
|
else:
|
||||||
|
self.root = Node(occurrence, '', left = old_root, right = tree2.root)
|
||||||
|
|
||||||
|
# add new root to nodes list
|
||||||
|
self.nodes.append(self.root)
|
||||||
|
|
||||||
|
# add tree2 nodes
|
||||||
|
self.nodes += tree2.nodes
|
||||||
|
|
||||||
|
# regenerate unique identifiers
|
||||||
|
self.set_id()
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
def seek(self, node):
|
||||||
|
for node in self.nodes:
|
||||||
|
if node == node:
|
||||||
|
return node
|
||||||
|
return None
|
Loading…
x
Reference in New Issue
Block a user