Skip to content

Commit ee6e314

Browse files
committed
Deployed 662bdbc with MkDocs version: 1.0.4
1 parent 2d1a20c commit ee6e314

File tree

15 files changed

+153
-51
lines changed

15 files changed

+153
-51
lines changed
5.81 KB
Binary file not shown.
2.61 KB
Binary file not shown.

05_栈/stack.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# NOTE: 这里拷贝的 double_link_list.py 里的代码
44

5+
from collections import deque
6+
57

68
class Node(object):
79

@@ -151,6 +153,21 @@ def pop(self):
151153
return self.deque.pop()
152154

153155

156+
class Stack2(object):
157+
158+
def __init__(self):
159+
self._deque = deque()
160+
161+
def push(self, value):
162+
return self._deque.append(value)
163+
164+
def pop(self):
165+
return self._deque.pop()
166+
167+
def empty(self):
168+
return len(self._deque) == 0
169+
170+
154171
def test_stack():
155172
s = Stack()
156173
s.push(0)

05_栈/stack/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
<li class="toctree-l2"><a href="#_3">思考题</a></li>
9999

100100

101+
<li class="toctree-l2"><a href="#leetcode">Leetcode 练习</a></li>
102+
103+
101104
</ul>
102105
</li>
103106

@@ -264,6 +267,8 @@ <h1 id="_3">思考题</h1>
264267
<li>这里我们自己实现了 Deque,你能用 python 内置的 collections.deque 实现栈吗?有轮子能直接用的话看起来就简单多了,这里我们为了学习数据结构的实现就避免了直接使用内置结构</li>
265268
<li>哪些经典算法里使用到了栈呢?</li>
266269
</ul>
270+
<h1 id="leetcode">Leetcode 练习</h1>
271+
<p>https://leetcode.com/problems/implement-queue-using-stacks/</p>
267272

268273
</div>
269274
</div>
Binary file not shown.
Binary file not shown.

13_高级排序算法/quick_sort/index.html

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,14 @@ <h1 id="_1">快速排序</h1>
245245
<p><img alt="" src="../quick_sort.png" /></p>
246246
<p>根据这个想法我们可以快速写出快排的代码,简直就是在翻译上边的描述:</p>
247247
<pre><code class="py">def quicksort(array):
248-
if len(array) &lt; 2: # 递归出口,空数组或者只有一个元素的数组都是有序的
248+
size = len(array)
249+
if not array or size &lt; 2: # NOTE: 递归出口,空数组或者只有一个元素的数组都是有序的
249250
return array
250-
else:
251-
pivot_index = 0 # 选择第一个元素作为主元 pivot
252-
pivot = array[pivot_index]
253-
less_part = [i for i in array[pivot_index+1:] if i &lt;= pivot]
254-
great_part = [i for i in array[pivot_index+1:] if i &gt; pivot]
255-
return quicksort(less_part) + [pivot] + quicksort(great_part)
256-
251+
pivot_idx = 0
252+
pivot = array[pivot_idx]
253+
less_part = [array[i] for i in range(size) if array[i] &lt;= pivot and pivot_idx != i]
254+
great_part = [array[i] for i in range(size) if array[i] &gt; pivot and pivot_idx != i]
255+
return quicksort(less_part) + [pivot] + quicksort(great_part)
257256

258257
def test_quicksort():
259258
import random

13_高级排序算法/quicksort.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33

44
def quicksort(array):
5-
if len(array) < 2: # 递归出口,空数组或者只有一个元素的数组都是有序的
5+
size = len(array)
6+
if not array or size < 2: # NOTE: 递归出口,空数组或者只有一个元素的数组都是有序的
67
return array
7-
else:
8-
pivot_index = 0 # 选择第一个元素作为主元 pivot
9-
pivot = array[pivot_index]
10-
less_part = [i for i in array[pivot_index+1:] if i <= pivot]
11-
great_part = [i for i in array[pivot_index+1:] if i > pivot]
12-
return quicksort(less_part) + [pivot] + quicksort(great_part)
8+
pivot_idx = 0
9+
pivot = array[pivot_idx]
10+
less_part = [array[i] for i in range(size) if array[i] <= pivot and pivot_idx != i]
11+
great_part = [array[i] for i in range(size) if array[i] > pivot and pivot_idx != i]
12+
return quicksort(less_part) + [pivot] + quicksort(great_part)
1313

