-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree
67 lines (57 loc) · 1.66 KB
/
tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Tree:
def __init__(self):
self.raiz = None
def insere( self, valor):
if self.raiz == None:
self.raiz = No(valor)
else:
self.raiz.insere(valor)
def contaDescendentes(self, valor):
if self.raiz != None:
return self.raiz.contaDescendentes(valor)
else:
return "Árvore vazia"
def decresce(self):
if self.raiz != None:
self.raiz.decresce()
else:
print("Árvore Vazia!")
class No:
def __init__(self, valor):
self.info = valor
self.esq = None
self.dir = None
def insere(self, valor):
if valor <= self.info:
if self.esq == None:
self.esq = No(valor)
else:
self.esq.insere(valor)
else:
if self.dir == None:
self.dir = No(valor)
else:
self.dir.insere(valor)
def contaDescendentes(self, valor):
if self.info == valor:
return (self.somaQtd(0) - 1)
elif valor <= self.info:
return self.esq.contaDescendentes(valor)
else:
return self.dir.contaDescendentes(valor)
def somaQtd(self, cont):
if self.esq != None:
cont = self.esq.somaQtd(cont)
if self.dir != None:
cont = self.dir.somaQtd(cont)
return cont+1
def decresce(self):
if self.dir!= None:
self.dir.decresce()
else:
return False
print(self.info)
if self.esq!= None:
self.esq.decresce()
else:
return False