def str_tree(atree,indent_char ='.',indent_delta=2):
def str_tree_1(indent,atree):
if atree == None:
return ''
else:
answer = ''
answer += str_tree_1(indent+indent_delta,atree.right)
answer += indent*indent_char+str(atree.value)+'\n'
answer += str_tree_1(indent+indent_delta,atree.left)
return answer
return str_tree_1(0,atree)
def build_balanced_bst(l):
if len(l) == 0:
return None
else:
mid = (len(l)-1)/2
if mid >= 1:
build_balanced_bst(l[:mid])
build_balanced_bst(l[mid:])
else:
return
に固定するための増加にソートされているユニークな値のリストを受け取り、バイナリツリーを印刷します。 build_ballanced_bst呼び出し(リスト(irange(1,10))として印刷し、高さ3のバイナリ検索ツリーを返します。str_tree機能は何build_balanced_bst()関数が返すを印刷するために使用されるどのように私はbuild_balanced_bst(L)、build_balanced_bst(L)に取り組んでいますbuild_balanced_bst機能
......10
....9
..8
......7
....6
5
......4
....3
..2
....1
を私str_tree機能を
私はリストの中の値をルートの値として使用しました。下のbuild_balanced_bst(l)を呼び出すと、その値が使用されていましたが、それを変更することはできません。何も印刷しません。
l = list(irange(1,10))
t = build_balanced_bst(l)
print('Tree is\n',str_tree(t),sep='')
誰かが私のbuild_balanced_bst(l)関数を修正するのを助けることができますか?どうもありがとう。
あなたのツリーの構造はどのようなものでしょうか? 'Node'クラスなのでしょうか? ( 'str_tree'は' left'、 'right'、' value'属性を持つものと思われます。) 'build_balanced_bst'関数は何も返しません(技術的には常に' None'を返します)。それはあなたがそれを与えるリストで再帰しますが、実際にはリストのデータで何もしません。 – Blckknght