Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import sys class Node: def __init__(self, customer_id, year_of_expiry): self.customer_id = customer_id self.year_of_expiry = year_of_expiry self.left = None self.right = None class BinaryTree: def __init__(self): self.root
import sys class Node: def __init__(self, customer_id, year_of_expiry): self.customer_id = customer_id self.year_of_expiry = year_of_expiry self.left = None self.right = None class BinaryTree: def __init__(self): self.root = None def insert(self, customer_id, year_of_expiry): new_node = Node(customer_id, year_of_expiry) if self.root is None: self.root = new_node else: current_node = self.root while True: if year_of_expiry < current_node.year_of_expiry: if current_node.left is None: current_node.left = new_node break else: current_node = current_node.left else: if current_node.right is None: current_node.right = new_node break else: current_node = current_node.right def delete(self, year_of_expiry): if self.root is None: print("Tree is empty") return else: parent = None current_node = self.root while current_node is not None and current_node.year_of_expiry != year_of_expiry: parent = current_node if year_of_expiry < current_node.year_of_expiry: current_node = current_node.left else: current_node = current_node.right if current_node is None: print("Node with year of expiry", year_of_expiry, "not found") return if current_node.left is None and current_node.right is None: if parent is None: self.root = None elif parent.left == current_node: parent.left = None else: parent.right = None elif current_node.left is None: if parent is None: self.root = current_node.right elif parent.left == current_node: parent.left = current_node.right else: parent.right = current_node.right elif current_node.right is None: if parent is None: self.root = current_node.left elif parent.left == current_node: parent.left = current_node.left else: parent.right = current_node.left else: successor = current_node.right while successor.left is not None: successor = successor.left current_node.customer_id = successor.customer_id current_node.year_of_expiry = successor.year_of_expiry def in_order_traversal(self, node): if node is not None: self.in_order_traversal(node.left) print(node.customer_id, node.year_of_expiry) self.in_order_traversal(node.right) def list_customer_details(tree): print("CustomerID, Year of Expiry") tree.in_order_traversal(tree.root) def list_customer_details_with_expiry_less_than_5_years(tree, current_year): print("Customer Details with Year of expiry < 5 years to the current year:") def traverse(node): if node is not None: traverse(node.left) if node.year_of_expiry < current_year + 5: print(node.customer_id, node.year_of_expiry) traverse(node.right) traverse(tree.root) def list_customer_details_with_expiry_greater_than_current_year(tree, current_year): print("Customer Details with Year of expiry > current year:") def traverse(node): if node is not None: traverse(node.left) if node.year_of_expiry > current_year: print(node.customer_id, node.year_of_expiry) traverse(node.right) traverse(tree.root) def list_customer_details_with_expiry_in_range(tree, start_year, end_year): print("Customer Details with Year of expiry in between", start_year, "and", end_year, "years:") def traverse(node): if node is not None: traverse(node.left) if start_year < node.year_of_expiry > end_year: print(node.customer_id, node.year_of_expiry) node.year_of_expiry += 13 traverse(node.right) traverse(tree.root) def main(): tree = BinaryTree() with open("inputPS10.txt", "r") as f: lines = f.readlines() for line in lines[1:]: customer_id, year_of_expiry = map(int, line.strip().split(",")) tree.insert(customer_id, year_of_expiry) with open("outputPS10.txt", "w") as f: sys.stdout = f print("These are the total of X number of customers") list_customer_details(tree) current_year = 2022 # update this to current year list_customer_details_with_expiry_less_than_5_years(tree, current_year) list def main(): tree = BinaryTree() with open("inputPS10.txt", "r") as f: lines = f.readlines() for line in lines[1:]: customer_id, year_of_expiry = map(int, line.strip().split(",")) tree.insert(customer_id, year_of_expiry) with open("outputPS10.txt", "w") as f: sys.stdout = f print("These are the total of X number of customers") list_customer_details(tree) current_year = 2022 # update this to current year list_customer_details_with_expiry_less_than_5_years(tree, current_year) list_customer_details_with_expiry_greater_than_current_year(tree, current_year) list_customer_details_with_expiry_in_range(tree, 2019, 2023) print("New list of customer details with updated years of all the nodes:") list_customer_details(tree) print("Updated customer details in In-order Traversal:") tree.in_order_traversal(tree.root) list if __name__ == "__main__": main()
--------------------------------------------------------------------------------------------
This condition not working need help
def list_customer_details_with_expiry_in_range(tree, start_year, end_year): print("Customer Details with Year of expiry in between", start_year, "and", end_year, "years:") def traverse(node): if node is not None: traverse(node.left) if start_year < node.year_of_expiry > end_year: print(node.customer_id, node.year_of_expiry) node.year_of_expiry += 13 traverse(node.right) traverse(tree.root)
--------------------------------------------------------------------------------------------------------
inputPS10.txt
CustomerID, Year of Expiry 61015, 2021 10987, 2020 11986, 2028 90673, 2035 48058, 2023 39009, 2016 76109, 2009 99013, 2015 46976, 2022 19822, 2036 34590, 2037 55628, 2028 10972, 2020 22091, 2014 29019, 2022 18654, 2020 19753, 2021 81456, 2022 60709, 2021
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started