เมื่อจำเป็นต้องสร้างไบนารีทรี และดำเนินการต่างๆ เช่น การแทรกองค์ประกอบ การลบองค์ประกอบ และการแสดงองค์ประกอบของทรี คลาสจะถูกกำหนดด้วยเมธอดในนั้น มีการกำหนดอินสแตนซ์ของคลาสและใช้เพื่อเข้าถึงองค์ประกอบและดำเนินการ
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ตัวอย่าง
class Tree_struct:
def __init__(self, data=None, parent=None):
self.key = data
self.children = []
self.parent = parent
def set_root(self, data):
self.key = data
def add_node(self, node):
self.children.append(node)
def search_node(self, key):
if self.key == key:
return self
for child in self.children:
temp = child.search_node(key)
if temp is not None:
return temp
return None
def remove_node(self):
parent = self.parent
index = parent.children.index(self)
parent.children.remove(self)
for child in reversed(self.children):
parent.children.insert(index, child)
child.parent = parent
def bfs(self):
queue = [self]
while queue != []:
popped = queue.pop(0)
for child in popped.children:
queue.append(child)
print(popped.key, end=' ')
my_instance = None
print('Menu (this assumes no duplicate keys)')
print('add <data> at root')
print('add <data> below <data>')
print('remove <data>')
print('display')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'add':
data = int(do[1])
new_node = Tree_struct(data)
suboperation = do[2].strip().lower()
if suboperation == 'at':
my_instance = new_node
elif suboperation == 'below':
position = do[3].strip().lower()
key = int(position)
ref_node = None
if my_instance is not None:
ref_node = my_instance.search_node(key)
if ref_node is None:
print('No such key.')
continue
new_node.parent = ref_node
ref_node.add_node(new_node)
elif operation == 'remove':
data = int(do[1])
to_remove = my_instance.search_node(data)
if my_instance == to_remove:
if my_instance.children == []:
my_instance = None
else:
leaf = my_instance.children[0]
while leaf.children != []:
leaf = leaf.children[0]
leaf.parent.children.remove_node(leaf)
leaf.parent = None
leaf.children = my_instance.children
my_instance = leaf
else:
to_remove.remove_node()
elif operation == 'display':
if my_instance is not None:
print('Breadth First Search traversal is : ', end='')
my_instance.bfs()
print()
else:
print('The tree is empty')
elif operation == 'quit':
break ผลลัพธ์
Menu (this assumes no duplicate keys) add <data> at root add <data> below <data> remove <data> display quit What would you like to do? add 5 at root What would you like to do? add 6 below 5 What would you like to do? add 8 below 6 What would you like to do? remove 8 What would you like to do? display Breadth First Search traversal is : 5 6 What would you like to do? quit
คำอธิบาย
-
สร้างคลาส 'Tree_struct' พร้อมแอตทริบิวต์ที่จำเป็นแล้ว
-
มีฟังก์ชัน 'init' ที่ใช้สร้างรายการว่าง
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'set_root' เพื่อระบุรูทของต้นไม้
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'add_node' ซึ่งช่วยเพิ่มโหนดให้กับทรี
-
มีการกำหนดวิธีการอื่นที่เรียกว่า 'search_node' ซึ่งช่วยในการค้นหาองค์ประกอบเฉพาะ
-
มีการกำหนดเมธอดชื่อ 'remove_node' ซึ่งจะลบองค์ประกอบออกจากทรี
-
มีการกำหนดวิธีการอื่นที่ชื่อว่า 'bfs' ซึ่งช่วยในการค้นหาแบบกว้างๆ ในทรีเป็นครั้งแรก
-
มีการสร้างและกำหนดอินสแตนซ์ให้กับ "ไม่มี"
-
ผู้ใช้ป้อนข้อมูลสำหรับการดำเนินการที่จำเป็นต้องดำเนินการ
-
ขึ้นอยู่กับทางเลือกของผู้ใช้ การดำเนินการจะดำเนินการ
-
เอาต์พุตที่เกี่ยวข้องจะแสดงบนคอนโซล