เมื่อจำเป็นต้องค้นหาโหนดที่ 'n'th โดยใช้การข้ามผ่านแบบไม่เรียงลำดับของไบนารีทรี คลาสไบนารีทรีจะถูกสร้างขึ้นด้วยวิธีการตั้งค่าองค์ประกอบรูท เพิ่มองค์ประกอบไปทางซ้ายหรือขวา ดำเนินการตามคำสั่ง เป็นต้น มีการสร้างอินสแตนซ์ของคลาส และสามารถใช้เพื่อเข้าถึงเมธอดได้
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ตัวอย่าง
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' ถูกสร้างขึ้น
-
ผู้ใช้ป้อนข้อมูลสำหรับการดำเนินการที่จำเป็นต้องดำเนินการ
-
ขึ้นอยู่กับทางเลือกของผู้ใช้ การดำเนินการจะดำเนินการ
-
เอาต์พุตที่เกี่ยวข้องจะแสดงบนคอนโซล