All pastes #1470151 Raw Edit

rvore

public python v1 · immutable
#1470151 ·published 2009-06-22 18:52 UTC
rendered paste body
# -*- coding: utf-8 -*-"""    Árvore em Python        Copyright (c) 2009 Vindemiatrix Almuredin.    É dada permissão para copiar, distribuir e/ou modificar este documento    sob os termos da Licença de Documentação FAIL,    Versão 97.545.668.112.666.002 Build 69 Release 42;    Uma cópia da licença talvez esteja inclusa na seção entitulada    "Licença de Documentação FAIL"."""class No:    def __init__(self, valor=None, pai=None):        self.valor = valor        self.pai = pai        self.__populacao = []    def __len__(self):        return len(self.__populacao)    def __getitem__(self, indice):        return self.__populacao[indice]    def __repr__(self):        xrepr = ''        if len(self.__populacao) > 0:            for x in self.__populacao:                xrepr += x.__repr__() + ','            xrepr = self.valor.__repr__() + '(' + xrepr[:-1] + ')'            return xrepr        else:            return self.valor.__repr__()    def __iter__(self):        return iter(self.__populacao)    def criaRamo(self, valor=None):        if valor is None:            NovoNo = No(pai=self)        else:            NovoNo = No(valor, self)        self.__populacao.append(NovoNo)        return NovoNo    def adicionaRamo(self, ramo):        if isinstance(ramo, No):            self.__populacao.append(ramo)            ramo.pai = self        else:            raise TypeError('Item informado não é descendente de No()')    def removeRamo(self, ramo):        for x in enumerate(self.__populacao):            if x[1] is ramo:                self.__populacao[x[0]].pai = None                del self.__populacao[x[0]]