class Tree:
class Node:
def __init__(self, left=None, right=None, parent=None, element=None):
self.left = left
self.right = right
self.parent = parent
self.element = element
class Position:
def make_position(self, node):
def __init__(self):
"""Create an initially empty binary tree."""
self.root = None
self.size = 0
def root(self):
"""Return the root Position of the tree( or None if tree is empty)."""
return self.make_position(self.root)
def add_root(self, e):
"""Place element e at the root of an empty tree and return new Position.
Raise ValueError if tree nonempty."""
if self.root is not None:
raise ValueError("Root Exist")
self.size = 1
self.root = self.Node(e)
return self.make_position(self.root)
我是python和python数据结构的初学者。 如何在文件.py的末尾调用add_root方法并打印此方法以查看根节点中的元素? 我试图写
root = Tree.Position()
print(root.make_position(root))
root = Tree.Position.make_position()
print(root.make_position(root))
但是解释器返回AttributeError