Lab 5
● Declare and implement a BSTNode ADT with a data attribute and two pointe
attributes, one for the left child and the other for the right child. Implement the
usual getters/setters for these attributes.
● Declare and implement a BST as a link-based ADT whose data will be Money
objects - the data will be inserted based on the actual money value of you
MOney objects as a combination of the whole value and fractional value
attributes.
● For the BST, implement the four traversal methods as well as methods for the
usual search, insert, delete, print, count, isEmpty, empty operations and any
other needed.
● Your pgm will use the following 20 Money (think of them in Dollar terms)
objects to be created in the exact order in your main to seed the tree:
1. $57.12
2. $23.44
3. $87.43
4. $68.99
5. $111.22
6. $44.55
7. $77.77
8. $18.36
9. $543.21
10.$20.21
11. $345.67
12.$36.18
13.$48.48
14.$101.00
15.$11.00
16.$21.00
17.$51.00
18.$1.00
19.$251.00
20.$151.00
● Also, create an output file to write program output as specified in one o
more instructions below.
● After seeding the data, perform your traversal operations in the specific
sequence of
eadth-first, in-order, pre-order, post-order, ensuring that output
is written out to both screen and file concu
ently.
● Then provide interactivity for the user to add/search/delete nodes from the
console after the data has been seeded into the application.
● Perform adequate input data validation when reading data from the user into
the tree - if any data item is invalid, ignore the data item and continue to next
item but print a message to output (both screen and same output file) to
indicate which data items were ignored.
● Also, provide the user the option to print output of traversals or exit the
program. Once the user selects the option to print data or exits the program,
the data in the BST should be printed out to both screen and output file in all
four traversal methods in the specific sequence of
eadth-first, in-order,
pre-order, post-order.
● demonstrate use of your Lab 4 queue without any modifications for the
tree traversals
Lab 4 Tannaz Anvari/IDE Test #1.jpg
Lab 4 Tannaz Anvari/IDE Test #2.jpg
Lab 4 Tannaz Anvari/linkedlist.py
"""
CIS 22C Summer Quarte
Professor: Manish Goel
Student: Tannaz Anvari
**********
Lab 4
Started date: 7/21/2021
Final submission date: 07/26/2021
"""
"""
A Singly Linked List class which will be composed of three attributes - a count attribute, a LinkedNode
pointe
eference attribute pointing to the start of the list and a LinkedNode pointe
eference attribute pointing
to the end of the list. Since this is a class, make sure all these attributes are private
"""
# LinkedNode Class Starts
class LinkedNode:
# Constructo
def __init__(self, data):
XXXXXXXXXXself.data = data
XXXXXXXXXXself.next = None
# Getters
def get_data(self):
XXXXXXXXXXreturn self.data
def get_next(self):
XXXXXXXXXXreturn self.next
# Setters
def set_data(self, data):
XXXXXXXXXXself.data = data
def set_next(self, next):
XXXXXXXXXXself.next = next
XXXXXXXXXXdef __str__(self):
XXXXXXXXXXreturn str(self.data)
# LinkedNode Class Ends
# SinglyLinkedList Class Starts
class SinglyLinkedList:
# Constructo
def __init__(self):
XXXXXXXXXXself.count = 0
XXXXXXXXXXself.start = self.end = None
# Getters
def get_count(self):
XXXXXXXXXXreturn self.count
def get_start(self):
XXXXXXXXXXreturn self.start
def get_end(self):
XXXXXXXXXXreturn self.end
# Setters
def set_count(self, count):
XXXXXXXXXXself.count = count
def set_start(self, start):
XXXXXXXXXXself.start = start
def set_end(self, end):
XXXXXXXXXXself.end = end
# Other methods
def add_to_front(self, data):
XXXXXXXXXXnode = LinkedNode(data)
XXXXXXXXXXif self.count == 0: # List is empty
XXXXXXXXXXself.start = node
XXXXXXXXXXself.end = node
XXXXXXXXXXelse: # List is not empty
XXXXXXXXXXnode.set_next(self.start)
XXXXXXXXXXself.start = node
XXXXXXXXXXself.count += 1
def add_to_end(self, data):
XXXXXXXXXXnode = LinkedNode(data)
XXXXXXXXXXif self.count == 0: # List is empty
XXXXXXXXXXself.start = node
XXXXXXXXXXself.end = node
XXXXXXXXXXelse:
XXXXXXXXXXself.end.set_next(node)
XXXXXXXXXXself.end = node
XXXXXXXXXXself.count += 1
def get_front_data(self):
XXXXXXXXXXif self.count == 0:
XXXXXXXXXXreturn None
XXXXXXXXXXreturn self.start.get_data()
def get_end_data(self):
XXXXXXXXXXif self.count == 0:
XXXXXXXXXXreturn None
XXXXXXXXXXreturn self.end.get_data()
def is_empty(self):
XXXXXXXXXXreturn self.count == 0
def delete_from_front(self):
XXXXXXXXXXif self.count == 0:
XXXXXXXXXXreturn
XXXXXXXXXXelif self.count == 1:
XXXXXXXXXXself.start = self.end = None
XXXXXXXXXXelse:
XXXXXXXXXXself.start = self.start.get_next()
XXXXXXXXXXself.count -= 1
def find_data(self, data):
XXXXXXXXXXcu
= self.start
XXXXXXXXXXwhile cu
is not None:
XXXXXXXXXXd = cu
.get_data()
XXXXXXXXXXif d == data:
XXXXXXXXXXreturn True
XXXXXXXXXXcu
= cu
.get_next()
XXXXXXXXXXreturn False
def __str__(self):
XXXXXXXXXXcu
= self.start
# The attribute names for the Node and Linked List are the words in bold in #1 and #2
XXXXXXXXXXs = "SLL with " + str(self.count) + " nodes: "
XXXXXXXXXXwhile cu
is not None:
XXXXXXXXXXd = cu
.get_data()
XXXXXXXXXXs = s + str(d) + " -> "
XXXXXXXXXXcu
= cu
.get_next()
XXXXXXXXXXreturn s
# SinglyLinkedList Class Ends
Lab 4 Tannaz Anvari/main.py
"""
CIS 22C Summer Quarte
Professor: Manish Goel
Student: Tannaz Anvari
**********
Lab 4
Started date: 7/21/2021
Final submission date: 07/26/2021
"""
# importing files
import linkedlist as LL
import queue as Q
import stack as S
import money as M
import random
# main function definition
def main():
LinkedList_tests()
Stack_tests()
Queue_tests()
# definition of all the test methods
def create_test_data():
data = []
# Create seven (7) Node objects of Money class to be used in the program.
for i in range(7):
XXXXXXXXXXamount = random.randint(1, 100)
XXXXXXXXXXm = M.Money(amount)
XXXXXXXXXXdata.append(m)
return data
def LinkedList_tests():
File = open('outcome.txt', 'a')
print("Linked List tests")
File.write("Linked List tests\n")
data = create_test_data()
L = LL.SinglyLinkedList()
L.add_to_front(data[0])
print(str(L))
File.write(str(L)+'\n')
L.add_to_front(data[1])
print(str(L))
File.write(str(L)+'\n')
L.add_to_end(data[2])
print(str(L))
File.write(str(L)+'\n')
print("The number at the front of the list: " + str(L.get_front_data()))
File.write("The number at the front of the list: " + str(L.get_front_data())+'\n')
print("The number at the end of the list: " + str(L.get_end_data()))
File.write("The number at the end of the list: " + str(L.get_end_data())+'\n')
L.delete_from_front()
print(str(L))
File.write(str(L)+'\n')
L.add_to_end(data[3])
print(str(L))
File.write(str(L)+'\n')
print("count = " + str(L.get_count()))
File.write("count = " + str(L.get_count())+'\n')
print("_" * 50)
File.write(("_" * 50)+'\n')
File.close()
def Stack_tests():
File = open('outcome.txt', 'a')
print("Stack tests")
File.write("Stack tests\n")
s