Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using python #NODE CODE class Node: def __init__(self, id, gpa): self.gpa = gpa self.id = id self.left = None self.right = None def get_gpa(self): return

Using python

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

#NODE CODE

class Node: def __init__(self, id, gpa): self.gpa = gpa self.id = id self.left = None self.right = None def get_gpa(self): return self.gpa def get_id(self): return self.id def get_left(self): return self.left def get_right(self): return self.right def set_gpa(self, gpa): self.gpa = gpa def set_id(self, id): self.id = id def set_left(self, left): self.left = left def set_right(self, right): self.right = right def __str__(self): return "({},{})".format(self.id,self.gpa) 

#BST CODE

from Node import Node class BSTree: # constructor def __init__(self): self.Root = None def exist(self, id): return self.existRec(self.Root, id) def existRec(self, r, id): if r is None: return False elif r.get_id() == id: return True elif id > r.get_id(): return self.existRec(r.get_right(), id) else: return self.existRec(r.get_left(), id) def IterativeExist(self, id): temp = self.Root while temp is not None: if temp.get_id() == id: return True elif id > temp.get_id(): temp = temp.get_right() else: temp = temp.get_left() return False def insert(self, id, gpa): if self.exist(id) == True: return False temp = Node(id, gpa) if self.Root is None: self.Root = temp else: self.insertRec(self.Root, temp) return True def insertRec(self, r, temp): if temp.get_id()  key: prev = curr curr = curr.getLeft() elif curr.getID()  

# TEST CODE

from BSTree import BSTree bst = BSTree() while True: print() print(" Please, choose a number from the following list: ") print("1 - Insert Students ") print("2 - Remove a Student ") print("3 - Check if a Student Exists ") print("4 - Print inOrder") print("5 - exit") choice = int(input("Please enter your selection: ")) if choice == 1: id = int(input("Enter the student's details or -1 or stop ")) while id != -1: gpa = float(input("Enter the GPA: ")) if not (bst.insert(id, gpa)): print("Error! duplicate id number") id = int(input("Enter the student's details or -1 or stop ")) elif choice == 2: id = int(input("Enter the course number: ")) if bst.remove(id) is True: print("The student info was deleted from the tree") else: print("The course is not in the tree") elif choice == 3: id = int(input("Enter the course number: ")) res = bst.exist(id) if res is True: print("The student id is exist in the Tree") else: print("The student id does not exist in the Tree") elif choice == 4: bst.inorder() elif choice == 5: print("Exiting the program") break 

BY USING PYTHON WRITE THE NEW METHODS IN THE CODE ABOVE PLEASE

