从 python 二叉树中打印出 python 列表
printing out python list from python binary tree
我想定义一个 returns 树节点值列表的函数。该列表按级别顺序排列(从上到下,从左到右),如果
child 丢失然后在其位置,插入 "None"。
这是二叉树实现
class BinaryTree:
def __init__(self, data, left = None, right = None):
self.left = left
self.right = right
def insert_left(self, data):
self.left = BinaryTree(data, left=self.left)
def insert_right(self, data):
self.right = BinaryTree(data, right=self.right)
def set_value(self, val):
self.key = val
def get_value(self):
return self.key
def create_string(self, indent):
string = str(self.key) + '---+'
if self.left:
string += '\n(l)' + indent + self.left.create_string(indent + ' ')
if self.right:
string += '\n(r)' + indent + self.right.create_string(indent + ' ')
return string
def __str__(self):
return self.create_string(' ')
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
tree.return_list(templist)
main()
你应该向这个函数传递一个空列表
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
调用此方法后,templist 将包含您想要的列表
如果您正在寻找广度优先搜索,那么此方法可能会有所帮助
def return_b_list(tree,templist,queue):
if tree is None:
return;
queue.append(tree)
while len(queue) !=0:
node = queue.popleft()
if node is None:
templist.append(None)
else:
templist.append(node.key)
queue.append(node.left)
queue.append(node.right)
如何调用? (此方法不必是 class 的一部分)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
queue = deque() # you should do a from collections import deque
return_b_list(tree,templist,queue)
print templist
这不是答案,只是想添加我得到的结果和说明
$ python binarayTree.py
3---+
(l) 29---+
(l) 26---+
(r) 4---+
(r) 2---+
(l) 9---+
[3, 29, 4, 26, None, None, 2, None, None, 9, None, None, None]
这里是结果列表的解释
first level 3
second level 29,4
third level 26, None, None ,2
fourth level None, None,9, None, None, None
添加整个 .py 文件如果你运行这应该可以工作
from collections import deque
class BinaryTree:
def __init__(self, data, left = None, right = None):
self.key = data
self.left = left
self.right = right
def insert_left(self, data):
self.left = BinaryTree(data, left=self.left)
def insert_right(self, data):
self.right = BinaryTree(data, right=self.right)
def get_left_subtree(self):
return self.left
def get_right_subtree(self):
return self.right
def set_value(self, val):
self.key = val
def get_value(self):
return self.key
def create_string(self, indent):
string = str(self.key) + '---+'
if self.left:
string += '\n(l)' + indent + self.left.create_string(indent + ' ')
if self.right:
string += '\n(r)' + indent + self.right.create_string(indent + ' ')
return string
def __str__(self):
return self.create_string(' ')
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
def return_b_list(tree,templist,queue):
if tree is None:
return;
queue.append(tree)
while len(queue) !=0:
node = queue.popleft()
if node is None:
templist.append(None)
else:
templist.append(node.key)
queue.append(node.left)
queue.append(node.right)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
queue = deque() # you should do a from collections import deque
return_b_list(tree,templist,queue)
print tree.create_string(' ')
print templist
main()
我想定义一个 returns 树节点值列表的函数。该列表按级别顺序排列(从上到下,从左到右),如果 child 丢失然后在其位置,插入 "None"。
这是二叉树实现
class BinaryTree:
def __init__(self, data, left = None, right = None):
self.left = left
self.right = right
def insert_left(self, data):
self.left = BinaryTree(data, left=self.left)
def insert_right(self, data):
self.right = BinaryTree(data, right=self.right)
def set_value(self, val):
self.key = val
def get_value(self):
return self.key
def create_string(self, indent):
string = str(self.key) + '---+'
if self.left:
string += '\n(l)' + indent + self.left.create_string(indent + ' ')
if self.right:
string += '\n(r)' + indent + self.right.create_string(indent + ' ')
return string
def __str__(self):
return self.create_string(' ')
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
tree.return_list(templist)
main()
你应该向这个函数传递一个空列表
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
调用此方法后,templist 将包含您想要的列表
如果您正在寻找广度优先搜索,那么此方法可能会有所帮助
def return_b_list(tree,templist,queue):
if tree is None:
return;
queue.append(tree)
while len(queue) !=0:
node = queue.popleft()
if node is None:
templist.append(None)
else:
templist.append(node.key)
queue.append(node.left)
queue.append(node.right)
如何调用? (此方法不必是 class 的一部分)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
queue = deque() # you should do a from collections import deque
return_b_list(tree,templist,queue)
print templist
这不是答案,只是想添加我得到的结果和说明
$ python binarayTree.py
3---+
(l) 29---+
(l) 26---+
(r) 4---+
(r) 2---+
(l) 9---+
[3, 29, 4, 26, None, None, 2, None, None, 9, None, None, None]
这里是结果列表的解释
first level 3
second level 29,4
third level 26, None, None ,2
fourth level None, None,9, None, None, None
添加整个 .py 文件如果你运行这应该可以工作
from collections import deque
class BinaryTree:
def __init__(self, data, left = None, right = None):
self.key = data
self.left = left
self.right = right
def insert_left(self, data):
self.left = BinaryTree(data, left=self.left)
def insert_right(self, data):
self.right = BinaryTree(data, right=self.right)
def get_left_subtree(self):
return self.left
def get_right_subtree(self):
return self.right
def set_value(self, val):
self.key = val
def get_value(self):
return self.key
def create_string(self, indent):
string = str(self.key) + '---+'
if self.left:
string += '\n(l)' + indent + self.left.create_string(indent + ' ')
if self.right:
string += '\n(r)' + indent + self.right.create_string(indent + ' ')
return string
def __str__(self):
return self.create_string(' ')
def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)
def return_b_list(tree,templist,queue):
if tree is None:
return;
queue.append(tree)
while len(queue) !=0:
node = queue.popleft()
if node is None:
templist.append(None)
else:
templist.append(node.key)
queue.append(node.left)
queue.append(node.right)
def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
queue = deque() # you should do a from collections import deque
return_b_list(tree,templist,queue)
print tree.create_string(' ')
print templist
main()