1414

1515
def test_quicksort():
@@ -23,7 +23,7 @@ def quicksort_inplace(array, beg, end): # 注意这里我们都用左闭右
2323
if beg < end: # beg == end 的时候递归出口
2424
pivot = partition(array, beg, end)
2525
quicksort_inplace(array, beg, pivot)
26-
quicksort_inplace(array, pivot+1, end)
26+
quicksort_inplace(array, pivot + 1, end)
2727

2828

2929
def partition(array, beg, end):

14_树与二叉树/btree.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33

4+
45
class BinTreeNode(object):
56
def __init__(self, data, left=None, right=None):
67
self.data, self.left, self.right = data, left, right
@@ -47,6 +48,30 @@ def reverse(self, subtree):
4748
self.reverse(subtree.left)
4849
self.reverse(subtree.right)
4950

51+
def layer_trav(self, subtree):
52+
cur_nodes = [subtree]
53+
next_nodes = []
54+
while cur_nodes or next_nodes:
55+
for node in cur_nodes:
56+
print(node.data)
57+
if node.left:
58+
next_nodes.append(node.left)
59+
if node.right:
60+
next_nodes.append(node.right)
61+
cur_nodes = next_nodes # 继续遍历下一层
62+
next_nodes = []
63+
64+
def layer_trav_use_queue(self, subtree):
65+
q = Queue()
66+
q.append(subtree)
67+
while not q.empty():
68+
cur_node = q.pop()
69+
print(cur_node.data)
70+
if cur_node.left:
71+
q.append(cur_node.left)
72+
if cur_node.right:
73+
q.append(cur_node.right)
74+
5075

