जब एक बाइनरी ट्री बनाने की आवश्यकता होती है, और एक तत्व डालने, एक तत्व को हटाने और पेड़ के तत्वों को प्रदर्शित करने जैसे संचालन करने की आवश्यकता होती है, तो इसमें विधियों के साथ एक वर्ग परिभाषित किया जाता है। वर्ग का एक उदाहरण परिभाषित किया गया है और इसका उपयोग तत्वों तक पहुँचने और संचालन करने के लिए किया जाता है।
नीचे उसी का एक प्रदर्शन है -
उदाहरण
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' नाम की एक विधि परिभाषित की गई है, जो पेड़ से तत्वों को हटाती है।
-
'बीएफएस' नाम की एक अन्य विधि परिभाषित की गई है, जो पेड़ पर चौड़ाई पहले खोज करने में मदद करती है।
-
एक उदाहरण बनाया जाता है और 'कोई नहीं' को असाइन किया जाता है।
-
उपयोगकर्ता इनपुट उस ऑपरेशन के लिए लिया जाता है जिसे निष्पादित करने की आवश्यकता होती है।
-
उपयोगकर्ता की पसंद के आधार पर, ऑपरेशन किया जाता है।
-
प्रासंगिक आउटपुट कंसोल पर प्रदर्शित होता है।