Answered step by step
Verified Expert Solution
Link Copied!

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

getting wrong output

Customer Details with Year of expiry in between 2019 and 2023 years: 11986 2028 55628 2028 90673 2035 19822 2036 34590 2037

it should be in 2019 to 2023

61015, 2021 10987, 2020 46976, 2022 10972, 2020 29019, 2022 18654, 2020 19753, 2021 81456, 2022 60709, 2021

Customer Details with Year of expiry < 5 years to the current year 39009, 2016 76109, 2009 99013, 2015 22091, 2014 Deleted node with customer ID: 39009 Deleted node with customer ID: 76109 Deleted node with customer ID: 99013 Deleted node with customer ID: 22091

Please need urgent solution

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions

Question

(a) What is the complete defining relation?

Answered: 1 week ago

Question

Define the goals of persuasive speaking

Answered: 1 week ago