0% found this document useful (0 votes)
15 views7 pages

17-Free Trees

The document describes a Node class and an ADTTree class. The Node class represents the nodes in a tree and has methods for accessing elements, adding children, and equality checks. The ADTTree class represents a tree and has methods for accessing the root, checking if a node is the root, getting the parent of a node, counting children, iterating over children, checking if a node is a leaf, and getting the depth and height of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

17-Free Trees

The document describes a Node class and an ADTTree class. The Node class represents the nodes in a tree and has methods for accessing elements, adding children, and equality checks. The ADTTree class represents a tree and has methods for accessing the root, checking if a node is the root, getting the parent of a node, counting children, iterating over children, checking if a node is a leaf, and getting the depth and height of nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7


𝑇 𝑁

• 𝑇 𝑟∈𝑁 𝑟 𝑟 𝑇

• 𝑛∈𝑁 𝑣∈𝑁 𝑣
𝑣

𝑣 𝑣
𝑣 𝑣

𝑢 𝑣 𝑢=𝑣 𝑢 𝑣
𝑣 𝑢 𝑢 𝑣
𝑇 𝑣 𝑣 𝑇
𝑣

𝑇 𝑢, 𝑣 𝑢 𝑣

𝑛
𝑛−1
𝛼 𝛽 𝛾
𝑒 𝑓 𝑔

api/ controllers/
api/ models/
api/ routes/
api/ __init__.py
api/ database.py
controllers/ channel_controller.py
models/ channel.py
routes/ channel_routes.py
𝑇 𝑝0 , 𝑝1 , . . . , 𝑝𝑛 (𝑝𝑖 , 𝑝𝑖+1 ) 𝑇
𝑖 0 𝑛−1
𝑇

api/ models/ channel.py


api/ controllers/ channel_controller.py
api/ routes/ channel_routes.py
api/ __init__.py
api/ database.py

𝑇
𝑒 api/ controllers/ 𝑗 controllers/ channel_controller.py

𝑒 api/ controllers/
𝑗 controllers/ channel_controller.py
𝑒 controllers/
N

self._element
self._parent
self._children

N.element()
N.add(element, parent)

Node

class Node:

def __init__(self, element, parent=None, children=None):


self._element = element
self._parent = parent
if children is None:
self._children = []
else:
self._children = children

def element(self):
return self._element

def add(self, element):


temp = Node(element)
if temp not in self._children:
temp._parent = self
self._children.append(temp)
return temp
else:
raise ValueError("El nodo ya tiene un padre.")

def __eq__(self, other):


return type(other) is type(self) and other._element is
self._element

def __ne__(self, other):


return not (self == other)
def __str__(self):
return f"* {self.element()}"

def __repr__(self):
return f"* {self.element()}"

ADTTree

T.root() T None T
T.is_root(p) True p T
T.parent(p) p None p
T.num_children(p) p
T.children(p) p
T.is_leaf(p) True p
len(T) T
T.is_empty() True T
T.nodes() T
Node
iter(T) T
T Node
from node import Node

class ADTTree:

def __init__(self, root=None):


self._size = 0
if root is not None:
self.add_root(root)
else:
self._root = None

def add_root(self, element):


if not self.is_empty():
raise ValueError("El árbol ya tiene una raíz.")
self._root = Node(element)
self._size += 1

def __len__(self):
return self._size

def root(self):
return self._root

def is_root(self, p):


return p == self.root()

def parent(self, p):


return p._parent

def is_empty(self):
if self.root() is None:
return True
else:
return False
def num_children(self, p):
return len(p._children)

def children(self, p):


for child in p._children:
yield child

def is_leaf(self, p):


if self.num_children(p) > 0:
return False
else:
return True

def nodes(self):
pass

def __iter__(self):
pass

def __str__(self):
return self._str_recursive(self._root)

def _str_recursive(self, node, depth=0):


result = " " * depth + str(node) + "\n"
for child in node._children:
result += self._str_recursive(child, depth + 1)
return result

nodes iter

ADTTree

𝒑
𝑻

𝒑 𝒑

• 𝒑 𝒑

• 𝒑 𝒑

𝑨𝑫𝑻𝑻𝒓𝒆𝒆 𝒑 𝑻
def depth(self, p):
"""Calcula la profundidad de un nodo p para un árbol T"""
if self.is_root(p):
return 0
else:
return 1 + self.depth(self.parent(p))

𝒑 𝒑

𝒑 𝒑

𝑨𝑫𝑻𝑻𝒓𝒆𝒆 𝒑 𝑻

def height(self, p):


"""Calcula la altura de un nodo p para un árbol T"""
if self.is_leaf(p):
return 0
else:
heights = []
for child in self.children(p):
heights.append(self.height1(child))
return 1 + max(heights) # 1 + máxima altura de los hijos de p

𝒑 𝑻 height
_recursive_height

def height(self, p=None):


"""Calcula la altura de un nodo p para un árbol T"""
if p is None:
p = self.root()
return self._recursive_height(p)

height
height

You might also like