Question

Write a program to implement binary search tree (BST). Also, write functions to traverse the BST in In-order, Pre-order and Post-order forms.

07 Aug 2023
Answer :
Word Count : 496

Below is a Python program that implements a Binary Search Tree (BST) and provides functions to traverse it in In-order, Pre-order, and Post-order forms:

class TreeNode:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.val = key

class BST:
    def __init__(self):
        self.root = None
    
    def insert(self, root, key):
        if root is None:
            return TreeNode(key)
__________ ______ ____ ___ _________ _______ __________.
______ ___ ______ ______ ___ ___ ________ ___ ____.
_________ ___ ____ ________ ___.
________ __________ _________ ____ _______ _______ ______ _____ ______ _______.
______ _______ ____ _____ __________ _____ _________.
________ ____ _________ _____ ______ ____ __________ ___ __________ ____ _______.
__________ ______ __________ ____ ________ _________ ____ _____ ________ ____.
____ ____ _______ ____ ____ _________ _________ ____ ____ ____.
____ _____ ___ ________ ___.
______ ____ ___ ___ __________ ________ _______.
____ ____ ___ __________ ___ ___ ___ _________ ___ __________.
______ ____ ___ ____ _________ ________ ______.
_____ __________ ____ ____ ____ ____.
____ ___ _________ _______ __________.
__________ ______ _______ ___ ____ _________ _______ ____ _____ ____ __________ _____.
______ ________ _________ ________ _________ _____ _______ ___ __________ _____ _________ _________.
____ _________ ________ ________ _________ ___ _____.
_________ _______ ________ _____ ___ ______.
___ _______ ________ _____ ________ ____ __________ ____.
_______ __________ ________ ________ ______ ____.
_________ ___ _________ ___ _____ ________ _______ ______ _____ _____ _________ _________.
________ _____ _______ ______ _______ _______ _____ ____ ____ ____ __________.
_____ ___ _______ __________ ________.
_____ ________ _______ ______ _________ ___.
___ _____ _________ ________ _____ ___ ________ __________.
_________ ___ _________ ______ __________ ________ ____.
__________ ____ _____ __________ _____ ___.
___ __________ _____ _______ ______ ______ _________ ________.
_____ _________ _________ ___ _________ _______ __________.
____ ______ _________ _______ ____ __________ _________ _________ _______ _________ _________ _________.
_____ _______ ______ ________ _______ ___ _______ ____ _______ _____ __________.
________ _____ _____ ___ ________ ______.
_____ ___ ___ ______ __________ ______ ___ ___ _________ __________ ________.
______ __________ ____ _____ ________ ______ ____ _____.
_____ ______ ________ __________ ____ ____.
________ __________ ________ ______ ________ ______.
_________ ______ _______ ___ ___ ______ ________ ________ __________ ___ _________.
_________ ___ ___ __________ ________ ____.
__________ ______ _________ ____ ____ ___ __________ _______ ____ ________.
_____ ____ ______ ________ _________ ____ __________ ___ _____ __________ ________.
__________ __________ ____ _____ ________.
______ ___ ______ ______ ________.
___ ______ __________ ______ _______.
_________ ______ ___ ________ _________ _________ ________.
_________ _________ _______ __________ ___ ____ ____.
_____ _______ _________ _______ ____ ___ ______ ______ ___ ____ _________ _____.
_____ ________ ______ _______ ____.
_________ ___ ____ ________ ________ ______ _____.
_____ ______ _____ _______ __________ ________ ________ __________ ______.
_________ __________ ____ _____ ____ ____ ______.

IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
'; fetch( "query.php?type=1&method=0&lr=0&app=401&mycartproduct=1", { method: "GET", cache: "no-store" } ) .then(function(response) { if (!response.ok) { throw new Error( "HTTP " + response.status ); } return response.text(); }) .then(function(html) { loader.innerHTML = html; /* Your existing function, if available. */ if (typeof updateTotals === "function") { updateTotals(); } else { calculateCartTotal(); } }) .catch(function(error) { console.error( "Cart loading error:", error ); loader.innerHTML = '
Unable to load cart.
'; });}/* ========================================================= CALCULATE TOTAL========================================================= */function calculateCartTotal() { let subtotal = 0; /* Looks for .item-price inside AJAX products. Examples accepted: ₹200 ₹200.00 Rs 200 Rs. 200 200 200.00 */ const prices = document.querySelectorAll( '#loadmycart .item-price' ); prices.forEach(function(element) { let text = element.textContent || ''; text = text .replace(/₹/g, '') .replace(/Rs\.?/gi, '') .replace(/,/g, '') .trim(); let value = parseFloat(text); if (!isNaN(value)) { subtotal += value; } }); updateCartDisplay(subtotal);}/* ========================================================= UPDATE TOTAL DISPLAY========================================================= */function updateCartDisplay(total) { const subtotalElement = document.getElementById('subtotal'); const totalElement = document.getElementById('total'); const checkoutButton = document.getElementById('checkoutButton'); const noItems = document.getElementById('noitems'); const amount = Number(total) || 0; if (subtotalElement) { subtotalElement.textContent = '₹' + amount.toFixed(2); } if (totalElement) { totalElement.textContent = '₹' + amount.toFixed(2); } /* EMPTY CART */ if (amount
Top