This commit is contained in:
krahets 2023-04-17 21:00:28 +08:00
parent ca2ccfea0b
commit cc58db2cfa
59 changed files with 3271 additions and 382 deletions

View file

@ -970,6 +970,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1017,9 +1019,23 @@
<li class="md-nav__item">
<a href="/chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="/chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1033,7 +1049,7 @@
<li class="md-nav__item">
<a href="/chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1047,7 +1063,7 @@
<li class="md-nav__item">
<a href="/chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1045,6 +1045,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1092,9 +1094,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1108,7 +1124,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1122,7 +1138,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1045,6 +1045,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1092,9 +1094,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1108,7 +1124,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1122,7 +1138,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1031,6 +1031,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1078,9 +1080,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1094,7 +1110,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1108,7 +1124,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1045,6 +1045,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1092,9 +1094,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1108,7 +1124,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1122,7 +1138,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1058,6 +1058,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1105,9 +1107,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1121,7 +1137,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1086,6 +1086,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1133,9 +1135,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1149,7 +1165,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1163,7 +1179,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1134,6 +1134,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1181,9 +1183,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1197,7 +1213,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1211,7 +1227,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1031,6 +1031,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1078,9 +1080,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1094,7 +1110,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1108,7 +1124,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1058,6 +1058,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1105,9 +1107,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1121,7 +1137,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1058,6 +1058,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1105,9 +1107,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1121,7 +1137,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1038,6 +1038,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1085,9 +1087,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1101,7 +1117,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1115,7 +1131,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -1167,34 +1183,21 @@
<li class="md-nav__item">
<a href="#921" class="md-nav__link">
9.2.1. &nbsp; 两种建堆方法
9.2.1. &nbsp; 借助入堆方法实现
</a>
<nav class="md-nav" aria-label="9.2.1. &nbsp; 两种建堆方法">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
借助入堆方法实现
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
基于堆化操作实现
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#922" class="md-nav__link">
9.2.2. &nbsp; 复杂度分析
9.2.2. &nbsp; 基于堆化操作实现
</a>
</li>
<li class="md-nav__item">
<a href="#923" class="md-nav__link">
9.2.3. &nbsp; 复杂度分析
</a>
</li>
@ -1802,34 +1805,21 @@
<li class="md-nav__item">
<a href="#921" class="md-nav__link">
9.2.1. &nbsp; 两种建堆方法
9.2.1. &nbsp; 借助入堆方法实现
</a>
<nav class="md-nav" aria-label="9.2.1. &nbsp; 两种建堆方法">
<ul class="md-nav__list">
<li class="md-nav__item">
<a href="#_1" class="md-nav__link">
借助入堆方法实现
</a>
</li>
<li class="md-nav__item">
<a href="#_2" class="md-nav__link">
基于堆化操作实现
</a>
</li>
</ul>
</nav>
</li>
<li class="md-nav__item">
<a href="#922" class="md-nav__link">
9.2.2. &nbsp; 复杂度分析
9.2.2. &nbsp; 基于堆化操作实现
</a>
</li>
<li class="md-nav__item">
<a href="#923" class="md-nav__link">
9.2.3. &nbsp; 复杂度分析
</a>
</li>
@ -1859,11 +1849,10 @@
<h1 id="92">9.2. &nbsp; 建堆操作 *<a class="headerlink" href="#92" title="Permanent link">&para;</a></h1>
<p>如果我们想要根据输入列表生成一个堆,这个过程被称为「建堆」。</p>
<h2 id="921">9.2.1. &nbsp; 两种建堆方法<a class="headerlink" href="#921" title="Permanent link">&para;</a></h2>
<h3 id="_1">借助入堆方法实现<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<h2 id="921">9.2.1. &nbsp; 借助入堆方法实现<a class="headerlink" href="#921" title="Permanent link">&para;</a></h2>
<p>最直接的方法是借助“元素入堆操作”实现,首先创建一个空堆,然后将列表元素依次添加到堆中。</p>
<p>设元素数量为 <span class="arithmatex">\(n\)</span> ,则最后一个元素入堆的时间复杂度为 <span class="arithmatex">\(O(\log n)\)</span> 。在依次添加元素时,堆的平均长度为 <span class="arithmatex">\(\frac{n}{2}\)</span> ,因此该方法的总体时间复杂度为 <span class="arithmatex">\(O(n \log n)\)</span></p>
<h3 id="_2">基于堆化操作实现<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h3>
<h2 id="922">9.2.2. &nbsp; 基于堆化操作实现<a class="headerlink" href="#922" title="Permanent link">&para;</a></h2>
<p>有趣的是,存在一种更高效的建堆方法,其时间复杂度仅为 <span class="arithmatex">\(O(n)\)</span> 。我们先将列表所有元素原封不动添加到堆中,<strong>然后迭代地对各个节点执行“从顶至底堆化”</strong>。当然,<strong>我们不需要对叶节点执行堆化操作</strong>,因为它们没有子节点。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="1:10"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Java</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Python</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">JavaScript</label><label for="__tabbed_1_6">TypeScript</label><label for="__tabbed_1_7">C</label><label for="__tabbed_1_8">C#</label><label for="__tabbed_1_9">Swift</label><label for="__tabbed_1_10">Zig</label></div>
<div class="tabbed-content">
@ -1986,7 +1975,7 @@
</div>
</div>
</div>
<h2 id="922">9.2.2. &nbsp; 复杂度分析<a class="headerlink" href="#922" title="Permanent link">&para;</a></h2>
<h2 id="923">9.2.3. &nbsp; 复杂度分析<a class="headerlink" href="#923" title="Permanent link">&para;</a></h2>
<p>为什么第二种建堆方法的时间复杂度是 <span class="arithmatex">\(O(n)\)</span> ?我们来展开推算一下。</p>
<ul>
<li>完全二叉树中,设节点总数为 <span class="arithmatex">\(n\)</span> ,则叶节点数量为 <span class="arithmatex">\((n + 1) / 2\)</span> ,其中 <span class="arithmatex">\(/\)</span> 为向下整除。因此,在排除叶节点后,需要堆化的节点数量为 <span class="arithmatex">\((n - 1)/2\)</span> ,复杂度为 <span class="arithmatex">\(O(n)\)</span> </li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -3186,7 +3202,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../../chapter_tree/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.5. &amp;nbsp; 小结" rel="prev">
<a href="../../chapter_tree/summary/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.6. &amp;nbsp; 小结" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -3195,7 +3211,7 @@
上一页
</span>
<div class="md-ellipsis">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</div>
</div>
</a>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1038,6 +1038,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1085,9 +1087,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1101,7 +1117,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1115,7 +1131,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1036,6 +1036,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1083,9 +1085,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1099,7 +1115,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1113,7 +1129,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1052,6 +1052,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1099,9 +1101,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1115,7 +1131,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1129,7 +1145,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -979,6 +979,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1026,9 +1028,23 @@
<li class="md-nav__item">
<a href="../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1042,7 +1058,7 @@
<li class="md-nav__item">
<a href="../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -981,6 +981,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1028,9 +1030,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1044,7 +1060,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1058,7 +1074,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1058,6 +1058,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1105,9 +1107,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1121,7 +1137,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1058,6 +1058,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1105,9 +1107,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1121,7 +1137,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -1092,6 +1092,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1139,9 +1141,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1155,7 +1171,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1169,7 +1185,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