iclass Node: def _init__(self, id, gpa): self.gpa = gpa self.id = id self.left = None self.right = None def get_gpa(self): return self.gpa def get_id(self): return self.id def get_left(self): return self.left def get_right(self): return self.right def set_gpa (self, gpa): self.gpa = gpa def set_id(self, id): self.id = id def set_left(self, left): self.left = left def set_right(self, right): self.right = right def str_(self): return "C{}, {})".format(self.id, self.gpa) from Node import Node class BSTree: # constructor def _init__(self): e self.Root = None o def exist(self, id): return self.existRec(self.Root, id) - def existRec(self, r, id): A if r is None: return false elif r.get_id() == id: return True elif id > r.get_id(): return self.existRec(r.get_right(), id) else: return self.existRec(r.get_left(), id) A def IterativeExist(self, id): temp = self. Root while temp is not None: if temp.get_id() == id: return True elif id > temp.get_id(): temp = temp.get_right() else: temp temp.get_left() return false def insert(self, id, gpa): A if self.exist(id) == True: return false temp Node(id, gpa) if self.Root is None: self.Root = temp else: self.insertRec(self.Root, temp) return True def insertRec(self, r, temp): if temp.get_id() key: prev = curr curr = curr.getLeft() elif curr.getID() key: prev = curr curr = curr.getRight() else: break # 3. check if there exists a successor to the node if curr.getRight() is None: # 4. check if the node is the root if curr is self.Root: self.Root = self.Root.getLeft() return True # node is internal to the tree if prev.getLeft() is curr: prev.setLeft(curr.getLeft() else: prev.setRight(curr.getLeft() curr.setLeft(None) return True # 5. find inorder successor prev_SUCC, SUCC = curr, curr.getRight() while succ.getLeft(): prev_SUCC = SUCC SUCC = succ.getLeft() # 6. swap the values curr.setID(succ.getID)) curr.setGPA(SUCC.getGPA()) # 7. handle the right child of the successor if prev_succ is curr: prev_succ.setRight(succ.getRight()) else: prev_succ.setLeft(succ.getRight() SUCC.setRight(None) return True from BSTree import BSTree bst = BSTree() while True: print() print(" Please, choose a number from the following list: ") print("1 - Insert Students ") print("2 - Remove a Student ") print("3 - Check if a Student Exists") print("4 - Print inorder") print("5 - exit") choice = int(input("Please enter your selection: ")) if choice == 1: id = int(input("Enter the student's details or -1 or stop ")) while id != -1: gpa - float(input("Enter the GPA: )) if not (bst.insert(id, gpa)): print("Error! duplicate id number") id = int(input("Enter the student's details or -1 or stop ")) elif choice == 2: id = int(input("Enter the course number: ")) if bst.remove(id) is True: print("The student info was deleted from the tree") else: print("The course is not in the tree") elif choice == 3: id = int(input("Enter the course number: ")) res = bst.exist(id) if res is True: print("The student id is exist in the Tree") else: print("The student id does not exist in the Tree") elif choice == 4: bst.inorder() elif choice == 5: print("Exiting the program") break Add the following methods to class BSTree: preOder() preOrderRec (root) Traverses and prints the contents of the tree pre-order according to the ID. postOder() postOrderRec (root) Traverses and prints the contents of the tree post-order according to the ID. height() heightRec (root) Returns the height of the tree. countLeafNodes o countLeafNodesRec (root) Returns the number of leaf nodes in the tree. Write the expected time and space complexity as a comment at the beginning of each method of your class. Modify the test program form lab 7 to do the following: The program can perform the following: 1. Insert Students 2- Remove a student 3- Check if a student Exists 4- Print inorder 5. Print preorder 6- Print postorder 7- Height 8- Number of leaf nodes 9- Exit Please enter your selection: Sample Output Please, choose a number from the following list: 1 - Insert Students 2 - Remove a Student 3 - Check if a Student Exists 4 - Print inOrder 5 - Print preOrder 6 - Print postOrder 7 - Height 8 - Number of leaf nodes 9 - exit Your choice is: 1 Enter the student's details or -l or stop Enter a student's ID: 117 Enter the GPA: 2.3 Enter a student's ID: 113 Enter the GPA: 3.1 Enter a student's ID: 121 Enter the GPA: 4.0 Enter a student's ID: 112 Enter the GPA: 3.8 Enter a student's ID: 118 Enter the GPA: 1.9 Enter a student's ID: 119 Enter the GPA: 3.3 Enter a student's ID: 116 Enter the GPA: 2.8 Enter a student's ID: 115 Enter the GPA: 3.5 Enter a student's ID: 122 Enter the GPA: 2.6 Enter a student's ID: - Please, choose a number from the following list: 1 - Insert Students 2 - Remove a Student 3 - Check if a Student Exists 4 - Print inOrder 5 - Print preOrder 6 - Print postOrder 7 - Height 8 - Number of leaf nodes 9 - exit Your choice is: 4 (112,3.8) (113, 3.1) (115, 3.5) (116,2.8) (117,2.3) (118,1.9) (119, 3.3) 1, 4.0 (122,2.6) Please, choose a number from the following list: 1 - Insert Students 2 - Remove a Student 3 - Check if a Student Exists 4 - Print inOrder 5 - Print preOrder 6 - Print postOrder 7 - Height 8 - Number of leaf nodes 9 - exit Your choice is: 5 (117,2.3) (113, 3.1) (112,3.8) (116,2.8) (115, 3.5) (121, 4.0) (118,1.9) (119,3.3) (122,2.6) - Please, choose a number from the following list: 1 - Insert Students 2 Remove a Student 3 3 - Check if a Student Exists 4 - Print in order 5 - Print preOrder 6 - Print postOrder 7 - Height 8 - Number of leaf nodes 9 - exit Your choice is: 6 (112,3.8) (115, 3.5) (116,2.8) (113, 3.1) (119, 3.3) (118,1.9) (122,2.6) (121, 4.0) (117,2.3) Please, choose a number from the following list: 1 - Insert Students 2 Remove a Student 3 - Check if a Student Exists

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_2

Step: 3

blur-text-image_3

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

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions