ツリーのインオーダートラバーサルでN番目のノードを見つけるPythonプログラム
二分木の順序トラバーサルを使用して「n」番目のノードを見つける必要がある場合、ルート要素の設定、左または右への要素の追加、順序トラバーサルの実行などのメソッドを使用して、二分木クラスが作成されます。クラスのインスタンスが作成され、メソッドにアクセスするために使用できます。
以下は同じのデモンストレーションです-
例
class BinaryTree_struct:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None
def set_root(self, key):
self.key = key
def inorder_nth(self, n):
return self.inorder_nth_helper_fun(n, [])
def inorder_nth_helper_fun(self, n, in_ord):
if self.left is not None:
temp = self.left.inorder_nth_helper_fun(n, in_ord)
if temp is not None:
return temp
in_ord.append(self)
if n == len(in_ord):
return self
if self.right is not None:
temp = self.right.inorder_nth_helper_fun(n, in_ord)
if temp is not None:
return temp
def insert_t0_left(self, new_node):
self.left = new_node
def insert_to_right(self, new_node):
self.right = new_node
def search_elem(self, key):
if self.key == key:
return self
if self.left is not None:
temp = self.left.search_elem(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search_elem(key)
return temp
return None
btree_instance = None
print('Menu (this assumes no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('inorder ')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'insert':
data = int(do[1])
new_node = BinaryTree_struct(data)
suboperation = do[2].strip().lower()
if suboperation == 'at':
btree_instance = new_node
else:
position = do[4].strip().lower()
key = int(position)
ref_node = None
if btree_instance is not None:
ref_node = btree_instance.search_elem(key)
if ref_node is None:
print('No such key.')
continue
if suboperation == 'left':
ref_node.insert_t0_left(new_node)
elif suboperation == 'right':
ref_node.insert_to_right(new_node)
elif operation == 'inorder':
if btree_instance is not None:
index = int(do[1].strip().lower())
node = btree_instance.inorder_nth(index)
if node is not None:
print('nth term of inorder traversal: {}'.format(node.key))
else:
print('The index exceeds maximum possible index.')
else:
print('The tree is empty...')
elif operation == 'quit':
break 出力
Menu (this assumes no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> inorder quit What would you like to do? insert 5 at root What would you like to do? insert 6 left of 5 What would you like to do? insert 8 right of 5 What would you like to do? inorder 5 The index exceeds maximum possible index. What would you like to do? 6 6
説明
-
必要な属性を持つ「BinaryTree_struct」クラスが作成されます。
-
左右のノードを「なし」に設定するために使用される「init」関数があります。
-
バイナリツリーのルートを設定するのに役立つ「set_root」メソッドがあります。
-
再帰を使用して順序どおりの走査を実行する「inorder_nth」という名前の別のメソッド。
-
したがって、ヘルパー関数が一緒に定義されています。
-
ルートノードの右側に要素を追加するのに役立つ「insert_to_right」という名前の別のメソッドが定義されています。
-
ルートノードの左側に要素を追加するのに役立つ「insert_to_left」という名前のメソッドが定義されています。
-
「search_elem」という名前のメソッドが定義されており、特定の要素の検索に役立ちます。
-
「BinaryTree_struct」クラスのオブジェクトが作成されます。
-
実行する必要のある操作に対してユーザー入力が行われます。
-
ユーザーの選択に応じて、操作が実行されます。
-
関連する出力がコンソールに表示されます。
-
Pythonでパスカルの三角形のn番目の行を見つけるプログラム
数値がnであるとすると、パスカルの三角形のn番目(0インデックス)の行を見つける必要があります。パスカルの三角形は次のように作成できることを知っています- 一番上の行には、1の配列があります。 次の行は、上と左に番号を追加し、上と右に番号を追加することによって作成されます。 いくつかの行は次のとおりです- したがって、入力が4のような場合、出力は[1、4、6、4、1]になります。 これを解決するには、次の手順に従います- nが0と同じ場合、 リターン[1] nが1と同じ場合、 return [1,1] ls:=[1,1]のリスト、temp:=[1,1]のリス
-
Pythonで二分木の最大幅を見つけるプログラム
二分木があるとすると、ツリー内の任意のレベルの最大幅を見つける必要があります。ここで、レベルの幅は、左端のノードと右端のノードの間に保持できるノードの数です。 したがって、入力がのような場合 その場合、出力は2になります これを解決するために、次の手順に従います- マップdを作成し、最小値と最大値を保持するには、最小値は最初は無限大で、最大値は0です 関数dfs()を定義します。これはルートを取ります、pos:=0、depth:=0 ルートがnullの場合、戻り値はありません d [depth、0] =d [depth、0]とposの最小値 d [d