View file

@ -993,6 +993,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1040,9 +1042,23 @@
<li class="md-nav__item">
<a href="../../chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../../chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1056,7 +1072,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1070,7 +1086,7 @@
<li class="md-nav__item">
<a href="../../chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

File diff suppressed because it is too large Load diff

View file

@ -25,7 +25,7 @@
<title>8.4.   AVL 树 * - Hello 算法</title>
<title>8.5.   AVL 树 * - Hello 算法</title>
@ -79,7 +79,7 @@
<div data-md-component="skip">
<a href="#84-avl" class="md-skip">
<a href="#85-avl" class="md-skip">
跳转至
</a>
@ -113,7 +113,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</span>
</div>
@ -983,6 +983,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1030,9 +1032,23 @@
<li class="md-nav__item">
<a href="../array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1055,12 +1071,12 @@
<label class="md-nav__link md-nav__link--active" for="__toc">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
<span class="md-nav__icon md-icon"></span>
</label>
<a href="./" class="md-nav__link md-nav__link--active">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
@ -1079,11 +1095,11 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#841-avl" class="md-nav__link">
8.4.1. &nbsp; AVL 树常见术语
<a href="#851-avl" class="md-nav__link">
8.5.1. &nbsp; AVL 树常见术语
</a>
<nav class="md-nav" aria-label="8.4.1. &nbsp; AVL 树常见术语">
<nav class="md-nav" aria-label="8.5.1. &nbsp; AVL 树常见术语">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1106,11 +1122,11 @@
</li>
<li class="md-nav__item">
<a href="#842-avl" class="md-nav__link">
8.4.2. &nbsp; AVL 树旋转
<a href="#852-avl" class="md-nav__link">
8.5.2. &nbsp; AVL 树旋转
</a>
<nav class="md-nav" aria-label="8.4.2. &nbsp; AVL 树旋转">
<nav class="md-nav" aria-label="8.5.2. &nbsp; AVL 树旋转">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1154,11 +1170,11 @@
</li>
<li class="md-nav__item">
<a href="#843-avl" class="md-nav__link">
8.4.3. &nbsp; AVL 树常用操作
<a href="#853-avl" class="md-nav__link">
8.5.3. &nbsp; AVL 树常用操作
</a>
<nav class="md-nav" aria-label="8.4.3. &nbsp; AVL 树常用操作">
<nav class="md-nav" aria-label="8.5.3. &nbsp; AVL 树常用操作">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1188,8 +1204,8 @@
</li>
<li class="md-nav__item">
<a href="#844-avl" class="md-nav__link">
8.4.4. &nbsp; AVL 树典型应用
<a href="#854-avl" class="md-nav__link">
8.5.4. &nbsp; AVL 树典型应用
</a>
</li>
@ -1210,7 +1226,7 @@
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -1883,11 +1899,11 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#841-avl" class="md-nav__link">
8.4.1. &nbsp; AVL 树常见术语
<a href="#851-avl" class="md-nav__link">
8.5.1. &nbsp; AVL 树常见术语
</a>
<nav class="md-nav" aria-label="8.4.1. &nbsp; AVL 树常见术语">
<nav class="md-nav" aria-label="8.5.1. &nbsp; AVL 树常见术语">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1910,11 +1926,11 @@
</li>
<li class="md-nav__item">
<a href="#842-avl" class="md-nav__link">
8.4.2. &nbsp; AVL 树旋转
<a href="#852-avl" class="md-nav__link">
8.5.2. &nbsp; AVL 树旋转
</a>
<nav class="md-nav" aria-label="8.4.2. &nbsp; AVL 树旋转">
<nav class="md-nav" aria-label="8.5.2. &nbsp; AVL 树旋转">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1958,11 +1974,11 @@
</li>
<li class="md-nav__item">
<a href="#843-avl" class="md-nav__link">
8.4.3. &nbsp; AVL 树常用操作
<a href="#853-avl" class="md-nav__link">
8.5.3. &nbsp; AVL 树常用操作
</a>
<nav class="md-nav" aria-label="8.4.3. &nbsp; AVL 树常用操作">
<nav class="md-nav" aria-label="8.5.3. &nbsp; AVL 树常用操作">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1992,8 +2008,8 @@
</li>
<li class="md-nav__item">
<a href="#844-avl" class="md-nav__link">
8.4.4. &nbsp; AVL 树典型应用
<a href="#854-avl" class="md-nav__link">
8.5.4. &nbsp; AVL 树典型应用
</a>
</li>
@ -2021,7 +2037,7 @@
<h1 id="84-avl">8.4. &nbsp; AVL 树 *<a class="headerlink" href="#84-avl" title="Permanent link">&para;</a></h1>
<h1 id="85-avl">8.5. &nbsp; AVL 树 *<a class="headerlink" href="#85-avl" title="Permanent link">&para;</a></h1>
<p>在二叉搜索树章节中,我们提到了在多次插入和删除操作后,二叉搜索树可能退化为链表。这种情况下,所有操作的时间复杂度将从 <span class="arithmatex">\(O(\log n)\)</span> 恶化为 <span class="arithmatex">\(O(n)\)</span></p>
<p>如下图所示,经过两次删除节点操作,这个二叉搜索树便会退化为链表。</p>
<p><img alt="AVL 树在删除节点后发生退化" src="../avl_tree.assets/avltree_degradation_from_removing_node.png" /></p>
@ -2032,7 +2048,7 @@
<p align="center"> Fig. AVL 树在插入节点后发生退化 </p>
<p>G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorithm for the organization of information" 中提出了「AVL 树」。论文中详细描述了一系列操作确保在持续添加和删除节点后AVL 树不会退化,从而使得各种操作的时间复杂度保持在 <span class="arithmatex">\(O(\log n)\)</span> 级别。换句话说在需要频繁进行增删查改操作的场景中AVL 树能始终保持高效的数据操作性能,具有很好的应用价值。</p>
<h2 id="841-avl">8.4.1. &nbsp; AVL 树常见术语<a class="headerlink" href="#841-avl" title="Permanent link">&para;</a></h2>
<h2 id="851-avl">8.5.1. &nbsp; AVL 树常见术语<a class="headerlink" href="#851-avl" title="Permanent link">&para;</a></h2>
<p>「AVL 树」既是二叉搜索树也是平衡二叉树,同时满足这两类二叉树的所有性质,因此也被称为「平衡二叉搜索树」。</p>
<h3 id="_1">节点高度<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>在操作 AVL 树时,我们需要获取节点的高度,因此需要为 AVL 树的节点类添加 <code>height</code> 变量。</p>
@ -2405,7 +2421,7 @@
<p class="admonition-title">Note</p>
<p>设平衡因子为 <span class="arithmatex">\(f\)</span> ,则一棵 AVL 树的任意节点的平衡因子皆满足 <span class="arithmatex">\(-1 \le f \le 1\)</span></p>
</div>
<h2 id="842-avl">8.4.2. &nbsp; AVL 树旋转<a class="headerlink" href="#842-avl" title="Permanent link">&para;</a></h2>
<h2 id="852-avl">8.5.2. &nbsp; AVL 树旋转<a class="headerlink" href="#852-avl" title="Permanent link">&para;</a></h2>
<p>AVL 树的特点在于「旋转 Rotation」操作它能够在不影响二叉树的中序遍历序列的前提下使失衡节点重新恢复平衡。换句话说<strong>旋转操作既能保持树的「二叉搜索树」属性,也能使树重新变为「平衡二叉树」</strong></p>
<p>我们将平衡因子绝对值 <span class="arithmatex">\(&gt; 1\)</span> 的节点称为「失衡节点」。根据节点失衡情况的不同,旋转操作分为四种:右旋、左旋、先右旋后左旋、先左旋后右旋。下面我们将详细介绍这些旋转操作。</p>
<h3 id="_3">右旋<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h3>
@ -3096,7 +3112,7 @@
</div>
</div>
</div>
<h2 id="843-avl">8.4.3. &nbsp; AVL 树常用操作<a class="headerlink" href="#843-avl" title="Permanent link">&para;</a></h2>
<h2 id="853-avl">8.5.3. &nbsp; AVL 树常用操作<a class="headerlink" href="#853-avl" title="Permanent link">&para;</a></h2>
<h3 id="_8">插入节点<a class="headerlink" href="#_8" title="Permanent link">&para;</a></h3>
<p>「AVL 树」的节点插入操作与「二叉搜索树」在主体上类似。唯一的区别在于,在 AVL 树中插入节点后,从该节点到根节点的路径上可能会出现一系列失衡节点。因此,<strong>我们需要从这个节点开始,自底向上执行旋转操作,使所有失衡节点恢复平衡</strong></p>
<div class="tabbed-set tabbed-alternate" data-tabs="8:10"><input checked="checked" id="__tabbed_8_1" name="__tabbed_8" type="radio" /><input id="__tabbed_8_2" name="__tabbed_8" type="radio" /><input id="__tabbed_8_3" name="__tabbed_8" type="radio" /><input id="__tabbed_8_4" name="__tabbed_8" type="radio" /><input id="__tabbed_8_5" name="__tabbed_8" type="radio" /><input id="__tabbed_8_6" name="__tabbed_8" type="radio" /><input id="__tabbed_8_7" name="__tabbed_8" type="radio" /><input id="__tabbed_8_8" name="__tabbed_8" type="radio" /><input id="__tabbed_8_9" name="__tabbed_8" type="radio" /><input id="__tabbed_8_10" name="__tabbed_8" type="radio" /><div class="tabbed-labels"><label for="__tabbed_8_1">Java</label><label for="__tabbed_8_2">C++</label><label for="__tabbed_8_3">Python</label><label for="__tabbed_8_4">Go</label><label for="__tabbed_8_5">JavaScript</label><label for="__tabbed_8_6">TypeScript</label><label for="__tabbed_8_7">C</label><label for="__tabbed_8_8">C#</label><label for="__tabbed_8_9">Swift</label><label for="__tabbed_8_10">Zig</label></div>
@ -3746,7 +3762,7 @@
</div>
<h3 id="_10">查找节点<a class="headerlink" href="#_10" title="Permanent link">&para;</a></h3>
<p>AVL 树的节点查找操作与二叉搜索树一致,在此不再赘述。</p>
<h2 id="844-avl">8.4.4. &nbsp; AVL 树典型应用<a class="headerlink" href="#844-avl" title="Permanent link">&para;</a></h2>
<h2 id="854-avl">8.5.4. &nbsp; AVL 树典型应用<a class="headerlink" href="#854-avl" title="Permanent link">&para;</a></h2>
<ul>
<li>组织和存储大型数据,适用于高频查找、低频增删的场景;</li>
<li>用于构建数据库中的索引系统;</li>
@ -3832,7 +3848,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../binary_search_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.3. &amp;nbsp; 二叉搜索树" rel="prev">
<a href="../binary_search_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.4. &amp;nbsp; 二叉搜索树" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -3841,20 +3857,20 @@
上一页
</span>
<div class="md-ellipsis">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</div>
</div>
</a>
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.5. &amp;nbsp; 小结" rel="next">
<a href="../summary/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.6. &amp;nbsp; 小结" rel="next">
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</div>
</div>
<div class="md-footer__button md-icon">

