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