-1
traversal orderで印刷する方法がわかりませんし、binary_strを入力した場合の結果の出力方法もわかりません。 Inoderトラバーサルのためにinorder traversalを使用すると、バイナリツリーのすべての項目が印刷されることがあります
class TreeNode:
def __init__(self , item , left , right):
self.item = item
self.right = right
self.left = left
class BinaryTree:
def __init__(self):
self.root = None
def add(self , item , binary_str):
binary_str_itr = iter(binary_str)
self.root = self.add_aux(self.root , item , binary_str_itr)
def add_aux(self , current , item , binary_str_itr):
if current is None:
current = TreeNode(None , None , None)
try:
bit = next(binary_str_itr)
if bit == '0':
current.left = self.add_aux(current.left , item , binary_str_itr)
elif bit == '1':
current.right = self.add_aux(current.right , item , binary_str_itr)
except StopIteration:
current.item = item
return current
def print_inorder(self):
self.print_inorder_aux(self.root)
def print_inorder_aux(self,current):
if current is not None: #if not a base case
self.print_inorder_aux(current.left)
print(current)
self.print_inorder_aux(current.right)
@Uncle SAM以下のフォローが、あなたのケースのための黄金の親指のルールはself.leftである - self.item - 自己。右 –