View file

@ -15,7 +15,7 @@
<link rel="canonical" href="https://www.hello-algo.com/chapter_tree/binary_search_tree/">
<link rel="prev" href="../binary_tree_traversal/">
<link rel="prev" href="../array_representation_of_tree/">
<link rel="next" href="../avl_tree/">
@ -25,7 +25,7 @@
<title>8.3.   二叉搜索树 - Hello 算法</title>
<title>8.4.   二叉搜索树 - Hello 算法</title>
@ -79,7 +79,7 @@
<div data-md-component="skip">
<a href="#83" class="md-skip">
<a href="#84" class="md-skip">
跳转至
</a>
@ -113,7 +113,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</span>
</div>
@ -983,6 +983,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1029,6 +1031,20 @@
<li class="md-nav__item">
<a href="../array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
@ -1041,12 +1057,12 @@
<label class="md-nav__link md-nav__link--active" for="__toc">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
<span class="md-nav__icon md-icon"></span>
</label>
<a href="./" class="md-nav__link md-nav__link--active">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
@ -1065,11 +1081,11 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#831" class="md-nav__link">
8.3.1. &nbsp; 二叉搜索树的操作
<a href="#841" class="md-nav__link">
8.4.1. &nbsp; 二叉搜索树的操作
</a>
<nav class="md-nav" aria-label="8.3.1. &nbsp; 二叉搜索树的操作">
<nav class="md-nav" aria-label="8.4.1. &nbsp; 二叉搜索树的操作">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1106,15 +1122,15 @@
</li>
<li class="md-nav__item">
<a href="#832" class="md-nav__link">
8.3.2. &nbsp; 二叉搜索树的效率
<a href="#842" class="md-nav__link">
8.4.2. &nbsp; 二叉搜索树的效率
</a>
</li>
<li class="md-nav__item">
<a href="#833" class="md-nav__link">
8.3.3. &nbsp; 二叉搜索树常见应用
<a href="#843" class="md-nav__link">
8.4.3. &nbsp; 二叉搜索树常见应用
</a>
</li>
@ -1135,7 +1151,7 @@
<li class="md-nav__item">
<a href="../avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1149,7 +1165,7 @@
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -1822,11 +1838,11 @@
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
<li class="md-nav__item">
<a href="#831" class="md-nav__link">
8.3.1. &nbsp; 二叉搜索树的操作
<a href="#841" class="md-nav__link">
8.4.1. &nbsp; 二叉搜索树的操作
</a>
<nav class="md-nav" aria-label="8.3.1. &nbsp; 二叉搜索树的操作">
<nav class="md-nav" aria-label="8.4.1. &nbsp; 二叉搜索树的操作">
<ul class="md-nav__list">
<li class="md-nav__item">
@ -1863,15 +1879,15 @@
</li>
<li class="md-nav__item">
<a href="#832" class="md-nav__link">
8.3.2. &nbsp; 二叉搜索树的效率
<a href="#842" class="md-nav__link">
8.4.2. &nbsp; 二叉搜索树的效率
</a>
</li>
<li class="md-nav__item">
<a href="#833" class="md-nav__link">
8.3.3. &nbsp; 二叉搜索树常见应用
<a href="#843" class="md-nav__link">
8.4.3. &nbsp; 二叉搜索树常见应用
</a>
</li>
@ -1899,7 +1915,7 @@
<h1 id="83">8.3. &nbsp; 二叉搜索树<a class="headerlink" href="#83" title="Permanent link">&para;</a></h1>
<h1 id="84">8.4. &nbsp; 二叉搜索树<a class="headerlink" href="#84" title="Permanent link">&para;</a></h1>
<p>「二叉搜索树 Binary Search Tree」满足以下条件</p>
<ol>
<li>对于根节点,左子树中所有节点的值 <span class="arithmatex">\(&lt;\)</span> 根节点的值 <span class="arithmatex">\(&lt;\)</span> 右子树中所有节点的值;</li>
@ -1908,7 +1924,7 @@
<p><img alt="二叉搜索树" src="../binary_search_tree.assets/binary_search_tree.png" /></p>
<p align="center"> Fig. 二叉搜索树 </p>
<h2 id="831">8.3.1. &nbsp; 二叉搜索树的操作<a class="headerlink" href="#831" title="Permanent link">&para;</a></h2>
<h2 id="841">8.4.1. &nbsp; 二叉搜索树的操作<a class="headerlink" href="#841" title="Permanent link">&para;</a></h2>
<h3 id="_1">查找节点<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h3>
<p>给定目标节点值 <code>num</code> ,可以根据二叉搜索树的性质来查找。我们声明一个节点 <code>cur</code> ,从二叉树的根节点 <code>root</code> 出发,循环比较节点值 <code>cur.val</code><code>num</code> 之间的大小关系</p>
<ul>
@ -2912,7 +2928,7 @@
<p><img alt="二叉搜索树的中序遍历序列" src="../binary_search_tree.assets/bst_inorder_traversal.png" /></p>
<p align="center"> Fig. 二叉搜索树的中序遍历序列 </p>
<h2 id="832">8.3.2. &nbsp; 二叉搜索树的效率<a class="headerlink" href="#832" title="Permanent link">&para;</a></h2>
<h2 id="842">8.4.2. &nbsp; 二叉搜索树的效率<a class="headerlink" href="#842" title="Permanent link">&para;</a></h2>
<p>给定一组数据,我们考虑使用数组或二叉搜索树存储。</p>
<p>观察可知,二叉搜索树的各项操作的时间复杂度都是对数阶,具有稳定且高效的性能表现。只有在高频添加、低频查找删除的数据适用场景下,数组比二叉搜索树的效率更高。</p>
<div class="center-table">
@ -2948,7 +2964,7 @@
<p><img alt="二叉搜索树的平衡与退化" src="../binary_search_tree.assets/bst_degradation.png" /></p>
<p align="center"> Fig. 二叉搜索树的平衡与退化 </p>
<h2 id="833">8.3.3. &nbsp; 二叉搜索树常见应用<a class="headerlink" href="#833" title="Permanent link">&para;</a></h2>
<h2 id="843">8.4.3. &nbsp; 二叉搜索树常见应用<a class="headerlink" href="#843" title="Permanent link">&para;</a></h2>
<ul>
<li>用作系统中的多级索引,实现高效的查找、插入、删除操作。</li>
<li>作为某些搜索算法的底层数据结构。</li>
@ -3031,7 +3047,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../binary_tree_traversal/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.2. &amp;nbsp; 二叉树遍历" rel="prev">
<a href="../array_representation_of_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.3. &amp;nbsp; 二叉树数组表示" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -3040,20 +3056,20 @@
上一页
</span>
<div class="md-ellipsis">
8.2. &nbsp; 二叉树遍历
8.3. &nbsp; 二叉树数组表示
</div>
</div>
</a>
<a href="../avl_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.4. &amp;nbsp; AVL 树 *" rel="next">
<a href="../avl_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.5. &amp;nbsp; AVL 树 *" rel="next">
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</div>
</div>
<div class="md-footer__button md-icon">

