เมื่อจำเป็นต้องพิมพ์โหนดในทรีย่อยด้านซ้าย สามารถสร้างคลาสที่ประกอบด้วยเมธอดที่สามารถกำหนดเพื่อตั้งค่าโหนดรูท ดำเนินการตามคำสั่ง แทรกองค์ประกอบทางด้านขวาของโหนดรูท ทางด้านซ้ายของ โหนดรูท เป็นต้น มีการสร้างอินสแตนซ์ของคลาสและสามารถใช้เมธอดเพื่อดำเนินการที่จำเป็นได้
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ตัวอย่าง
class BinaryTree_struct:
def __init__(self, data=None):
self.key = data
self.left = None
self.right = None
def set_root(self, data):
self.key = data
def inorder_traversal(self):
if self.left is not None:
self.left.inorder_traversal()
print(self.key, end=' ')
if self.right is not None:
self.right.inorder_traversal()
def insert_at_left(self, new_node):
self.left = new_node
def insert_at_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
def print_left_part(self):
if self.left is not None:
self.left.inorder_traversal()
my_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('left')
print('quit')
while True:
my_input = input('What operation would you do ? ').split()
operation = my_input[0].strip().lower()
if operation == 'insert':
data = int(my_input[1])
new_node = BinaryTree_struct(data)
suboperation = my_input[2].strip().lower()
if suboperation == 'at':
my_instance = new_node
else:
position = my_input[4].strip().lower()
key = int(position)
ref_node = None
if my_instance is not None:
ref_node = my_instance.search_elem(key)
if ref_node is None:
print('No such key')
continue
if suboperation == 'left':
ref_node.insert_at_left(new_node)
elif suboperation == 'right':
ref_node.insert_at_right(new_node)
elif operation == 'left':
print('Nodes of the left subtree are : ', end='')
if my_instance is not None:
my_instance.print_left_part()
print()
elif operation == 'quit':
break ผลลัพธ์
Menu (this assumes no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> left quit What operation would you do ? insert 5 at root What operation would you do ? insert 6 left of 5 What operation would you do ? insert 8 right of 5 What operation would you do ? left Nodes of the left subtree are : 6 What operation would you do ? quit Use quit() or Ctrl-D (i.e. EOF) to exit
คำอธิบาย
-
คลาส 'BinaryTree_struct' พร้อมแอตทริบิวต์ที่จำเป็นจะถูกสร้างขึ้น
-
มีฟังก์ชัน 'init' ที่ใช้กำหนดโหนดซ้ายและขวาเป็น 'ไม่มี'
-
มีการกำหนดวิธีการ "set_root" ที่ช่วยตั้งค่ารูทของไบนารีทรี
-
มีวิธีการ "insert_at_right" ที่ช่วยเพิ่มองค์ประกอบไปยังโหนดด้านขวาของต้นไม้
-
มีวิธีการ "insert_at_left" ที่ช่วยเพิ่มองค์ประกอบไปยังโหนดด้านซ้ายของต้นไม้
-
อีกวิธีหนึ่งที่ชื่อว่า 'inorder_traversal' ซึ่งดำเนินการตามลำดับการข้ามผ่าน
-
มีการกำหนดเมธอดที่ชื่อว่า "search_elem" ซึ่งช่วยในการค้นหาองค์ประกอบเฉพาะ
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า "print_left_part" ซึ่งช่วยแสดงเฉพาะส่วนด้านซ้ายของไบนารีทรีบนคอนโซล
-
มีการสร้างและกำหนดอินสแตนซ์ให้กับ "ไม่มี"
-
ผู้ใช้ป้อนข้อมูลสำหรับการดำเนินการที่จำเป็นต้องดำเนินการ
-
ขึ้นอยู่กับทางเลือกของผู้ใช้ การดำเนินการจะดำเนินการ • ผลลัพธ์ที่เกี่ยวข้องจะแสดงบนคอนโซล