📅  最后修改于: 2023-12-03 14:58:22.896000             🧑  作者: Mango
【题目简介】 本次mock考试为计算机科学门考试的第37章,主要涉及数据结构和算法相关的知识点。以下是该次考试的题目简介和要求,希望大家认真复习,准备好迎接挑战!
BST是一种常见的数据结构,是一种支持快速查找、插入和删除的结构。请写一个简单的BST程序,要求实现以下功能:
插入(insert)
:将一个元素插入到BST中删除(delete)
:删除指定元素搜索(search)
:查找一个指定元素打印(print)
:按照中序遍历,打印树中所有元素请使用列表实现该结构,并完成上述操作。
# 以下是BST的实现代码
class BST:
def __init__(self, val=None):
self.val = val
self.left = None
self.right = None
def insert(self, val):
if self.val is None:
self.val = val
return
if val < self.val:
if self.left:
self.left.insert(val)
else:
self.left = BST(val)
else:
if self.right:
self.right.insert(val)
else:
self.right = BST(val)
def search(self, val):
if self.val is None:
return False
if val == self.val:
return True
elif val < self.val:
if self.left:
return self.left.search(val)
else:
return False
else:
if self.right:
return self.right.search(val)
else:
return False
def inorder(self):
if self.left:
self.left.inorder()
if self.val is not None:
print(self.val, end=' ')
if self.right:
self.right.inorder()
def delete(self, val):
if self.val is None:
return False
if self.val == val:
if self.left is None:
self.val = self.right.val
self.left = self.right.left
self.right = self.right.right
elif self.right is None:
self.val = self.left.val
self.left = self.left.left
self.right = self.left.right
else:
self.val = self.left.maximum()
self.left.delete(self.val)
elif val < self.val:
if self.left:
self.left.delete(val)
else:
if self.right:
self.right.delete(val)
def maximum(self):
if self.right is None:
return self.val
else:
return self.right.maximum()
哈希表是一种非常高效的数据结构,用于存储和查找数据。请写一个使用链表法解决冲突的哈希表程序,要求实现以下功能:
插入(insert)
:将一个元素插入到哈希表中删除(delete)
:删除指定元素搜索(search)
:查找一个指定元素请使用字典实现该结构,并完成上述操作。
# 以下是哈希表的实现代码
class HashTable:
def __init__(self):
self.size = 11
self.slots = [None] * self.size
self.data = [None] * self.size
def hashfunction(self, key, size):
return key % size
def rehash(self, oldhash, size):
return (oldhash + 1) % size
def put(self, key, data):
hashvalue = self.hashfunction(key, len(self.slots))
if self.slots[hashvalue] is None:
self.slots[hashvalue] = key
self.data[hashvalue] = data
else:
if self.slots[hashvalue] == key:
self.data[hashvalue] = data # 替换原来的值
else:
nextslot = self.rehash(hashvalue, len(self.slots))
while self.slots[nextslot] is not None and self.slots[nextslot] != key:
nextslot = self.rehash(nextslot, len(self.slots))
if self.slots[nextslot] is None:
self.slots[nextslot] = key
self.data[nextslot] = data
else:
self.data[nextslot] = data
def get(self, key):
startslot = self.hashfunction(key, len(self.slots))
data = None
stop = False
found = False
position = startslot
while self.slots[position] is not None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position = self.rehash(position, len(self.slots))
if position == startslot:
stop = True
return data
def remove(self, key):
startslot = self.hashfunction(key, len(self.slots))
found = False
position = startslot
while self.slots[position] is not None and not found:
if self.slots[position] == key:
found = True
self.slots[position] = None
self.data[position] = None
else:
position = self.rehash(position, len(self.slots))
快速排序是一种高效的排序算法,它使用分治策略来排序元素。请写一个快速排序算法程序,要求实现以下功能:
# 以下是随机化快排算法的实现代码
import random
def quicksort(arr, start, end):
if start < end:
pivot = partition(arr, start, end)
quicksort(arr, start, pivot - 1)
quicksort(arr, pivot + 1, end)
def partition(arr, start, end):
pivot = random.randint(start, end)
arr[pivot], arr[end] = arr[end], arr[pivot]
i = start - 1
for j in range(start, end):
if arr[j] < arr[end]:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[end] = arr[end], arr[i+1]
return i+1
请写一个Python函数来计算二叉树的最大深度。
# 以下是计算二叉树最大深度的实现代码
class Tree:
def __init__(self, val=None):
self.val = val
self.left = None
self.right = None
def max_depth(tree):
if tree is None:
return 0
left_depth = max_depth(tree.left)
right_depth = max_depth(tree.right)
return max(left_depth, right_depth) + 1
# 可以通过以下方式来构造一棵二叉树进行测试
"""
3
/ \
9 20
/ \
15 7
"""
tree = Tree(3)
tree.left = Tree(9)
tree.right = Tree(20)
tree.right.left = Tree(15)
tree.right.right = Tree(7)
print(max_depth(tree)) # output: 3
以上是本次考试的题目和要求,希望大家认真复习,准备好迎接挑战!