View file

@ -983,6 +983,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1096,13 +1098,6 @@
8.1.4. &nbsp; 二叉树的退化
</a>
</li>
<li class="md-nav__item">
<a href="#815" class="md-nav__link">
8.1.5. &nbsp; 二叉树表示方式 *
</a>
</li>
</ul>
@ -1133,9 +1128,23 @@
<li class="md-nav__item">
<a href="../array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1149,7 +1158,7 @@
<li class="md-nav__item">
<a href="../avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1163,7 +1172,7 @@
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -1895,13 +1904,6 @@
8.1.4. &nbsp; 二叉树的退化
</a>
</li>
<li class="md-nav__item">
<a href="#815" class="md-nav__link">
8.1.5. &nbsp; 二叉树表示方式 *
</a>
</li>
</ul>
@ -2356,87 +2358,6 @@
</tbody>
</table>
</div>
<h2 id="815">8.1.5. &nbsp; 二叉树表示方式 *<a class="headerlink" href="#815" title="Permanent link">&para;</a></h2>
<p>我们通常使用二叉树的「链表表示」,即存储单位为节点 <code>TreeNode</code> ,节点之间通过指针相连接。本文前述示例代码展示了二叉树在链表表示下的各项基本操作。</p>
<p>那么,能否用「数组」来表示二叉树呢?答案是肯定的。先来分析一个简单案例,给定一个「完美二叉树」,将节点按照层序遍历的顺序编号(从 0 开始),那么可以推导得出父节点索引与子节点索引之间的“映射公式”:<strong>若节点的索引为 <span class="arithmatex">\(i\)</span> ,则该节点的左子节点索引为 <span class="arithmatex">\(2i + 1\)</span> ,右子节点索引为 <span class="arithmatex">\(2i + 2\)</span></strong></p>
<p><strong>本质上,映射公式的作用相当于链表中的指针</strong>。对于层序遍历序列中的任意节点,我们都可以使用映射公式来访问其子节点。因此,我们可以将二叉树的层序遍历序列存储到数组中,利用以上映射公式来表示二叉树。</p>
<p><img alt="完美二叉树的数组表示" src="../binary_tree.assets/array_representation_mapping.png" /></p>
<p align="center"> Fig. 完美二叉树的数组表示 </p>
<p>然而,完美二叉树只是一个特例。在二叉树的中间层,通常存在许多 <span class="arithmatex">\(\text{null}\)</span> ,而层序遍历序列并不包含这些 <span class="arithmatex">\(\text{null}\)</span> 。我们无法仅凭序列来推测空节点的数量和分布位置,<strong>这意味着理论上存在许多种二叉树都符合该层序遍历序列</strong>。显然,在这种情况下,我们无法使用数组来存储二叉树。</p>
<p><img alt="给定数组对应多种二叉树可能性" src="../binary_tree.assets/array_representation_without_empty.png" /></p>
<p align="center"> Fig. 给定数组对应多种二叉树可能性 </p>
<p>为了解决这个问题,我们可以考虑按照完美二叉树的形式来表示所有二叉树,<strong>并在序列中使用特殊符号来显式地表示 <span class="arithmatex">\(\text{null}\)</span></strong>。如下图所示,这样处理后,层序遍历序列就可以唯一表示二叉树了。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="4:10"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JavaScript</label><label for="__tabbed_4_6">TypeScript</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-30-1" name="__codelineno-30-1" href="#__codelineno-30-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-30-2" name="__codelineno-30-2" href="#__codelineno-30-2"></a><span class="c1">// 使用 int 的包装类 Integer ,就可以使用 null 来标记空位</span>
<a id="__codelineno-30-3" name="__codelineno-30-3" href="#__codelineno-30-3"></a><span class="n">Integer</span><span class="o">[]</span><span class="w"> </span><span class="n">tree</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="p">};</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-31-1" name="__codelineno-31-1" href="#__codelineno-31-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-31-2" name="__codelineno-31-2" href="#__codelineno-31-2"></a><span class="c1">// 为了符合数据类型为 int ,使用 int 最大值标记空位</span>
<a id="__codelineno-31-3" name="__codelineno-31-3" href="#__codelineno-31-3"></a><span class="c1">// 该方法的使用前提是没有节点的值 = INT_MAX</span>
<a id="__codelineno-31-4" name="__codelineno-31-4" href="#__codelineno-31-4"></a><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">tree</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">INT_MAX</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="n">INT_MAX</span><span class="p">,</span><span class="w"> </span><span class="n">INT_MAX</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="n">INT_MAX</span><span class="p">,</span><span class="w"> </span><span class="n">INT_MAX</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="p">};</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-32-1" name="__codelineno-32-1" href="#__codelineno-32-1"></a><span class="c1"># 二叉树的数组表示</span>
<a id="__codelineno-32-2" name="__codelineno-32-2" href="#__codelineno-32-2"></a><span class="c1"># 直接使用 None 来表示空位</span>
<a id="__codelineno-32-3" name="__codelineno-32-3" href="#__codelineno-32-3"></a><span class="n">tree</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">15</span><span class="p">]</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-33-1" name="__codelineno-33-1" href="#__codelineno-33-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-33-2" name="__codelineno-33-2" href="#__codelineno-33-2"></a><span class="c1">// 使用 any 类型的切片, 就可以使用 nil 来标记空位</span>
<a id="__codelineno-33-3" name="__codelineno-33-3" href="#__codelineno-33-3"></a><span class="nx">tree</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">[]</span><span class="kt">any</span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="kc">nil</span><span class="p">,</span><span class="w"> </span><span class="mi">15</span><span class="p">}</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-34-1" name="__codelineno-34-1" href="#__codelineno-34-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-34-2" name="__codelineno-34-2" href="#__codelineno-34-2"></a><span class="c1">// 直接使用 null 来表示空位</span>
<a id="__codelineno-34-3" name="__codelineno-34-3" href="#__codelineno-34-3"></a><span class="kd">let</span><span class="w"> </span><span class="nx">tree</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span><span class="w"> </span><span class="mf">9</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">12</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">15</span><span class="p">];</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-35-1" name="__codelineno-35-1" href="#__codelineno-35-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-35-2" name="__codelineno-35-2" href="#__codelineno-35-2"></a><span class="c1">// 直接使用 null 来表示空位</span>
<a id="__codelineno-35-3" name="__codelineno-35-3" href="#__codelineno-35-3"></a><span class="kd">let</span><span class="w"> </span><span class="nx">tree</span><span class="o">:</span><span class="w"> </span><span class="p">(</span><span class="kt">number</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="kc">null</span><span class="p">)[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span><span class="w"> </span><span class="mf">9</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">12</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="mf">15</span><span class="p">];</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-36-1" name="__codelineno-36-1" href="#__codelineno-36-1"></a>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-37-1" name="__codelineno-37-1" href="#__codelineno-37-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-37-2" name="__codelineno-37-2" href="#__codelineno-37-2"></a><span class="c1">// 使用 int? 可空类型 ,就可以使用 null 来标记空位</span>
<a id="__codelineno-37-3" name="__codelineno-37-3" href="#__codelineno-37-3"></a><span class="kt">int?</span><span class="p">[]</span><span class="w"> </span><span class="n">tree</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="m">1</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="k">null</span><span class="p">,</span><span class="w"> </span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="k">null</span><span class="p">,</span><span class="w"> </span><span class="k">null</span><span class="p">,</span><span class="w"> </span><span class="m">12</span><span class="p">,</span><span class="w"> </span><span class="k">null</span><span class="p">,</span><span class="w"> </span><span class="k">null</span><span class="p">,</span><span class="w"> </span><span class="m">15</span><span class="w"> </span><span class="p">};</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-38-1" name="__codelineno-38-1" href="#__codelineno-38-1"></a><span class="cm">/* 二叉树的数组表示 */</span>
<a id="__codelineno-38-2" name="__codelineno-38-2" href="#__codelineno-38-2"></a><span class="c1">// 使用 Int? 可空类型 ,就可以使用 nil 来标记空位</span>
<a id="__codelineno-38-3" name="__codelineno-38-3" href="#__codelineno-38-3"></a><span class="kd">let</span> <span class="nv">tree</span><span class="p">:</span> <span class="p">[</span><span class="nb">Int</span><span class="p">?]</span> <span class="p">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="kc">nil</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="kc">nil</span><span class="p">,</span> <span class="kc">nil</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="kc">nil</span><span class="p">,</span> <span class="kc">nil</span><span class="p">,</span> <span class="mi">15</span><span class="p">]</span>
</code></pre></div>
</div>
<div class="tabbed-block">
<div class="highlight"><pre><span></span><code><a id="__codelineno-39-1" name="__codelineno-39-1" href="#__codelineno-39-1"></a>
</code></pre></div>
</div>
</div>
</div>
<p><img alt="任意类型二叉树的数组表示" src="../binary_tree.assets/array_representation_with_empty.png" /></p>
<p align="center"> Fig. 任意类型二叉树的数组表示 </p>
<p><strong>完全二叉树非常适合使用数组来表示</strong>。回顾「完全二叉树」的定义,<span class="arithmatex">\(\text{null}\)</span> 只出现在最底层,并且最底层的节点尽量靠左。这意味着,<strong>所有空节点一定出现在层序遍历序列的末尾</strong>。由于我们事先知道了所有 <span class="arithmatex">\(\text{null}\)</span> 的位置,因此在使用数组表示完全二叉树时,可以省略存储它们。</p>
<p><img alt="完全二叉树的数组表示" src="../binary_tree.assets/array_representation_complete_binary_tree.png" /></p>
<p align="center"> Fig. 完全二叉树的数组表示 </p>
<p>数组表示具有两个显著优点:首先,它不需要存储指针,从而节省了空间;其次,它允许随机访问节点。然而,当二叉树中存在大量 <span class="arithmatex">\(\text{null}\)</span> 时,数组中包含的节点数据比重较低,导致有效空间利用率降低。</p>

