我正在阅读以下数据,作为分配到二叉树(不是严格二叉搜索树)的一部分:
5
4 1 2
2 3 4
5 -1 -1
1 -1 -1
3 -1 -1
他们被读入python self.key
,self.left
和self.right
中的三个列表,其中第一行的整数n
是节点数。接下来的n行是键,左,右。左边是父亲的左子女的密钥是key[left]
,同样右子的密钥是key[right]
,所以例如第一行是4的关键是根和{{ 1}}含义2是4的左子,key[1]
意味着5是4的右子,依此类推,左和右的-1表示该键是叶子:
Tree structure for this example
问题是正在添加根的左右子节点,但是没有添加这些节点的子节点。我是否正确地向树中添加节点?我不能只根据密钥的值添加它们,因为它不是严格的二进制搜索树,因为其他一些例子清楚,例如 root = 0 和 left child = 70 和 right child = 20 。 inOrder遍历的输出为key[2]
(应为2 4 5
),这使我相信我没有添加其他节点。任何有关添加方法的帮助都将受到赞赏......
1 2 3 4 5
答案 0 :(得分:0)
谢谢我让它工作 - 我将节点Node(key[i])
添加到字典和self.nodes[val] = [node, node.l, node.r]
,并在添加左边并递归搜索字典以获取inOrder,preOrder和postOrder树遍历。
class Node:
def __init__(self, val):
self.l = None
self.r = None
self.v = val
class Tree:
def __init__(self):
self.root = None
self.nodes = {}
def getRoot(self):
return self.root
def add_root(self, val):
if(self.root is None):
self.root = Node(val)
self.nodes[val] = [self.root,-1,-1]
def add_left(self, val, node):
if(node.l is None):
node.l = Node(val)
self.nodes[node.v][1] = node.l
def add_right(self, val, node):
if(node.r is None):
node.r = Node(val)
self.nodes[node.v][2] = node.r
def inOrder(self):
self.result = []
if(self.root is not None):
self._inOrder(self.root, self.result)
return self.result
else:
print('root is None')
def _inOrder(self, node, result):
if(node is not None):
try:
self._inOrder(self.nodes[node.v][1], self.result)
except (IndexError, AttributeError):
pass
self.result.append(node.v)
try:
self._inOrder(self.nodes[node.v][2], self.result)
except (IndexError, AttributeError):
pass
def preOrder(self):
self.result = []
if(self.root is not None):
self._preOrder(self.root, self.result)
return self.result
else:
print('root is None')
def _preOrder(self, node, result):
if(node is not None):
self.result.append(node.v)
try:
self._preOrder(self.nodes[node.v][1], self.result)
except (IndexError, AttributeError):
pass
try:
self._preOrder(self.nodes[node.v][2], self.result)
except (IndexError, AttributeError):
pass
def postOrder(self):
self.result = []
if(self.root is not None):
self._postOrder(self.root, self.result)
return self.result
else:
print('root is None')
def _postOrder(self, node, result):
if(node is not None):
try:
self._postOrder(self.nodes[node.v][1], self.result)
except (IndexError, AttributeError):
pass
try:
self._postOrder(self.nodes[node.v][2], self.result)
except (IndexError, AttributeError):
pass
self.result.append(node.v)
def read(self):
self.n = int(sys.stdin.readline())
self.key = [0 for i in range(self.n)]
self.left = [0 for i in range(self.n)]
self.right = [0 for i in range(self.n)]
for i in range(self.n):
[a, b, c] = map(int, sys.stdin.readline().split())
self.key[i] = a
self.left[i] = b
self.right[i] = c
#adding root
self.add_root(self.key[0])
for i in range(1, self.n):
self.nodes[self.key[i]] = [Node(self.key[i]),-1,-1]
for i in range(0, self.n):
if self.left[i] != -1:
# adding the other left nodes
self.add_left(self.key[self.left[i]], self.nodes[self.key[i]][0])
if self.right[i] != -1:
# adding the other right nodes
self.add_right(self.key[self.right[i]], self.nodes[self.key[i]][0])