Polish notation के साथ pre-order traversal का उपयोग
Expression trees एक प्रकार की binary tree होती हैं जो arithmetic expressions को दर्शाती हैं:

किसी expression tree पर in-order traversal लगाने से आपको infix notation मिलता है। दिए गए tree के लिए यह notation (10-5)*3 होगा।
किसी expression tree पर pre-order traversal लगाने से आपको prefix notation मिलता है, जिसे Polish notation भी कहते हैं, जहाँ operator अपने operands से पहले आता है। दिए गए tree के लिए यह notation *-10 5 3 होगा।
किसी expression tree पर post-order traversal लगाने से आपको postfix notation मिलता है, जिसे reverse Polish notation भी कहा जाता है, जहाँ operator अपने operands के बाद आता है। दिए गए tree के लिए यह notation 10 5- 3* होगा।
इस expression tree की prefix notation पाने के लिए pre-order traversal का कोड लिखिए।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Data Structures और Algorithms
अभ्यास निर्देश
- जाँचें कि
current_nodeमौजूद है या नहीं। current_nodeका मान प्रिंट करें।- पेड़ के उचित हिस्सों पर
pre_order()फंक्शन को recursively कॉल करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
import queue
class ExpressionTree:
def __init__(self):
self.root = None
def pre_order(self, current_node):
# Check if current_node exists
____:
# Print the value of the current_node
____
# Call pre_order recursively on the appropriate half of the tree
____
____
et = CreateExpressionTree()
et.pre_order(et.root)