View file

@ -18,7 +18,7 @@
<link rel="prev" href="../binary_tree/">
<link rel="next" href="../binary_search_tree/">
<link rel="next" href="../array_representation_of_tree/">
<link rel="icon" href="../../assets/images/favicon.png">
<meta name="generator" content="mkdocs-1.4.2, mkdocs-material-9.1.6">
@ -983,6 +983,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1118,9 +1120,23 @@
<li class="md-nav__item">
<a href="../array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1134,7 +1150,7 @@
<li class="md-nav__item">
<a href="../avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1148,7 +1164,7 @@
<li class="md-nav__item">
<a href="../summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -2552,13 +2568,13 @@
<a href="../binary_search_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.3. &amp;nbsp; 二叉搜索树" rel="next">
<a href="../array_representation_of_tree/" class="md-footer__link md-footer__link--next" aria-label="下一页: 8.3. &amp;nbsp; 二叉数组表示" rel="next">
<div class="md-footer__title">
<span class="md-footer__direction">
下一页
</span>
<div class="md-ellipsis">
8.3. &nbsp; 二叉搜索
8.3. &nbsp; 二叉树数组表示
</div>
</div>
<div class="md-footer__button md-icon">

View file

@ -25,7 +25,7 @@
<title>8.5.   小结 - Hello 算法</title>
<title>8.6.   小结 - Hello 算法</title>
@ -79,7 +79,7 @@
<div data-md-component="skip">
<a href="#85" class="md-skip">
<a href="#86" class="md-skip">
跳转至
</a>
@ -113,7 +113,7 @@
<div class="md-header__topic" data-md-component="header-topic">
<span class="md-ellipsis">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</span>
</div>
@ -983,6 +983,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1030,9 +1032,23 @@
<li class="md-nav__item">
<a href="../array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="../binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1046,7 +1062,7 @@
<li class="md-nav__item">
<a href="../avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1069,7 +1085,7 @@
<a href="./" class="md-nav__link md-nav__link--active">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>
@ -1757,7 +1773,7 @@
<h1 id="85">8.5. &nbsp; 小结<a class="headerlink" href="#85" title="Permanent link">&para;</a></h1>
<h1 id="86">8.6. &nbsp; 小结<a class="headerlink" href="#86" title="Permanent link">&para;</a></h1>
<ul>
<li>二叉树是一种非线性数据结构,体现“一分为二”的分治逻辑。每个二叉树节点包含一个值以及两个指针,分别指向其左子节点和右子节点。</li>
<li>对于二叉树中的某个节点,其左(右)子节点及其以下形成的树被称为该节点的左(右)子树。</li>
@ -1848,7 +1864,7 @@
<nav class="md-footer__inner md-grid" aria-label="页脚" >
<a href="../avl_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.4. &amp;nbsp; AVL 树 *" rel="prev">
<a href="../avl_tree/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 8.5. &amp;nbsp; AVL 树 *" rel="prev">
<div class="md-footer__button md-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
</div>
@ -1857,7 +1873,7 @@
上一页
</span>
<div class="md-ellipsis">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</div>
</div>
</a>

