103 lines
3.7 KiB
Python
103 lines
3.7 KiB
Python
|
#
|
|||
|
# 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<65>re la liste des sommets d'un arbre <20> 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<6F>rateur
|
|||
|
if self.recherche(noeud) != None:
|
|||
|
self.sommets.remove(noeud)
|
|||
|
# on supprime <20>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<63>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 <20> notre liste de sommets
|
|||
|
self.sommets.append(self.racine)
|
|||
|
|
|||
|
# on ajoute les sommets de arbre2
|
|||
|
self.sommets += arbre2.sommets
|
|||
|
|
|||
|
# on cr<63>e les id
|
|||
|
self.set_id()
|
|||
|
|
|||
|
return self
|
|||
|
|
|||
|
def recherche(self, noeud):
|
|||
|
for sommet in self.sommets:
|
|||
|
if sommet == noeud:
|
|||
|
return sommet
|
|||
|
return None
|