Upload files to 'huffman_py'

This commit is contained in:
sam.hadow 2023-05-25 00:39:10 +02:00
parent df156bde72
commit c60ec5f428
3 changed files with 210 additions and 0 deletions

102
huffman_py/Arbre.py Normal file
View File

@ -0,0 +1,102 @@
#
# 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.Sommets import *
class Arbre(object):
def __init__(self, racine = None):
# racine de l'arbre (un objet Sommet)
self.racine = racine
# les sommets dans l'arbre (une liste d'objets Sommet)
self.sommets = []
self.gen_arbre()
self.set_id()
# getters et setters des attributs avec _attribut pour stocker l'attribut dans un espace non public
# on utilisera obligatoirement ces méthodes pour manipuler les attributs
def get_racine(self):
return self.__racine
def set_racine(self, valeur):
self.__racine = valeur
racine = property(fget=get_racine, fset=set_racine, doc="racine de l'arbre")
def get_sommets(self):
return self.__sommets
def set_sommets(self, valeur):
self.__sommets = valeur
sommets = property(fget=get_sommets, fset=set_sommets, doc="liste des sommets dans l'arbre")
def gen_arbre(self):
# genère la liste des sommets d'un arbre à partir de la racine de celui ci
L = list()
L.append(self.racine)
while len(L) > 0:
traitement = L.pop(0)
self.sommets.append(traitement)
if traitement.left:
L.append(traitement.left)
if traitement.right:
L.append(traitement.right)
def set_id(self):
# attribue des identifiants aux sommets d'un arbre
for i,elem in enumerate(self.sommets):
elem.identifiant = i
def __isub__(self, noeud):
# suppression d'un sommet en surchargeant un opérateur
if self.recherche(noeud) != None:
self.sommets.remove(noeud)
# on supprime également les fils
if noeud.right != None:
self.__isub__(noeud.right)
if noeud.left != None:
self.__isub__(noeud.left)
return self
def __iadd__(self, arbre2):
# fusion d'arbres
# on crée une nouvelle racined
occurrence = self.racine.occurrence
ancienne_racine = self.racine
if occurrence > arbre2.racine.occurrence:
self.racine = Sommets(occurrence, '', left = arbre2.racine, right = ancienne_racine)
else:
self.racine = Sommets(occurrence, '', left = ancienne_racine, right = arbre2.racine)
# on ajoute la nouvelle racine à notre liste de sommets
self.sommets.append(self.racine)
# on ajoute les sommets de arbre2
self.sommets += arbre2.sommets
# on crée les id
self.set_id()
return self
def recherche(self, noeud):
for sommet in self.sommets:
if sommet == noeud:
return sommet
return None

77
huffman_py/Sommets.py Normal file
View File

@ -0,0 +1,77 @@
#
# 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 Sommets(object):
def __init__(self, occurrence, lettres, left = None, right = None):
# nombre d'occurence de la lettre
self.occurrence = occurrence
# la lettre
self.lettres = lettres
# fils gauche
self.left = left
# fils droit
self.right = right
# direction par rapport au père (0 pour gauche, 1 pour droite)
self.code = ''
# ID du sommet (unique dans un arbre)
self.identifiant = None
# getters et setters des attributs avec _attribut pour stocker l'attribut dans un espace non public
# on utilisera obligatoirement ces méthodes pour manipuler les attributs
def get_occurence(self):
return self.__occurence
def set_occurence(self, valeur):
self.__occurence = valeur
occurence = property(fget=get_occurence, fset=set_occurence, doc="Nombre d'occurence de la lettre")
def get_lettres(self):
return self.__lettres
def set_lettres(self, valeur):
self.__lettres = valeur
lettres = property(fget=get_lettres, fset=set_lettres, doc="Lettre correspondante")
def get_left(self):
return self.__left
def set_left(self, valeur):
self.__left = valeur
left = property(fget=get_left, fset=set_left, doc="fils gauche")
def get_right(self):
return self.__right
def set_right(self, valeur):
self.__right = valeur
right = property(fget=get_right, fset=set_right, doc="fils droit")
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 dans l'arbre binaire par rapport au père, 0 pour gauche, 1 pour droite")
def get_id(self):
return self.__identifiant
def set_id(self, valeur):
self.__identifiant = valeur
identifiant = property(fget=get_id, fset=set_id, doc="identifiant d'un sommet de l'arbre, ces id sont tous différents dans un même arbre")

31
huffman_py/main.py Normal file
View File

@ -0,0 +1,31 @@
#
# 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/>.
#
from huffman_py.gui.Ui_MainWindow import *
import sys
def main():
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()