Created
August 8, 2015 00:21
-
-
Save everaldo/e0a8f9f62897c73504dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Spyder Editor | |
This is a temporary script file. | |
""" | |
class Node: | |
"""Node é um container""" | |
def __init__(self, value): | |
self.value = value | |
def __str__(self): | |
return str(self.value) | |
def __lt__(self, other): | |
return (self.value < other) | |
def __le__(self, other): | |
return(self.value <= other) | |
def __gt__(self,other): | |
return(self.value > other) | |
def __ge__(self,other): | |
return(self.value >= other) | |
def __eq__(self,other): | |
return(self.value == other) | |
__repr__ = __str__ | |
class Arvore: | |
"""Arvore é uma árvore binária""" | |
def __init__(self, node, esquerda, direita): | |
self.node = node | |
self.esquerda = esquerda | |
self.direita = direita | |
def vazia(): | |
return ArvoreVazia() | |
@classmethod | |
def criar(self, node): | |
return Arvore(node, self.vazia(), self.vazia()) | |
def __str__(self): | |
return self.esquerda.__str__() + " " + self.node.__str__() + " " \ | |
+ self.direita.__str__() | |
def eh_vazia(self): | |
return False | |
def insere(self, node): | |
if node < self.node: | |
if self.esquerda.eh_vazia(): | |
self.esquerda = Arvore.criar(node) | |
else: | |
self.esquerda.insere(node) | |
else: | |
if self.direita.eh_vazia(): | |
self.direita = Arvore.criar(node) | |
else: | |
self.direita.insere(node) | |
class ArvoreVazia: | |
"""Representa uma árvore vazia""" | |
def __init__(self): | |
pass | |
def __str__(self): | |
return "" | |
def eh_vazia(self): | |
return True | |
x = Node(8) | |
a = Arvore.criar(x) | |
print(a) | |
a.insere(Node(7)) | |
a.insere(Node(5)) | |
a.insere(Node(2)) | |
a.insere(Node(300)) | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment