Modify the exception handling in Java and Python.
This commit is contained in:
parent
7e59e2c7fb
commit
3590262c7e
@ -90,7 +90,7 @@ class MaxHeap {
|
|||||||
public int pop() {
|
public int pop() {
|
||||||
// 判空处理
|
// 判空处理
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
// 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||||
swap(0, size() - 1);
|
swap(0, size() - 1);
|
||||||
// 删除节点
|
// 删除节点
|
||||||
|
@ -89,14 +89,14 @@ class ArrayDeque {
|
|||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
public int peekFirst() {
|
public int peekFirst() {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return nums[front];
|
return nums[front];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问队尾元素 */
|
/* 访问队尾元素 */
|
||||||
public int peekLast() {
|
public int peekLast() {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
// 计算尾元素索引
|
// 计算尾元素索引
|
||||||
int last = index(front + queSize - 1);
|
int last = index(front + queSize - 1);
|
||||||
return nums[last];
|
return nums[last];
|
||||||
|
@ -60,7 +60,7 @@ class ArrayQueue {
|
|||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return nums[front];
|
return nums[front];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,14 +35,14 @@ class ArrayStack {
|
|||||||
/* 出栈 */
|
/* 出栈 */
|
||||||
public int pop() {
|
public int pop() {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return stack.remove(size() - 1);
|
return stack.remove(size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 访问栈顶元素 */
|
/* 访问栈顶元素 */
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return stack.get(size() - 1);
|
return stack.get(size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ class LinkedListQueue {
|
|||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (size() == 0)
|
if (size() == 0)
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return front.val;
|
return front.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ class LinkedListStack {
|
|||||||
/* 访问栈顶元素 */
|
/* 访问栈顶元素 */
|
||||||
public int peek() {
|
public int peek() {
|
||||||
if (size() == 0)
|
if (size() == 0)
|
||||||
throw new EmptyStackException();
|
throw new IndexOutOfBoundsException();
|
||||||
return stackPeek.val;
|
return stackPeek.val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,12 +26,14 @@ class MyList:
|
|||||||
def get(self, index: int) -> int:
|
def get(self, index: int) -> int:
|
||||||
"""访问元素"""
|
"""访问元素"""
|
||||||
# 索引如果越界则抛出异常,下同
|
# 索引如果越界则抛出异常,下同
|
||||||
assert index >= 0 and index < self.__size, "索引越界"
|
if index < 0 or index >= self.__size:
|
||||||
|
raise IndexError("索引越界")
|
||||||
return self.__nums[index]
|
return self.__nums[index]
|
||||||
|
|
||||||
def set(self, num: int, index: int) -> None:
|
def set(self, num: int, index: int) -> None:
|
||||||
"""更新元素"""
|
"""更新元素"""
|
||||||
assert index >= 0 and index < self.__size, "索引越界"
|
if index < 0 or index >= self.__size:
|
||||||
|
raise IndexError("索引越界")
|
||||||
self.__nums[index] = num
|
self.__nums[index] = num
|
||||||
|
|
||||||
def add(self, num: int) -> None:
|
def add(self, num: int) -> None:
|
||||||
@ -44,7 +46,8 @@ class MyList:
|
|||||||
|
|
||||||
def insert(self, num: int, index: int) -> None:
|
def insert(self, num: int, index: int) -> None:
|
||||||
"""中间插入元素"""
|
"""中间插入元素"""
|
||||||
assert index >= 0 and index < self.__size, "索引越界"
|
if index < 0 or index >= self.__size:
|
||||||
|
raise IndexError("索引越界")
|
||||||
# 元素数量超出容量时,触发扩容机制
|
# 元素数量超出容量时,触发扩容机制
|
||||||
if self.__size == self.capacity():
|
if self.__size == self.capacity():
|
||||||
self.extend_capacity()
|
self.extend_capacity()
|
||||||
@ -57,7 +60,8 @@ class MyList:
|
|||||||
|
|
||||||
def remove(self, index: int) -> int:
|
def remove(self, index: int) -> int:
|
||||||
"""删除元素"""
|
"""删除元素"""
|
||||||
assert index >= 0 and index < self.__size, "索引越界"
|
if index < 0 or index >= self.__size:
|
||||||
|
raise IndexError("索引越界")
|
||||||
num = self.__nums[index]
|
num = self.__nums[index]
|
||||||
# 索引 i 之后的元素都向前移动一位
|
# 索引 i 之后的元素都向前移动一位
|
||||||
for j in range(index, self.__size - 1):
|
for j in range(index, self.__size - 1):
|
||||||
@ -114,5 +118,5 @@ if __name__ == "__main__":
|
|||||||
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||||
my_list.add(i)
|
my_list.add(i)
|
||||||
print(
|
print(
|
||||||
"扩容后的列表 {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}"
|
f"扩容后的列表 {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}"
|
||||||
)
|
)
|
||||||
|
@ -30,7 +30,7 @@ class GraphAdjList:
|
|||||||
def add_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
def add_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
||||||
"""添加边"""
|
"""添加边"""
|
||||||
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
||||||
raise ValueError
|
raise ValueError()
|
||||||
# 添加边 vet1 - vet2
|
# 添加边 vet1 - vet2
|
||||||
self.adj_list[vet1].append(vet2)
|
self.adj_list[vet1].append(vet2)
|
||||||
self.adj_list[vet2].append(vet1)
|
self.adj_list[vet2].append(vet1)
|
||||||
@ -38,7 +38,7 @@ class GraphAdjList:
|
|||||||
def remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
def remove_edge(self, vet1: Vertex, vet2: Vertex) -> None:
|
||||||
"""删除边"""
|
"""删除边"""
|
||||||
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:
|
||||||
raise ValueError
|
raise ValueError()
|
||||||
# 删除边 vet1 - vet2
|
# 删除边 vet1 - vet2
|
||||||
self.adj_list[vet1].remove(vet2)
|
self.adj_list[vet1].remove(vet2)
|
||||||
self.adj_list[vet2].remove(vet1)
|
self.adj_list[vet2].remove(vet1)
|
||||||
@ -53,7 +53,7 @@ class GraphAdjList:
|
|||||||
def remove_vertex(self, vet: Vertex) -> None:
|
def remove_vertex(self, vet: Vertex) -> None:
|
||||||
"""删除顶点"""
|
"""删除顶点"""
|
||||||
if vet not in self.adj_list:
|
if vet not in self.adj_list:
|
||||||
raise ValueError
|
raise ValueError()
|
||||||
# 在邻接表中删除顶点 vet 对应的链表
|
# 在邻接表中删除顶点 vet 对应的链表
|
||||||
self.adj_list.pop(vet)
|
self.adj_list.pop(vet)
|
||||||
# 遍历其他顶点的链表,删除所有包含 vet 的边
|
# 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||||
|
@ -73,7 +73,8 @@ class MaxHeap:
|
|||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""元素出堆"""
|
"""元素出堆"""
|
||||||
# 判空处理
|
# 判空处理
|
||||||
assert not self.is_empty()
|
if self.is_empty():
|
||||||
|
raise IndexError("堆为空")
|
||||||
# 交换根节点与最右叶节点(即交换首元素与尾元素)
|
# 交换根节点与最右叶节点(即交换首元素与尾元素)
|
||||||
self.swap(0, self.size() - 1)
|
self.swap(0, self.size() - 1)
|
||||||
# 删除节点
|
# 删除节点
|
||||||
|
@ -72,12 +72,14 @@ class ArrayDeque:
|
|||||||
|
|
||||||
def peek_first(self) -> int:
|
def peek_first(self) -> int:
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
assert not self.is_empty(), "双向队列为空"
|
if self.is_empty():
|
||||||
|
raise IndexError("双向队列为空")
|
||||||
return self.__nums[self.__front]
|
return self.__nums[self.__front]
|
||||||
|
|
||||||
def peek_last(self) -> int:
|
def peek_last(self) -> int:
|
||||||
"""访问队尾元素"""
|
"""访问队尾元素"""
|
||||||
assert not self.is_empty(), "双向队列为空"
|
if self.is_empty():
|
||||||
|
raise IndexError("双向队列为空")
|
||||||
# 计算尾元素索引
|
# 计算尾元素索引
|
||||||
last = self.index(self.__front + self.__size - 1)
|
last = self.index(self.__front + self.__size - 1)
|
||||||
return self.__nums[last]
|
return self.__nums[last]
|
||||||
|
@ -28,9 +28,10 @@ class ArrayQueue:
|
|||||||
|
|
||||||
def push(self, num: int) -> None:
|
def push(self, num: int) -> None:
|
||||||
"""入队"""
|
"""入队"""
|
||||||
assert self.__size < self.capacity(), "队列已满"
|
if self.__size == self.capacity():
|
||||||
|
raise IndexError("队列已满")
|
||||||
# 计算尾指针,指向队尾索引 + 1
|
# 计算尾指针,指向队尾索引 + 1
|
||||||
# 通过取余操作,实现 rear 越过数组尾部后回到头部
|
# 通过取余操作,实现 rear 越过数组尾部后回到头部F
|
||||||
rear: int = (self.__front + self.__size) % self.capacity()
|
rear: int = (self.__front + self.__size) % self.capacity()
|
||||||
# 将 num 添加至队尾
|
# 将 num 添加至队尾
|
||||||
self.__nums[rear] = num
|
self.__nums[rear] = num
|
||||||
@ -46,7 +47,8 @@ class ArrayQueue:
|
|||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问队首元素"""
|
"""访问队首元素"""
|
||||||
assert not self.is_empty(), "队列为空"
|
if self.is_empty():
|
||||||
|
raise IndexError("队列为空")
|
||||||
return self.__nums[self.__front]
|
return self.__nums[self.__front]
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
|
@ -26,12 +26,14 @@ class ArrayStack:
|
|||||||
|
|
||||||
def pop(self) -> int:
|
def pop(self) -> int:
|
||||||
"""出栈"""
|
"""出栈"""
|
||||||
assert not self.is_empty(), "栈为空"
|
if self.is_empty():
|
||||||
|
raise IndexError("栈为空")
|
||||||
return self.__stack.pop()
|
return self.__stack.pop()
|
||||||
|
|
||||||
def peek(self) -> int:
|
def peek(self) -> int:
|
||||||
"""访问栈顶元素"""
|
"""访问栈顶元素"""
|
||||||
assert not self.is_empty(), "栈为空"
|
if self.is_empty():
|
||||||
|
raise IndexError("栈为空")
|
||||||
return self.__stack[-1]
|
return self.__stack[-1]
|
||||||
|
|
||||||
def to_list(self) -> list[int]:
|
def to_list(self) -> list[int]:
|
||||||
|
@ -39,4 +39,3 @@ if __name__ == "__main__":
|
|||||||
# 层序遍历
|
# 层序遍历
|
||||||
res: list[int] = level_order(root)
|
res: list[int] = level_order(root)
|
||||||
print("\n层序遍历的节点打印序列 = ", res)
|
print("\n层序遍历的节点打印序列 = ", res)
|
||||||
assert res == [1, 2, 3, 4, 5, 6, 7]
|
|
||||||
|
@ -52,16 +52,13 @@ if __name__ == "__main__":
|
|||||||
res = []
|
res = []
|
||||||
pre_order(root)
|
pre_order(root)
|
||||||
print("\n前序遍历的节点打印序列 = ", res)
|
print("\n前序遍历的节点打印序列 = ", res)
|
||||||
assert res == [1, 2, 4, 5, 3, 6, 7]
|
|
||||||
|
|
||||||
# 中序遍历
|
# 中序遍历
|
||||||
res.clear()
|
res.clear()
|
||||||
in_order(root)
|
in_order(root)
|
||||||
print("\n中序遍历的节点打印序列 = ", res)
|
print("\n中序遍历的节点打印序列 = ", res)
|
||||||
assert res == [4, 2, 5, 1, 6, 3, 7]
|
|
||||||
|
|
||||||
# 后序遍历
|
# 后序遍历
|
||||||
res.clear()
|
res.clear()
|
||||||
post_order(root)
|
post_order(root)
|
||||||
print("\n后序遍历的节点打印序列 = ", res)
|
print("\n后序遍历的节点打印序列 = ", res)
|
||||||
assert res == [4, 5, 2, 6, 7, 3, 1]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user