View file

@ -974,6 +974,8 @@
<label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
@ -1021,9 +1023,23 @@
<li class="md-nav__item">
<a href="chapter_tree/array_representation_of_tree/" class="md-nav__link">
8.3. &nbsp; 二叉树数组表示
</a>
</li>
<li class="md-nav__item">
<a href="chapter_tree/binary_search_tree/" class="md-nav__link">
8.3. &nbsp; 二叉搜索树
8.4. &nbsp; 二叉搜索树
</a>
</li>
@ -1037,7 +1053,7 @@
<li class="md-nav__item">
<a href="chapter_tree/avl_tree/" class="md-nav__link">
8.4. &nbsp; AVL 树 *
8.5. &nbsp; AVL 树 *
</a>
</li>
@ -1051,7 +1067,7 @@
<li class="md-nav__item">
<a href="chapter_tree/summary/" class="md-nav__link">
8.5. &nbsp; 小结
8.6. &nbsp; 小结
</a>
</li>

File diff suppressed because one or more lines are too long

View file

@ -245,6 +245,11 @@
<lastmod>2023-04-17</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://www.hello-algo.com/chapter_tree/array_representation_of_tree/</loc>
<lastmod>2023-04-17</lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>https://www.hello-algo.com/chapter_tree/avl_tree/</loc>
<lastmod>2023-04-17</lastmod>

Binary file not shown.