เมื่อจำเป็นต้องทำการค้นหาเชิงลึกครั้งแรกบนทรีโดยใช้การเรียกซ้ำ คลาสจะถูกกำหนดและเมธอดจะถูกกำหนดในนั้นซึ่งจะช่วยในการค้นหาแบบกว้างก่อน
ด้านล่างนี้เป็นการสาธิตสำหรับสิ่งเดียวกัน -
ตัวอย่าง
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 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(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search(key)
return temp
return None
def depth_first_search(self):
print('entering {}...'.format(self.key))
if self.left is not None:
self.left.depth_first_search()
print('at {}...'.format(self.key))
if self.right is not None:
self.right.depth_first_search()
print('leaving {}...'.format(self.key))
btree_instance = None
print('Menu (no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('dfs')
print('quit')
while True:
my_input = input('What would you like to do? ').split()
op = my_input[0].strip().lower()
if op == 'insert':
data = int(my_input[1])
new_node = BinaryTree_struct(data)
sub_op = my_input[2].strip().lower()
if sub_op == 'at':
btree_instance = new_node
else:
position = my_input[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 sub_op == 'left':
ref_node.insert_at_left(new_node)
elif sub_op == 'right':
ref_node.insert_at_right(new_node)
elif op == 'dfs':
print('depth-first search traversal:')
if btree_instance is not None:
btree_instance.depth_first_search()
print()
elif op == 'quit':
break ผลลัพธ์
Menu (no duplicate keys) insert <data> at root insert <data> left of <data> insert <data> right of <data> dfs 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? dfs depth-first search traversal: entering 5... entering 6... at 6... leaving 6... at 5... entering 8... at 8... leaving 8... leaving 5... What would you like to do? quit Use quit() or Ctrl-D (i.e. EOF) to exit
คำอธิบาย
-
คลาส 'BinaryTree_struct' พร้อมแอตทริบิวต์ที่จำเป็นจะถูกสร้างขึ้น
-
มีฟังก์ชัน 'init' ที่ใช้กำหนดโหนด 'left' และ 'right' ให้กับ 'None'
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'set_root' เพื่อระบุรูทของต้นไม้
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'insert_at_left' ซึ่งช่วยเพิ่มโหนดทางด้านซ้ายของทรี
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'insert_at_right' ซึ่งช่วยเพิ่มโหนดทางด้านขวาของแผนผัง
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า "search_elem" ซึ่งช่วยในการค้นหาองค์ประกอบเฉพาะ
-
มีการกำหนดเมธอดที่ชื่อว่า "deep_first_search" ซึ่งช่วยในการค้นหาในเชิงลึกก่อนบนไบนารีทรี
-
มีการสร้างอินสแตนซ์ของคลาสและกำหนดเป็น "ไม่มี"
-
มีเมนูมาให้
-
ผู้ใช้ป้อนข้อมูลสำหรับการดำเนินการที่จำเป็นต้องดำเนินการ
-
ขึ้นอยู่กับทางเลือกของผู้ใช้ การดำเนินการจะดำเนินการ
-
เอาต์พุตที่เกี่ยวข้องจะแสดงบนคอนโซล