Compare commits
4 commits
b76eb5df0b
...
f36541aa45
Author | SHA1 | Date | |
---|---|---|---|
|
f36541aa45 | ||
|
dad0a3fd95 | ||
|
e41b0a3156 | ||
|
235f4e9fa4 |
|
@ -111,16 +111,29 @@ int popLast(ArrayDeque *deque) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回数组用于打印 */
|
||||||
|
int *toArray(ArrayDeque *deque, int *queSize) {
|
||||||
|
*queSize = deque->queSize;
|
||||||
|
int *res = (int *)calloc(deque->queSize, sizeof(int));
|
||||||
|
int j = deque->front;
|
||||||
|
for (int i = 0; i < deque->queSize; i++) {
|
||||||
|
res[i] = deque->nums[j % deque->queCapacity];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayDeque *deque = newArrayDeque(capacity);
|
ArrayDeque *deque = newArrayDeque(capacity);
|
||||||
pushLast(deque, 3);
|
pushLast(deque, 3);
|
||||||
pushLast(deque, 2);
|
pushLast(deque, 2);
|
||||||
pushLast(deque, 5);
|
pushLast(deque, 5);
|
||||||
printf("双向队列 deque = ");
|
printf("双向队列 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 访问元素 */
|
/* 访问元素 */
|
||||||
int peekFirstNum = peekFirst(deque);
|
int peekFirstNum = peekFirst(deque);
|
||||||
|
@ -131,18 +144,18 @@ int main() {
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
pushLast(deque, 4);
|
pushLast(deque, 4);
|
||||||
printf("元素 4 队尾入队后 deque = ");
|
printf("元素 4 队尾入队后 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
pushFirst(deque, 1);
|
pushFirst(deque, 1);
|
||||||
printf("元素 1 队首入队后 deque = ");
|
printf("元素 1 队首入队后 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
int popLastNum = popLast(deque);
|
int popLastNum = popLast(deque);
|
||||||
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
printf("队尾出队元素 = %d ,队尾出队后 deque= ", popLastNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
int popFirstNum = popFirst(deque);
|
int popFirstNum = popFirst(deque);
|
||||||
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
printf("队首出队元素 = %d ,队首出队后 deque= ", popFirstNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int dequeSize = size(deque);
|
int dequeSize = size(deque);
|
||||||
|
|
|
@ -74,10 +74,23 @@ int pop(ArrayQueue *queue) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回数组用于打印 */
|
||||||
|
int *toArray(ArrayQueue *queue, int *queSize) {
|
||||||
|
*queSize = queue->queSize;
|
||||||
|
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
||||||
|
int j = queue->front;
|
||||||
|
for (int i = 0; i < queue->queSize; i++) {
|
||||||
|
res[i] = queue->nums[j % queue->queCapacity];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化队列 */
|
/* 初始化队列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayQueue *queue = newArrayQueue(capacity);
|
ArrayQueue *queue = newArrayQueue(capacity);
|
||||||
|
|
||||||
/* 元素入队 */
|
/* 元素入队 */
|
||||||
|
@ -87,7 +100,7 @@ int main() {
|
||||||
push(queue, 5);
|
push(queue, 5);
|
||||||
push(queue, 4);
|
push(queue, 4);
|
||||||
printf("队列 queue = ");
|
printf("队列 queue = ");
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 访问队首元素 */
|
/* 访问队首元素 */
|
||||||
int peekNum = peek(queue);
|
int peekNum = peek(queue);
|
||||||
|
@ -96,7 +109,7 @@ int main() {
|
||||||
/* 元素出队 */
|
/* 元素出队 */
|
||||||
peekNum = pop(queue);
|
peekNum = pop(queue);
|
||||||
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
printf("出队元素 pop = %d ,出队后 queue = ", peekNum);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 获取队列的长度 */
|
/* 获取队列的长度 */
|
||||||
int queueSize = size(queue);
|
int queueSize = size(queue);
|
||||||
|
@ -111,7 +124,7 @@ int main() {
|
||||||
push(queue, i);
|
push(queue, i);
|
||||||
pop(queue);
|
pop(queue);
|
||||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 释放内存
|
// 释放内存
|
||||||
|
|
Before Width: | Height: | Size: 17 KiB After Width: | Height: | Size: 17 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
@ -30,9 +30,9 @@
|
||||||
|
|
||||||
## 致谢
|
## 致谢
|
||||||
|
|
||||||
本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、Gonglja、nuomi1、codingonion、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、RiverTwilight、gyt95、zhuoqinyue、Zuoxun、mingXta、hello-ikun、khoaxuantu、FangYuan33、GN-Yu、longsizhuo、mgisr、Cathay-Chen、guowei-gong、xBLACKICEx、K3v123、IsChristina、JoseHung、qualifier1024、pengchzn、Guanngxu、QiLOL、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、rongyi、JeffersonHuang、longranger2、theNefelibatas、yuelinxin、xiongsp、nanlei、a16su、cy-by-side、gaofer、malone6、Wonderdch、hongyun-robot、XiaChuerwu、yd-j、bluebean-cloud、iron-irax、he-weilai、Nigh、MolDuM、Phoenix0415、XC-Zero、SamJin98、reeswell、NI-SW、Horbin-Magician、xjr7670、YangXuanyi、DullSword、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、luluxia、boloboloda、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、coderlef、czruby、beintentional、KeiichiKasai、xb534、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、siqyka、selear、sdshaoda、noobcodemaker、chadyi、lyl625760、lucaswangdev、liuxjerry、0130w、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、ShiMaRing、FreddieLi、FloranceYeh、Transmigration-zhou、fanchenggang、gltianwen、Dr-XYZ、curly210102、CuB3y0nd、youshaoXG、bubble9um、fanenr、52coder、foursevenlove、KorsChen、ZongYangL、hezhizhen、linzeyan、ZJKung、GaochaoZhu、yang-le、Evilrabbit520、Turing-1024-Lee、Suremotoo、Allen-Scai、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、ZnYang2018、hugtyftg、logan-qiu、psychelzh 和 Keynman 。
|
本书在开源社区众多贡献者的共同努力下不断完善。感谢每一位投入时间与精力的撰稿人,他们是(按照 GitHub 自动生成的顺序):krahets、coderonion、Gonglja、nuomi1、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、khoaxuantu、RiverTwilight、rongyi、gyt95、zhuoqinyue、K3v123、Zuoxun、mingXta、hello-ikun、FangYuan33、GN-Yu、yuelinxin、longsizhuo、Cathay-Chen、guowei-gong、xBLACKICEx、IsChristina、JoseHung、qualifier1024、QiLOL、pengchzn、Guanngxu、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、theNefelibatas、longranger2、cy-by-side、xiongsp、JeffersonHuang、Transmigration-zhou、magentaqin、Wonderdch、malone6、xiaomiusa87、gaofer、bluebean-cloud、a16su、Shyam-Chen、nanlei、hongyun-robot、Phoenix0415、MolDuM、Nigh、he-weilai、junminhong、mgisr、iron-irax、yd-j、XiaChuerwu、XC-Zero、seven1240、SamJin98、wodray、reeswell、NI-SW、Horbin-Magician、Enlightenus、xjr7670、YangXuanyi、DullSword、boloboloda、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、liuxjerry、lucaswangdev、lyl625760、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、Yucao-cy、coderlef、czruby、bongbongbakudan、beintentional、ZongYangL、ZhongYuuu、luluxia、xb534、bitsmi、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、steventimes、sslmj2020、smilelsb、siqyka、selear、sdshaoda、Xi-Row、popozhu、nuquist19、noobcodemaker、XiaoK29、chadyi、ZhongGuanbin、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、BlindTerran、ShiMaRing、FreddieLi、FloranceYeh、iFleey、fanchenggang、gltianwen、goerll、Dr-XYZ、nedchu、curly210102、CuB3y0nd、KraHsu、CarrotDLaw、youshaoXG、bubble9um、fanenr、eagleanurag、LifeGoesOnionOnionOnion、52coder、foursevenlove、KorsChen、hezhizhen、linzeyan、ZJKung、GaochaoZhu、hopkings2008、yang-le、Evilrabbit520、Turing-1024-Lee、thomasq0、Suremotoo、Allen-Scai、Risuntsy、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、martinx、ZnYang2018、hugtyftg、logan-qiu、psychelzh、Keynman、KeiichiKasai 和 0130w。
|
||||||
|
|
||||||
本书的代码审阅工作由 codingonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。
|
本书的代码审阅工作由 coderonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1、Reanon 和 rongyi 完成(按照首字母顺序排列)。感谢他们付出的时间与精力,正是他们确保了各语言代码的规范与统一。
|
||||||
|
|
||||||
在本书的创作过程中,我得到了许多人的帮助。
|
在本书的创作过程中,我得到了许多人的帮助。
|
||||||
|
|
||||||
|
|
|
@ -258,9 +258,9 @@
|
||||||
<h3>代码审阅者</h3>
|
<h3>代码审阅者</h3>
|
||||||
<div class="profile-div">
|
<div class="profile-div">
|
||||||
<div class="profile-cell">
|
<div class="profile-cell">
|
||||||
<a href="https://github.com/codingonion">
|
<a href="https://github.com/coderonion">
|
||||||
<img class="profile-img" src="assets/avatar/avatar_codingonion.jpg" alt="Reviewer: codingonion" />
|
<img class="profile-img" src="assets/avatar/avatar_coderonion.jpg" alt="Reviewer: coderonion" />
|
||||||
<br><b>codingonion</b>
|
<br><b>coderonion</b>
|
||||||
<br><sub>Zig, Rust</sub>
|
<br><sub>Zig, Rust</sub>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 12 KiB |
|
@ -30,9 +30,9 @@ The main content of the book is shown in the figure below.
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
|
|
||||||
This book is continuously improved with the joint efforts of many contributors from the open-source community. Thanks to each writer who invested their time and energy, listed in the order generated by GitHub: krahets, codingonion, nuomi1, Gonglja, Reanon, justin-tse, danielsss, hpstory, S-N-O-R-L-A-X, night-cruise, msk397, gvenusleo, RiverTwilight, gyt95, zhuoqinyue, Zuoxun, Xia-Sang, mingXta, FangYuan33, GN-Yu, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, mgisr, JoseHung, qualifier1024, pengchzn, Guanngxu, longsizhuo, L-Super, what-is-me, yuan0221, lhxsm, Slone123c, WSL0809, longranger2, theNefelibatas, xiongsp, JeffersonHuang, hongyun-robot, K3v123, yuelinxin, a16su, gaofer, malone6, Wonderdch, xjr7670, DullSword, Horbin-Magician, NI-SW, reeswell, XC-Zero, XiaChuerwu, yd-j, iron-irax, huawuque404, MolDuM, Nigh, KorsChen, foursevenlove, 52coder, bubble9um, youshaoXG, curly210102, gltianwen, fanchenggang, Transmigration-zhou, FloranceYeh, FreddieLi, ShiMaRing, lipusheng, Javesun99, JackYang-hellobobo, shanghai-Jerry, 0130w, Keynman, psychelzh, logan-qiu, ZnYang2018, MwumLi, 1ch0, Phoenix0415, qingpeng9802, Richard-Zhang1019, QiLOL, Suremotoo, Turing-1024-Lee, Evilrabbit520, GaochaoZhu, ZJKung, linzeyan, hezhizhen, ZongYangL, beintentional, czruby, coderlef, dshlstarr, szu17dmy, fbigm, gledfish, hts0000, boloboloda, iStig, jiaxianhua, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, liuxjerry, lucaswangdev, lyl625760, chadyi, noobcodemaker, selear, siqyka, syd168, 4yDX3906, tao363, wangwang105, weibk, yabo083, yi427, yishangzhang, zhouLion, baagod, ElaBosak233, xb534, luluxia, yanedie, thomasq0, YangXuanyi and th1nk3r-ing.
|
This book is continuously improved with the joint efforts of many contributors from the open-source community. Thanks to each writer who invested their time and energy, listed in the order generated by GitHub: krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, msk397, gvenusleo, khoaxuantu, RiverTwilight, rongyi, gyt95, zhuoqinyue, K3v123, Zuoxun, mingXta, hello-ikun, FangYuan33, GN-Yu, yuelinxin, longsizhuo, Cathay-Chen, guowei-gong, xBLACKICEx, IsChristina, JoseHung, qualifier1024, QiLOL, pengchzn, Guanngxu, L-Super, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, theNefelibatas, longranger2, cy-by-side, xiongsp, JeffersonHuang, Transmigration-zhou, magentaqin, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, Shyam-Chen, nanlei, hongyun-robot, Phoenix0415, MolDuM, Nigh, he-weilai, junminhong, mgisr, iron-irax, yd-j, XiaChuerwu, XC-Zero, seven1240, SamJin98, wodray, reeswell, NI-SW, Horbin-Magician, Enlightenus, xjr7670, YangXuanyi, DullSword, boloboloda, iStig, qq909244296, jiaxianhua, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, liuxjerry, lucaswangdev, lyl625760, hts0000, gledfish, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, luluxia, xb534, bitsmi, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, steventimes, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, ZhongGuanbin, shanghai-Jerry, JackYang-hellobobo, Javesun99, lipusheng, BlindTerran, ShiMaRing, FreddieLi, FloranceYeh, iFleey, fanchenggang, gltianwen, goerll, Dr-XYZ, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, fanenr, eagleanurag, LifeGoesOnionOnionOnion, 52coder, foursevenlove, KorsChen, hezhizhen, linzeyan, ZJKung, GaochaoZhu, hopkings2008, yang-le, Evilrabbit520, Turing-1024-Lee, thomasq0, Suremotoo, Allen-Scai, Risuntsy, Richard-Zhang1019, qingpeng9802, primexiao, nidhoggfgg, 1ch0, MwumLi, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai and 0130w.
|
||||||
|
|
||||||
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justin‐tse, khoaxuantu, krahets, night-cruise, nuomi1, and Reanon (listed in alphabetical order). Thanks to them for their time and effort, ensuring the standardization and uniformity of the code in various languages.
|
The code review work for this book was completed by coderonion, Gonglja, gvenusleo, hpstory, justin‐tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon and rongyi (listed in alphabetical order). Thanks to them for their time and effort, ensuring the standardization and uniformity of the code in various languages.
|
||||||
|
|
||||||
Throughout the creation of this book, numerous individuals provided invaluable assistance, including but not limited to:
|
Throughout the creation of this book, numerous individuals provided invaluable assistance, including but not limited to:
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
# Summary
|
# Summary
|
||||||
|
|
||||||
### Key review
|
### Key Highlights
|
||||||
|
|
||||||
- A binary tree is a non-linear data structure that reflects the "divide and conquer" logic of splitting one into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes, respectively.
|
- A binary tree is a non-linear data structure that reflects the "divide and conquer" logic of splitting one into two. Each binary tree node contains a value and two pointers, which point to its left and right child nodes, respectively.
|
||||||
- For a node in a binary tree, the tree formed by its left (right) child node and all nodes under it is called the node's left (right) subtree.
|
- For a node in a binary tree, its left (right) child node and the tree formed below it is called the node's left (right) subtree.
|
||||||
- Related terminology of binary trees includes root node, leaf node, level, degree, edge, height, and depth, among others.
|
- Terms related to binary trees include root node, leaf node, level, degree, edge, height, and depth.
|
||||||
- The operations of initializing a binary tree, inserting nodes, and removing nodes are similar to those of linked list operations.
|
- The operations of initializing a binary tree, inserting nodes, and removing nodes are similar to those of linked list operations.
|
||||||
- Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree represents the ideal state, while the linked list is the worst state after degradation.
|
- Common types of binary trees include perfect binary trees, complete binary trees, full binary trees, and balanced binary trees. The perfect binary tree represents the ideal state, while the linked list is the worst state after degradation.
|
||||||
- A binary tree can be represented using an array by arranging the node values and empty slots in a level-order traversal sequence and implementing pointers based on the index mapping relationship between parent nodes and child nodes.
|
- A binary tree can be represented using an array by arranging the node values and empty slots in a level-order traversal sequence and implementing pointers based on the index mapping relationship between parent nodes and child nodes.
|
||||||
|
@ -12,7 +12,7 @@
|
||||||
- Pre-order, in-order, and post-order traversals are all depth-first search methods, reflecting the traversal manner of "going to the end first, then backtracking to continue." They are usually implemented using recursion.
|
- Pre-order, in-order, and post-order traversals are all depth-first search methods, reflecting the traversal manner of "going to the end first, then backtracking to continue." They are usually implemented using recursion.
|
||||||
- A binary search tree is an efficient data structure for element searching, with the time complexity of search, insert, and remove operations all being $O(\log n)$. When a binary search tree degrades into a linked list, these time complexities deteriorate to $O(n)$.
|
- A binary search tree is an efficient data structure for element searching, with the time complexity of search, insert, and remove operations all being $O(\log n)$. When a binary search tree degrades into a linked list, these time complexities deteriorate to $O(n)$.
|
||||||
- An AVL tree, also known as a balanced binary search tree, ensures that the tree remains balanced after continuous node insertions and removals through rotation operations.
|
- An AVL tree, also known as a balanced binary search tree, ensures that the tree remains balanced after continuous node insertions and removals through rotation operations.
|
||||||
- Rotation operations in an AVL tree include right rotation, left rotation, right-then-left rotation, and left-then-right rotation. After inserting or removing nodes, an AVL tree performs rotation operations from bottom to top to rebalance the tree.
|
- Rotation operations in an AVL tree include right rotation, left rotation, right-left rotation, and left-right rotation. After inserting or removing nodes, the AVL tree performs rotation operations from bottom to top to rebalance the tree.
|
||||||
|
|
||||||
### Q & A
|
### Q & A
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@
|
||||||
|
|
||||||
Yes, because height and depth are typically defined as "the number of edges passed."
|
Yes, because height and depth are typically defined as "the number of edges passed."
|
||||||
|
|
||||||
**Q**: The insertion and removal in a binary tree are generally completed by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
|
**Q**: The insertion and removal in a binary tree are generally accomplished by a set of operations. What does "a set of operations" refer to here? Can it be understood as the release of resources of the child nodes?
|
||||||
|
|
||||||
Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.
|
Taking the binary search tree as an example, the operation of removing a node needs to be handled in three different scenarios, each requiring multiple steps of node operations.
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ We need to view this problem from a recursive perspective. The `right_rotate(roo
|
||||||
|
|
||||||
**Q**: In C++, functions are divided into `private` and `public` sections. What considerations are there for this? Why are the `height()` function and the `updateHeight()` function placed in `public` and `private`, respectively?
|
**Q**: In C++, functions are divided into `private` and `public` sections. What considerations are there for this? Why are the `height()` function and the `updateHeight()` function placed in `public` and `private`, respectively?
|
||||||
|
|
||||||
It depends on the scope of the method's use. If a method is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
|
It depends on the scope of the function's use. If a function is only used within the class, then it is designed to be `private`. For example, it makes no sense for users to call `updateHeight()` on their own, as it is just a step in the insertion or removal operations. However, `height()` is for accessing node height, similar to `vector.size()`, thus it is set to `public` for use.
|
||||||
|
|
||||||
**Q**: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
|
**Q**: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
|
||||||
|
|
||||||
|
|
|
@ -258,9 +258,9 @@
|
||||||
<h3>Code reviewers</h3>
|
<h3>Code reviewers</h3>
|
||||||
<div class="profile-div">
|
<div class="profile-div">
|
||||||
<div class="profile-cell">
|
<div class="profile-cell">
|
||||||
<a href="https://github.com/codingonion">
|
<a href="https://github.com/coderonion">
|
||||||
<img class="profile-img" src="../assets/avatar/avatar_codingonion.jpg" alt="Reviewer: codingonion" />
|
<img class="profile-img" src="../assets/avatar/avatar_coderonion.jpg" alt="Reviewer: coderonion" />
|
||||||
<br><b>codingonion</b>
|
<br><b>coderonion</b>
|
||||||
<br><sub>Zig, Rust</sub>
|
<br><sub>Zig, Rust</sub>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -9,7 +9,7 @@ site_dir: site
|
||||||
repo_name: krahets/hello-algo
|
repo_name: krahets/hello-algo
|
||||||
repo_url: https://github.com/krahets/hello-algo
|
repo_url: https://github.com/krahets/hello-algo
|
||||||
edit_uri: tree/main/docs
|
edit_uri: tree/main/docs
|
||||||
version: 1.1.0
|
version: 1.2.0
|
||||||
|
|
||||||
# Copyright
|
# Copyright
|
||||||
copyright: Copyright © 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
|
copyright: Copyright © 2024 krahets<br>The website content is licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a>
|
||||||
|
|
|
@ -111,16 +111,29 @@ int popLast(ArrayDeque *deque) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回陣列用於列印 */
|
||||||
|
int *toArray(ArrayDeque *deque, int *queSize) {
|
||||||
|
*queSize = deque->queSize;
|
||||||
|
int *res = (int *)calloc(deque->queSize, sizeof(int));
|
||||||
|
int j = deque->front;
|
||||||
|
for (int i = 0; i < deque->queSize; i++) {
|
||||||
|
res[i] = deque->nums[j % deque->queCapacity];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化佇列 */
|
/* 初始化佇列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayDeque *deque = newArrayDeque(capacity);
|
ArrayDeque *deque = newArrayDeque(capacity);
|
||||||
pushLast(deque, 3);
|
pushLast(deque, 3);
|
||||||
pushLast(deque, 2);
|
pushLast(deque, 2);
|
||||||
pushLast(deque, 5);
|
pushLast(deque, 5);
|
||||||
printf("雙向佇列 deque = ");
|
printf("雙向佇列 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 訪問元素 */
|
/* 訪問元素 */
|
||||||
int peekFirstNum = peekFirst(deque);
|
int peekFirstNum = peekFirst(deque);
|
||||||
|
@ -131,18 +144,18 @@ int main() {
|
||||||
/* 元素入列 */
|
/* 元素入列 */
|
||||||
pushLast(deque, 4);
|
pushLast(deque, 4);
|
||||||
printf("元素 4 佇列尾入列後 deque = ");
|
printf("元素 4 佇列尾入列後 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
pushFirst(deque, 1);
|
pushFirst(deque, 1);
|
||||||
printf("元素 1 佇列首入列後 deque = ");
|
printf("元素 1 佇列首入列後 deque = ");
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 元素出列 */
|
/* 元素出列 */
|
||||||
int popLastNum = popLast(deque);
|
int popLastNum = popLast(deque);
|
||||||
printf("佇列尾出列元素 = %d ,佇列尾出列後 deque= ", popLastNum);
|
printf("佇列尾出列元素 = %d ,佇列尾出列後 deque= ", popLastNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
int popFirstNum = popFirst(deque);
|
int popFirstNum = popFirst(deque);
|
||||||
printf("佇列首出列元素 = %d ,佇列首出列後 deque= ", popFirstNum);
|
printf("佇列首出列元素 = %d ,佇列首出列後 deque= ", popFirstNum);
|
||||||
printArray(deque->nums, deque->queSize);
|
printArray(toArray(deque, &queSize), queSize);
|
||||||
|
|
||||||
/* 獲取佇列的長度 */
|
/* 獲取佇列的長度 */
|
||||||
int dequeSize = size(deque);
|
int dequeSize = size(deque);
|
||||||
|
|
|
@ -74,10 +74,23 @@ int pop(ArrayQueue *queue) {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 返回陣列用於列印 */
|
||||||
|
int *toArray(ArrayQueue *queue, int *queSize) {
|
||||||
|
*queSize = queue->queSize;
|
||||||
|
int *res = (int *)calloc(queue->queSize, sizeof(int));
|
||||||
|
int j = queue->front;
|
||||||
|
for (int i = 0; i < queue->queSize; i++) {
|
||||||
|
res[i] = queue->nums[j % queue->queCapacity];
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/* Driver Code */
|
/* Driver Code */
|
||||||
int main() {
|
int main() {
|
||||||
/* 初始化佇列 */
|
/* 初始化佇列 */
|
||||||
int capacity = 10;
|
int capacity = 10;
|
||||||
|
int queSize;
|
||||||
ArrayQueue *queue = newArrayQueue(capacity);
|
ArrayQueue *queue = newArrayQueue(capacity);
|
||||||
|
|
||||||
/* 元素入列 */
|
/* 元素入列 */
|
||||||
|
@ -87,7 +100,7 @@ int main() {
|
||||||
push(queue, 5);
|
push(queue, 5);
|
||||||
push(queue, 4);
|
push(queue, 4);
|
||||||
printf("佇列 queue = ");
|
printf("佇列 queue = ");
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 訪問佇列首元素 */
|
/* 訪問佇列首元素 */
|
||||||
int peekNum = peek(queue);
|
int peekNum = peek(queue);
|
||||||
|
@ -96,7 +109,7 @@ int main() {
|
||||||
/* 元素出列 */
|
/* 元素出列 */
|
||||||
peekNum = pop(queue);
|
peekNum = pop(queue);
|
||||||
printf("出列元素 pop = %d ,出列後 queue = ", peekNum);
|
printf("出列元素 pop = %d ,出列後 queue = ", peekNum);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
|
|
||||||
/* 獲取佇列的長度 */
|
/* 獲取佇列的長度 */
|
||||||
int queueSize = size(queue);
|
int queueSize = size(queue);
|
||||||
|
@ -111,7 +124,7 @@ int main() {
|
||||||
push(queue, i);
|
push(queue, i);
|
||||||
pop(queue);
|
pop(queue);
|
||||||
printf("第 %d 輪入列 + 出列後 queue = ", i);
|
printf("第 %d 輪入列 + 出列後 queue = ", i);
|
||||||
printArray(queue->nums, queue->queSize);
|
printArray(toArray(queue, &queSize), queSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 釋放記憶體
|
// 釋放記憶體
|
||||||
|
|
Before Width: | Height: | Size: 434 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 465 KiB After Width: | Height: | Size: 143 KiB |
Before Width: | Height: | Size: 477 KiB After Width: | Height: | Size: 122 KiB |
|
@ -50,6 +50,7 @@
|
||||||
| front of the queue | 队首 | 佇列首 |
|
| front of the queue | 队首 | 佇列首 |
|
||||||
| rear of the queue | 队尾 | 佇列尾 |
|
| rear of the queue | 队尾 | 佇列尾 |
|
||||||
| hash table | 哈希表 | 雜湊表 |
|
| hash table | 哈希表 | 雜湊表 |
|
||||||
|
| hash set | 哈希集合 | 雜湊集合 |
|
||||||
| bucket | 桶 | 桶 |
|
| bucket | 桶 | 桶 |
|
||||||
| hash function | 哈希函数 | 雜湊函式 |
|
| hash function | 哈希函数 | 雜湊函式 |
|
||||||
| hash collision | 哈希冲突 | 雜湊衝突 |
|
| hash collision | 哈希冲突 | 雜湊衝突 |
|
||||||
|
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 13 KiB |
|
@ -30,9 +30,9 @@
|
||||||
|
|
||||||
## 致謝
|
## 致謝
|
||||||
|
|
||||||
本書在開源社群眾多貢獻者的共同努力下不斷完善。感謝每一位投入時間與精力的撰稿人,他們是(按照 GitHub 自動生成的順序):krahets、Gonglja、nuomi1、codingonion、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、RiverTwilight、gyt95、zhuoqinyue、Zuoxun、mingXta、hello-ikun、khoaxuantu、FangYuan33、GN-Yu、longsizhuo、mgisr、Cathay-Chen、guowei-gong、xBLACKICEx、K3v123、IsChristina、JoseHung、qualifier1024、pengchzn、Guanngxu、QiLOL、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、rongyi、JeffersonHuang、longranger2、theNefelibatas、yuelinxin、xiongsp、nanlei、a16su、cy-by-side、gaofer、malone6、Wonderdch、hongyun-robot、XiaChuerwu、yd-j、bluebean-cloud、iron-irax、he-weilai、Nigh、MolDuM、Phoenix0415、XC-Zero、SamJin98、reeswell、NI-SW、Horbin-Magician、xjr7670、YangXuanyi、DullSword、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、luluxia、boloboloda、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、coderlef、czruby、beintentional、KeiichiKasai、xb534、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、siqyka、selear、sdshaoda、noobcodemaker、chadyi、lyl625760、lucaswangdev、liuxjerry、0130w、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、ShiMaRing、FreddieLi、FloranceYeh、Transmigration-zhou、fanchenggang、gltianwen、Dr-XYZ、curly210102、CuB3y0nd、youshaoXG、bubble9um、fanenr、52coder、foursevenlove、KorsChen、ZongYangL、hezhizhen、linzeyan、ZJKung、GaochaoZhu、yang-le、Evilrabbit520、Turing-1024-Lee、Suremotoo、Allen-Scai、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、ZnYang2018、hugtyftg、logan-qiu、psychelzh 和 Keynman 。
|
本書在開源社群眾多貢獻者的共同努力下不斷完善。感謝每一位投入時間與精力的撰稿人,他們是(按照 GitHub 自動生成的順序):krahets、coderonion、Gonglja、nuomi1、Reanon、justin-tse、hpstory、danielsss、curtishd、night-cruise、S-N-O-R-L-A-X、msk397、gvenusleo、khoaxuantu、RiverTwilight、rongyi、gyt95、zhuoqinyue、K3v123、Zuoxun、mingXta、hello-ikun、FangYuan33、GN-Yu、yuelinxin、longsizhuo、Cathay-Chen、guowei-gong、xBLACKICEx、IsChristina、JoseHung、qualifier1024、QiLOL、pengchzn、Guanngxu、L-Super、WSL0809、Slone123c、lhxsm、yuan0221、what-is-me、theNefelibatas、longranger2、cy-by-side、xiongsp、JeffersonHuang、Transmigration-zhou、magentaqin、Wonderdch、malone6、xiaomiusa87、gaofer、bluebean-cloud、a16su、Shyam-Chen、nanlei、hongyun-robot、Phoenix0415、MolDuM、Nigh、he-weilai、junminhong、mgisr、iron-irax、yd-j、XiaChuerwu、XC-Zero、seven1240、SamJin98、wodray、reeswell、NI-SW、Horbin-Magician、Enlightenus、xjr7670、YangXuanyi、DullSword、boloboloda、iStig、qq909244296、jiaxianhua、wenjianmin、keshida、kilikilikid、lclc6、lwbaptx、liuxjerry、lucaswangdev、lyl625760、hts0000、gledfish、fbigm、echo1937、szu17dmy、dshlstarr、Yucao-cy、coderlef、czruby、bongbongbakudan、beintentional、ZongYangL、ZhongYuuu、luluxia、xb534、bitsmi、ElaBosak233、baagod、zhouLion、yishangzhang、yi427、yabo083、weibk、wangwang105、th1nk3r-ing、tao363、4yDX3906、syd168、steventimes、sslmj2020、smilelsb、siqyka、selear、sdshaoda、Xi-Row、popozhu、nuquist19、noobcodemaker、XiaoK29、chadyi、ZhongGuanbin、shanghai-Jerry、JackYang-hellobobo、Javesun99、lipusheng、BlindTerran、ShiMaRing、FreddieLi、FloranceYeh、iFleey、fanchenggang、gltianwen、goerll、Dr-XYZ、nedchu、curly210102、CuB3y0nd、KraHsu、CarrotDLaw、youshaoXG、bubble9um、fanenr、eagleanurag、LifeGoesOnionOnionOnion、52coder、foursevenlove、KorsChen、hezhizhen、linzeyan、ZJKung、GaochaoZhu、hopkings2008、yang-le、Evilrabbit520、Turing-1024-Lee、thomasq0、Suremotoo、Allen-Scai、Risuntsy、Richard-Zhang1019、qingpeng9802、primexiao、nidhoggfgg、1ch0、MwumLi、martinx、ZnYang2018、hugtyftg、logan-qiu、psychelzh、Keynman、KeiichiKasai 和 0130w。
|
||||||
|
|
||||||
本書的程式碼審閱工作由 codingonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1 和 Reanon 完成(按照首字母順序排列)。感謝他們付出的時間與精力,正是他們確保了各語言程式碼的規範與統一。
|
本書的程式碼審閱工作由 coderonion、curtishd、Gonglja、gvenusleo、hpstory、justin-tse、khoaxuantu、krahets、night-cruise、nuomi1、Reanon 和 rongyi 完成(按照首字母順序排列)。感謝他們付出的時間與精力,正是他們確保了各語言程式碼的規範與統一。
|
||||||
|
|
||||||
在本書的創作過程中,我得到了許多人的幫助。
|
在本書的創作過程中,我得到了許多人的幫助。
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 386 KiB After Width: | Height: | Size: 106 KiB |
Before Width: | Height: | Size: 386 KiB After Width: | Height: | Size: 74 KiB |
|
@ -258,9 +258,9 @@
|
||||||
<h3>程式碼審閱者</h3>
|
<h3>程式碼審閱者</h3>
|
||||||
<div class="profile-div">
|
<div class="profile-div">
|
||||||
<div class="profile-cell">
|
<div class="profile-cell">
|
||||||
<a href="https://github.com/codingonion">
|
<a href="https://github.com/coderonion">
|
||||||
<img class="profile-img" src="../assets/avatar/avatar_codingonion.jpg" alt="Reviewer: codingonion" />
|
<img class="profile-img" src="../assets/avatar/avatar_coderonion.jpg" alt="Reviewer: coderonion" />
|
||||||
<br><b>codingonion</b>
|
<br><b>coderonion</b>
|
||||||
<br><sub>Zig, Rust</sub>
|
<br><sub>Zig, Rust</sub>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -9,7 +9,7 @@ docs_dir: ../build/zh-hant/docs
|
||||||
site_dir: ../site/zh-hant
|
site_dir: ../site/zh-hant
|
||||||
# Repository
|
# Repository
|
||||||
edit_uri: tree/main/zh-hant/docs
|
edit_uri: tree/main/zh-hant/docs
|
||||||
version: 1.1.0
|
version: 1.2.0
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
theme:
|
theme:
|
||||||
|
|