5176
node_list = [
5277
{'data': 'A', 'left': 'B', 'right': 'C', 'is_root': True},
@@ -63,7 +88,13 @@ def reverse(self, subtree):
6388

6489

6590
btree = BinTree.build_from(node_list)
91+
print('====先序遍历=====')
6692
btree.preorder_trav(btree.root)
93+
print('====层序遍历=====')
94+
btree.layer_trav(btree.root)
95+
print('====用队列层序遍历=====')
96+
btree.layer_trav_use_queue(btree.root)
97+
6798
btree.reverse(btree.root)
68-
print('====我是华丽丽滴分割线=====')
99+
print('====反转之后的结果=====')
69100
btree.preorder_trav(btree.root)

14_树与二叉树/tree/index.html

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,16 @@
170170
<li class="toctree-l2"><a href="#_6">二叉树的遍历</a></li>
171171

172172

173-
<li class="toctree-l2"><a href="#_7">反转二叉树</a></li>
173+
<li class="toctree-l2"><a href="#_7">二叉树层序遍历</a></li>
174174

175175

176-
<li class="toctree-l2"><a href="#_8">练习题</a></li>
176+
<li class="toctree-l2"><a href="#_8">反转二叉树</a></li>
177177

178178

179-
<li class="toctree-l2"><a href="#_9">延伸阅读</a></li>
179+
<li class="toctree-l2"><a href="#_9">练习题</a></li>
180+
181+
182+
<li class="toctree-l2"><a href="#_10">延伸阅读</a></li>
180183

181184

182185
</ul>
@@ -413,7 +416,53 @@ <h1 id="_6">二叉树的遍历</h1>
413416
</code></pre>
414417

415418
<p>怎么样是不是挺简单的,比较直白的递归函数。如果你不明白,视频里我们会画个图来说明。</p>
416-
<h1 id="_7">反转二叉树</h1>
419+
<h1 id="_7">二叉树层序遍历</h1>
420+
<p>除了递归的方式遍历之外,我们还可以使用层序遍历的方式。层序遍历比较直白,就是从根节点开始按照一层一层的方式遍历节点。
421+
我们可以从根节点开始,之后把所有当前层的孩子都按照从左到右的顺序放到一个列表里,下一次遍历所有这些孩子就可以了。</p>
422+
<pre><code class="py"> def layer_trav(self, subtree):
423+
cur_nodes = [subtree]
424+
next_nodes = []
425+
while cur_nodes or next_nodes:
426+
for node in cur_nodes:
427+
print(node.data)
428+
if node.left:
429+
next_nodes.append(node.left)
430+
if node.right:
431+
next_nodes.append(node.right)
432+
cur_nodes = next_nodes # 继续遍历下一层
433+
next_nodes = []
434+
</code></pre>
435+
436+
<p>还有一种方式就是使用一个队列,之前我们知道队列是一个先进先出结构,如果我们按照一层一层的顺序从左往右把节点放到一个队列里,
437+
也可以实现层序遍历:</p>
438+
<pre><code class="py"> def layer_trav_use_queue(self, subtree):
439+
q = Queue()
440+
q.append(subtree)
441+
while not q.empty():
442+
cur_node = q.pop()
443+
print(cur_node.data)
444+
if cur_node.left:
445+
q.append(cur_node.left)
446+
if cur_node.right:
447+
q.append(cur_node.right)
448+
449+
450+
from collections import deque
451+
class Queue(object): # 借助内置的 deque 我们可以迅速实现一个 Queue
452+
def __init__(self):
453+
self._items = deque()
454+
455+
def append(self, value):
456+
return self._items.append(value)
457+
458+
def pop(self):
459+
return self._items.popleft()
460+
461+
def empty(self):
462+
return len(self._items) == 0
463+
</code></pre>
464+
465+
<h1 id="_8">反转二叉树</h1>
417466
<p>之所以单拎出来说这个是因为 mac 下著名的 brew 工具作者据说是因为面试 google 白板编程没写出来反转二叉树跪了。然后人家就去了苹果 😂。其实吧和遍历操作相比也没啥太大区别,递归交换就是了:</p>
418467
<pre><code class="py"> def reverse(self, subtree):
419468
if subtree is not None:
@@ -422,14 +471,15 @@ <h1 id="_7">反转二叉树</h1>
422471
self.reverse(subtree.right)
423472
</code></pre>
424473

425-
<h1 id="_8">练习题</h1>
474+
<h1 id="_9">练习题</h1>
426475
<ul>
427476
<li>请你完成二叉树的中序遍历和后序遍历以及单元测试</li>
428477
<li>树的遍历我们用了 print,请你尝试换成一个 callback,这样就能自定义处理树节点的方式了。</li>
429478
<li>请问树的遍历操作时间复杂度是多少?假设它的 size 是 n</li>
430479
<li>你能用非递归的方式来实现树的遍历吗?我们知道计算机内部使用了 stack,如果我们自己模拟如何实现?请你尝试完成</li>
480+
<li>使用树的层序遍历我们能实现一个树的左右视图,比如从一个二叉树的左边能看到哪些节点。请你尝试做这个练习题 https://leetcode.com/problems/binary-tree-right-side-view/description/</li>
431481
</ul>
432-
<h1 id="_9">延伸阅读</h1>
482+
<h1 id="_10">延伸阅读</h1>
433483
<ul>
434484
<li>《Data Structures and Algorithms in Python》 13 章 Binary Trees</li>
435485
<li><a href="https://www.geeksforgeeks.org/iterative-preorder-traversal/">https://www.geeksforgeeks.org/iterative-preorder-traversal/</a></li>

0 commit comments

Comments
 (0)