# 02-CISC-235 Data Structures W23「Assignment 2」

AI悦创原创1v1Python 1v1python数据结构与算法一对一辅导1v1Python 1v1python数据结构与算法一对一辅导大约 7 分钟...约 2195 字

## # Assignment 2

February 14, 2023

## # General Instructions

Write your own program(s) using Python. Once you complete your assignment, place all Python files in a zip file and name it according to the same method, i.e., “235-1234-Assn2.zip”. Unzip this file should get all your Python file(s).

Then upload 235-1234-Assn2.zip into Assignment 2’s entry on onQ. You may upload several times if you wish. However, onQ keeps only the last uploaded file. The newly uploaded file will overwrite the old file. Please check your files after uploading. We will check the latest submission you made following the required naming.

You must ensure your code is executable and document your code to help TA mark your solution. We suggest you follow PEP81 style to improve the readability of your code.

All data structures involved must be implemented by yourself, except for the built-in data types, i.e., List in Python.

An “I uploaded the wrong file” excuse will result in a mark of zero.

## # 1. Binary Search Tree (55 points)

Binary search tree (BST) is a special type of binary tree that satisfies the binary search property, i.e., the key in each node must be greater than any key stored in the left sub-tree, and less than any key stored in the right sub-tree.

Your task is to implement a BST class, satisfying the following requirements (you can create more methods/attributes if needed):

1. (5 points) Must have an insert (self, value) function that inserts a new node with a given value into the BST. You may assume that the values to be stored in the tree are integers.

1)(5点)必须有一个insert (self, value)函数插入一个 new 将给定值的节点放入 BST中。您可以假定值为被存储在树中的都是整数。

1. (10 points) Must have a get total height(self) function that computes the sum of the heights of all nodes in the tree. Your get total height function should run in O(n) time in the worst case, where n refers to the total number of nodes in the tree.

(10点)必须有一个获取总高度(self)函数，计算树中所有节点的高度之和。在最坏的情况下，get总高度函数应该在O(n)时间内运行，其中n指的是树中的节点总数。

1. (15 points) Must have a delete(self, value) function that could be used to delete one node from the BST by its value, recursively.

3)(15分)必须有一个可以使用的删除(self, value)功能递归地从BST中删除一个节点。

1. (20 points) Write a save(self) and a restore(self, input string) function for your BST class. These two functions can transfer a BST into a string and reconstruct it back to the same tree.

4)(20分)为你的BST类写一个save(self)和restore(self, input string)函数。这两个函数可以将BST转换为 字符串并将其重建回同一棵树。

1. (5 points) Write test code in the main function, covering all functions mentioned above.

5)(5分)在主功能中编写测试代码，覆盖所有功能上面提到的。

## # 2. AVLTreeMap: A Modified AVL Tree (45 points)

AVLTreeMap:修改后的AVL树(45点)

AVL Tree is one type of BST that ensures its balance during insertion/deletion. Your task is to implement a special AVL tree in a class named AVLTreeMap. This AVLTreeMap should have a load from file(self, file path) function. This function aims to read content from a file and save word-frequency information for all words appearing in the file in the AVLTreeMap.

AVL树是一种BST，它保证了插入/删除过程中的平衡。你的任务是在一个名为AVLTreeMap的类中实现一个特殊的AVL树。这个AVLTreeMap应该有一个从文件(self, file path)加载函数。这函数的目的是从文件中读取内容并保存词频信息对于AVLTreeMap文件中出现的所有单词。

Specifically, load from file function takes a file path as input, reads lines from the file, and extracts word tokens appearing in lines following their appearance order in the file. Next, it inserts extracted tokens one by one into an empty AVLTreeMap (you should empty the current AVLTreeMap each time before adding content from the file). Each AVLTreeMap node contains five attributes: leftchild, rightchild, word, frequency, and height.

For instance, if a given document contains a sentence ”Binary search tree is a special binary tree.”. load from file function will first get a list of meaningful words, ”binary”, ”search”, ”tree”, ”special”, ”binary”, ”tree” using the following supporting functions (you need to modify the code and add them to your AVLTreeMap class, e.g., adding self):

``````import re
import string
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
def parse_file(file):
with open(file, 'r') as input:
preprocessed = []
for line in content:
line = line.strip().lower()
#remove punctuation
line = line.translate(str.maketrans('', '', string.punctuation))
#remove stop words that care no specific meaning
line = remove_stopwords(line)
#remove numbers
line = re.sub('\d+','', line)
#remove extra white space
line = re.sub(' +', ' ', line)
if line:
preprocessed.extend(line.split(" "))
print(" ".join(preprocessed))
return preprocessed
def remove_stopwords(text):
return " ".join([word for word in str(text).split() if word not in stop_words])
``````

Then we scan the list from the first word and insert them one by one, the first node inserted would contain “binary” as the word, 1 as the frequency. When we see the second ”binary” in the list, since the node having word = ”binary” already exists, we will just update the frequency attribute of the root node to be 2. The final AVLTreeMap can be presented in Figure 1. Word ”binary” and ”tree” has a frequency = 2 because they appear twice in the input file.

You should implement other supporting functions to ensure the accuracy of attribute values in each node in the AVLTreeMap. In the main function, test your AVLTreeMap.

40 points for the implementation of required AVLTreeMap class and 5 points for the testing code in the main function. Your test file should be packed together with your python code in the zipped submission.

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度