Skip to content

Commit

Permalink
Several enhancements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Sep 13, 2023
1 parent a9d70e9 commit d190dbf
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 70 deletions.
6 changes: 3 additions & 3 deletions codes/cpp/chapter_heap/my_heap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class MaxHeap {
}

/* 判断堆是否为空 */
bool empty() {
bool isEmpty() {
return size() == 0;
}

Expand All @@ -98,7 +98,7 @@ class MaxHeap {
/* 元素出堆 */
void pop() {
// 判空处理
if (empty()) {
if (isEmpty()) {
throw out_of_range("堆为空");
}
// 交换根节点与最右叶节点(即交换首元素与尾元素)
Expand Down Expand Up @@ -149,7 +149,7 @@ int main() {
cout << "\n堆元素数量为 " << size << endl;

/* 判断堆是否为空 */
bool isEmpty = maxHeap.empty();
bool isEmpty = maxHeap.isEmpty();
cout << "\n堆是否为空 " << isEmpty << endl;

return 0;
Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_stack_and_queue/array_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ArrayQueue {
}

/* 判断队列是否为空 */
bool empty() {
bool isEmpty() {
return size() == 0;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ class ArrayQueue {

/* 访问队首元素 */
int peek() {
if (empty())
if (isEmpty())
throw out_of_range("队列为空");
return nums[front];
}
Expand Down Expand Up @@ -110,7 +110,7 @@ int main() {
cout << "队列长度 size = " << size << endl;

/* 判断队列是否为空 */
bool empty = queue->empty();
bool empty = queue->isEmpty();
cout << "队列是否为空 = " << empty << endl;

/* 测试环形数组 */
Expand Down
8 changes: 4 additions & 4 deletions codes/cpp/chapter_stack_and_queue/array_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ArrayStack {
}

/* 判断栈是否为空 */
bool empty() {
return stack.empty();
bool isEmpty() {
return stack.size() == 0;
}

/* 入栈 */
Expand All @@ -35,7 +35,7 @@ class ArrayStack {

/* 访问栈顶元素 */
int top() {
if (empty())
if (isEmpty())
throw out_of_range("栈为空");
return stack.back();
}
Expand Down Expand Up @@ -74,7 +74,7 @@ int main() {
cout << "栈的长度 size = " << size << endl;

/* 判断是否为空 */
bool empty = stack->empty();
bool empty = stack->isEmpty();
cout << "栈是否为空 = " << empty << endl;

// 释放内存
Expand Down
11 changes: 7 additions & 4 deletions codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ class LinkedListDeque {

/* 出队操作 */
int pop(bool isFront) {
// 若队列为空,直接返回 -1
if (isEmpty())
return -1;
throw out_of_range("队列为空");
int val;
// 队首出队操作
if (isFront) {
Expand Down Expand Up @@ -124,12 +123,16 @@ class LinkedListDeque {

/* 访问队首元素 */
int peekFirst() {
return isEmpty() ? -1 : front->val;
if (isEmpty())
throw out_of_range("双向队列为空");
return front->val;
}

/* 访问队尾元素 */
int peekLast() {
return isEmpty() ? -1 : rear->val;
if (isEmpty())
throw out_of_range("双向队列为空");
return rear->val;
}

/* 返回数组用于打印 */
Expand Down
4 changes: 2 additions & 2 deletions codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LinkedListQueue {
}

/* 判断队列是否为空 */
bool empty() {
bool isEmpty() {
return queSize == 0;
}

Expand Down Expand Up @@ -109,7 +109,7 @@ int main() {
cout << "队列长度 size = " << size << endl;

/* 判断队列是否为空 */
bool empty = queue->empty();
bool empty = queue->isEmpty();
cout << "队列是否为空 = " << empty << endl;

// 释放内存
Expand Down
6 changes: 3 additions & 3 deletions codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LinkedListStack {
}

/* 判断栈是否为空 */
bool empty() {
bool isEmpty() {
return size() == 0;
}

Expand All @@ -53,7 +53,7 @@ class LinkedListStack {

/* 访问栈顶元素 */
int top() {
if (size() == 0)
if (isEmpty())
throw out_of_range("栈为空");
return stackTop->val;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ int main() {
cout << "栈的长度 size = " << size << endl;

/* 判断是否为空 */
bool empty = stack->empty();
bool empty = stack->isEmpty();
cout << "栈是否为空 = " << empty << endl;

// 释放内存
Expand Down
21 changes: 10 additions & 11 deletions codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,29 @@ public void pushLast(int num) {

/* 出队操作 */
private int? pop(bool isFront) {
// 若队列为空,直接返回 null
if (isEmpty()) {
return null;
}

if (isEmpty())
throw new Exception();
int val;
// 队首出队操作
if (isFront) {
val = front.val; // 暂存头节点值
// 删除头节点
// 删除头节点
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}

front = fNext; // 更新头节点
}
// 队尾出队操作
else {
val = rear.val; // 暂存尾节点值
// 删除尾节点
// 删除尾节点
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
}

rear = rPrev; // 更新尾节点
}

Expand All @@ -124,12 +119,16 @@ public void pushLast(int num) {

/* 访问队首元素 */
public int? peekFirst() {
return isEmpty() ? null : front.val;
if (isEmpty())
throw new Exception();
return front.val;
}

/* 访问队尾元素 */
public int? peekLast() {
return isEmpty() ? null : rear.val;
if (isEmpty())
throw new Exception();
return rear.val;
}

/* 返回数组用于打印 */
Expand Down
2 changes: 1 addition & 1 deletion codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public int pop() {

/* 访问队首元素 */
public int peek() {
if (size() == 0 || front == null)
if (isEmpty())
throw new Exception();
return front.val;
}
Expand Down
5 changes: 1 addition & 4 deletions codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public void push(int num) {

/* 出栈 */
public int pop() {
if (stackPeek == null)
throw new Exception();

int num = peek();
stackPeek = stackPeek.next;
stkSize--;
Expand All @@ -46,7 +43,7 @@ public int pop() {

/* 访问栈顶元素 */
public int peek() {
if (size() == 0 || stackPeek == null)
if (isEmpty())
throw new Exception();
return stackPeek.val;
}
Expand Down
21 changes: 12 additions & 9 deletions codes/java/chapter_stack_and_queue/linkedlist_deque.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ public void pushLast(int num) {
}

/* 出队操作 */
private Integer pop(boolean isFront) {
// 若队列为空,直接返回 null
private int pop(boolean isFront) {
if (isEmpty())
return null;
throw new IndexOutOfBoundsException();
int val;
// 队首出队操作
if (isFront) {
Expand Down Expand Up @@ -103,23 +102,27 @@ private Integer pop(boolean isFront) {
}

/* 队首出队 */
public Integer popFirst() {
public int popFirst() {
return pop(true);
}

/* 队尾出队 */
public Integer popLast() {
public int popLast() {
return pop(false);
}

/* 访问队首元素 */
public Integer peekFirst() {
return isEmpty() ? null : front.val;
public int peekFirst() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}

/* 访问队尾元素 */
public Integer peekLast() {
return isEmpty() ? null : rear.val;
public int peekLast() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return rear.val;
}

/* 返回数组用于打印 */
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_stack_and_queue/linkedlist_queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int pop() {

/* 访问队首元素 */
public int peek() {
if (size() == 0)
if (isEmpty())
throw new IndexOutOfBoundsException();
return front.val;
}
Expand Down
2 changes: 1 addition & 1 deletion codes/java/chapter_stack_and_queue/linkedlist_stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int pop() {

/* 访问栈顶元素 */
public int peek() {
if (size() == 0)
if (isEmpty())
throw new IndexOutOfBoundsException();
return stackPeek.val;
}
Expand Down
3 changes: 1 addition & 2 deletions codes/python/chapter_stack_and_queue/linkedlist_deque.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ def push_last(self, num: int):

def pop(self, is_front: bool) -> int:
"""出队操作"""
# 若队列为空,直接返回 None
if self.is_empty():
return None
raise IndexError("双向队列为空")
# 队首出队操作
if is_front:
val: int = self.front.val # 暂存头节点值
Expand Down
8 changes: 4 additions & 4 deletions docs/chapter_array_and_linkedlist/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

!!! question "数组存储在栈上和存储在堆上,对时间效率和空间效率是否有影响?"

栈内存分配由编译器自动完成,而堆内存由程序员在代码中分配(注意,这里的栈和堆和数据结构中的栈和堆不是同一概念)
存储在栈上和堆上的数组都被存储在连续内存空间内,数据操作效率是基本一致的。然而,栈和堆具有各自的特点,从而导致以下不同点

1. 栈不灵活,分配的内存大小不可更改;堆相对灵活,可以动态分配内存
2. 栈是一块比较小的内存,容易出现内存不足;堆内存很大,但是由于是动态分配,容易碎片化,管理堆内存的难度更大、成本更高
3. 访问栈比访问堆更快,因为栈内存较小、对缓存友好,堆帧分散在很大的空间内,会出现更多的缓存未命中
1. 分配和释放效率:栈是一块较小的内存,分配由编译器自动完成;而堆内存相对更大,可以在代码中动态分配,更容易碎片化。因此,堆上的分配和释放操作通常比栈上的慢
2. 大小限制:栈内存相对较小,堆的大小一般受限于可用内存。因此堆更加适合存储大型数组
3. 灵活性:栈上的数组的大小需要在编译时确定,而堆上的数组的大小可以在运行时动态确定

!!! question "为什么数组要求相同类型的元素,而在链表中却没有强调同类型呢?"

Expand Down
15 changes: 8 additions & 7 deletions docs/chapter_heap/build_heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

在某些情况下,我们希望使用一个列表的所有元素来构建一个堆,这个过程被称为“建堆操作”。

## 自上而下构建
## 借助入堆操作实现

我们首先创建一个空堆,然后遍历列表,依次对每个元素执行“入堆操作”,即先将元素添加至堆的尾部,再对该元素执行“从底至顶”堆化。

每当一个元素入堆,堆的长度就加一,因此堆是“自上而下”地构建的。
每当一个元素入堆,堆的长度就加一。由于节点是从顶到底依次被添加进二叉树的,因此堆是“自上而下”地构建的。

设元素数量为 $n$ ,每个元素的入堆操作使用 $O(\log{n})$ 时间,因此该建堆方法的时间复杂度为 $O(n \log n)$ 。

## 自下而上构建
## 通过遍历堆化实现

实际上,我们可以实现一种更为高效的建堆方法,共分为两步。

1. 将列表所有元素原封不动添加到堆中。
1. 将列表所有元素原封不动添加到堆中,此时堆的性质尚未得到满足
2. 倒序遍历堆(即层序遍历的倒序),依次对每个非叶节点执行“从顶至底堆化”。

在倒序遍历中,堆是“自下而上”地构建的,需要重点理解以下两点
**每当堆化一个节点后,以该节点为根节点的子树就形成一个合法的子堆**。而由于是倒序遍历,因此堆是“自下而上”地被构建的

- 由于叶节点没有子节点,因此无需对它们执行堆化。最后一个节点的父节点是最后一个非叶节点。
- 在倒序遍历中,我们能够保证当前节点之下的子树已经完成堆化(已经是合法的堆),而这是堆化当前节点的前置条件。
之所以选择倒序遍历,是因为这样能够保证当前节点之下的子树已经是合法的子堆,这样堆化当前节点才是有效的。

值得说明的是,**叶节点没有子节点,天然就是合法的子堆,因此无需堆化**。如以下代码所示,最后一个非叶节点是最后一个节点的父节点,我们从它开始倒序遍历并执行堆化。

=== "Python"

Expand Down
8 changes: 8 additions & 0 deletions docs/chapter_stack_and_queue/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@
!!! question "双向队列像是两个栈拼接在了一起,它的用途是什么?"

双向队列就像是栈和队列的组合,或者是两个栈拼在了一起。它表现的是栈 + 队列的逻辑,因此可以实现栈与队列的所有应用,并且更加灵活。

!!! question "撤销(undo)和反撤销(redo)具体是如何实现的?"

使用两个堆栈,栈 `A` 用于撤销,栈 `B` 用于反撤销。

1. 每当用户执行一个操作,将这个操作压入栈 `A` ,并清空栈 `B` 。
2. 当用户执行“撤销”时,从栈 `A` 中弹出最近的操作,并将其压入栈 `B` 。
3. 当用户执行“反撤销”时,从栈 `B` 中弹出最近的操作,并将其压入栈 `A` 。
Loading

0 comments on commit d190dbf

Please sign in to comment.