diff --git a/chapter_appendix/index.html b/chapter_appendix/index.html index 3bfe271a9..d235e9994 100644 --- a/chapter_appendix/index.html +++ b/chapter_appendix/index.html @@ -3600,9 +3600,9 @@
The world of data structures resembles a sturdy brick wall.
In arrays, envision bricks snugly aligned, each resting seamlessly beside the next, creating a unified formation. Meanwhile, in linked lists, these bricks disperse freely, embraced by vines gracefully knitting connections between them.
-Complexity analysis is like a space-time navigator in the vast universe of algorithms.
It guides us in exploring deeper within the the dimensions of time and space, seeking more elegant solutions.
-Data structures serve as a robust and diverse framework.
They offer a blueprint for the orderly organization of data, upon which algorithms come to life.
-In the journey of life, we are like individual nodes, connected by countless invisible edges.
Every encountering and parting leaves a unique mark on this vast network graph.
-In the world of computing, a hash table is akin to an intelligent librarian.
It understands how to compute index numbers, enabling swift retrieval of the desired book.
-The heap is like mountain peaks, stacked and undulating, each with its unique shape.
Among these peaks, the highest one always catches the eye first.
-A graceful maiden dances, intertwined with the data, her skirt swaying to the melody of algorithms.
She invites you to a dance, follow her steps, and enter the world of algorithms full of logic and beauty.
-Algorithms are like a beautiful symphony, with each line of code flowing like a rhythm.
May this book ring softly in your mind, leaving a unique and profound melody.
-A stack is like cats placed on top of each other, while a queue is like cats lined up one by one.
They represent the logical relationships of Last-In-First-Out (LIFO) and First-In-First-Out (FIFO), respectively.
-The towering tree, full of vitality with its roots deep and leaves lush, branches spreading wide.
It vividly demonstrates the form of data divide-and-conquer.
-Data Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code
Dive In Clone Repo Get PDF
The English version is brewing...
Feel free to engage in Chinese-to-English translation and pull request review! For guidelines, please see #914.
Quote
\"An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!\"
\u2014\u2014 Junhui Deng, Professor of Computer Science, Tsinghua University
Quote
\"If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!\"
\u2014\u2014 Mu Li, Senior Principal Scientist, Amazon
Animated illustrationsEasy to understandSmooth learning curve
\"A picture is worth a thousand words.\"
Off-the-Shelf CodeMulti programming languagesRun with one click
\"Talk is cheap. Show me the code.\"
Learning TogetherDiscussion and questions welcomeReaders progress together
\"Learning by teaching.\"
PrefaceTwo years ago, I shared the \"Sword Offer\" series of problem solutions on LeetCode, which received much love and support from many students. During my interactions with readers, the most common question I encountered was \"How to get started with algorithms.\" Gradually, I developed a deep interest in this question.
Blindly solving problems seems to be the most popular method, being simple, direct, and effective. However, problem-solving is like playing a \"Minesweeper\" game, where students with strong self-learning abilities can successfully clear the mines one by one, but those with insufficient foundations may end up bruised from explosions, retreating step by step in frustration. Thoroughly reading textbooks is also common, but for students aiming for job applications, the energy consumed by graduation, resume submissions, and preparing for written tests and interviews makes tackling thick books a daunting challenge.
If you are facing similar troubles, then you are lucky to have found this book. This book is my answer to this question, not necessarily the best solution, but at least an active attempt. Although this book won't directly land you an Offer, it will guide you through the \"knowledge map\" of data structures and algorithms, help you understand the shape, size, and distribution of different \"mines,\" and equip you with various \"demining methods.\" With these skills, I believe you can more comfortably solve problems and read literature, gradually building a complete knowledge system.
I deeply agree with Professor Feynman's saying: \"Knowledge isn't free. You have to pay attention.\" In this sense, this book is not entirely \"free.\" To not disappoint the precious \"attention\" you pay to this book, I will do my utmost, investing the greatest \"attention\" to complete the creation of this book.
AuthorYudong Jin(krahets), Senior Algorithm Engineer in a top tech company, Master's degree from Shanghai Jiao Tong University. The highest-read blogger across the entire LeetCode, his published \"Illustration of Algorithm Data Structures\" has been subscribed to by over 300k.
ContributionThis 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:
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, 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.
codingonionRust, Zig GongljaC, C++ gvenusleoDart hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C"},{"location":"chapter_array_and_linkedlist/","title":"Chapter 4. \u00a0 Arrays and linked lists","text":"Abstract
The world of data structures resembles a sturdy brick wall.
In arrays, envision bricks snugly aligned, each resting seamlessly beside the next, creating a unified formation. Meanwhile, in linked lists, these bricks disperse freely, embraced by vines gracefully knitting connections between them.
"},{"location":"chapter_array_and_linkedlist/#chapter-contents","title":"Chapter Contents","text":"An \"array\" is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an \"index\". Please refer to the Figure 4-1 to observe how arrays work and grasp these key terms.
Figure 4-1 \u00a0 Array definition and storage method
"},{"location":"chapter_array_and_linkedlist/array/#411-common-operations-on-arrays","title":"4.1.1 \u00a0 Common operations on arrays","text":""},{"location":"chapter_array_and_linkedlist/array/#1-initializing-arrays","title":"1. \u00a0 Initializing arrays","text":"Arrays can be initialized in two ways depending on the needs: either without initial values or with specified initial values. When initial values are not specified, most programming languages will set the array elements to \\(0\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig array.py# Initialize array\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
array.cpp/* Initialize array */\n// Stored on stack\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// Stored on heap (manual memory release needed)\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java/* Initialize array */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs/* Initialize array */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
array.go/* Initialize array */\nvar arr [5]int\n// In Go, specifying the length ([5]int) denotes an array, while not specifying it ([]int) denotes a slice.\n// Since Go's arrays are designed to have compile-time fixed length, only constants can be used to specify the length.\n// For convenience in implementing the extend() method, the Slice will be considered as an Array here.\nnums := []int{1, 3, 2, 5, 4}\n
array.swift/* Initialize array */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js/* Initialize array */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts/* Initialize array */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart/* Initialize array */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs/* Initialize array */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c/* Initialize array */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.kt\n
array.zig// Initialize array\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2-accessing-elements","title":"2. \u00a0 Accessing elements","text":"Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element.
Figure 4-2 \u00a0 Memory address calculation for array elements
As observed in the above illustration, array indexing conventionally begins at \\(0\\). While this might appear counterintuitive, considering counting usually starts at \\(1\\), within the address calculation formula, an index is essentially an offset from the memory address. For the first element's address, this offset is \\(0\\), validating its index as \\(0\\).
Accessing elements in an array is highly efficient, allowing us to randomly access any element in \\(O(1)\\) time.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef random_access(nums: list[int]) -> int:\n \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = random.randint(0, len(nums) - 1)\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n random_num = nums[random_index]\n return random_num\n
array.cpp/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.java/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.cs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n Random random = new();\n // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = random.Next(nums.Length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.go/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n randomIndex := rand.Intn(len(nums))\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n randomNum = nums[randomIndex]\n return\n}\n
array.swift/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let randomIndex = nums.indices.randomElement()!\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let randomNum = nums[randomIndex]\n return randomNum\n}\n
array.js/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.ts/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.dart/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = Random().nextInt(nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.rs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let random_index = rand::thread_rng().gen_range(0..nums.len());\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let random_num = nums[random_index];\n random_num\n}\n
array.c/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.kt/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfun randomAccess(nums: IntArray): Int {\n // \u5728\u533a\u95f4 [0, nums.size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n val randomNum = nums[randomIndex]\n return randomNum\n}\n
array.rb### \u968f\u673a\u8bbf\u95ee\u5143\u7d20 ###\ndef random_access(nums)\n # \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = Random.rand(0...nums.length)\n\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n nums[random_index]\nend\n
array.zig// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n // \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\n var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n var randomNum = nums[randomIndex];\n return randomNum;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#3-inserting-elements","title":"3. \u00a0 Inserting elements","text":"Array elements are tightly packed in memory, with no space available to accommodate additional data between them. Illustrated in Figure below, inserting an element in the middle of an array requires shifting all subsequent elements back by one position to create room for the new element.
Figure 4-3 \u00a0 Array element insertion example
It's important to note that due to the fixed length of an array, inserting an element will unavoidably result in the loss of the last element in the array. Solutions to address this issue will be explored in the \"List\" chapter.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef insert(nums: list[int], num: int, index: int):\n \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in range(len(nums) - 1, index, -1):\n nums[i] = nums[i - 1]\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n
array.cpp/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.java/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.cs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.Length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.go/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i := len(nums) - 1; i > index; i-- {\n nums[i] = nums[i-1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.swift/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).reversed() {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.js/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.ts/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.dart/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n nums[index] = _num;\n}\n
array.rs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (index + 1..nums.len()).rev() {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.c/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.kt/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfun insert(nums: IntArray, num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (i in nums.size - 1 downTo index + 1) {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.rb### \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num ###\ndef insert(nums, num, index)\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (nums.length - 1).downto(index + 1)\n nums[i] = nums[i - 1]\n end\n\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\nend\n
array.zig// \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var i = nums.len - 1;\n while (i > index) : (i -= 1) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#4-deleting-elements","title":"4. \u00a0 Deleting elements","text":"Similarly, as depicted in the Figure 4-4 , to delete an element at index \\(i\\), all elements following index \\(i\\) must be moved forward by one position.
Figure 4-4 \u00a0 Array element deletion example
Please note that after deletion, the former last element becomes \"meaningless,\" hence requiring no specific modification.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef remove(nums: list[int], index: int):\n \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in range(index, len(nums) - 1):\n nums[i] = nums[i + 1]\n
array.cpp/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.java/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.cs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.Length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.go/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i := index; i < len(nums)-1; i++ {\n nums[i] = nums[i+1]\n }\n}\n
array.swift/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).dropLast() {\n nums[i] = nums[i + 1]\n }\n}\n
array.js/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.ts/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.dart/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.rs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index..nums.len() - 1 {\n nums[i] = nums[i + 1];\n }\n}\n
array.c/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.kt/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfun remove(nums: IntArray, index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (i in index..<nums.size - 1) {\n nums[i] = nums[i + 1]\n }\n}\n
array.rb### \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 ###\ndef remove(nums, index)\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index...nums.length\n nums[i] = nums[i + 1] || 0\n end\nend\n
array.zig// \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var i = index;\n while (i < nums.len - 1) : (i += 1) {\n nums[i] = nums[i + 1];\n }\n}\n
Code Visualization Full Screen >
In summary, the insertion and deletion operations in arrays present the following disadvantages:
In most programming languages, we can traverse an array either by using indices or by directly iterating over each element:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef traverse(nums: list[int]):\n \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n count = 0\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in range(len(nums)):\n count += nums[i]\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums:\n count += num\n # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num in enumerate(nums):\n count += nums[i]\n count += num\n
array.cpp/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.java/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int num : nums) {\n count += num;\n }\n}\n
array.cs/* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n foreach (int num in nums) {\n count += num;\n }\n}\n
array.go/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n count := 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i := 0; i < len(nums); i++ {\n count += nums[i]\n }\n count = 0\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for _, num := range nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num := range nums {\n count += nums[i]\n count += num\n }\n}\n
array.swift/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in nums.indices {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for (i, num) in nums.enumerated() {\n count += nums[i]\n count += num\n }\n}\n
array.js/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.ts/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.dart/* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int _num in nums) {\n count += _num;\n }\n // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n nums.forEach((_num) {\n count += _num;\n });\n}\n
array.rs/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n let mut _count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0..nums.len() {\n _count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n _count += num;\n }\n}\n
array.c/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.kt/* \u904d\u5386\u6570\u7ec4 */\nfun traverse(nums: IntArray) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (i in nums.indices) {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (j: Int in nums) {\n count += j\n }\n}\n
array.rb### \u904d\u5386\u6570\u7ec4 ###\ndef traverse(nums)\n count = 0\n\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0...nums.length\n count += nums[i]\n end\n\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums\n count += num\n end\nend\n
array.zig// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\n var count: i32 = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n var i: i32 = 0;\n while (i < nums.len) : (i += 1) {\n count += nums[i];\n }\n count = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (nums) |num| {\n count += num;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#6-finding-elements","title":"6. \u00a0 Finding elements","text":"Locating a specific element within an array involves iterating through the array, checking each element to determine if it matches the desired value.
Because arrays are linear data structures, this operation is commonly referred to as \"linear search.\"
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef find(nums: list[int], target: int) -> int:\n \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n for i in range(len(nums)):\n if nums[i] == target:\n return i\n return -1\n
array.cpp/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.java/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n for (int i = 0; i < nums.length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.cs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n for (int i = 0; i < nums.Length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.go/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n index = -1\n for i := 0; i < len(nums); i++ {\n if nums[i] == target {\n index = i\n break\n }\n }\n return\n}\n
array.swift/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n for i in nums.indices {\n if nums[i] == target {\n return i\n }\n }\n return -1\n}\n
array.js/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) return i;\n }\n return -1;\n}\n
array.ts/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) {\n return i;\n }\n }\n return -1;\n}\n
array.dart/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n for (var i = 0; i < nums.length; i++) {\n if (nums[i] == target) return i;\n }\n return -1;\n}\n
array.rs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n for i in 0..nums.len() {\n if nums[i] == target {\n return Some(i);\n }\n }\n None\n}\n
array.c/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.kt/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfun find(nums: IntArray, target: Int): Int {\n for (i in nums.indices) {\n if (nums[i] == target) return i\n }\n return -1\n}\n
array.rb### \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 ###\ndef find(nums, target)\n for i in 0...nums.length\n return i if nums[i] == target\n end\n\n -1\nend\n
array.zig// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\n for (nums, 0..) |num, i| {\n if (num == target) return @intCast(i);\n }\n return -1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#7-expanding-arrays","title":"7. \u00a0 Expanding arrays","text":"In complex system environments, ensuring the availability of memory space after an array for safe capacity extension becomes challenging. Consequently, in most programming languages, the length of an array is immutable.
To expand an array, it's necessary to create a larger array and then copy the elements from the original array. This operation has a time complexity of \\(O(n)\\) and can be time-consuming for large arrays. The code are as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef extend(nums: list[int], enlarge: int) -> list[int]:\n \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = [0] * (len(nums) + enlarge)\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in range(len(nums)):\n res[i] = nums[i]\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n
array.cpp/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = new int[size + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] nums;\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.java/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.cs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.Length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.go/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res := make([]int, len(nums)+enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i, num := range nums {\n res[i] = num\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.swift/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = Array(repeating: 0, count: nums.count + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in nums.indices {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.js/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.ts/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.dart/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n List<int> res = List.filled(nums.length + enlarge, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.rs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n for i in 0..nums.len() {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\n}\n
array.c/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n for (int i = size; i < size + enlarge; i++) {\n res[i] = 0;\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.kt/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n val res = IntArray(nums.size + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (i in nums.indices) {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.rb### \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 ###\n# \u8bf7\u6ce8\u610f\uff0cRuby \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n# \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\ndef extend(nums, enlarge)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = Array.new(nums.length + enlarge, 0)\n\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in 0...nums.length\n res[i] = nums[i]\n end\n\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\nend\n
array.zig// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = try mem_allocator.alloc(i32, nums.len + enlarge);\n @memset(res, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(i32, res, nums);\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#412-advantages-and-limitations-of-arrays","title":"4.1.2 \u00a0 Advantages and limitations of arrays","text":"Arrays are stored in contiguous memory spaces and consist of elements of the same type. This approach provides substantial prior information that systems can leverage to optimize the efficiency of data structure operations.
However, continuous space storage is a double-edged sword, with the following limitations:
Arrays are fundamental and widely used data structures. They find frequent application in various algorithms and serve in the implementation of complex data structures.
Memory space is a shared resource among all programs. In a complex system environment, available memory can be dispersed throughout the memory space. We understand that the memory allocated for an array must be continuous. However, for very large arrays, finding a sufficiently large contiguous memory space might be challenging. This is where the flexible advantage of linked lists becomes evident.
A \"linked list\" is a linear data structure in which each element is a node object, and the nodes are interconnected through \"references\". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.
The design of linked lists allows for their nodes to be distributed across memory locations without requiring contiguous memory addresses.
Figure 4-5 \u00a0 Linked list definition and storage method
As shown in the figure, we see that the basic building block of a linked list is the \"node\" object. Each node comprises two key components: the node's \"value\" and a \"reference\" to the next node.
null
in Java, nullptr
in C++, and None
in Python.As the code below illustrates, a ListNode
in a linked list, besides holding a value, must also maintain an additional reference (or pointer). Therefore, a linked list occupies more memory space than an array when storing the same quantity of data..
class ListNode:\n \"\"\"Linked list node class\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.next: ListNode | None = None # Reference to the next node\n
/* Linked list node structure */\nstruct ListNode {\n int val; // Node value\n ListNode *next; // Pointer to the next node\n ListNode(int x) : val(x), next(nullptr) {} // Constructor\n};\n
/* Linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode(int x) { val = x; } // Constructor\n}\n
/* Linked list node class */\nclass ListNode(int x) { // Constructor\n int val = x; // Node value\n ListNode? next; // Reference to the next node\n}\n
/* Linked list node structure */\ntype ListNode struct {\n Val int // Node value\n Next *ListNode // Pointer to the next node\n}\n\n// NewListNode Constructor, creates a new linked list\nfunc NewListNode(val int) *ListNode {\n return &ListNode{\n Val: val,\n Next: nil,\n }\n}\n
/* Linked list node class */\nclass ListNode {\n var val: Int // Node value\n var next: ListNode? // Reference to the next node\n\n init(x: Int) { // Constructor\n val = x\n }\n}\n
/* Linked list node class */\nclass ListNode {\n constructor(val, next) {\n this.val = (val === undefined ? 0 : val); // Node value\n this.next = (next === undefined ? null : next); // Reference to the next node\n }\n}\n
/* Linked list node class */\nclass ListNode {\n val: number;\n next: ListNode | null;\n constructor(val?: number, next?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the next node\n }\n}\n
/* Linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode? next; // Reference to the next node\n ListNode(this.val, [this.next]); // Constructor\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* Linked list node class */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // Node value\n next: Option<Rc<RefCell<ListNode>>>, // Pointer to the next node\n}\n
/* Linked list node structure */\ntypedef struct ListNode {\n int val; // Node value\n struct ListNode *next; // Pointer to the next node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n return node;\n}\n
\n
// Linked list node class\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // Node value\n next: ?*Self = null, // Pointer to the next node\n\n // Constructor\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421-common-operations-on-linked-lists","title":"4.2.1 \u00a0 Common operations on linked lists","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1-initializing-a-linked-list","title":"1. \u00a0 Initializing a linked list","text":"Constructing a linked list is a two-step process: first, initializing each node object, and second, forming the reference links between the nodes. After initialization, we can traverse all nodes sequentially from the head node by following the next
reference.
# Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.cpp/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.java/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.cs/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.go/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// Build references between nodes\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
linked_list.swift/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.js/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.ts/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.dart/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rs/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// Build references between nodes\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.kt\n
linked_list.zig// Initialize linked list\n// Initialize each node\nvar n0 = inc.ListNode(i32){.val = 1};\nvar n1 = inc.ListNode(i32){.val = 3};\nvar n2 = inc.ListNode(i32){.val = 2};\nvar n3 = inc.ListNode(i32){.val = 5};\nvar n4 = inc.ListNode(i32){.val = 4};\n// Build references between nodes\nn0.next = &n1;\nn1.next = &n2;\nn2.next = &n3;\nn3.next = &n4;\n
The array as a whole is a variable, for instance, the array nums
includes elements like nums[0]
, nums[1]
, and so on, whereas a linked list is made up of several distinct node objects. We typically refer to a linked list by its head node, for example, the linked list in the previous code snippet is referred to as n0
.
Inserting a node into a linked list is very easy. As shown in the figure, let's assume we aim to insert a new node P
between two adjacent nodes n0
and n1
. This can be achieved by simply modifying two node references (pointers), with a time complexity of \\(O(1)\\).
By comparison, inserting an element into an array has a time complexity of \\(O(n)\\), which becomes less efficient when dealing with large data volumes.
Figure 4-6 \u00a0 Linked list node insertion example
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef insert(n0: ListNode, P: ListNode):\n \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n n1 = n0.next\n P.next = n1\n n0.next = P\n
linked_list.cpp/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n n1 := n0.Next\n P.Next = n1\n n0.Next = P\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n let n1 = n0.next\n P.next = n1\n n0.next = P\n}\n
linked_list.js/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n let n1 = n0.borrow_mut().next.take();\n P.borrow_mut().next = n1;\n n0.borrow_mut().next = Some(P);\n}\n
linked_list.c/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.kt/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9p */\nfun insert(n0: ListNode?, p: ListNode?) {\n val n1 = n0?.next\n p?.next = n1\n n0?.next = p\n}\n
linked_list.rb### \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 _p ###\n# Ruby \u7684 `p` \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c `P` \u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 `_p` \u4ee3\u66ff\ndef insert(n0, _p)\n n1 = n0.next\n _p.next = n1\n n0.next = _p\nend\n
linked_list.zig// \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\n var n1 = n0.?.next;\n P.?.next = n1;\n n0.?.next = P;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#3-deleting-nodes","title":"3. \u00a0 Deleting nodes","text":"As shown in the figure, deleting a node from a linked list is also very easy, involving only the modification of a single node's reference (pointer).
It's important to note that even though node P
continues to point to n1
after being deleted, it becomes inaccessible during linked list traversal. This effectively means that P
is no longer a part of the linked list.
Figure 4-7 \u00a0 Linked list node deletion
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef remove(n0: ListNode):\n \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n if not n0.next:\n return\n # n0 -> P -> n1\n P = n0.next\n n1 = P.next\n n0.next = n1\n
linked_list.cpp/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n if (n0->next == nullptr)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n delete P;\n}\n
linked_list.java/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode n1 = P.next;\n n0.next = n1;\n}\n
linked_list.cs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.go/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n if n0.Next == nil {\n return\n }\n // n0 -> P -> n1\n P := n0.Next\n n1 := P.Next\n n0.Next = n1\n}\n
linked_list.swift/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n if n0.next == nil {\n return\n }\n // n0 -> P -> n1\n let P = n0.next\n let n1 = P?.next\n n0.next = n1\n}\n
linked_list.js/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n if (!n0.next) return;\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.ts/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n if (!n0.next) {\n return;\n }\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.dart/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null) return;\n // n0 -> P -> n1\n ListNode P = n0.next!;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.rs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n if n0.borrow().next.is_none() {\n return;\n };\n // n0 -> P -> n1\n let P = n0.borrow_mut().next.take();\n if let Some(node) = P {\n let n1 = node.borrow_mut().next.take();\n n0.borrow_mut().next = n1;\n }\n}\n
linked_list.c/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n if (!n0->next)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n free(P);\n}\n
linked_list.kt/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfun remove(n0: ListNode?) {\n val p = n0?.next\n val n1 = p?.next\n n0?.next = n1\n}\n
linked_list.rb### \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 ###\ndef remove(n0)\n return if n0.next.nil?\n\n # n0 -> remove_node -> n1\n remove_node = n0.next\n n1 = remove_node.next\n n0.next = n1\nend\n
linked_list.zig// \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\n if (n0.?.next == null) return;\n // n0 -> P -> n1\n var P = n0.?.next;\n var n1 = P.?.next;\n n0.?.next = n1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#4-accessing-nodes","title":"4. \u00a0 Accessing nodes","text":"Accessing nodes in a linked list is less efficient. As previously mentioned, any element in an array can be accessed in \\(O(1)\\) time. In contrast, with a linked list, the program involves starting from the head node and sequentially traversing through the nodes until the desired node is found. In other words, to access the \\(i\\)-th node in a linked list, the program must iterate through \\(i - 1\\) nodes, resulting in a time complexity of \\(O(n)\\).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef access(head: ListNode, index: int) -> ListNode | None:\n \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n for _ in range(index):\n if not head:\n return None\n head = head.next\n return head\n
linked_list.cpp/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == nullptr)\n return nullptr;\n head = head->next;\n }\n return head;\n}\n
linked_list.java/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.cs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.go/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n for i := 0; i < index; i++ {\n if head == nil {\n return nil\n }\n head = head.Next\n }\n return head\n}\n
linked_list.swift/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n var head: ListNode? = head\n for _ in 0 ..< index {\n if head == nil {\n return nil\n }\n head = head?.next\n }\n return head\n}\n
linked_list.js/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.ts/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.dart/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n for (var i = 0; i < index; i++) {\n if (head == null) return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.rs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\n if index <= 0 {\n return head;\n };\n if let Some(node) = &head.borrow().next {\n return access(node.clone(), index - 1);\n }\n\n return head;\n}\n
linked_list.c/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == NULL)\n return NULL;\n head = head->next;\n }\n return head;\n}\n
linked_list.kt/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfun access(head: ListNode?, index: Int): ListNode? {\n var h = head\n for (i in 0..<index) {\n h = h?.next\n }\n return h\n}\n
linked_list.rb### \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 ###\ndef access(head, index)\n for i in 0...index\n return nil if head.nil?\n head = head.next\n end\n\n head\nend\n
linked_list.zig// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\n var head = node;\n var i: i32 = 0;\n while (i < index) : (i += 1) {\n head = head.?.next;\n if (head == null) return null;\n }\n return head;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#5-finding-nodes","title":"5. \u00a0 Finding nodes","text":"Traverse the linked list to locate a node whose value matches target
, and then output the index of that node within the linked list. This procedure is also an example of linear search. The corresponding code is provided below:
def find(head: ListNode, target: int) -> int:\n \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n index = 0\n while head:\n if head.val == target:\n return index\n head = head.next\n index += 1\n return -1\n
linked_list.cpp/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head != nullptr) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n index := 0\n for head != nil {\n if head.Val == target {\n return index\n }\n head = head.Next\n index++\n }\n return -1\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n var head: ListNode? = head\n var index = 0\n while head != nil {\n if head?.val == target {\n return index\n }\n head = head?.next\n index += 1\n }\n return -1\n}\n
linked_list.js/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target) {\n return index;\n }\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\n if head.borrow().val == target {\n return index;\n };\n if let Some(node) = &head.borrow_mut().next {\n return find(node.clone(), target, index + 1);\n }\n return -1;\n}\n
linked_list.c/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.kt/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfun find(head: ListNode?, target: Int): Int {\n var index = 0\n var h = head\n while (h != null) {\n if (h.value == target) return index\n h = h.next\n index++\n }\n return -1\n}\n
linked_list.rb### \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 ###\ndef find(head, target)\n index = 0\n while head\n return index if head.val == target\n head = head.next\n index += 1\n end\n\n -1\nend\n
linked_list.zig// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\n var head = node;\n var index: i32 = 0;\n while (head != null) {\n if (head.?.val == target) return index;\n head = head.?.next;\n index += 1;\n }\n return -1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-arrays-vs-linked-lists","title":"4.2.2 \u00a0 Arrays vs. linked lists","text":"The Table 4-1 summarizes the characteristics of arrays and linked lists, and it also compares their efficiencies in various operations. Because they utilize opposing storage strategies, their respective properties and operational efficiencies exhibit distinct contrasts.
Table 4-1 \u00a0 Efficiency comparison of arrays and linked lists
Arrays Linked Lists Storage Contiguous Memory Space Dispersed Memory Space Capacity Expansion Fixed Length Flexible Expansion Memory Efficiency Less Memory per Element, Potential Space Wastage More Memory per Element Accessing Elements \\(O(1)\\) \\(O(n)\\) Adding Elements \\(O(n)\\) \\(O(1)\\) Deleting Elements \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423-common-types-of-linked-lists","title":"4.2.3 \u00a0 Common types of linked lists","text":"As shown in the figure, there are three common types of linked lists.
None
), is the tail node.class ListNode:\n \"\"\"Bidirectional linked list node class\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.next: ListNode | None = None # Reference to the successor node\n self.prev: ListNode | None = None # Reference to a predecessor node\n
/* Bidirectional linked list node structure */\nstruct ListNode {\n int val; // Node value\n ListNode *next; // Pointer to the successor node\n ListNode *prev; // Pointer to the predecessor node\n ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // Constructor\n};\n
/* Bidirectional linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n ListNode(int x) { val = x; } // Constructor\n}\n
/* Bidirectional linked list node class */\nclass ListNode(int x) { // Constructor\n int val = x; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n}\n
/* Bidirectional linked list node structure */\ntype DoublyListNode struct {\n Val int // Node value\n Next *DoublyListNode // Pointer to the successor node\n Prev *DoublyListNode // Pointer to the predecessor node\n}\n\n// NewDoublyListNode initialization\nfunc NewDoublyListNode(val int) *DoublyListNode {\n return &DoublyListNode{\n Val: val,\n Next: nil,\n Prev: nil,\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n var val: Int // Node value\n var next: ListNode? // Reference to the next node\n var prev: ListNode? // Reference to the predecessor node\n\n init(x: Int) { // Constructor\n val = x\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n constructor(val, next, prev) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the successor node\n this.prev = prev === undefined ? null : prev; // Reference to the predecessor node\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n val: number;\n next: ListNode | null;\n prev: ListNode | null;\n constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the successor node\n this.prev = prev === undefined ? null : prev; // Reference to the predecessor node\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n ListNode(this.val, [this.next, this.prev]); // Constructor\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Bidirectional linked list node type */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // Node value\n next: Option<Rc<RefCell<ListNode>>>, // Pointer to successor node\n prev: Option<Rc<RefCell<ListNode>>>, // Pointer to predecessor node\n}\n\n/* Constructors */\nimpl ListNode {\n fn new(val: i32) -> Self {\n ListNode {\n val,\n next: None,\n prev: None,\n }\n }\n}\n
/* Bidirectional linked list node structure */\ntypedef struct ListNode {\n int val; // Node value\n struct ListNode *next; // Pointer to the successor node\n struct ListNode *prev; // Pointer to the predecessor node\n} ListNode;\n\n/* Constructors */\nListNode *newListNode(int val) {\n ListNode *node, *next;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n node->prev = NULL;\n return node;\n}\n
\n
// Bidirectional linked list node class\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // Node value\n next: ?*Self = null, // Pointer to the successor node\n prev: ?*Self = null, // Pointer to the predecessor node\n\n // Constructor\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n
Figure 4-8 \u00a0 Common types of linked lists
"},{"location":"chapter_array_and_linkedlist/linked_list/#424-typical-applications-of-linked-lists","title":"4.2.4 \u00a0 Typical applications of linked lists","text":"Singly linked lists are frequently utilized in implementing stacks, queues, hash tables, and graphs.
Doubly linked lists are ideal for scenarios requiring rapid access to preceding and succeeding elements.
Circular linked lists are ideal for applications that require periodic operations, such as resource scheduling in operating systems.
A \"list\" is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.
When implementing lists using arrays, the immutability of length reduces the practicality of the list. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.
To solve this problem, we can implement lists using a \"dynamic array.\" It inherits the advantages of arrays and can dynamically expand during program execution.
In fact, many programming languages' standard libraries implement lists using dynamic arrays, such as Python's list
, Java's ArrayList
, C++'s vector
, and C#'s List
. In the following discussion, we will consider \"list\" and \"dynamic array\" as synonymous concepts.
We typically use two initialization methods: \"without initial values\" and \"with initial values\".
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Initialize list\n# Without initial values\nnums1: list[int] = []\n# With initial values\nnums: list[int] = [1, 3, 2, 5, 4]\n
list.cpp/* Initialize list */\n// Note, in C++ the vector is the equivalent of nums described here\n// Without initial values\nvector<int> nums1;\n// With initial values\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java/* Initialize list */\n// Without initial values\nList<Integer> nums1 = new ArrayList<>();\n// With initial values (note the element type should be the wrapper class Integer[] for int[])\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs/* Initialize list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
list_test.go/* Initialize list */\n// Without initial values\nnums1 := []int{}\n// With initial values\nnums := []int{1, 3, 2, 5, 4}\n
list.swift/* Initialize list */\n// Without initial values\nlet nums1: [Int] = []\n// With initial values\nvar nums = [1, 3, 2, 5, 4]\n
list.js/* Initialize list */\n// Without initial values\nconst nums1 = [];\n// With initial values\nconst nums = [1, 3, 2, 5, 4];\n
list.ts/* Initialize list */\n// Without initial values\nconst nums1: number[] = [];\n// With initial values\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart/* Initialize list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs/* Initialize list */\n// Without initial values\nlet nums1: Vec<i32> = Vec::new();\n// With initial values\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Initialize list\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2-accessing-elements","title":"2. \u00a0 Accessing elements","text":"Lists are essentially arrays, thus they can access and update elements in \\(O(1)\\) time, which is very efficient.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Access elements\nnum: int = nums[1] # Access the element at index 1\n\n# Update elements\nnums[1] = 0 # Update the element at index 1 to 0\n
list.cpp/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.java/* Access elements */\nint num = nums.get(1); // Access the element at index 1\n\n/* Update elements */\nnums.set(1, 0); // Update the element at index 1 to 0\n
list.cs/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list_test.go/* Access elements */\nnum := nums[1] // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0 // Update the element at index 1 to 0\n
list.swift/* Access elements */\nlet num = nums[1] // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0 // Update the element at index 1 to 0\n
list.js/* Access elements */\nconst num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.ts/* Access elements */\nconst num: number = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.dart/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.rs/* Access elements */\nlet num: i32 = nums[1]; // Access the element at index 1\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Access elements\nvar num = nums.items[1]; // Access the element at index 1\n\n// Update elements\nnums.items[1] = 0; // Update the element at index 1 to 0 \n
"},{"location":"chapter_array_and_linkedlist/list/#3-inserting-and-removing-elements","title":"3. \u00a0 Inserting and removing elements","text":"Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an \\(O(1)\\) operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of \\(O(n)\\).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Clear list\nnums.clear()\n\n# Append elements at the end\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# Insert element in the middle\nnums.insert(3, 6) # Insert number 6 at index 3\n\n# Remove elements\nnums.pop(3) # Remove the element at index 3\n
list.cpp/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* Insert element in the middle */\nnums.insert(nums.begin() + 3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.erase(nums.begin() + 3); // Remove the element at index 3\n
list.java/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert element in the middle */\nnums.add(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(3); // Remove the element at index 3\n
list.cs/* Clear list */\nnums.Clear();\n\n/* Append elements at the end */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* Insert element in the middle */\nnums.Insert(3, 6);\n\n/* Remove elements */\nnums.RemoveAt(3);\n
list_test.go/* Clear list */\nnums = nil\n\n/* Append elements at the end */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* Insert element in the middle */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3\n\n/* Remove elements */\nnums = append(nums[:3], nums[4:]...) // Remove the element at index 3\n
list.swift/* Clear list */\nnums.removeAll()\n\n/* Append elements at the end */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* Insert element in the middle */\nnums.insert(6, at: 3) // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(at: 3) // Remove the element at index 3\n
list.js/* Clear list */\nnums.length = 0;\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.splice(3, 0, 6);\n\n/* Remove elements */\nnums.splice(3, 1);\n
list.ts/* Clear list */\nnums.length = 0;\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.splice(3, 0, 6);\n\n/* Remove elements */\nnums.splice(3, 1);\n
list.dart/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.removeAt(3); // Remove the element at index 3\n
list.rs/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(3); // Remove the element at index 3\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Clear list\nnums.clearRetainingCapacity();\n\n// Append elements at the end\ntry nums.append(1);\ntry nums.append(3);\ntry nums.append(2);\ntry nums.append(5);\ntry nums.append(4);\n\n// Insert element in the middle\ntry nums.insert(3, 6); // Insert number 6 at index 3\n\n// Remove elements\n_ = nums.orderedRemove(3); // Remove the element at index 3\n
"},{"location":"chapter_array_and_linkedlist/list/#4-iterating-the-list","title":"4. \u00a0 Iterating the list","text":"Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Iterate through the list by index\ncount = 0\nfor i in range(len(nums)):\n count += nums[i]\n\n# Iterate directly through list elements\nfor num in nums:\n count += num\n
list.cpp/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (int num : nums) {\n count += num;\n}\n
list.java/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums.get(i);\n}\n\n/* Iterate directly through list elements */\nfor (int num : nums) {\n count += num;\n}\n
list.cs/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nforeach (int num in nums) {\n count += num;\n}\n
list_test.go/* Iterate through the list by index */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n count += nums[i]\n}\n\n/* Iterate directly through list elements */\ncount = 0\nfor _, num := range nums {\n count += num\n}\n
list.swift/* Iterate through the list by index */\nvar count = 0\nfor i in nums.indices {\n count += nums[i]\n}\n\n/* Iterate directly through list elements */\ncount = 0\nfor num in nums {\n count += num\n}\n
list.js/* Iterate through the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.ts/* Iterate through the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.dart/* Iterate through the list by index */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (var num in nums) {\n count += num;\n}\n
list.rs// Iterate through the list by index\nlet mut _count = 0;\nfor i in 0..nums.len() {\n _count += nums[i];\n}\n\n// Iterate directly through list elements\n_count = 0;\nfor num in &nums {\n _count += num;\n}\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Iterate through the list by index\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\n count += nums[i];\n}\n\n// Iterate directly through list elements\ncount = 0;\nfor (nums.items) |num| {\n count += num;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5-concatenating-lists","title":"5. \u00a0 Concatenating lists","text":"Given a new list nums1
, we can append it to the end of the original list.
# Concatenate two lists\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1 # Concatenate nums1 to the end of nums\n
list.cpp/* Concatenate two lists */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// Concatenate nums1 to the end of nums\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java/* Concatenate two lists */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1); // Concatenate nums1 to the end of nums\n
list.cs/* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1); // Concatenate nums1 to the end of nums\n
list_test.go/* Concatenate two lists */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...) // Concatenate nums1 to the end of nums\n
list.swift/* Concatenate two lists */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // Concatenate nums1 to the end of nums\n
list.js/* Concatenate two lists */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1); // Concatenate nums1 to the end of nums\n
list.ts/* Concatenate two lists */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1); // Concatenate nums1 to the end of nums\n
list.dart/* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1); // Concatenate nums1 to the end of nums\n
list.rs/* Concatenate two lists */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Concatenate two lists\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // Concatenate nums1 to the end of nums\n
"},{"location":"chapter_array_and_linkedlist/list/#6-sorting-the-list","title":"6. \u00a0 Sorting the list","text":"Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as \"binary search\" and \"two-pointer\" algorithms.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Sort the list\nnums.sort() # After sorting, the list elements are in ascending order\n
list.cpp/* Sort the list */\nsort(nums.begin(), nums.end()); // After sorting, the list elements are in ascending order\n
list.java/* Sort the list */\nCollections.sort(nums); // After sorting, the list elements are in ascending order\n
list.cs/* Sort the list */\nnums.Sort(); // After sorting, the list elements are in ascending order\n
list_test.go/* Sort the list */\nsort.Ints(nums) // After sorting, the list elements are in ascending order\n
list.swift/* Sort the list */\nnums.sort() // After sorting, the list elements are in ascending order\n
list.js/* Sort the list */ \nnums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order\n
list.ts/* Sort the list */\nnums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order\n
list.dart/* Sort the list */\nnums.sort(); // After sorting, the list elements are in ascending order\n
list.rs/* Sort the list */\nnums.sort(); // After sorting, the list elements are in ascending order\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Sort the list\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432-list-implementation","title":"4.3.2 \u00a0 List implementation","text":"Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.
To enhance our understanding of how lists work, we will attempt to implement a simplified version of a list, focusing on three crucial design aspects:
size
to record the current number of elements in the list, updating in real-time with element insertion and deletion. With this variable, we can locate the end of the list and determine whether expansion is needed.class MyList:\n \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._capacity: int = 10 # \u5217\u8868\u5bb9\u91cf\n self._arr: list[int] = [0] * self._capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n self._size: int = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n self._extend_ratio: int = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n return self._size\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n return self._capacity\n\n def get(self, index: int) -> int:\n \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n return self._arr[index]\n\n def set(self, num: int, index: int):\n \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n self._arr[index] = num\n\n def add(self, num: int):\n \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size() == self.capacity():\n self.extend_capacity()\n self._arr[self._size] = num\n self._size += 1\n\n def insert(self, num: int, index: int):\n \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self._size == self.capacity():\n self.extend_capacity()\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in range(self._size - 1, index - 1, -1):\n self._arr[j + 1] = self._arr[j]\n self._arr[index] = num\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size += 1\n\n def remove(self, index: int) -> int:\n \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n num = self._arr[index]\n # \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in range(index, self._size - 1):\n self._arr[j] = self._arr[j + 1]\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size -= 1\n # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n\n def extend_capacity(self):\n \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self._capacity = len(self._arr)\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n return self._arr[: self._size]\n
my_list.cpp/* \u5217\u8868\u7c7b */\nclass MyList {\n private:\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~MyList() {\n delete[] arr;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n arr[size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size() - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size() - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n int newCapacity = capacity() * extendRatio;\n int *tmp = arr;\n arr = new int[newCapacity];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n arr[i] = tmp[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] tmp;\n arrCapacity = newCapacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> vec(size());\n for (int i = 0; i < size(); i++) {\n vec[i] = arr[i];\n }\n return vec;\n }\n};\n
my_list.java/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[capacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n public int size() {\n return size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int capacity() {\n return capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void set(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n arr[size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void insert(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int remove(int index) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = Arrays.copyOf(arr, capacity() * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] toArray() {\n int size = size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[size];\n for (int i = 0; i < size; i++) {\n arr[i] = get(i);\n }\n return arr;\n }\n}\n
my_list.cs/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private readonly int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public int Size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int Capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int Get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void Set(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void Add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n arr[arrSize] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void Insert(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = arrSize - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int Remove(int index) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < arrSize - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void ExtendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n Array.Resize(ref arr, arrCapacity * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n arrCapacity = arr.Length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[arrSize];\n for (int i = 0; i < arrSize; i++) {\n arr[i] = Get(i);\n }\n return arr;\n }\n}\n
my_list.go/* \u5217\u8868\u7c7b */\ntype myList struct {\n arrCapacity int\n arr []int\n arrSize int\n extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n return &myList{\n arrCapacity: 10, // \u5217\u8868\u5bb9\u91cf\n arr: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n return l.arrSize\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n l.arr[l.arrSize] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j := l.arrSize - 1; j >= index; j-- {\n l.arr[j+1] = l.arr[j]\n }\n l.arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n num := l.arr[index]\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j := index; j < l.arrSize-1; j++ {\n l.arr[j] = l.arr[j+1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize--\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n return l.arr[:l.arrSize]\n}\n
my_list.swift/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var _capacity: Int // \u5217\u8868\u5bb9\u91cf\n private var _size: Int // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private let extendRatio: Int // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n _capacity = 10\n _size = 0\n extendRatio = 2\n arr = Array(repeating: 0, count: _capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n func size() -> Int {\n _size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n func capacity() -> Int {\n _capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n func get(index: Int) -> Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n func set(index: Int, num: Int) {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n func add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if size() == capacity() {\n extendCapacity()\n }\n arr[size()] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n func insert(index: Int, num: Int) {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if size() == capacity() {\n extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index ..< size()).reversed() {\n arr[j + 1] = arr[j]\n }\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n @discardableResult\n func remove(index: Int) -> Int {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n let num = arr[index]\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index ..< (size() - 1) {\n arr[j] = arr[j + 1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size -= 1\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n func extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = arr.count\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n func toArray() -> [Int] {\n Array(arr.prefix(size()))\n }\n}\n
my_list.js/* \u5217\u8868\u7c7b */\nclass MyList {\n #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#arr = new Array(this.#capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n size() {\n return this.#size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n capacity() {\n return this.#capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n get(index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.#arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n set(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.#arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n add(num) {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.#arr[this.#size] = num;\n this.#size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n insert(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this.#size - 1; j >= index; j--) {\n this.#arr[j + 1] = this.#arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#arr[index] = num;\n this.#size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n remove(index) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.#arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this.#size - 1; j++) {\n this.#arr[j] = this.#arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n this.#arr = this.#arr.concat(\n new Array(this.capacity() * (this.#extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this.#capacity = this.#arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n toArray() {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.ts/* \u5217\u8868\u7c7b */\nclass MyList {\n private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.arr = new Array(this._capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public size(): number {\n return this._size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public capacity(): number {\n return this._capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public get(index: number): number {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public set(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public add(num: number): void {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this._size === this._capacity) this.extendCapacity();\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.arr[this._size] = num;\n this._size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public insert(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this._size === this._capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this._size - 1; j >= index; j--) {\n this.arr[j + 1] = this.arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.arr[index] = num;\n this._size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public remove(index: number): number {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this._size - 1; j++) {\n this.arr[j] = this.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this._size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public extendCapacity(): void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n this.arr = this.arr.concat(\n new Array(this.capacity() * (this.extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this._capacity = this.arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public toArray(): number[] {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.dart/* \u5217\u8868\u7c7b */\nclass MyList {\n late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n _arr = List.filled(_capacity, 0);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() => _size;\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() => _capacity;\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n return _arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n _arr[index] = _num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int _num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n _arr[_size] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var j = _size - 1; j >= index; j--) {\n _arr[j + 1] = _arr[j];\n }\n _arr[index] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n int _num = _arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var j = index; j < _size - 1; j++) {\n _arr[j] = _arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return _num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n final _newNums = List.filled(_capacity * _extendRatio, 0);\n // \u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n List.copyRange(_newNums, 0, _arr);\n // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n _arr = _newNums;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = _arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n List<int> toArray() {\n List<int> arr = [];\n for (var i = 0; i < _size; i++) {\n arr.add(get(i));\n }\n return arr;\n }\n}\n
my_list.rs/* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n arr: Vec<i32>, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n capacity: usize, // \u5217\u8868\u5bb9\u91cf\n size: usize, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n let mut vec = Vec::new();\n vec.resize(capacity, 0);\n Self {\n arr: vec,\n capacity,\n size: 0,\n extend_ratio: 2,\n }\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n pub fn size(&self) -> usize {\n return self.size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n return self.capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n pub fn get(&self, index: usize) -> i32 {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index >= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n return self.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n pub fn set(&mut self, index: usize, num: i32) {\n if index >= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n self.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n pub fn add(&mut self, num: i32) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n self.arr[self.size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n pub fn insert(&mut self, index: usize, num: i32) {\n if index >= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size).rev() {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n pub fn remove(&mut self, index: usize) -> i32 {\n if index >= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n let num = self.arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size - 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n pub fn extend_capacity(&mut self) {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n let new_capacity = self.capacity * self.extend_ratio;\n self.arr.resize(new_capacity, 0);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.capacity = new_capacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n pub fn to_array(&mut self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut arr = Vec::new();\n for i in 0..self.size {\n arr.push(self.get(i));\n }\n arr\n }\n}\n
my_list.c/* \u5217\u8868\u7c7b */\ntypedef struct {\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int capacity; // \u5217\u8868\u5bb9\u91cf\n int size; // \u5217\u8868\u5927\u5c0f\n int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n MyList *nums = malloc(sizeof(MyList));\n nums->capacity = 10;\n nums->arr = malloc(sizeof(int) * nums->capacity);\n nums->size = 0;\n nums->extendRatio = 2;\n return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n free(nums->arr);\n free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n assert(index >= 0 && index < nums->size);\n return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n assert(index >= 0 && index < nums->size);\n nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n nums->arr[size(nums)] = num;\n nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n assert(index >= 0 && index < size(nums));\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n for (int i = size(nums); i > index; --i) {\n nums->arr[i] = nums->arr[i - 1];\n }\n nums->arr[index] = num;\n nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n assert(index >= 0 && index < size(nums));\n int num = nums->arr[index];\n for (int i = index; i < size(nums) - 1; i++) {\n nums->arr[i] = nums->arr[i + 1];\n }\n nums->size--;\n return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n // \u5148\u5206\u914d\u7a7a\u95f4\n int newCapacity = capacity(nums) * nums->extendRatio;\n int *extend = (int *)malloc(sizeof(int) * newCapacity);\n int *temp = nums->arr;\n\n // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n for (int i = 0; i < size(nums); i++)\n extend[i] = nums->arr[i];\n\n // \u91ca\u653e\u65e7\u6570\u636e\n free(temp);\n\n // \u66f4\u65b0\u65b0\u6570\u636e\n nums->arr = extend;\n nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n return nums->arr;\n}\n
my_list.kt/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: IntArray = intArrayOf() // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var capacity = 10 // \u5217\u8868\u5bb9\u91cf\n private var size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private var extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n arr = IntArray(capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n fun size(): Int {\n return size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n fun capacity(): Int {\n return capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n fun get(index: Int): Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException()\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n fun set(index: Int, num: Int) {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n fun add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity()\n arr[size] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n fun insert(index: Int, num: Int) {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity()\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (j in size - 1 downTo index)\n arr[j + 1] = arr[j]\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n fun remove(index: Int): Int {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n val num: Int = arr[index]\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (j in index..<size - 1)\n arr[j] = arr[j + 1]\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n fun extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = arr.copyOf(capacity() * extendRatio)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.size\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n fun toArray(): IntArray {\n val size = size()\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val arr = IntArray(size)\n for (i in 0..<size) {\n arr[i] = get(i)\n }\n return arr\n }\n}\n
my_list.rb### \u5217\u8868\u7c7b ###\nclass MyList\n attr_reader :size # \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n attr_reader :capacity # \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n\n ### \u6784\u9020\u65b9\u6cd5 ###\n def initialize\n @capacity = 10\n @size = 0\n @extend_ratio = 2\n @arr = Array.new(capacity)\n end\n\n ### \u8bbf\u95ee\u5143\u7d20 ###\n def get(index)\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n @arr[index]\n end\n\n ### \u8bbf\u95ee\u5143\u7d20 ###\n def set(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n @arr[index] = num\n end\n\n ### \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 ###\n def add(num)\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n extend_capacity if size == capacity\n @arr[size] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size += 1\n end\n\n ### \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 ###\n def insert(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n extend_capacity if size == capacity\n\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (size - 1).downto(index)\n @arr[j + 1] = @arr[j] \n end\n @arr[index] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size += 1\n end\n\n ### \u5220\u9664\u5143\u7d20 ###\n def remove(index)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n num = @arr[index]\n\n # \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index...size\n @arr[j] = @arr[j + 1]\n end\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size -= 1\n\n # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n num\n end\n\n ### \u5217\u8868\u6269\u5bb9 ###\n def extend_capacity\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n @capacity = arr.length\n end\n\n ### \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 ###\n def to_array\n sz = size\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n arr = Array.new(sz)\n for i in 0...sz\n arr[i] = get(i)\n end\n arr\n end\nend\n
my_list.zig// \u5217\u8868\u7c7b\nfn MyList(comptime T: type) type {\n return struct {\n const Self = @This();\n\n arr: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\n numSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n @memset(self.arr, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n pub fn size(self: *Self) usize {\n return self.numSize;\n }\n\n // \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.arrCapacity;\n }\n\n // \u8bbf\u95ee\u5143\u7d20\n pub fn get(self: *Self, index: usize) T {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n return self.arr[index];\n } \n\n // \u66f4\u65b0\u5143\u7d20\n pub fn set(self: *Self, index: usize, num: T) void {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n self.arr[index] = num;\n } \n\n // \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\n pub fn add(self: *Self, num: T) !void {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n self.arr[self.size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n } \n\n // \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\n pub fn insert(self: *Self, index: usize, num: T) !void {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var j = self.size() - 1;\n while (j >= index) : (j -= 1) {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n }\n\n // \u5220\u9664\u5143\u7d20\n pub fn remove(self: *Self, index: usize) T {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n var num = self.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var j = index;\n while (j < self.size() - 1) : (j += 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n // \u5217\u8868\u6269\u5bb9\n pub fn extendCapacity(self: *Self) !void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n var newCapacity = self.capacity() * self.extendRatio;\n var extend = try self.mem_allocator.alloc(T, newCapacity);\n @memset(extend, @as(T, 0));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(T, extend, self.arr);\n self.arr = extend;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.arrCapacity = newCapacity;\n }\n\n // \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var arr = try self.mem_allocator.alloc(T, self.size());\n @memset(arr, @as(T, 0));\n for (arr, 0..) |*num, i| {\n num.* = self.get(i);\n }\n return arr;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 Memory and cache *","text":"In the first two sections of this chapter, we explored arrays and linked lists, two fundamental and important data structures, representing \"continuous storage\" and \"dispersed storage\" respectively.
In fact, the physical structure largely determines the efficiency of a program's use of memory and cache, which in turn affects the overall performance of the algorithm.
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441-computer-storage-devices","title":"4.4.1 \u00a0 Computer storage devices","text":"There are three types of storage devices in computers: \"hard disk,\" \"random-access memory (RAM),\" and \"cache memory.\" The following table shows their different roles and performance characteristics in computer systems.
Table 4-2 \u00a0 Computer storage devices
Hard Disk Memory Cache Usage Long-term storage of data, including OS, programs, files, etc. Temporary storage of currently running programs and data being processed Stores frequently accessed data and instructions, reducing the number of CPU accesses to memory Volatility Data is not lost after power off Data is lost after power off Data is lost after power off Capacity Larger, TB level Smaller, GB level Very small, MB level Speed Slower, several hundred to thousands MB/s Faster, several tens of GB/s Very fast, several tens to hundreds of GB/s Price Cheaper, several cents to yuan / GB More expensive, tens to hundreds of yuan / GB Very expensive, priced with CPUWe can imagine the computer storage system as a pyramid structure shown in the Figure 4-9 . The storage devices closer to the top of the pyramid are faster, have smaller capacity, and are more costly. This multi-level design is not accidental, but the result of careful consideration by computer scientists and engineers.
Figure 4-9 \u00a0 Computer storage system
Note
The storage hierarchy of computers reflects a delicate balance between speed, capacity, and cost. In fact, this kind of trade-off is common in all industrial fields, requiring us to find the best balance between different advantages and limitations.
Overall, hard disks are used for long-term storage of large amounts of data, memory is used for temporary storage of data being processed during program execution, and cache is used to store frequently accessed data and instructions to improve program execution efficiency. Together, they ensure the efficient operation of computer systems.
As shown in the Figure 4-10 , during program execution, data is read from the hard disk into memory for CPU computation. The cache can be considered a part of the CPU, smartly loading data from memory to provide fast data access to the CPU, significantly enhancing program execution efficiency and reducing reliance on slower memory.
Figure 4-10 \u00a0 Data flow between hard disk, memory, and cache
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442-memory-efficiency-of-data-structures","title":"4.4.2 \u00a0 Memory efficiency of data structures","text":"In terms of memory space utilization, arrays and linked lists have their advantages and limitations.
On one hand, memory is limited and cannot be shared by multiple programs, so we hope that data structures can use space as efficiently as possible. The elements of an array are tightly packed without extra space for storing references (pointers) between linked list nodes, making them more space-efficient. However, arrays require allocating sufficient continuous memory space at once, which may lead to memory waste, and array expansion also requires additional time and space costs. In contrast, linked lists allocate and reclaim memory dynamically on a per-node basis, providing greater flexibility.
On the other hand, during program execution, as memory is repeatedly allocated and released, the degree of fragmentation of free memory becomes higher, leading to reduced memory utilization efficiency. Arrays, due to their continuous storage method, are relatively less likely to cause memory fragmentation. In contrast, the elements of a linked list are dispersedly stored, and frequent insertion and deletion operations make memory fragmentation more likely.
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443-cache-efficiency-of-data-structures","title":"4.4.3 \u00a0 Cache efficiency of data structures","text":"Although caches are much smaller in space capacity than memory, they are much faster and play a crucial role in program execution speed. Since the cache's capacity is limited and can only store a small part of frequently accessed data, when the CPU tries to access data not in the cache, a \"cache miss\" occurs, forcing the CPU to load the needed data from slower memory.
Clearly, the fewer the cache misses, the higher the CPU's data read-write efficiency, and the better the program performance. The proportion of successful data retrieval from the cache by the CPU is called the \"cache hit rate,\" a metric often used to measure cache efficiency.
To achieve higher efficiency, caches adopt the following data loading mechanisms.
In fact, arrays and linked lists have different cache utilization efficiencies, mainly reflected in the following aspects.
Overall, arrays have a higher cache hit rate and are generally more efficient in operation than linked lists. This makes data structures based on arrays more popular in solving algorithmic problems.
It should be noted that high cache efficiency does not mean that arrays are always better than linked lists. Which data structure to choose in actual applications should be based on specific requirements. For example, both arrays and linked lists can implement the \"stack\" data structure (which will be detailed in the next chapter), but they are suitable for different scenarios.
Q: Does storing arrays on the stack versus the heap affect time and space efficiency?
Arrays stored on both the stack and heap are stored in contiguous memory spaces, and data operation efficiency is essentially the same. However, stacks and heaps have their own characteristics, leading to the following differences.
Q: Why do arrays require elements of the same type, while linked lists do not emphasize same-type elements?
Linked lists consist of nodes connected by references (pointers), and each node can store data of different types, such as int, double, string, object, etc.
In contrast, array elements must be of the same type, allowing the calculation of offsets to access the corresponding element positions. For example, an array containing both int and long types, with single elements occupying 4 bytes and 8 bytes respectively, cannot use the following formula to calculate offsets, as the array contains elements of two different lengths.
# Element memory address = array memory address + element length * element index\n
Q: After deleting a node, is it necessary to set P.next
to None
?
Not modifying P.next
is also acceptable. From the perspective of the linked list, traversing from the head node to the tail node will no longer encounter P
. This means that node P
has been effectively removed from the list, and where P
points no longer affects the list.
From a garbage collection perspective, for languages with automatic garbage collection mechanisms like Java, Python, and Go, whether node P
is collected depends on whether there are still references pointing to it, not on the value of P.next
. In languages like C and C++, we need to manually free the node's memory.
Q: In linked lists, the time complexity for insertion and deletion operations is O(1)
. But searching for the element before insertion or deletion takes O(n)
time, so why isn't the time complexity O(n)
?
If an element is searched first and then deleted, the time complexity is indeed O(n)
. However, the O(1)
advantage of linked lists in insertion and deletion can be realized in other applications. For example, in the implementation of double-ended queues using linked lists, we maintain pointers always pointing to the head and tail nodes, making each insertion and deletion operation O(1)
.
Q: In the image \"Linked List Definition and Storage Method\", do the light blue storage nodes occupy a single memory address, or do they share half with the node value?
The diagram is just a qualitative representation; quantitative analysis depends on specific situations.
Q: Is adding elements to the end of a list always O(1)
?
If adding an element exceeds the list length, the list needs to be expanded first. The system will request a new memory block and move all elements of the original list over, in which case the time complexity becomes O(n)
.
Q: The statement \"The emergence of lists greatly improves the practicality of arrays, but may lead to some memory space wastage\" - does this refer to the memory occupied by additional variables like capacity, length, and expansion multiplier?
The space wastage here mainly refers to two aspects: on the one hand, lists are set with an initial length, which we may not always need; on the other hand, to prevent frequent expansion, expansion usually multiplies by a coefficient, such as \\(\\times 1.5\\). This results in many empty slots, which we typically cannot fully fill.
Q: In Python, after initializing n = [1, 2, 3]
, the addresses of these 3 elements are contiguous, but initializing m = [2, 1, 3]
shows that each element's id
is not consecutive but identical to those in n
. If the addresses of these elements are not contiguous, is m
still an array?
If we replace list elements with linked list nodes n = [n1, n2, n3, n4, n5]
, these 5 node objects are also typically dispersed throughout memory. However, given a list index, we can still access the node's memory address in O(1)
time, thereby accessing the corresponding node. This is because the array stores references to the nodes, not the nodes themselves.
Unlike many languages, in Python, numbers are also wrapped as objects, and lists store references to these numbers, not the numbers themselves. Therefore, we find that the same number in two arrays has the same id
, and these numbers' memory addresses need not be contiguous.
Q: The std::list
in C++ STL has already implemented a doubly linked list, but it seems that some algorithm books don't directly use it. Is there any limitation?
On the one hand, we often prefer to use arrays to implement algorithms, only using linked lists when necessary, mainly for two reasons.
std::list
usually occupies more space than std::vector
.std::list
has a lower cache utilization rate. Generally, std::vector
performs better.On the other hand, linked lists are primarily necessary for binary trees and graphs. Stacks and queues are often implemented using the programming language's stack
and queue
classes, rather than linked lists.
Q: Does initializing a list res = [0] * self.size()
result in each element of res
referencing the same address?
No. However, this issue arises with two-dimensional arrays, for example, initializing a two-dimensional list res = [[0] * self.size()]
would reference the same list [0]
multiple times.
Q: In deleting a node, is it necessary to break the reference to its successor node?
From the perspective of data structures and algorithms (problem-solving), it's okay not to break the link, as long as the program's logic is correct. From the perspective of standard libraries, breaking the link is safer and more logically clear. If the link is not broken, and the deleted node is not properly recycled, it could affect the recycling of the successor node's memory.
"},{"location":"chapter_computational_complexity/","title":"Chapter 2. \u00a0 Complexity analysis","text":"Abstract
Complexity analysis is like a space-time navigator in the vast universe of algorithms.
It guides us in exploring deeper within the the dimensions of time and space, seeking more elegant solutions.
"},{"location":"chapter_computational_complexity/#chapter-contents","title":"Chapter Contents","text":"In algorithms, the repeated execution of a task is quite common and is closely related to the analysis of complexity. Therefore, before delving into the concepts of time complexity and space complexity, let's first explore how to implement repetitive tasks in programming. This involves understanding two fundamental programming control structures: iteration and recursion.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221-iteration","title":"2.2.1 \u00a0 Iteration","text":"\"Iteration\" is a control structure for repeatedly performing a task. In iteration, a program repeats a block of code as long as a certain condition is met until this condition is no longer satisfied.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for-loops","title":"1. \u00a0 For loops","text":"The for
loop is one of the most common forms of iteration, and it's particularly suitable when the number of iterations is known in advance.
The following function uses a for
loop to perform a summation of \\(1 + 2 + \\dots + n\\), with the sum being stored in the variable res
. It's important to note that in Python, range(a, b)
creates an interval that is inclusive of a
but exclusive of b
, meaning it iterates over the range from \\(a\\) up to \\(b\u22121\\).
def for_loop(n: int) -> int:\n \"\"\"for \u5faa\u73af\"\"\"\n res = 0\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res\n
iteration.cpp/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n res += i;\n }\n return res;\n}\n
iteration.java/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.cs/* for \u5faa\u73af */\nint ForLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.go/* for \u5faa\u73af */\nfunc forLoop(n int) int {\n res := 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n res += i\n }\n return res\n}\n
iteration.swift/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1 ... n {\n res += i\n }\n return res\n}\n
iteration.js/* for \u5faa\u73af */\nfunction forLoop(n) {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.ts/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.dart/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.rs/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n let mut res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..=n {\n res += i;\n }\n res\n}\n
iteration.c/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.kt/* for \u5faa\u73af */\nfun forLoop(n: Int): Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (i in 1..n) {\n res += i\n }\n return res\n}\n
iteration.rb### for \u5faa\u73af ###\ndef for_loop(n)\n res = 0\n\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..n\n res += i\n end\n\n res\nend\n
iteration.zig// for \u5faa\u73af\nfn forLoop(n: usize) i32 {\n var res: i32 = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n res = res + @as(i32, @intCast(i));\n }\n return res;\n} \n
Code Visualization Full Screen >
The flowchart below represents this sum function.
Figure 2-1 \u00a0 Flowchart of the sum function
The number of operations in this summation function is proportional to the size of the input data \\(n\\), or in other words, it has a \"linear relationship.\" This \"linear relationship\" is what time complexity describes. This topic will be discussed in more detail in the next section.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while-loops","title":"2. \u00a0 While loops","text":"Similar to for
loops, while
loops are another approach for implementing iteration. In a while
loop, the program checks a condition at the beginning of each iteration; if the condition is true, the execution continues, otherwise, the loop ends.
Below we use a while
loop to implement the sum \\(1 + 2 + \\dots + n\\).
def while_loop(n: int) -> int:\n \"\"\"while \u5faa\u73af\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n:\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n return res\n
iteration.cpp/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af */\nint WhileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i\n i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af */\nfunction whileLoop(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n res\n}\n
iteration.c/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.kt/* while \u5faa\u73af */\nfun whileLoop(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i\n i++ // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.rb### while \u5faa\u73af ###\ndef while_loop(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n end\n\n res\nend\n
iteration.zig// while \u5faa\u73af\nfn whileLoop(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += @intCast(i);\n i += 1;\n }\n return res;\n}\n
Code Visualization Full Screen >
While
loops provide more flexibility than for
loops, especially since they allow for custom initialization and modification of the condition variable at each step.
For example, in the following code, the condition variable \\(i\\) is updated twice each round, which would be inconvenient to implement with a for
loop.
def while_loop_ii(n: int) -> int:\n \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n:\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n return res\n
iteration.cpp/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1; \n i *= 2;\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n res\n}\n
iteration.c/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.kt/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfun whileLoopII(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.rb### while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09###\ndef while_loop_ii(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n end\n\n res\nend\n
iteration.zig// while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\nfn whileLoopII(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += @intCast(i);\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n return res;\n}\n
Code Visualization Full Screen >
Overall, for
loops are more concise, while while
loops are more flexible. Both can implement iterative structures. Which one to use should be determined based on the specific requirements of the problem.
We can nest one loop structure within another. Below is an example using for
loops:
def nested_for_loop(n: int) -> str:\n \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n res = \"\"\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res\n
iteration.cpp/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n ostringstream res;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; ++j) {\n res << \"(\" << i << \", \" << j << \"), \";\n }\n }\n return res.str();\n}\n
iteration.java/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n StringBuilder res = new StringBuilder();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.append(\"(\" + i + \", \" + j + \"), \");\n }\n }\n return res.toString();\n}\n
iteration.cs/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n StringBuilder res = new();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.Append($\"({i}, {j}), \");\n }\n }\n return res.ToString();\n}\n
iteration.go/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n res := \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n for j := 1; j <= n; j++ {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n res += fmt.Sprintf(\"(%d, %d), \", i, j)\n }\n }\n return res\n}\n
iteration.swift/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n var res = \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1 ... n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1 ... n {\n res.append(\"(\\(i), \\(j)), \")\n }\n }\n return res\n}\n
iteration.js/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.ts/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.dart/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n String res = \"\";\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res += \"($i, $j), \";\n }\n }\n return res;\n}\n
iteration.rs/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n let mut res = vec![];\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..=n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..=n {\n res.push(format!(\"({}, {}), \", i, j));\n }\n }\n res.join(\"\")\n}\n
iteration.c/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n int size = n * n * 26 + 1;\n char *res = malloc(size * sizeof(char));\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n char tmp[26];\n snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n strncat(res, tmp, size - strlen(res) - 1);\n }\n }\n return res;\n}\n
iteration.kt/* \u53cc\u5c42 for \u5faa\u73af */\nfun nestedForLoop(n: Int): String {\n val res = StringBuilder()\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (i in 1..n) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (j in 1..n) {\n res.append(\" ($i, $j), \")\n }\n }\n return res.toString()\n}\n
iteration.rb### \u53cc\u5c42 for \u5faa\u73af ###\ndef nested_for_loop(n)\n res = \"\"\n\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..n\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..n\n res += \"(#{i}, #{j}), \"\n end\n end\n\n res\nend\n
iteration.zig// \u53cc\u5c42 for \u5faa\u73af\nfn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {\n var res = std.ArrayList(u8).init(allocator);\n defer res.deinit();\n var buffer: [20]u8 = undefined;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (1..n+1) |j| {\n var _str = try std.fmt.bufPrint(&buffer, \"({d}, {d}), \", .{i, j});\n try res.appendSlice(_str);\n }\n }\n return res.toOwnedSlice();\n}\n
Code Visualization Full Screen >
The flowchart below represents this nested loop.
Figure 2-2 \u00a0 Flowchart of the nested loop
In such cases, the number of operations of the function is proportional to \\(n^2\\), meaning the algorithm's runtime and the size of the input data \\(n\\) has a 'quadratic relationship.'
We can further increase the complexity by adding more nested loops, each level of nesting effectively \"increasing the dimension,\" which raises the time complexity to \"cubic,\" \"quartic,\" and so on.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222-recursion","title":"2.2.2 \u00a0 Recursion","text":"\"Recursion\" is an algorithmic strategy where a function solves a problem by calling itself. It primarily involves two phases:
From an implementation perspective, recursive code mainly includes three elements.
Observe the following code, where simply calling the function recur(n)
can compute the sum of \\(1 + 2 + \\dots + n\\):
def recur(n: int) -> int:\n \"\"\"\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 1:\n return 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n
recursion.cpp/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.java/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.cs/* \u9012\u5f52 */\nint Recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = Recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.go/* \u9012\u5f52 */\nfunc recur(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res := recur(n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.swift/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n: n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.js/* \u9012\u5f52 */\nfunction recur(n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.ts/* \u9012\u5f52 */\nfunction recur(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.dart/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.rs/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\n}\n
recursion.c/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.kt/* \u9012\u5f52 */\nfun recur(n: Int): Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1\n // \u9012: \u9012\u5f52\u8c03\u7528\n val res = recur(n - 1)\n // \u5f52: \u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.rb### \u9012\u5f52 ###\ndef recur(n)\n # \u7ec8\u6b62\u6761\u4ef6\n return 1 if n == 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\nend\n
recursion.zig// \u9012\u5f52\u51fd\u6570\nfn recur(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var res: i32 = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
Code Visualization Full Screen >
The Figure 2-3 shows the recursive process of this function.
Figure 2-3 \u00a0 Recursive process of the sum function
Although iteration and recursion can achieve the same results from a computational standpoint, they represent two entirely different paradigms of thinking and problem-solving.
Let's take the earlier example of the summation function, defined as \\(f(n) = 1 + 2 + \\dots + n\\).
Every time a recursive function calls itself, the system allocates memory for the newly initiated function to store local variables, the return address, and other relevant information. This leads to two primary outcomes.
As shown in the Figure 2-4 , there are \\(n\\) unreturned recursive functions before triggering the termination condition, indicating a recursion depth of \\(n\\).
Figure 2-4 \u00a0 Recursion call depth
In practice, the depth of recursion allowed by programming languages is usually limited, and excessively deep recursion can lead to stack overflow errors.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-tail-recursion","title":"2. \u00a0 Tail recursion","text":"Interestingly, if a function performs its recursive call as the very last step before returning, it can be optimized by the compiler or interpreter to be as space-efficient as iteration. This scenario is known as \"tail recursion.\"
For example, in calculating \\(1 + 2 + \\dots + n\\), we can make the result variable res
a parameter of the function, thereby achieving tail recursion:
def tail_recur(n, res):\n \"\"\"\u5c3e\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 0:\n return res\n # \u5c3e\u9012\u5f52\u8c03\u7528\n return tail_recur(n - 1, res + n)\n
recursion.cpp/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.java/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.cs/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return TailRecur(n - 1, res + n);\n}\n
recursion.go/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n-1, res+n)\n}\n
recursion.swift/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.ts/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.dart/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.rs/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\n}\n
recursion.c/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.kt/* \u5c3e\u9012\u5f52 */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n // \u6dfb\u52a0 tailrec \u5173\u952e\u8bcd\uff0c\u4ee5\u5f00\u542f\u5c3e\u9012\u5f52\u4f18\u5316\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n)\n}\n
recursion.rb### \u5c3e\u9012\u5f52 ###\ndef tail_recur(n, res)\n # \u7ec8\u6b62\u6761\u4ef6\n return res if n == 0\n # \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\nend\n
recursion.zig// \u5c3e\u9012\u5f52\u51fd\u6570\nfn tailRecur(n: i32, res: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
Code Visualization Full Screen >
The execution process of tail recursion is shown in the following figure. Comparing regular recursion and tail recursion, the point of the summation operation is different.
Figure 2-5 \u00a0 Tail recursion process
Tip
Note that many compilers or interpreters do not support tail recursion optimization. For example, Python does not support tail recursion optimization by default, so even if the function is in the form of tail recursion, it may still encounter stack overflow issues.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-recursion-tree","title":"3. \u00a0 Recursion tree","text":"When dealing with algorithms related to \"divide and conquer\", recursion often offers a more intuitive approach and more readable code than iteration. Take the \"Fibonacci sequence\" as an example.
Question
Given a Fibonacci sequence \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\), find the \\(n\\)th number in the sequence.
Let the \\(n\\)th number of the Fibonacci sequence be \\(f(n)\\), it's easy to deduce two conclusions:
Using the recursive relation, and considering the first two numbers as termination conditions, we can write the recursive code. Calling fib(n)
will yield the \\(n\\)th number of the Fibonacci sequence:
def fib(n: int) -> int:\n \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n
recursion.cpp/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.java/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.cs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = Fib(n - 1) + Fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.go/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res := fib(n-1) + fib(n-2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.swift/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n: n - 1) + fib(n: n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.js/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.ts/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.dart/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.rs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c\n res\n}\n
recursion.c/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.kt/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfun fib(n: Int): Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n val res = fib(n - 1) + fib(n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.rb### \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 ###\ndef fib(n)\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n return n - 1 if n == 1 || n == 2\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n res\nend\n
recursion.zig// \u6590\u6ce2\u90a3\u5951\u6570\u5217\nfn fib(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 or n == 2) {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n var res: i32 = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
Code Visualization Full Screen >
Observing the above code, we see that it recursively calls two functions within itself, meaning that one call generates two branching calls. As illustrated below, this continuous recursive calling eventually creates a \"recursion tree\" with a depth of \\(n\\).
Figure 2-6 \u00a0 Fibonacci sequence recursion tree
Fundamentally, recursion embodies the paradigm of \"breaking down a problem into smaller sub-problems.\" This divide-and-conquer strategy is crucial.
Summarizing the above content, the following table shows the differences between iteration and recursion in terms of implementation, performance, and applicability.
Table: Comparison of iteration and recursion characteristics
Iteration Recursion Approach Loop structure Function calls itself Time Efficiency Generally higher efficiency, no function call overhead Each function call generates overhead Memory Usage Typically uses a fixed size of memory space Accumulative function calls can use a substantial amount of stack frame space Suitable Problems Suitable for simple loop tasks, intuitive and readable code Suitable for problem decomposition, like trees, graphs, divide-and-conquer, backtracking, etc., concise and clear code structureTip
If you find the following content difficult to understand, consider revisiting it after reading the \"Stack\" chapter.
So, what is the intrinsic connection between iteration and recursion? Taking the above recursive function as an example, the summation operation occurs during the recursion's \"return\" phase. This means that the initially called function is the last to complete its summation operation, mirroring the \"last in, first out\" principle of a stack.
Recursive terms like \"call stack\" and \"stack frame space\" hint at the close relationship between recursion and stacks.
Therefore, we can use an explicit stack to simulate the behavior of the call stack, thus transforming recursion into an iterative form:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.pydef for_loop_recur(n: int) -> int:\n \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in range(n, 0, -1):\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while stack:\n # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n # res = 1+2+3+...+n\n return res\n
recursion.cpp/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack<int> stack;\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.empty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.top();\n stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.java/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<Integer> stack = new Stack<>();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.cs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<int> stack = new();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.Push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.Count > 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.go/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack := list.New()\n res := 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i := n; i > 0; i-- {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.PushBack(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n for stack.Len() != 0 {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Back().Value.(int)\n stack.Remove(stack.Back())\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.swift/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [Int] = []\n var res = 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1 ... n).reversed() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.isEmpty {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.js/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n const stack = [];\n let res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.ts/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n const stack: number[] = [];\n let res: number = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.dart/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n List<int> stack = [];\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.add(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.rs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n let mut stack = Vec::new();\n let mut res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1..=n).rev() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.is_empty() {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop().unwrap();\n }\n // res = 1+2+3+...+n\n res\n}\n
recursion.c/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n int top = -1; // \u6808\u9876\u7d22\u5f15\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack[1 + top++] = i;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (top >= 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack[top--];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.kt/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfun forLoopRecur(n: Int): Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n val stack = Stack<Int>()\n var res = 0\n // \u9012: \u9012\u5f52\u8c03\u7528\n for (i in n downTo 0) {\n stack.push(i)\n }\n // \u5f52: \u8fd4\u56de\u7ed3\u679c\n while (stack.isNotEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.rb### \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 ###\ndef for_loop_recur(n)\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in n.downto(0)\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack << i\n end\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.empty?\n res += stack.pop\n end\n\n # res = 1+2+3+...+n\n res\nend\n
recursion.zig// \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\nfn forLoopRecur(comptime n: i32) i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [n]i32 = undefined;\n var res: i32 = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var i: usize = n;\n while (i > 0) {\n stack[i - 1] = @intCast(i);\n i -= 1;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n var index: usize = n;\n while (index > 0) {\n index -= 1;\n res += stack[index];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
Code Visualization Full Screen >
Observing the above code, when recursion is transformed into iteration, the code becomes more complex. Although iteration and recursion can often be transformed into each other, it's not always advisable to do so for two reasons:
In conclusion, whether to choose iteration or recursion depends on the specific nature of the problem. In programming practice, it's crucial to weigh the pros and cons of both and choose the most suitable approach for the situation at hand.
"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 Algorithm efficiency assessment","text":"In algorithm design, we pursue the following two objectives in sequence.
In other words, under the premise of being able to solve the problem, algorithm efficiency has become the main criterion for evaluating the merits of an algorithm, which includes the following two dimensions.
In short, our goal is to design data structures and algorithms that are both fast and memory-efficient. Effectively assessing algorithm efficiency is crucial because only then can we compare various algorithms and guide the process of algorithm design and optimization.
There are mainly two methods of efficiency assessment: actual testing and theoretical estimation.
"},{"location":"chapter_computational_complexity/performance_evaluation/#211-actual-testing","title":"2.1.1 \u00a0 Actual testing","text":"Suppose we have algorithms A
and B
, both capable of solving the same problem, and we need to compare their efficiencies. The most direct method is to use a computer to run these two algorithms and monitor and record their runtime and memory usage. This assessment method reflects the actual situation but has significant limitations.
On one hand, it's difficult to eliminate interference from the testing environment. Hardware configurations can affect algorithm performance. For example, algorithm A
might run faster than B
on one computer, but the opposite result may occur on another computer with different configurations. This means we would need to test on a variety of machines to calculate average efficiency, which is impractical.
On the other hand, conducting a full test is very resource-intensive. As the volume of input data changes, the efficiency of the algorithms may vary. For example, with smaller data volumes, algorithm A
might run faster than B
, but the opposite might be true with larger data volumes. Therefore, to draw convincing conclusions, we need to test a wide range of input data sizes, which requires significant computational resources.
Due to the significant limitations of actual testing, we can consider evaluating algorithm efficiency solely through calculations. This estimation method is known as \"asymptotic complexity analysis,\" or simply \"complexity analysis.\"
Complexity analysis reflects the relationship between the time and space resources required for algorithm execution and the size of the input data. It describes the trend of growth in the time and space required by the algorithm as the size of the input data increases. This definition might sound complex, but we can break it down into three key points to understand it better.
Complexity analysis overcomes the disadvantages of actual testing methods, reflected in the following aspects:
Tip
If you're still confused about the concept of complexity, don't worry. We will introduce it in detail in subsequent chapters.
Complexity analysis provides us with a \"ruler\" to measure the time and space resources needed to execute an algorithm and compare the efficiency between different algorithms.
Complexity is a mathematical concept and may be abstract and challenging for beginners. From this perspective, complexity analysis might not be the best content to introduce first. However, when discussing the characteristics of a particular data structure or algorithm, it's hard to avoid analyzing its speed and space usage.
In summary, it's recommended that you establish a preliminary understanding of complexity analysis before diving deep into data structures and algorithms, so that you can carry out simple complexity analyses of algorithms.
"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 Space complexity","text":"\"Space complexity\" is used to measure the growth trend of the memory space occupied by an algorithm as the amount of data increases. This concept is very similar to time complexity, except that \"running time\" is replaced with \"occupied memory space\".
"},{"location":"chapter_computational_complexity/space_complexity/#241-space-related-to-algorithms","title":"2.4.1 \u00a0 Space related to algorithms","text":"The memory space used by an algorithm during its execution mainly includes the following types.
Generally, the scope of space complexity statistics includes both \"Temporary Space\" and \"Output Space\".
Temporary space can be further divided into three parts.
When analyzing the space complexity of a program, we typically count the Temporary Data, Stack Frame Space, and Output Data, as shown in the Figure 2-15 .
Figure 2-15 \u00a0 Space types used in algorithms
The relevant code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigclass Node:\n \"\"\"Classes\"\"\"\n def __init__(self, x: int):\n self.val: int = x # node value\n self.next: Node | None = None # reference to the next node\n\ndef function() -> int:\n \"\"\"Functions\"\"\"\n # Perform certain operations...\n return 0\n\ndef algorithm(n) -> int: # input data\n A = 0 # temporary data (constant, usually in uppercase)\n b = 0 # temporary data (variable)\n node = Node(0) # temporary data (object)\n c = function() # Stack frame space (call function)\n return A + b + c # output data\n
/* Structures */\nstruct Node {\n int val;\n Node *next;\n Node(int x) : val(x), next(nullptr) {}\n};\n\n/* Functions */\nint func() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node* node = new Node(0); // temporary data (object)\n int c = func(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* Functions */\nint function() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n final int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = new Node(0); // temporary data (object)\n int c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* Functions */\nint Function() {\n // Perform certain operations...\n return 0;\n}\n\nint Algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = new(0); // temporary data (object)\n int c = Function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Structures */\ntype node struct {\n val int\n next *node\n}\n\n/* Create node structure */\nfunc newNode(val int) *node {\n return &node{val: val}\n}\n\n/* Functions */\nfunc function() int {\n // Perform certain operations...\n return 0\n}\n\nfunc algorithm(n int) int { // input data\n const a = 0 // temporary data (constant)\n b := 0 // temporary storage of data (variable)\n newNode(0) // temporary data (object)\n c := function() // stack frame space (call function)\n return a + b + c // output data\n}\n
/* Classes */\nclass Node {\n var val: Int\n var next: Node?\n\n init(x: Int) {\n val = x\n }\n}\n\n/* Functions */\nfunc function() -> Int {\n // Perform certain operations...\n return 0\n}\n\nfunc algorithm(n: Int) -> Int { // input data\n let a = 0 // temporary data (constant)\n var b = 0 // temporary data (variable)\n let node = Node(x: 0) // temporary data (object)\n let c = function() // stack frame space (call function)\n return a + b + c // output data\n}\n
/* Classes */\nclass Node {\n val;\n next;\n constructor(val) {\n this.val = val === undefined ? 0 : val; // node value\n this.next = null; // reference to the next node\n }\n}\n\n/* Functions */\nfunction constFunc() {\n // Perform certain operations\n return 0;\n}\n\nfunction algorithm(n) { // input data\n const a = 0; // temporary data (constant)\n let b = 0; // temporary data (variable)\n const node = new Node(0); // temporary data (object)\n const c = constFunc(); // Stack frame space (calling function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n val: number;\n next: Node | null;\n constructor(val?: number) {\n this.val = val === undefined ? 0 : val; // node value\n this.next = null; // reference to the next node\n }\n}\n\n/* Functions */\nfunction constFunc(): number {\n // Perform certain operations\n return 0;\n}\n\nfunction algorithm(n: number): number { // input data\n const a = 0; // temporary data (constant)\n let b = 0; // temporary data (variable)\n const node = new Node(0); // temporary data (object)\n const c = constFunc(); // Stack frame space (calling function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(this.val, [this.next]);\n}\n\n/* Functions */\nint function() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = Node(0); // temporary data (object)\n int c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Structures */\nstruct Node {\n val: i32,\n next: Option<Rc<RefCell<Node>>>,\n}\n\n/* Constructor */\nimpl Node {\n fn new(val: i32) -> Self {\n Self { val: val, next: None }\n }\n}\n\n/* Functions */\nfn function() -> i32 { \n // Perform certain operations...\n return 0;\n}\n\nfn algorithm(n: i32) -> i32 { // input data\n const a: i32 = 0; // temporary data (constant)\n let mut b = 0; // temporary data (variable)\n let node = Node::new(0); // temporary data (object)\n let c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Functions */\nint func() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n int c = func(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
\n
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242-calculation-method","title":"2.4.2 \u00a0 Calculation method","text":"The method for calculating space complexity is roughly similar to that of time complexity, with the only change being the shift of the statistical object from \"number of operations\" to \"size of used space\".
However, unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we must ensure that there is enough memory space reserved under all input data.
Consider the following code, the term \"worst-case\" in worst-case space complexity has two meanings.
nums
occupies \\(O(n)\\) space, thus the worst-case space complexity is \\(O(n)\\).nums
, the program occupies \\(O(n)\\) space, hence the worst-case space complexity is \\(O(n)\\).def algorithm(n: int):\n a = 0 # O(1)\n b = [0] * 10000 # O(1)\n if n > 10:\n nums = [0] * n # O(n)\n
void algorithm(int n) {\n int a = 0; // O(1)\n vector<int> b(10000); // O(1)\n if (n > 10)\n vector<int> nums(n); // O(n)\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10)\n int[] nums = new int[n]; // O(n)\n}\n
void Algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10) {\n int[] nums = new int[n]; // O(n)\n }\n}\n
func algorithm(n int) {\n a := 0 // O(1)\n b := make([]int, 10000) // O(1)\n var nums []int\n if n > 10 {\n nums := make([]int, n) // O(n)\n }\n fmt.Println(a, b, nums)\n}\n
func algorithm(n: Int) {\n let a = 0 // O(1)\n let b = Array(repeating: 0, count: 10000) // O(1)\n if n > 10 {\n let nums = Array(repeating: 0, count: n) // O(n)\n }\n}\n
function algorithm(n) {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
function algorithm(n: number): void {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n List<int> b = List.filled(10000, 0); // O(1)\n if (n > 10) {\n List<int> nums = List.filled(n, 0); // O(n)\n }\n}\n
fn algorithm(n: i32) {\n let a = 0; // O(1)\n let b = [0; 10000]; // O(1)\n if n > 10 {\n let nums = vec![0; n as usize]; // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int b[10000]; // O(1)\n if (n > 10)\n int nums[n] = {0}; // O(n)\n}\n
\n
\n
In recursive functions, stack frame space must be taken into count. Consider the following code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef function() -> int:\n # Perform certain operations\n return 0\n\ndef loop(n: int):\n \"\"\"Loop O(1)\"\"\"\n for _ in range(n):\n function()\n\ndef recur(n: int):\n \"\"\"Recursion O(n)\"\"\"\n if n == 1:\n return\n return recur(n - 1)\n
int func() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int Function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid Loop(int n) {\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n/* Recursion O(n) */\nint Recur(int n) {\n if (n == 1) return 1;\n return Recur(n - 1);\n}\n
func function() int {\n // Perform certain operations\n return 0\n}\n\n/* Cycle O(1) */\nfunc loop(n int) {\n for i := 0; i < n; i++ {\n function()\n }\n}\n\n/* Recursion O(n) */\nfunc recur(n int) {\n if n == 1 {\n return\n }\n recur(n - 1)\n}\n
@discardableResult\nfunc function() -> Int {\n // Perform certain operations\n return 0\n}\n\n/* Cycle O(1) */\nfunc loop(n: Int) {\n for _ in 0 ..< n {\n function()\n }\n}\n\n/* Recursion O(n) */\nfunc recur(n: Int) {\n if n == 1 {\n return\n }\n recur(n: n - 1)\n}\n
function constFunc() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfunction loop(n) {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* Recursion O(n) */\nfunction recur(n) {\n if (n === 1) return;\n return recur(n - 1);\n}\n
function constFunc(): number {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfunction loop(n: number): void {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* Recursion O(n) */\nfunction recur(n: number): void {\n if (n === 1) return;\n return recur(n - 1);\n}\n
int function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
fn function() -> i32 {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfn loop(n: i32) {\n for i in 0..n {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(n: i32) {\n if n == 1 {\n return;\n }\n recur(n - 1);\n}\n
int func() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
\n
\n
The time complexity of both loop()
and recur()
functions is \\(O(n)\\), but their space complexities differ.
loop()
function calls function()
\\(n\\) times in a loop, where each iteration's function()
returns and releases its stack frame space, so the space complexity remains \\(O(1)\\).recur()
will have \\(n\\) instances of unreturned recur()
existing simultaneously during its execution, thus occupying \\(O(n)\\) stack frame space.Let the size of the input data be \\(n\\), the following chart displays common types of space complexities (arranged from low to high).
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{Constant Order} < \\text{Logarithmic Order} < \\text{Linear Order} < \\text{Quadratic Order} < \\text{Exponential Order} \\end{aligned} \\]Figure 2-16 \u00a0 Common types of space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant order \\(O(1)\\)","text":"Constant order is common in constants, variables, objects that are independent of the size of input data \\(n\\).
Note that memory occupied by initializing variables or calling functions in a loop, which is released upon entering the next cycle, does not accumulate over space, thus the space complexity remains \\(O(1)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef constant(n: int):\n \"\"\"\u5e38\u6570\u9636\"\"\"\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode(0)\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n c = 0\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n function()\n
space_complexity.cpp/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n vector<int> nums(10000);\n ListNode node(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.java/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.cs/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n
space_complexity.go/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0\n b := 0\n nums := make([]int, 10000)\n node := newNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n var c int\n for i := 0; i < n; i++ {\n c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i := 0; i < n; i++ {\n function()\n }\n b += 0\n c += 0\n nums[0] = 0\n node.val = 0\n}\n
space_complexity.swift/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n let a = 0\n var b = 0\n let nums = Array(repeating: 0, count: 10000)\n let node = ListNode(x: 0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n let c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n function()\n }\n}\n
space_complexity.js/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.ts/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.dart/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n List<int> nums = List.filled(10000, 0);\n ListNode node = ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.rs/* \u51fd\u6570 */\nfn function() -> i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const A: i32 = 0;\n let b = 0;\n let nums = vec![0; 10000];\n let node = ListNode::new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n let c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n function();\n }\n}\n
space_complexity.c/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n int nums[1000];\n ListNode *node = newListNode(0);\n free(node);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.kt/* \u51fd\u6570 */\nfun function(): Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfun constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n val a = 0\n var b = 0\n val nums = Array(10000) { 0 }\n val node = ListNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (i in 0..<n) {\n val c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (i in 0..<n) {\n function()\n }\n}\n
space_complexity.rb### \u51fd\u6570 ###\ndef function\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n 0\nend\n\n### \u5e38\u6570\u9636 ###\ndef constant(n)\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode.new\n\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n (0...n).each { c = 0 }\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n (0...n).each { function }\nend\n
space_complexity.zig// \u51fd\u6570\nfn function() i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a: i32 = 0;\n var b: i32 = 0;\n var nums = [_]i32{0}**10000;\n var node = inc.ListNode(i32){.val = 0};\n var i: i32 = 0;\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n while (i < n) : (i += 1) {\n var c: i32 = 0;\n _ = c;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n i = 0;\n while (i < n) : (i += 1) {\n _ = function();\n }\n _ = a;\n _ = b;\n _ = nums;\n _ = node;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/space_complexity/#2-linear-order-on","title":"2. \u00a0 Linear order \\(O(n)\\)","text":"Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef linear(n: int):\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = [0] * n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = dict[int, str]()\n for i in range(n):\n hmap[i] = str(i)\n
space_complexity.cpp/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n vector<int> nums(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n vector<ListNode> nodes;\n for (int i = 0; i < n; i++) {\n nodes.push_back(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n unordered_map<int, string> map;\n for (int i = 0; i < n; i++) {\n map[i] = to_string(i);\n }\n}\n
space_complexity.java/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n nodes.add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<Integer, String> map = new HashMap<>();\n for (int i = 0; i < n; i++) {\n map.put(i, String.valueOf(i));\n }\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (int i = 0; i < n; i++) {\n nodes.Add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Dictionary<int, string> map = [];\n for (int i = 0; i < n; i++) {\n map.Add(i, i.ToString());\n }\n}\n
space_complexity.go/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n _ = make([]int, n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes []*node\n for i := 0; i < n; i++ {\n nodes = append(nodes, newNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n m := make(map[int]string, n)\n for i := 0; i < n; i++ {\n m[i] = strconv.Itoa(i)\n }\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let nums = Array(repeating: 0, count: n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let nodes = (0 ..< n).map { ListNode(x: $0) }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
space_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes: ListNode[] = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n List<int> nums = List.filled(n, 0);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (var i = 0; i < n; i++) {\n nodes.add(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<int, String> map = HashMap();\n for (var i = 0; i < n; i++) {\n map.putIfAbsent(i, () => i.toString());\n }\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nums = vec![0; n as usize];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nodes = Vec::new();\n for i in 0..n {\n nodes.push(ListNode::new(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut map = HashMap::new();\n for i in 0..n {\n map.insert(i, i.to_string());\n }\n}\n
space_complexity.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int *nums = malloc(sizeof(int) * n);\n free(nums);\n\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n ListNode **nodes = malloc(sizeof(ListNode *) * n);\n for (int i = 0; i < n; i++) {\n nodes[i] = newListNode(i);\n }\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(nodes[i]);\n }\n free(nodes);\n\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n HashTable *h = NULL;\n for (int i = 0; i < n; i++) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = i;\n tmp->val = i;\n HASH_ADD_INT(h, key, tmp);\n }\n\n // \u5185\u5b58\u91ca\u653e\n HashTable *curr, *tmp;\n HASH_ITER(hh, h, curr, tmp) {\n HASH_DEL(h, curr);\n free(curr);\n }\n}\n
space_complexity.kt/* \u7ebf\u6027\u9636 */\nfun linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n val nums = Array(n) { 0 }\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n val nodes = mutableListOf<ListNode>()\n for (i in 0..<n) {\n nodes.add(ListNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n val map = mutableMapOf<Int, String>()\n for (i in 0..<n) {\n map[i] = i.toString()\n }\n}\n
space_complexity.rb### \u7ebf\u6027\u9636 ###\ndef linear(n)\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = Array.new(n, 0)\n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = {}\n for i in 0...n\n hmap[i] = i.to_s\n end\nend\n
space_complexity.zig// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n var nums = [_]i32{0}**n;\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes = std.ArrayList(i32).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n try nodes.append(i);\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\n defer map.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n const string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\n defer std.heap.page_allocator.free(string);\n try map.put(i, string);\n }\n _ = nums;\n}\n
Code Visualization Full Screen >
As shown below, this function's recursive depth is \\(n\\), meaning there are \\(n\\) instances of unreturned linear_recur()
function, using \\(O(n)\\) size of stack frame space:
def linear_recur(n: int):\n \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n print(\"\u9012\u5f52 n =\", n)\n if n == 1:\n return\n linear_recur(n - 1)\n
space_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n cout << \"\u9012\u5f52 n = \" << n << endl;\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.java/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n System.out.println(\"\u9012\u5f52 n = \" + n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n if (n == 1) return;\n LinearRecur(n - 1);\n}\n
space_complexity.go/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n fmt.Println(\"\u9012\u5f52 n =\", n)\n if n == 1 {\n return\n }\n spaceLinearRecur(n - 1)\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n print(\"\u9012\u5f52 n = \\(n)\")\n if n == 1 {\n return\n }\n linearRecur(n: n - 1)\n}\n
space_complexity.js/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n print('\u9012\u5f52 n = $n');\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n println!(\"\u9012\u5f52 n = {}\", n);\n if n == 1 {\n return;\n };\n linear_recur(n - 1);\n}\n
space_complexity.c/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.kt/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun linearRecur(n: Int) {\n println(\"\u9012\u5f52 n = $n\")\n if (n == 1)\n return\n linearRecur(n - 1)\n}\n
space_complexity.rb### \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef linear_recur(n)\n puts \"\u9012\u5f52 n = #{n}\"\n return if n == 1\n linear_recur(n - 1)\nend\n
space_complexity.zig// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\n std.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
Code Visualization Full Screen >
Figure 2-17 \u00a0 Recursive function generating linear order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic order \\(O(n^2)\\)","text":"Quadratic order is common in matrices and graphs, where the number of elements is quadratic to \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef quadratic(n: int):\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n num_matrix = [[0] * n for _ in range(n)]\n
space_complexity.cpp/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n vector<vector<int>> numMatrix;\n for (int i = 0; i < n; i++) {\n vector<int> tmp;\n for (int j = 0; j < n; j++) {\n tmp.push_back(0);\n }\n numMatrix.push_back(tmp);\n }\n}\n
space_complexity.java/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[][] numMatrix = new int[n][n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<Integer>> numList = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<Integer> tmp = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[,] numMatrix = new int[n, n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (int i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.Add(0);\n }\n numList.Add(tmp);\n }\n}\n
space_complexity.go/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n numMatrix := make([][]int, n)\n for i := 0; i < n; i++ {\n numMatrix[i] = make([]int, n)\n }\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (var i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n let num_matrix = vec![vec![0; n as usize]; n as usize];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let mut num_list = Vec::new();\n for i in 0..n {\n let mut tmp = Vec::new();\n for j in 0..n {\n tmp.push(0);\n }\n num_list.push(tmp);\n }\n}\n
space_complexity.c/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n int **numMatrix = malloc(sizeof(int *) * n);\n for (int i = 0; i < n; i++) {\n int *tmp = malloc(sizeof(int) * n);\n for (int j = 0; j < n; j++) {\n tmp[j] = 0;\n }\n numMatrix[i] = tmp;\n }\n\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(numMatrix[i]);\n }\n free(numMatrix);\n}\n
space_complexity.kt/* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n val numMatrix: Array<Array<Int>?> = arrayOfNulls(n)\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n val numList: MutableList<MutableList<Int>> = arrayListOf()\n for (i in 0..<n) {\n val tmp = mutableListOf<Int>()\n for (j in 0..<n) {\n tmp.add(0)\n }\n numList.add(tmp)\n }\n}\n
space_complexity.rb### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n Array.new(n) { Array.new(n, 0) }\nend\n
space_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n var tmp = std.ArrayList(i32).init(std.heap.page_allocator);\n defer tmp.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n try tmp.append(0);\n }\n try nodes.append(tmp);\n }\n}\n
Code Visualization Full Screen >
As shown below, the recursive depth of this function is \\(n\\), and in each recursive call, an array is initialized with lengths \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\), averaging \\(n/2\\), thus overall occupying \\(O(n^2)\\) space:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef quadratic_recur(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 0:\n return 0\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = [0] * n\n return quadratic_recur(n - 1)\n
space_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n vector<int> nums(n);\n cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n return quadraticRecur(n - 1);\n}\n
space_complexity.java/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n int[] nums = new int[n];\n System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n return quadraticRecur(n - 1);\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n if (n <= 0) return 0;\n int[] nums = new int[n];\n Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n return QuadraticRecur(n - 1);\n}\n
space_complexity.go/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n if n <= 0 {\n return 0\n }\n nums := make([]int, n)\n fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n return spaceQuadraticRecur(n - 1)\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n if n <= 0 {\n return 0\n }\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = Array(repeating: 0, count: n)\n print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n return quadraticRecur(n: n - 1)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0) return 0;\n List<int> nums = List.filled(n, 0);\n print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n return quadraticRecur(n - 1);\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n if n <= 0 {\n return 0;\n };\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = vec![0; n as usize];\n println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n return quadratic_recur(n - 1);\n}\n
space_complexity.c/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n int *nums = malloc(sizeof(int) * n);\n printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n int res = quadraticRecur(n - 1);\n free(nums);\n return res;\n}\n
space_complexity.kt/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\ntailrec fun quadraticRecur(n: Int): Int {\n if (n <= 0)\n return 0\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n val nums = Array(n) { 0 }\n println(\"\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.size}\")\n return quadraticRecur(n - 1)\n}\n
space_complexity.rb### \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef quadratic_recur(n)\n return 0 unless n > 0\n\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = Array.new(n, 0)\n quadratic_recur(n - 1)\nend\n
space_complexity.zig// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\n if (n <= 0) return 0;\n var nums = [_]i32{0}**n;\n std.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\n return quadraticRecur(n - 1);\n}\n
Code Visualization Full Screen >
Figure 2-18 \u00a0 Recursive function generating quadratic order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential order \\(O(2^n)\\)","text":"Exponential order is common in binary trees. Observe the below image, a \"full binary tree\" with \\(n\\) levels has \\(2^n - 1\\) nodes, occupying \\(O(2^n)\\) space:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef build_tree(n: int) -> TreeNode | None:\n \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n if n == 0:\n return None\n root = TreeNode(0)\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n return root\n
space_complexity.cpp/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return nullptr;\n TreeNode *root = new TreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.java/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n if (n == 0)\n return null;\n TreeNode root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.cs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n if (n == 0) return null;\n TreeNode root = new(0) {\n left = BuildTree(n - 1),\n right = BuildTree(n - 1)\n };\n return root;\n}\n
space_complexity.go/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *TreeNode {\n if n == 0 {\n return nil\n }\n root := NewTreeNode(0)\n root.Left = buildTree(n - 1)\n root.Right = buildTree(n - 1)\n return root\n}\n
space_complexity.swift/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n if n == 0 {\n return nil\n }\n let root = TreeNode(x: 0)\n root.left = buildTree(n: n - 1)\n root.right = buildTree(n: n - 1)\n return root\n}\n
space_complexity.js/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.ts/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.dart/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n if (n == 0) return null;\n TreeNode root = TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.rs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n if n == 0 {\n return None;\n };\n let root = TreeNode::new(0);\n root.borrow_mut().left = build_tree(n - 1);\n root.borrow_mut().right = build_tree(n - 1);\n return Some(root);\n}\n
space_complexity.c/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return NULL;\n TreeNode *root = newTreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.kt/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfun buildTree(n: Int): TreeNode? {\n if (n == 0)\n return null\n val root = TreeNode(0)\n root.left = buildTree(n - 1)\n root.right = buildTree(n - 1)\n return root\n}\n
space_complexity.rb### \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09###\ndef build_tree(n)\n return if n == 0\n\n TreeNode.new.tap do |root|\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n end\nend\n
space_complexity.zig// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\n if (n == 0) return null;\n const root = try mem_allocator.create(inc.TreeNode(i32));\n root.init(0);\n root.left = try buildTree(mem_allocator, n - 1);\n root.right = try buildTree(mem_allocator, n - 1);\n return root;\n}\n
Code Visualization Full Screen >
Figure 2-19 \u00a0 Full binary tree generating exponential order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic order \\(O(\\log n)\\)","text":"Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length \\(n\\) is recursively divided in half each round, forming a recursion tree of height \\(\\log n\\), using \\(O(\\log n)\\) stack frame space.
Another example is converting a number to a string. Given a positive integer \\(n\\), its number of digits is \\(\\log_{10} n + 1\\), corresponding to the length of the string, thus the space complexity is \\(O(\\log_{10} n + 1) = O(\\log n)\\).
"},{"location":"chapter_computational_complexity/space_complexity/#244-balancing-time-and-space","title":"2.4.4 \u00a0 Balancing time and space","text":"Ideally, we aim for both time complexity and space complexity to be optimal. However, in practice, optimizing both simultaneously is often difficult.
Lowering time complexity usually comes at the cost of increased space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm speed is known as \"space-time tradeoff\"; the reverse is known as \"time-space tradeoff\".
The choice depends on which aspect we value more. In most cases, time is more precious than space, so \"space-time tradeoff\" is often the more common strategy. Of course, controlling space complexity is also very important when dealing with large volumes of data.
"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 Summary","text":""},{"location":"chapter_computational_complexity/summary/#1-key-review","title":"1. \u00a0 Key review","text":"Algorithm Efficiency Assessment
Time Complexity
Space Complexity
Q: Is the space complexity of tail recursion \\(O(1)\\)?
Theoretically, the space complexity of a tail-recursive function can be optimized to \\(O(1)\\). However, most programming languages (such as Java, Python, C++, Go, C#) do not support automatic optimization of tail recursion, so it's generally considered to have a space complexity of \\(O(n)\\).
Q: What is the difference between the terms \"function\" and \"method\"?
A \"function\" can be executed independently, with all parameters passed explicitly. A \"method\" is associated with an object and is implicitly passed to the object calling it, able to operate on the data contained within an instance of a class.
Here are some examples from common programming languages:
Q: Does the \"Common Types of Space Complexity\" figure reflect the absolute size of occupied space?
No, the figure shows space complexities, which reflect growth trends, not the absolute size of the occupied space.
If you take \\(n = 8\\), you might find that the values of each curve don't correspond to their functions. This is because each curve includes a constant term, intended to compress the value range into a visually comfortable range.
In practice, since we usually don't know the \"constant term\" complexity of each method, it's generally not possible to choose the best solution for \\(n = 8\\) based solely on complexity. However, for \\(n = 8^5\\), it's much easier to choose, as the growth trend becomes dominant.
"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 Time complexity","text":"Time complexity is a concept used to measure how the run time of an algorithm increases with the size of the input data. Understanding time complexity is crucial for accurately assessing the efficiency of an algorithm.
+
might take 1 ns, a multiplication operation *
might take 10 ns, a print operation print()
might take 5 ns, etc.For example, consider the following code with an input size of \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig# Under an operating platform\ndef algorithm(n: int):\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # Cycle n times\n for _ in range(n): # 1 ns\n print(0) # 5 ns\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n cout << 0 << endl; // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n System.out.println(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid Algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n Console.WriteLine(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfunc algorithm(n int) {\n a := 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // Loop n times\n for i := 0; i < n; i++ { // 1 ns\n fmt.Println(a) // 5 ns\n }\n}\n
// Under a particular operating platform\nfunc algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // Loop n times\n for _ in 0 ..< n { // 1 ns\n print(0) // 5 ns\n }\n}\n
// Under a particular operating platform\nfunction algorithm(n) {\n var a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for(let i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n console.log(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfunction algorithm(n: number): void {\n var a: number = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for(let i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n console.log(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n print(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfn algorithm(n: i32) {\n let mut a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for _ in 0..n { // 1 ns for each round i++\n println!(\"{}\", 0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n printf(\"%d\", 0); // 5 ns\n }\n}\n
\n
// Under a particular operating platform\nfn algorithm(n: usize) void {\n var a: i32 = 2; // 1 ns\n a += 1; // 1 ns\n a *= 2; // 10 ns\n // Loop n times\n for (0..n) |_| { // 1 ns\n std.debug.print(\"{}\\n\", .{0}); // 5 ns\n }\n}\n
Using the above method, the run time of the algorithm can be calculated as \\((6n + 12)\\) ns:
\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]However, in practice, counting the run time of an algorithm is neither practical nor reasonable. First, we don't want to tie the estimated time to the running platform, as algorithms need to run on various platforms. Second, it's challenging to know the run time for each type of operation, making the estimation process difficult.
"},{"location":"chapter_computational_complexity/time_complexity/#231-assessing-time-growth-trend","title":"2.3.1 \u00a0 Assessing time growth trend","text":"Time complexity analysis does not count the algorithm's run time, but rather the growth trend of the run time as the data volume increases.
Let's understand this concept of \"time growth trend\" with an example. Assume the input data size is \\(n\\), and consider three algorithms A
, B
, and C
:
# Time complexity of algorithm A: constant order\ndef algorithm_A(n: int):\n print(0)\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n: int):\n for _ in range(n):\n print(0)\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n: int):\n for _ in range(1000000):\n print(0)\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n cout << 0 << endl;\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n cout << 0 << endl;\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n cout << 0 << endl;\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n System.out.println(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n System.out.println(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n System.out.println(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid AlgorithmA(int n) {\n Console.WriteLine(0);\n}\n// Time complexity of algorithm B: linear order\nvoid AlgorithmB(int n) {\n for (int i = 0; i < n; i++) {\n Console.WriteLine(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid AlgorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n Console.WriteLine(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfunc algorithm_A(n int) {\n fmt.Println(0)\n}\n// Time complexity of algorithm B: linear order\nfunc algorithm_B(n int) {\n for i := 0; i < n; i++ {\n fmt.Println(0)\n }\n}\n// Time complexity of algorithm C: constant order\nfunc algorithm_C(n int) {\n for i := 0; i < 1000000; i++ {\n fmt.Println(0)\n }\n}\n
// Time complexity of algorithm A: constant order\nfunc algorithmA(n: Int) {\n print(0)\n}\n\n// Time complexity of algorithm B: linear order\nfunc algorithmB(n: Int) {\n for _ in 0 ..< n {\n print(0)\n }\n}\n\n// Time complexity of algorithm C: constant order\nfunc algorithmC(n: Int) {\n for _ in 0 ..< 1_000_000 {\n print(0)\n }\n}\n
// Time complexity of algorithm A: constant order\nfunction algorithm_A(n) {\n console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n) {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n) {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfunction algorithm_A(n: number): void {\n console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n: number): void {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n: number): void {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithmA(int n) {\n print(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithmB(int n) {\n for (int i = 0; i < n; i++) {\n print(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n print(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfn algorithm_A(n: i32) {\n println!(\"{}\", 0);\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) {\n for _ in 0..n {\n println!(\"{}\", 0);\n }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) {\n for _ in 0..1000000 {\n println!(\"{}\", 0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n printf(\"%d\", 0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n printf(\"%d\", 0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n printf(\"%d\", 0);\n }\n}\n
\n
// Time complexity of algorithm A: constant order\nfn algorithm_A(n: usize) void {\n _ = n;\n std.debug.print(\"{}\\n\", .{0});\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) void {\n for (0..n) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) void {\n _ = n;\n for (0..1000000) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n
The following figure shows the time complexities of these three algorithms.
A
has just one print operation, and its run time does not grow with \\(n\\). Its time complexity is considered \"constant order.\"B
involves a print operation looping \\(n\\) times, and its run time grows linearly with \\(n\\). Its time complexity is \"linear order.\"C
has a print operation looping 1,000,000 times. Although it takes a long time, it is independent of the input data size \\(n\\). Therefore, the time complexity of C
is the same as A
, which is \"constant order.\"Figure 2-7 \u00a0 Time growth trend of algorithms a, b, and c
Compared to directly counting the run time of an algorithm, what are the characteristics of time complexity analysis?
B
has linearly growing run time, which is slower than algorithm A
when \\(n > 1\\) and slower than C
when \\(n > 1,000,000\\). In fact, as long as the input data size \\(n\\) is sufficiently large, a \"constant order\" complexity algorithm will always be better than a \"linear order\" one, demonstrating the essence of time growth trend.A
and C
have the same time complexity, their actual run times can be quite different. Similarly, even though algorithm B
has a higher time complexity than C
, it is clearly superior when the input data size \\(n\\) is small. In these cases, it's difficult to judge the efficiency of algorithms based solely on time complexity. Nonetheless, despite these issues, complexity analysis remains the most effective and commonly used method for evaluating algorithm efficiency.Consider a function with an input size of \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef algorithm(n: int):\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # Cycle n times\n for i in range(n): # +1\n print(0) # +1\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n cout << 0 << endl; // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n System.out.println(0); // +1\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n Console.WriteLine(0); // +1\n }\n}\n
func algorithm(n int) {\n a := 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // Loop n times\n for i := 0; i < n; i++ { // +1\n fmt.Println(a) // +1\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // Loop n times\n for _ in 0 ..< n { // +1\n print(0) // +1\n }\n}\n
function algorithm(n) {\n var a = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for(let i = 0; i < n; i++){ // +1 (execute i ++ every round)\n console.log(0); // +1\n }\n}\n
function algorithm(n: number): void{\n var a: number = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for(let i = 0; i < n; i++){ // +1 (execute i ++ every round)\n console.log(0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n print(0); // +1\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n\n // Loop n times\n for _ in 0..n { // +1 (execute i ++ every round)\n println!(\"{}\", 0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n printf(\"%d\", 0); // +1\n }\n} \n
\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for (0..n) |_| { // +1 (execute i ++ every round)\n std.debug.print(\"{}\\n\", .{0}); // +1\n }\n}\n
Given a function that represents the number of operations of an algorithm as a function of the input size \\(n\\), denoted as \\(T(n)\\), consider the following example:
\\[ T(n) = 3 + 2n \\]Since \\(T(n)\\) is a linear function, its growth trend is linear, and therefore, its time complexity is of linear order, denoted as \\(O(n)\\). This mathematical notation, known as \"big-O notation,\" represents the \"asymptotic upper bound\" of the function \\(T(n)\\).
In essence, time complexity analysis is about finding the asymptotic upper bound of the \"number of operations \\(T(n)\\)\". It has a precise mathematical definition.
Asymptotic Upper Bound
If there exist positive real numbers \\(c\\) and \\(n_0\\) such that for all \\(n > n_0\\), \\(T(n) \\leq c \\cdot f(n)\\), then \\(f(n)\\) is considered an asymptotic upper bound of \\(T(n)\\), denoted as \\(T(n) = O(f(n))\\).
As illustrated below, calculating the asymptotic upper bound involves finding a function \\(f(n)\\) such that, as \\(n\\) approaches infinity, \\(T(n)\\) and \\(f(n)\\) have the same growth order, differing only by a constant factor \\(c\\).
Figure 2-8 \u00a0 Asymptotic upper bound of a function
"},{"location":"chapter_computational_complexity/time_complexity/#233-calculation-method","title":"2.3.3 \u00a0 Calculation method","text":"While the concept of asymptotic upper bound might seem mathematically dense, you don't need to fully grasp it right away. Let's first understand the method of calculation, which can be practiced and comprehended over time.
Once \\(f(n)\\) is determined, we obtain the time complexity \\(O(f(n))\\). But how do we determine the asymptotic upper bound \\(f(n)\\)? This process generally involves two steps: counting the number of operations and determining the asymptotic upper bound.
"},{"location":"chapter_computational_complexity/time_complexity/#1-step-1-counting-the-number-of-operations","title":"1. \u00a0 Step 1: counting the number of operations","text":"This step involves going through the code line by line. However, due to the presence of the constant \\(c\\) in \\(c \\cdot f(n)\\), all coefficients and constant terms in \\(T(n)\\) can be ignored. This principle allows for simplification techniques in counting operations.
Given a function, we can use these techniques to count operations:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef algorithm(n: int):\n a = 1 # +0 (trick 1)\n a = a + n # +0 (trick 1)\n # +n (technique 2)\n for i in range(5 * n + 1):\n print(0)\n # +n*n (technique 3)\n for i in range(2 * n):\n for j in range(n + 1):\n print(0)\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n cout << 0 << endl;\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n cout << 0 << endl;\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n System.out.println(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n System.out.println(0);\n }\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n Console.WriteLine(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n Console.WriteLine(0);\n }\n }\n}\n
func algorithm(n int) {\n a := 1 // +0 (trick 1)\n a = a + n // +0 (trick 1)\n // +n (technique 2)\n for i := 0; i < 5 * n + 1; i++ {\n fmt.Println(0)\n }\n // +n*n (technique 3)\n for i := 0; i < 2 * n; i++ {\n for j := 0; j < n + 1; j++ {\n fmt.Println(0)\n }\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +0 (trick 1)\n a = a + n // +0 (trick 1)\n // +n (technique 2)\n for _ in 0 ..< (5 * n + 1) {\n print(0)\n }\n // +n*n (technique 3)\n for _ in 0 ..< (2 * n) {\n for _ in 0 ..< (n + 1) {\n print(0)\n }\n }\n}\n
function algorithm(n) {\n let a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n (technique 3)\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
function algorithm(n: number): void {\n let a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n (technique 3)\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n print(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n print(0);\n }\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n\n // +n (technique 2)\n for i in 0..(5 * n + 1) {\n println!(\"{}\", 0);\n }\n\n // +n*n (technique 3)\n for i in 0..(2 * n) {\n for j in 0..(n + 1) {\n println!(\"{}\", 0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n printf(\"%d\", 0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n printf(\"%d\", 0);\n }\n }\n}\n
\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +0 (trick 1)\n a = a + @as(i32, @intCast(n)); // +0 (trick 1)\n\n // +n (technique 2)\n for(0..(5 * n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n\n // +n*n (technique 3)\n for(0..(2 * n)) |_| {\n for(0..(n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n }\n}\n
The formula below shows the counting results before and after simplification, both leading to a time complexity of \\(O(n^2)\\):
\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{Complete Count (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{Simplified Count (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-step-2-determining-the-asymptotic-upper-bound","title":"2. \u00a0 Step 2: determining the asymptotic upper bound","text":"The time complexity is determined by the highest order term in \\(T(n)\\). This is because, as \\(n\\) approaches infinity, the highest order term dominates, rendering the influence of other terms negligible.
The following table illustrates examples of different operation counts and their corresponding time complexities. Some exaggerated values are used to emphasize that coefficients cannot alter the order of growth. When \\(n\\) becomes very large, these constants become insignificant.
Table: Time complexity for different operation counts
Operation Count \\(T(n)\\) Time Complexity \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234-common-types-of-time-complexity","title":"2.3.4 \u00a0 Common types of time complexity","text":"Let's consider the input data size as \\(n\\). The common types of time complexities are illustrated below, arranged from lowest to highest:
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{Constant Order} < \\text{Logarithmic Order} < \\text{Linear Order} < \\text{Linear-Logarithmic Order} < \\text{Quadratic Order} < \\text{Exponential Order} < \\text{Factorial Order} \\end{aligned} \\]Figure 2-9 \u00a0 Common types of time complexity
"},{"location":"chapter_computational_complexity/time_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant order \\(O(1)\\)","text":"Constant order means the number of operations is independent of the input data size \\(n\\). In the following function, although the number of operations size
might be large, the time complexity remains \\(O(1)\\) as it's unrelated to \\(n\\):
def constant(n: int) -> int:\n \"\"\"\u5e38\u6570\u9636\"\"\"\n count = 0\n size = 100000\n for _ in range(size):\n count += 1\n return count\n
time_complexity.cpp/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u5e38\u6570\u9636 */\nint Constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n count := 0\n size := 100000\n for i := 0; i < size; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n var count = 0\n let size = 100_000\n for _ in 0 ..< size {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (var i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n _ = n;\n let mut count = 0;\n let size = 100_000;\n for _ in 0..size {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n int i = 0;\n for (int i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u5e38\u6570\u9636 */\nfun constant(n: Int): Int {\n var count = 0\n val size = 10_0000\n for (i in 0..<size)\n count++\n return count\n}\n
time_complexity.rb### \u5e38\u6570\u9636 ###\ndef constant(n)\n count = 0\n size = 100000\n\n (0...size).each { count += 1 }\n\n count\nend\n
time_complexity.zig// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n _ = n;\n var count: i32 = 0;\n const size: i32 = 100_000;\n var i: i32 = 0;\n while(i<size) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/time_complexity/#2-linear-order-on","title":"2. \u00a0 Linear order \\(O(n)\\)","text":"Linear order indicates the number of operations grows linearly with the input data size \\(n\\). Linear order commonly appears in single-loop structures:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef linear(n: int) -> int:\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n count = 0\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n count := 0\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n var count = 0\n for _ in 0 ..< n {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n let mut count = 0;\n for _ in 0..n {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u9636 */\nfun linear(n: Int): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (i in 0..<n)\n count++\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u9636 ###\ndef linear(n)\n count = 0\n (0...n).each { count += 1 }\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
Operations like array traversal and linked list traversal have a time complexity of \\(O(n)\\), where \\(n\\) is the length of the array or list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef array_traversal(nums: list[int]) -> int:\n \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums:\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n foreach (int num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for range nums {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (var _num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfun arrayTraversal(nums: IntArray): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (num in nums) {\n count++\n }\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09###\ndef array_traversal(nums)\n count = 0\n\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums\n count += 1\n end\n\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\n var count: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (nums) |_| {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
It's important to note that the input data size \\(n\\) should be determined based on the type of input data. For example, in the first example, \\(n\\) represents the input data size, while in the second example, the length of the array \\(n\\) is the data size.
"},{"location":"chapter_computational_complexity/time_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic order \\(O(n^2)\\)","text":"Quadratic order means the number of operations grows quadratically with the input data size \\(n\\). Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of \\(O(n)\\), resulting in an overall complexity of \\(O(n^2)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef quadratic(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i in range(n):\n for j in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i := 0; i < n; i++ {\n for j := 0; j < n; j++ {\n count++\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0 ..< n {\n for _ in 0 ..< n {\n count += 1\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0..n {\n for _ in 0..n {\n count += 1;\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.kt/* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (i in 0..<n) {\n for (j in 0..<n) {\n count++\n }\n }\n return count\n}\n
time_complexity.rb### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n count = 0\n\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i in 0...n\n for j in 0...n\n count += 1\n end\n end\n\n count\nend\n
time_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n count += 1;\n }\n }\n return count;\n}\n
Code Visualization Full Screen >
The following image compares constant order, linear order, and quadratic order time complexities.
Figure 2-10 \u00a0 Constant, linear, and quadratic order time complexities
For instance, in bubble sort, the outer loop runs \\(n - 1\\) times, and the inner loop runs \\(n-1\\), \\(n-2\\), ..., \\(2\\), \\(1\\) times, averaging \\(n / 2\\) times, resulting in a time complexity of \\(O((n - 1) n / 2) = O(n^2)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef bubble_sort(nums: list[int]) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n count = 0 # \u8ba1\u6570\u5668\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(len(nums) - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp: int = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n count := 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp := nums[j]\n nums[j] = nums[j+1]\n nums[j+1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n var count = 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in nums.indices.dropFirst().reversed() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0 ..< i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (var i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (var j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n let mut count = 0; // \u8ba1\u6570\u5668\n\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = n - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.kt/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfun bubbleSort(nums: IntArray): Int {\n var count = 0\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (i in nums.size - 1 downTo 1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (j in 0..<i) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.rb### \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09###\ndef bubble_sort(nums)\n count = 0 # \u8ba1\u6570\u5668\n\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (nums.length - 1).downto(0)\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0...i\n if nums[j] > nums[j + 1]\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n end\n end\n end\n\n count\nend\n
time_complexity.zig// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\n var count: i32 = 0; // \u8ba1\u6570\u5668 \n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: i32 = @as(i32, @intCast(nums.len)) - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/time_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential order \\(O(2^n)\\)","text":"Biological \"cell division\" is a classic example of exponential order growth: starting with one cell, it becomes two after one division, four after two divisions, and so on, resulting in \\(2^n\\) cells after \\(n\\) divisions.
The following image and code simulate the cell division process, with a time complexity of \\(O(2^n)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef exponential(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n base = 1\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in range(n):\n for _ in range(base):\n count += 1\n base *= 2\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n int count = 0, bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n count, base := 0, 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for i := 0; i < n; i++ {\n for j := 0; j < base; j++ {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n var count = 0\n var base = 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0 ..< n {\n for _ in 0 ..< base {\n count += 1\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (var i = 0; i < n; i++) {\n for (var j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n let mut count = 0;\n let mut base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0..n {\n for _ in 0..base {\n count += 1\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0;\n int bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.kt/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun exponential(n: Int): Int {\n var count = 0\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n var base = 1\n for (i in 0..<n) {\n for (j in 0..<base) {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.rb### \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef exponential(n)\n count, base = 0, 1\n\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n (0...n).each do\n (0...base).each { count += 1 }\n base *= 2\n end\n\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\nend\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\n var count: i32 = 0;\n var bas: i32 = 1;\n var i: i32 = 0;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < bas) : (j += 1) {\n count += 1;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-11 \u00a0 Exponential order time complexity
In practice, exponential order often appears in recursive functions. For example, in the code below, it recursively splits into two halves, stopping after \\(n\\) divisions:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef exp_recur(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 1:\n return 1\n return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n if (n == 1) return 1;\n return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n if n == 1 {\n return 1\n }\n return expRecur(n-1) + expRecur(n-1) + 1\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n if n == 1 {\n return 1\n }\n return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n if n == 1 {\n return 1;\n }\n exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.kt/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun expRecur(n: Int): Int {\n if (n == 1) {\n return 1\n }\n return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
time_complexity.rb### \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef exp_recur(n)\n return 1 if n == 1\n exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
Code Visualization Full Screen >
Exponential order growth is extremely rapid and is commonly seen in exhaustive search methods (brute force, backtracking, etc.). For large-scale problems, exponential order is unacceptable, often requiring dynamic programming or greedy algorithms as solutions.
"},{"location":"chapter_computational_complexity/time_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic order \\(O(\\log n)\\)","text":"In contrast to exponential order, logarithmic order reflects situations where \"the size is halved each round.\" Given an input data size \\(n\\), since the size is halved each round, the number of iterations is \\(\\log_2 n\\), the inverse function of \\(2^n\\).
The following image and code simulate the \"halving each round\" process, with a time complexity of \\(O(\\log_2 n)\\), commonly abbreviated as \\(O(\\log n)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef logarithmic(n: int) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n while n > 1:\n n = n / 2\n count += 1\n return count\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n /= 2;\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n int) int {\n count := 0\n for n > 1 {\n n = n / 2\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Int) -> Int {\n var count = 0\n var n = n\n while n > 1 {\n n = n / 2\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n ~/ 2;\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: i32) -> i32 {\n let mut count = 0;\n while n > 1 {\n n = n / 2;\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun logarithmic(n: Int): Int {\n var n1 = n\n var count = 0\n while (n1 > 1) {\n n1 /= 2\n count++\n }\n return count\n}\n
time_complexity.rb### \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef logarithmic(n)\n count = 0\n\n while n > 1\n n /= 2\n count += 1\n end\n\n count\nend\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: i32) i32 {\n var count: i32 = 0;\n var n_var = n;\n while (n_var > 1)\n {\n n_var = n_var / 2;\n count +=1;\n }\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-12 \u00a0 Logarithmic order time complexity
Like exponential order, logarithmic order also frequently appears in recursive functions. The code below forms a recursive tree of height \\(\\log_2 n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef log_recur(n: int) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 1:\n return 0\n return log_recur(n / 2) + 1\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(int n) {\n if (n <= 1) return 0;\n return LogRecur(n / 2) + 1;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n int) int {\n if n <= 1 {\n return 0\n }\n return logRecur(n/2) + 1\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Int) -> Int {\n if n <= 1 {\n return 0\n }\n return logRecur(n: n / 2) + 1\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1) return 0;\n return logRecur(n ~/ 2) + 1;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: i32) -> i32 {\n if n <= 1 {\n return 0;\n }\n log_recur(n / 2) + 1\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.kt/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun logRecur(n: Int): Int {\n if (n <= 1)\n return 0\n return logRecur(n / 2) + 1\n}\n
time_complexity.rb### \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef log_recur(n)\n return 0 unless n > 1\n log_recur(n / 2) + 1\nend\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: i32) i32 {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
Code Visualization Full Screen >
Logarithmic order is typical in algorithms based on the divide-and-conquer strategy, embodying the \"split into many\" and \"simplify complex problems\" approach. It's slow-growing and is the most ideal time complexity after constant order.
What is the base of \\(O(\\log n)\\)?
Technically, \"splitting into \\(m\\)\" corresponds to a time complexity of \\(O(\\log_m n)\\). Using the logarithm base change formula, we can equate different logarithmic complexities:
\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]This means the base \\(m\\) can be changed without affecting the complexity. Therefore, we often omit the base \\(m\\) and simply denote logarithmic order as \\(O(\\log n)\\).
"},{"location":"chapter_computational_complexity/time_complexity/#6-linear-logarithmic-order-on-log-n","title":"6. \u00a0 Linear-logarithmic order \\(O(n \\log n)\\)","text":"Linear-logarithmic order often appears in nested loops, with the complexities of the two loops being \\(O(\\log n)\\) and \\(O(n)\\) respectively. The related code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef linear_log_recur(n: int) -> int:\n \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n if n <= 1:\n return 1\n count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(int n) {\n if (n <= 1) return 1;\n int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n int) int {\n if n <= 1 {\n return 1\n }\n count := linearLogRecur(n/2) + linearLogRecur(n/2)\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Int) -> Int {\n if n <= 1 {\n return 1\n }\n var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n for _ in stride(from: 0, to: n, by: 1) {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1) return 1;\n int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: i32) -> i32 {\n if n <= 1 {\n return 1;\n }\n let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n for _ in 0..n as i32 {\n count += 1;\n }\n return count;\n}\n
time_complexity.c/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfun linearLogRecur(n: Int): Int {\n if (n <= 1)\n return 1\n var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n for (i in 0..<n.toInt()) {\n count++\n }\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u5bf9\u6570\u9636 ###\ndef linear_log_recur(n)\n return 1 unless n > 1\n\n count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n (0...n).each { count += 1 }\n\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: i32) i32 {\n if (n <= 1) return 1;\n var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
The image below demonstrates how linear-logarithmic order is generated. Each level of a binary tree has \\(n\\) operations, and the tree has \\(\\log_2 n + 1\\) levels, resulting in a time complexity of \\(O(n \\log n)\\).
Figure 2-13 \u00a0 Linear-logarithmic order time complexity
Mainstream sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), such as quicksort, mergesort, and heapsort.
"},{"location":"chapter_computational_complexity/time_complexity/#7-factorial-order-on","title":"7. \u00a0 Factorial order \\(O(n!)\\)","text":"Factorial order corresponds to the mathematical problem of \"full permutation.\" Given \\(n\\) distinct elements, the total number of possible permutations is:
\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]Factorials are typically implemented using recursion. As shown in the image and code below, the first level splits into \\(n\\) branches, the second level into \\(n - 1\\) branches, and so on, stopping after the \\(n\\)th level:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef factorial_recur(n: int) -> int:\n \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 0:\n return 1\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in range(n):\n count += factorial_recur(n - 1)\n return count\n
time_complexity.cpp/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.java/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.cs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += FactorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.go/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n if n == 0 {\n return 1\n }\n count := 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for i := 0; i < n; i++ {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.swift/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n if n == 0 {\n return 1\n }\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0 ..< n {\n count += factorialRecur(n: n - 1)\n }\n return count\n}\n
time_complexity.js/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.ts/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.dart/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (var i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.rs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n if n == 0 {\n return 1;\n }\n let mut count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0..n {\n count += factorial_recur(n - 1);\n }\n count\n}\n
time_complexity.c/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.kt/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun factorialRecur(n: Int): Int {\n if (n == 0)\n return 1\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (i in 0..<n) {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.rb### \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef factorial_recur(n)\n return 1 if n == 0\n\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n (0...n).each { count += factorial_recur(n - 1) }\n\n count\nend\n
time_complexity.zig// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\n if (n == 0) return 1;\n var count: i32 = 0;\n var i: i32 = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n while (i < n) : (i += 1) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-14 \u00a0 Factorial order time complexity
Note that factorial order grows even faster than exponential order; it's unacceptable for larger \\(n\\) values.
"},{"location":"chapter_computational_complexity/time_complexity/#235-worst-best-and-average-time-complexities","title":"2.3.5 \u00a0 Worst, best, and average time complexities","text":"The time efficiency of an algorithm is often not fixed but depends on the distribution of the input data. Assume we have an array nums
of length \\(n\\), consisting of numbers from \\(1\\) to \\(n\\), each appearing only once, but in a randomly shuffled order. The task is to return the index of the element \\(1\\). We can draw the following conclusions:
nums = [?, ?, ..., 1]
, that is, when the last element is \\(1\\), it requires a complete traversal of the array, achieving the worst-case time complexity of \\(O(n)\\).nums = [1, ?, ?, ...]
, that is, when the first element is \\(1\\), no matter the length of the array, no further traversal is needed, achieving the best-case time complexity of \\(\\Omega(1)\\).The \"worst-case time complexity\" corresponds to the asymptotic upper bound, denoted by the big \\(O\\) notation. Correspondingly, the \"best-case time complexity\" corresponds to the asymptotic lower bound, denoted by \\(\\Omega\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig worst_best_time_complexity.pydef random_numbers(n: int) -> list[int]:\n \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = [i for i in range(1, n + 1)]\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n random.shuffle(nums)\n return nums\n\ndef find_one(nums: list[int]) -> int:\n \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n for i in range(len(nums)):\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1:\n return i\n return -1\n
worst_best_time_complexity.cpp/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n vector<int> nums(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n for (int i = 0; i < nums.size(); i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.java/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n Integer[] nums = new Integer[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n Collections.shuffle(Arrays.asList(nums));\n // Integer[] -> int[]\n int[] res = new int[n];\n for (int i = 0; i < n; i++) {\n res[i] = nums[i];\n }\n return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n for (int i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.cs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n int[] nums = new int[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = 0; i < nums.Length; i++) {\n int index = new Random().Next(i, nums.Length);\n (nums[i], nums[index]) = (nums[index], nums[i]);\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n for (int i = 0; i < nums.Length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.go/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n nums := make([]int, n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for i := 0; i < n; i++ {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n rand.Shuffle(len(nums), func(i, j int) {\n nums[i], nums[j] = nums[j], nums[i]\n })\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n for i := 0; i < len(nums); i++ {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.swift/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n var nums = Array(1 ... n)\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle()\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n for i in nums.indices {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.js/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.ts/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.dart/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n final nums = List.filled(n, 0);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (var i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle();\n\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n for (var i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1) return i;\n }\n\n return -1;\n}\n
worst_best_time_complexity.rs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n let mut nums = (1..=n).collect::<Vec<i32>>();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle(&mut thread_rng());\n nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n for i in 0..nums.len() {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return Some(i);\n }\n }\n None\n}\n
worst_best_time_complexity.c/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n int *nums = (int *)malloc(n * sizeof(int));\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = n - 1; i > 0; i--) {\n int j = rand() % (i + 1);\n int temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n for (int i = 0; i < n; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.kt/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfun randomNumbers(n: Int): Array<Int?> {\n val nums = IntArray(n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (i in 0..<n) {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n val mutableList = nums.toMutableList()\n mutableList.shuffle()\n // Integer[] -> int[]\n val res = arrayOfNulls<Int>(n)\n for (i in 0..<n) {\n res[i] = mutableList[i]\n }\n return res\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfun findOne(nums: Array<Int?>): Int {\n for (i in nums.indices) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i\n }\n return -1\n}\n
worst_best_time_complexity.rb### \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71 ###\ndef random_numbers(n)\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = Array.new(n) { |i| i + 1 }\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle!\nend\n\n### \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 ###\ndef find_one(nums)\n for i in 0...nums.length\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n return i if nums[i] == 1\n end\n\n -1\nend\n
worst_best_time_complexity.zig// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\n var nums: [n]i32 = undefined;\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (&nums, 0..) |*num, i| {\n num.* = @as(i32, @intCast(i)) + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n const rand = std.crypto.random;\n rand.shuffle(i32, &nums);\n return nums;\n}\n\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\nfn findOne(nums: []i32) i32 {\n for (nums, 0..) |num, i| {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (num == 1) return @intCast(i);\n }\n return -1;\n}\n
Code Visualization Full Screen >
It's important to note that the best-case time complexity is rarely used in practice, as it is usually only achievable under very low probabilities and might be misleading. The worst-case time complexity is more practical as it provides a safety value for efficiency, allowing us to confidently use the algorithm.
From the above example, it's clear that both the worst-case and best-case time complexities only occur under \"special data distributions,\" which may have a small probability of occurrence and may not accurately reflect the algorithm's run efficiency. In contrast, the average time complexity can reflect the algorithm's efficiency under random input data, denoted by the \\(\\Theta\\) notation.
For some algorithms, we can simply estimate the average case under a random data distribution. For example, in the aforementioned example, since the input array is shuffled, the probability of element \\(1\\) appearing at any index is equal. Therefore, the average number of loops for the algorithm is half the length of the array \\(n / 2\\), giving an average time complexity of \\(\\Theta(n / 2) = \\Theta(n)\\).
However, calculating the average time complexity for more complex algorithms can be quite difficult, as it's challenging to analyze the overall mathematical expectation under the data distribution. In such cases, we usually use the worst-case time complexity as the standard for judging the efficiency of the algorithm.
Why is the \\(\\Theta\\) symbol rarely seen?
Possibly because the \\(O\\) notation is more commonly spoken, it is often used to represent the average time complexity. However, strictly speaking, this practice is not accurate. In this book and other materials, if you encounter statements like \"average time complexity \\(O(n)\\)\", please understand it directly as \\(\\Theta(n)\\).
"},{"location":"chapter_data_structure/","title":"Chapter 3. \u00a0 Data structures","text":"Abstract
Data structures serve as a robust and diverse framework.
They offer a blueprint for the orderly organization of data, upon which algorithms come to life.
"},{"location":"chapter_data_structure/#chapter-contents","title":"Chapter Contents","text":"When discussing data in computers, various forms like text, images, videos, voice and 3D models comes to mind. Despite their different organizational forms, they are all composed of various basic data types.
Basic data types are those that the CPU can directly operate on and are directly used in algorithms, mainly including the following.
byte
, short
, int
, long
.float
, double
, used to represent decimals.char
, used to represent letters, punctuation, and even emojis in various languages.bool
, used to represent \"yes\" or \"no\" decisions.Basic data types are stored in computers in binary form. One binary digit is 1 bit. In most modern operating systems, 1 byte consists of 8 bits.
The range of values for basic data types depends on the size of the space they occupy. Below, we take Java as an example.
byte
occupies 1 byte = 8 bits and can represent \\(2^8\\) numbers.int
occupies 4 bytes = 32 bits and can represent \\(2^{32}\\) numbers.The following table lists the space occupied, value range, and default values of various basic data types in Java. While memorizing this table isn't necessary, having a general understanding of it and referencing it when required is recommended.
Table 3-1 \u00a0 Space occupied and value range of basic data types
Type Symbol Space Occupied Minimum Value Maximum Value Default Value Integerbyte
1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) 0 short
2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) 0 int
4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) 0 long
8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) 0 Float float
4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double
8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) 0.0 Char char
2 bytes 0 \\(2^{16} - 1\\) 0 Boolean bool
1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) Please note that the above table is specific to Java's basic data types. Every programming language has its own data type definitions, which might differ in space occupied, value ranges, and default values.
int
can be of any size, limited only by available memory; the floating-point float
is double precision 64-bit; there is no char
type, as a single character is actually a string str
of length 1.char
in C and C++ is 1 byte, while in most programming languages, it depends on the specific character encoding method, as detailed in the \"Character Encoding\" chapter.So, what is the connection between basic data types and data structures? We know that data structures are ways to organize and store data in computers. The focus here is on \"structure\" rather than \"data\".
If we want to represent \"a row of numbers\", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and the ordering of the numbers, but whether the stored content is an integer int
, a decimal float
, or a character char
, is irrelevant to the \"data structure\".
In other words, basic data types provide the \"content type\" of data, while data structures provide the \"way of organizing\" data. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including int
, float
, char
, bool
, etc.
# Using various basic data types to initialize arrays\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python's characters are actually strings of length 1\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python's lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// Using various basic data types to initialize arrays\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// Using various basic data types to initialize arrays\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// Using various basic data types to initialize arrays\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// Using various basic data types to initialize arrays\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// Using various basic data types to initialize arrays\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
// JavaScript's arrays can freely store various basic data types and objects\nconst array = [0, 0.0, 'a', false];\n
// Using various basic data types to initialize arrays\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// Using various basic data types to initialize arrays\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// Using various basic data types to initialize arrays\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// Using various basic data types to initialize arrays\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
// Using various basic data types to initialize arrays\nvar numbers: [5]i32 = undefined;\nvar decimals: [5]f32 = undefined;\nvar characters: [5]u8 = undefined;\nvar bools: [5]bool = undefined;\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 Character encoding *","text":"In the computer system, all data is stored in binary form, and characters (represented by char) are no exception. To represent characters, we need to develop a \"character set\" that defines a one-to-one mapping between each character and binary numbers. With the character set, computers can convert binary numbers to characters by looking up the table.
"},{"location":"chapter_data_structure/character_encoding/#341-ascii-character-set","title":"3.4.1 \u00a0 ASCII character set","text":"The \"ASCII code\" is one of the earliest character sets, officially known as the American Standard Code for Information Interchange. It uses 7 binary digits (the lower 7 bits of a byte) to represent a character, allowing for a maximum of 128 different characters. As shown in the Figure 3-6 , ASCII includes uppercase and lowercase English letters, numbers 0 ~ 9, various punctuation marks, and certain control characters (such as newline and tab).
Figure 3-6 \u00a0 ASCII code
However, ASCII can only represent English characters. With the globalization of computers, a character set called \"EASCII\" was developed to represent more languages. It expands from the 7-bit structure of ASCII to 8 bits, enabling the representation of 256 characters.
Globally, various region-specific EASCII character sets have been introduced. The first 128 characters of these sets are consistent with the ASCII, while the remaining 128 characters are defined differently to accommodate the requirements of different languages.
"},{"location":"chapter_data_structure/character_encoding/#342-gbk-character-set","title":"3.4.2 \u00a0 GBK character set","text":"Later, it was found that EASCII still could not meet the character requirements of many languages. For instance, there are nearly a hundred thousand Chinese characters, with several thousand used regularly. In 1980, the Standardization Administration of China released the \"GB2312\" character set, which included 6763 Chinese characters, essentially fulfilling the computer processing needs for the Chinese language.
However, GB2312 could not handle some rare and traditional characters. The \"GBK\" character set expands GB2312 and includes 21886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented with one byte, while Chinese characters use two bytes.
"},{"location":"chapter_data_structure/character_encoding/#343-unicode-character-set","title":"3.4.3 \u00a0 Unicode character set","text":"With the rapid evolution of computer technology and a plethora of character sets and encoding standards, numerous problems arose. On the one hand, these character sets generally only defined characters for specific languages and could not function properly in multilingual environments. On the other hand, the existence of multiple character set standards for the same language caused garbled text when information was exchanged between computers using different encoding standards.
Researchers of that era thought: What if a comprehensive character set encompassing all global languages and symbols was developed? Wouldn't this resolve the issues associated with cross-linguistic environments and garbled text? Inspired by this idea, the extensive character set, Unicode, was born.
\"Unicode\" is referred to as \"\u7edf\u4e00\u7801\" (Unified Code) in Chinese, theoretically capable of accommodating over a million characters. It aims to incorporate characters from all over the world into a single set, providing a universal character set for processing and displaying various languages and reducing the issues of garbled text due to different encoding standards.
Since its release in 1991, Unicode has continually expanded to include new languages and characters. As of September 2022, Unicode contains 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, while some rare characters may occupy 3 or even 4 bytes.
Unicode is a universal character set that assigns a number (called a \"code point\") to each character, but it does not specify how these character code points should be stored in a computer system. One might ask: How does a system interpret Unicode code points of varying lengths within a text? For example, given a 2-byte code, how does the system determine if it represents a single 2-byte character or two 1-byte characters?
A straightforward solution to this problem is to store all characters as equal-length encodings. As shown in the Figure 3-7 , each character in \"Hello\" occupies 1 byte, while each character in \"\u7b97\u6cd5\" (algorithm) occupies 2 bytes. We could encode all characters in \"Hello \u7b97\u6cd5\" as 2 bytes by padding the higher bits with zeros. This method would enable the system to interpret a character every 2 bytes, recovering the content of the phrase.
Figure 3-7 \u00a0 Unicode encoding example
However, as ASCII has shown us, encoding English only requires 1 byte. Using the above approach would double the space occupied by English text compared to ASCII encoding, which is a waste of memory space. Therefore, a more efficient Unicode encoding method is needed.
"},{"location":"chapter_data_structure/character_encoding/#344-utf-8-encoding","title":"3.4.4 \u00a0 UTF-8 encoding","text":"Currently, UTF-8 has become the most widely used Unicode encoding method internationally. It is a variable-length encoding, using 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters need only 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters need 3 bytes, and some other rare characters need 4 bytes.
The encoding rules for UTF-8 are not complex and can be divided into two cases:
The Figure 3-8 shows the UTF-8 encoding for \"Hello\u7b97\u6cd5\". It can be observed that since the highest \\(n\\) bits are set to \\(1\\), the system can determine the length of the character as \\(n\\) by counting the number of highest bits set to \\(1\\).
But why set the highest 2 bits of the remaining bytes to \\(10\\)? Actually, this \\(10\\) serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the \\(10\\) at the beginning of the byte can help the system quickly detect anomalies.
The reason for using \\(10\\) as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be \\(10\\). This can be proven by contradiction: If the highest two bits of a character are \\(10\\), it indicates that the character's length is \\(1\\), corresponding to ASCII. However, the highest bit of an ASCII character should be \\(0\\), which contradicts the assumption.
Figure 3-8 \u00a0 UTF-8 encoding example
Apart from UTF-8, other common encoding methods include:
From the perspective of storage space, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 to encode some non-English characters (such as Chinese) can be more efficient because it only requires 2 bytes, while UTF-8 might need 3 bytes.
From a compatibility perspective, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.
"},{"location":"chapter_data_structure/character_encoding/#345-character-encoding-in-programming-languages","title":"3.4.5 \u00a0 Character encoding in programming languages","text":"Historically, many programming languages utilized fixed-length encodings such as UTF-16 or UTF-32 for processing strings during program execution. This allows strings to be handled as arrays, offering several advantages:
The design of character encoding schemes in programming languages is an interesting topic involving various factors:
String
type uses UTF-16 encoding, with each character occupying 2 bytes. This was based on the initial belief that 16 bits were sufficient to represent all possible characters and proven incorrect later. As the Unicode standard expanded beyond 16 bits, characters in Java may now be represented by a pair of 16-bit values, known as \u201csurrogate pairs.\u201dDue to the underestimation of character counts, these languages had to use \"surrogate pairs\" to represent Unicode characters exceeding 16 bits. This approach has its drawbacks: strings containing surrogate pairs may have characters occupying 2 or 4 bytes, losing the advantage of fixed-length encoding. Additionally, handling surrogate pairs adds complexity and debugging difficulty to programming.
Addressing these challenges, some languages have adopted alternative encoding strategies:
str
type uses Unicode encoding with a flexible representation where the storage length of characters depends on the largest Unicode code point in the string. If all characters are ASCII, each character occupies 1 byte, 2 bytes for characters within the Basic Multilingual Plane (BMP), and 4 bytes for characters beyond the BMP.string
type internally uses UTF-8 encoding. Go also provides the rune
type for representing individual Unicode code points.str
and String
types use UTF-8 encoding internally. Rust also offers the char
type for individual Unicode code points.It\u2019s important to note that the above discussion pertains to how strings are stored in programming languages, which is different from how strings are stored in files or transmitted over networks. For file storage or network transmission, strings are usually encoded in UTF-8 format for optimal compatibility and space efficiency.
"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 Classification of data structures","text":"Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified into \"logical structure\" and \"physical structure\".
"},{"location":"chapter_data_structure/classification_of_data_structure/#311-logical-structure-linear-and-non-linear","title":"3.1.1 \u00a0 Logical structure: linear and non-linear","text":"The logical structures reveal the logical relationships between data elements. In arrays and linked lists, data are arranged in a specific sequence, demonstrating the linear relationship between data; while in trees, data are arranged hierarchically from the top down, showing the derived relationship between \"ancestors\" and \"descendants\"; and graphs are composed of nodes and edges, reflecting the intricate network relationship.
As shown in the Figure 3-1 , logical structures can be divided into two major categories: \"linear\" and \"non-linear\". Linear structures are more intuitive, indicating data is arranged linearly in logical relationships; non-linear structures, conversely, are arranged non-linearly.
Figure 3-1 \u00a0 Linear and non-linear data structures
Non-linear data structures can be further divided into tree structures and network structures.
During the execution of an algorithm, the data being processed is stored in memory. The Figure 3-2 shows a computer memory stick where each black square is a physical memory space. We can think of memory as a vast Excel spreadsheet, with each cell capable of storing a certain amount of data.
The system accesses the data at the target location by means of a memory address. As shown in the Figure 3-2 , the computer assigns a unique identifier to each cell in the table according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access the data stored in memory.
Figure 3-2 \u00a0 Memory stick, memory spaces, memory addresses
Tip
It's worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is more complex, involving concepts like address space, memory management, cache mechanisms, virtual memory, and physical memory.
Memory is a shared resource for all programs. When a block of memory is occupied by one program, it cannot be simultaneously used by other programs. Therefore, considering memory resources is crucial in designing data structures and algorithms. For instance, the algorithm's peak memory usage should not exceed the remaining free memory of the system; if there is a lack of contiguous memory blocks, then the data structure chosen must be able to be stored in non-contiguous memory blocks.
As illustrated in the Figure 3-3 , the physical structure reflects the way data is stored in computer memory and it can be divided into contiguous space storage (arrays) and non-contiguous space storage (linked lists). The two types of physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.
Figure 3-3 \u00a0 Contiguous space storage and dispersed space storage
It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both. For example, stacks and queues can be implemented using either arrays or linked lists; while implementations of hash tables may involve both arrays and linked lists. - Array-based implementations: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, Matrices, Tensors (arrays with dimensions \\(\\geq 3\\)). - Linked-list-based implementations: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, etc.
Data structures implemented based on arrays are also called \u201cStatic Data Structures,\u201d meaning their length cannot be changed after initialization. Conversely, those based on linked lists are called \u201cDynamic Data Structures,\u201d which can still adjust their size during program execution.
Tip
If you find it challenging to comprehend the physical structure, it is recommended that you read the next chapter, \"Arrays and Linked Lists,\" and revisit this section later.
"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 Number encoding *","text":"Note
In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.
"},{"location":"chapter_data_structure/number_encoding/#331-integer-encoding","title":"3.3.1 \u00a0 Integer encoding","text":"In the table from the previous section, we observed that all integer types can represent one more negative number than positive numbers, such as the byte
range of \\([-128, 127]\\). This phenomenon seems counterintuitive, and its underlying reason involves knowledge of sign-magnitude, one's complement, and two's complement encoding.
Firstly, it's important to note that numbers are stored in computers using the two's complement form. Before analyzing why this is the case, let's define these three encoding methods:
The following diagram illustrates the conversions among sign-magnitude, one's complement, and two's complement:
Figure 3-4 \u00a0 Conversions between sign-magnitude, one's complement, and two's complement
Although sign-magnitude is the most intuitive, it has limitations. For one, negative numbers in sign-magnitude cannot be directly used in calculations. For example, in sign-magnitude, calculating \\(1 + (-2)\\) results in \\(-3\\), which is incorrect.
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]To address this, computers introduced the one's complement. If we convert to one's complement and calculate \\(1 + (-2)\\), then convert the result back to sign-magnitude, we get the correct result of \\(-1\\).
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(Sign-magnitude)} + 1000 \\; 0010 \\; \\text{(Sign-magnitude)} \\newline & = 0000 \\; 0001 \\; \\text{(One's complement)} + 1111 \\; 1101 \\; \\text{(One's complement)} \\newline & = 1111 \\; 1110 \\; \\text{(One's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & \\rightarrow -1 \\end{aligned} \\]Additionally, there are two representations of zero in sign-magnitude: \\(+0\\) and \\(-0\\). This means two different binary encodings for zero, which could lead to ambiguity. For example, in conditional checks, not differentiating between positive and negative zero might result in incorrect outcomes. Addressing this ambiguity would require additional checks, potentially reducing computational efficiency.
\\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]Like sign-magnitude, one's complement also suffers from the positive and negative zero ambiguity. Therefore, computers further introduced the two's complement. Let's observe the conversion process for negative zero in sign-magnitude, one's complement, and two's complement:
\\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(Sign-magnitude)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(One's complement)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(Two's complement)} \\newline \\end{aligned} \\]Adding \\(1\\) to the one's complement of negative zero produces a carry, but with byte
length being only 8 bits, the carried-over \\(1\\) to the 9th bit is discarded. Therefore, the two's complement of negative zero is \\(0000 \\; 0000\\), the same as positive zero, thus resolving the ambiguity.
One last puzzle is the \\([-128, 127]\\) range for byte
, with an additional negative number, \\(-128\\). We observe that for the interval \\([-127, +127]\\), all integers have corresponding sign-magnitude, one's complement, and two's complement, allowing for mutual conversion between them.
However, the two's complement \\(1000 \\; 0000\\) is an exception without a corresponding sign-magnitude. According to the conversion method, its sign-magnitude would be \\(0000 \\; 0000\\), indicating zero. This presents a contradiction because its two's complement should represent itself. Computers designate this special two's complement \\(1000 \\; 0000\\) as representing \\(-128\\). In fact, the calculation of \\((-1) + (-127)\\) in two's complement results in \\(-128\\).
\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(Sign-magnitude)} + 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & = 1000 \\; 0000 \\; \\text{(One's complement)} + 1111 \\; 1110 \\; \\text{(One's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Two's complement)} + 1111 \\; 1111 \\; \\text{(Two's complement)} \\newline & = 1000 \\; 0000 \\; \\text{(Two's complement)} \\newline & \\rightarrow -128 \\end{aligned} \\]As you might have noticed, all these calculations are additions, hinting at an important fact: computers' internal hardware circuits are primarily designed around addition operations. This is because addition is simpler to implement in hardware compared to other operations like multiplication, division, and subtraction, allowing for easier parallelization and faster computation.
It's important to note that this doesn't mean computers can only perform addition. By combining addition with basic logical operations, computers can execute a variety of other mathematical operations. For example, the subtraction \\(a - b\\) can be translated into \\(a + (-b)\\); multiplication and division can be translated into multiple additions or subtractions.
We can now summarize the reason for using two's complement in computers: with two's complement representation, computers can use the same circuits and operations to handle both positive and negative number addition, eliminating the need for special hardware circuits for subtraction and avoiding the ambiguity of positive and negative zero. This greatly simplifies hardware design and enhances computational efficiency.
The design of two's complement is quite ingenious, and due to space constraints, we'll stop here. Interested readers are encouraged to explore further.
"},{"location":"chapter_data_structure/number_encoding/#332-floating-point-number-encoding","title":"3.3.2 \u00a0 Floating-point number encoding","text":"You might have noticed something intriguing: despite having the same length of 4 bytes, why does a float
have a much larger range of values compared to an int
? This seems counterintuitive, as one would expect the range to shrink for float
since it needs to represent fractions.
In fact, this is due to the different representation method used by floating-point numbers (float
). Let's consider a 32-bit binary number as:
According to the IEEE 754 standard, a 32-bit float
consists of the following three parts:
The value of a binary float
number is calculated as:
Converted to a decimal formula, this becomes:
\\[ \\text{val} = (-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} - 127} \\times (1 + \\mathrm{N}) \\]The range of each component is:
\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} \\times 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]Figure 3-5 \u00a0 Example calculation of a float in IEEE 754 standard
Observing the diagram, given an example data \\(\\mathrm{S} = 0\\), \\(\\mathrm{E} = 124\\), \\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\), we have:
\\[ \\text{val} = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]Now we can answer the initial question: The representation of float
includes an exponent bit, leading to a much larger range than int
. Based on the above calculation, the maximum positive number representable by float
is approximately \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\), and the minimum negative number is obtained by switching the sign bit.
However, the trade-off for float
's expanded range is a sacrifice in precision. The integer type int
uses all 32 bits to represent the number, with values evenly distributed; but due to the exponent bit, the larger the value of a float
, the greater the difference between adjacent numbers.
As shown in the Table 3-2 , exponent bits \\(E = 0\\) and \\(E = 255\\) have special meanings, used to represent zero, infinity, \\(\\mathrm{NaN}\\), etc.
Table 3-2 \u00a0 Meaning of exponent bits
Exponent Bit E Fraction Bit \\(\\mathrm{N} = 0\\) Fraction Bit \\(\\mathrm{N} \\ne 0\\) Calculation Formula \\(0\\) \\(\\pm 0\\) Subnormal Numbers \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) Normal Numbers Normal Numbers \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)It's worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is \\(2^{-126}\\), and the smallest positive subnormal number is \\(2^{-126} \\times 2^{-23}\\).
Double-precision double
also uses a similar representation method to float
, which is not elaborated here for brevity.
byte
, short
, int
, long
), floating-point numbers (float
, double
), characters (char
), and booleans (boolean
). Their range depends on the size of the space occupied and the representation method.Q: Why does a hash table contain both linear and non-linear data structures?
The underlying structure of a hash table is an array. To resolve hash collisions, we may use \"chaining\": each bucket in the array points to a linked list, which, when exceeding a certain threshold, might be transformed into a tree (usually a red-black tree). From a storage perspective, the foundation of a hash table is an array, where each bucket slot might contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).
Q: Is the length of the char
type 1 byte?
The length of the char
type is determined by the encoding method used by the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to save Unicode code points), so the length of the char type is 2 bytes.
Q: Is there ambiguity in calling data structures based on arrays \"static data structures\"? Because operations like push and pop on stacks are \"dynamic\".
While stacks indeed allow for dynamic data operations, the data structure itself remains \"static\" (with unchangeable length). Even though data structures based on arrays can dynamically add or remove elements, their capacity is fixed. If the data volume exceeds the pre-allocated size, a new, larger array needs to be created, and the contents of the old array copied into it.
Q: When building stacks (queues) without specifying their size, why are they considered \"static data structures\"?
In high-level programming languages, we don't need to manually specify the initial capacity of stacks (queues); this task is automatically handled internally by the class. For example, the initial capacity of Java's ArrayList is usually 10. Furthermore, the expansion operation is also implemented automatically. See the subsequent \"List\" chapter for details.
"},{"location":"chapter_graph/","title":"Chapter 9. \u00a0 Graph","text":"Abstract
In the journey of life, we are like individual nodes, connected by countless invisible edges.
Every encountering and parting leaves a unique mark on this vast network graph.
"},{"location":"chapter_graph/#chapter-contents","title":"Chapter Contents","text":"A \"graph\" is a type of nonlinear data structure, consisting of \"vertices\" and \"edges\". A graph \\(G\\) can be abstractly represented as a collection of a set of vertices \\(V\\) and a set of edges \\(E\\). The following example shows a graph containing 5 vertices and 7 edges.
\\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown below, compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom.
Figure 9-1 \u00a0 Relationship between linked lists, trees, and graphs
"},{"location":"chapter_graph/graph/#911-common-types-of-graphs","title":"9.1.1 \u00a0 Common types of graphs","text":"Based on whether edges have direction, graphs can be divided into \"undirected graphs\" and \"directed graphs\", as shown below.
Figure 9-2 \u00a0 Directed and undirected graphs
Based on whether all vertices are connected, graphs can be divided into \"connected graphs\" and \"disconnected graphs\", as shown below.
Figure 9-3 \u00a0 Connected and disconnected graphs
We can also add a \"weight\" variable to edges, resulting in \"weighted graphs\" as shown below. For example, in mobile games like \"Honor of Kings\", the system calculates the \"closeness\" between players based on shared gaming time, and this closeness network can be represented with a weighted graph.
Figure 9-4 \u00a0 Weighted and unweighted graphs
Graph data structures include the following commonly used terms.
Common representations of graphs include \"adjacency matrices\" and \"adjacency lists\". The following examples use undirected graphs.
"},{"location":"chapter_graph/graph/#1-adjacency-matrix","title":"1. \u00a0 Adjacency matrix","text":"Let the number of vertices in the graph be \\(n\\), the \"adjacency matrix\" uses an \\(n \\times n\\) matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with \\(1\\) or \\(0\\) indicating whether there is an edge between two vertices.
As shown below, let the adjacency matrix be \\(M\\), and the list of vertices be \\(V\\), then the matrix element \\(M[i, j] = 1\\) indicates there is an edge between vertex \\(V[i]\\) and vertex \\(V[j]\\), conversely \\(M[i, j] = 0\\) indicates there is no edge between the two vertices.
Figure 9-5 \u00a0 Representation of a graph with an adjacency matrix
Adjacency matrices have the following characteristics.
When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, thus operations of addition, deletion, lookup, and modification are very efficient, all with a time complexity of \\(O(1)\\). However, the space complexity of the matrix is \\(O(n^2)\\), which consumes more memory.
"},{"location":"chapter_graph/graph/#2-adjacency-list","title":"2. \u00a0 Adjacency list","text":"The \"adjacency list\" uses \\(n\\) linked lists to represent the graph, with each linked list node representing a vertex. The \\(i\\)-th linked list corresponds to vertex \\(i\\) and contains all adjacent vertices (vertices connected to that vertex). The Figure 9-6 shows an example of a graph stored using an adjacency list.
Figure 9-6 \u00a0 Representation of a graph with an adjacency list
The adjacency list only stores actual edges, and the total number of edges is often much less than \\(n^2\\), making it more space-efficient. However, finding edges in the adjacency list requires traversing the linked list, so its time efficiency is not as good as that of the adjacency matrix.
Observing the above figure, the structure of the adjacency list is very similar to the \"chaining\" in hash tables, hence we can use similar methods to optimize efficiency. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from \\(O(n)\\) to \\(O(\\log n)\\); the linked list can also be transformed into a hash table, thus reducing the time complexity to \\(O(1)\\).
"},{"location":"chapter_graph/graph/#913-common-applications-of-graphs","title":"9.1.3 \u00a0 Common applications of graphs","text":"As shown in the Table 9-1 , many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.
Table 9-1 \u00a0 Common graphs in real life
Vertices Edges Graph Computing Problem Social Networks Users Friendships Potential Friend Recommendations Subway Lines Stations Connectivity Between Stations Shortest Route Recommendations Solar System Celestial Bodies Gravitational Forces Between Celestial Bodies Planetary Orbit Calculations"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 Basic operations on graphs","text":"The basic operations on graphs can be divided into operations on \"edges\" and operations on \"vertices\". Under the two representation methods of \"adjacency matrix\" and \"adjacency list\", the implementation methods are different.
"},{"location":"chapter_graph/graph_operations/#921-implementation-based-on-adjacency-matrix","title":"9.2.1 \u00a0 Implementation based on adjacency matrix","text":"Given an undirected graph with \\(n\\) vertices, the various operations are implemented as shown in the Figure 9-7 .
vertices
of length \\(n\\), using \\(O(n)\\) time; initialize an \\(n \\times n\\) size adjacency matrix adjMat
, using \\(O(n^2)\\) time.Figure 9-7 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency matrix
Below is the implementation code for graphs represented using an adjacency matrix:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_adjacency_matrix.pyclass GraphAdjMat:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, vertices: list[int], edges: list[list[int]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.vertices: list[int] = []\n # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.adj_mat: list[list[int]] = []\n # \u6dfb\u52a0\u9876\u70b9\n for val in vertices:\n self.add_vertex(val)\n # \u6dfb\u52a0\u8fb9\n # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges:\n self.add_edge(e[0], e[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.vertices)\n\n def add_vertex(self, val: int):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n n = self.size()\n # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.append(val)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n new_row = [0] * n\n self.adj_mat.append(new_row)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in self.adj_mat:\n row.append(0)\n\n def remove_vertex(self, index: int):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if index >= self.size():\n raise IndexError()\n # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in self.adj_mat:\n row.pop(index)\n\n def add_edge(self, i: int, j: int):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1\n self.adj_mat[j][i] = 1\n\n def remove_edge(self, i: int, j: int):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n self.adj_mat[i][j] = 0\n self.adj_mat[j][i] = 0\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n print(\"\u90bb\u63a5\u77e9\u9635 =\")\n print_matrix(self.adj_mat)\n
graph_adjacency_matrix.cpp/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vector<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const vector<int> &edge : edges) {\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() const {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.push_back(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n adjMat.emplace_back(vector<int>(n, 0));\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (vector<int> &row : adjMat) {\n row.push_back(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.erase(vertices.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.erase(adjMat.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (vector<int> &row : adjMat) {\n row.erase(row.begin() + index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void print() {\n cout << \"\u9876\u70b9\u5217\u8868 = \";\n printVector(vertices);\n cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n printVectorMatrix(adjMat);\n }\n};\n
graph_adjacency_matrix.java/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = new ArrayList<>();\n this.adjMat = new ArrayList<>();\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (int[] e : edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<Integer> newRow = new ArrayList<>(n);\n for (int j = 0; j < n; j++) {\n newRow.add(0);\n }\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<Integer> row : adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(int index) {\n if (index >= size())\n throw new IndexOutOfBoundsException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<Integer> row : adjMat) {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat.get(i).set(j, 1);\n adjMat.get(j).set(i, 1);\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n adjMat.get(i).set(j, 0);\n adjMat.get(j).set(i, 0);\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void print() {\n System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n System.out.println(vertices);\n System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.cs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n foreach (int val in vertices) {\n AddVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n foreach (int[] e in edges) {\n AddEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return vertices.Count;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(int val) {\n int n = Size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.Add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = new(n);\n for (int j = 0; j < n; j++) {\n newRow.Add(0);\n }\n adjMat.Add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n foreach (List<int> row in adjMat) {\n row.Add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(int index) {\n if (index >= Size())\n throw new IndexOutOfRangeException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n foreach (List<int> row in adjMat) {\n row.RemoveAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void AddEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void RemoveEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void Print() {\n Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n PrintUtil.PrintList(vertices);\n Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.PrintMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.go/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vertices []int\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n // \u6dfb\u52a0\u9876\u70b9\n n := len(vertices)\n adjMat := make([][]int, n)\n for i := range adjMat {\n adjMat[i] = make([]int, n)\n }\n // \u521d\u59cb\u5316\u56fe\n g := &graphAdjMat{\n vertices: vertices,\n adjMat: adjMat,\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for i := range edges {\n g.addEdge(edges[i][0], edges[i][1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n n := g.size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n g.vertices = append(g.vertices, val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n newRow := make([]int, n)\n g.adjMat = append(g.adjMat, newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i], 0)\n }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n if index >= g.size() {\n return\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n g.adjMat[i][j] = 1\n g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n g.adjMat[i][j] = 0\n g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n for i := range g.adjMat {\n fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n }\n}\n
graph_adjacency_matrix.swift/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(vertices: [Int], edges: [[Int]]) {\n self.vertices = []\n adjMat = []\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n addVertex(val: val)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges {\n addEdge(i: e[0], j: e[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n func size() -> Int {\n vertices.count\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n func addVertex(val: Int) {\n let n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.append(val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n let newRow = Array(repeating: 0, count: n)\n adjMat.append(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i in adjMat.indices {\n adjMat[i].append(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n func removeVertex(index: Int) {\n if index >= size() {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i in adjMat.indices {\n adjMat[i].remove(at: index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n func print() {\n Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n Swift.print(vertices)\n Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n PrintUtil.printMatrix(matrix: adjMat)\n }\n}\n
graph_adjacency_matrix.js/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices, edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val) {\n const n = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow = [];\n for (let j = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index) {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print() {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.ts/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices: number[], edges: number[][]) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val: number): void {\n const n: number = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow: number[] = [];\n for (let j: number = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index: number): void {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print(): void {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.dart/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (int val in vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (List<int> e in edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = List.filled(n, 0, growable: true);\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<int> row in adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw IndexError;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<int> row in adjMat) {\n row.removeAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void printAdjMat() {\n print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n print(\"\u90bb\u63a5\u77e9\u9635 = \");\n printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.rs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub vertices: Vec<i32>,\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n let mut graph = GraphAdjMat {\n vertices: vec![],\n adj_mat: vec![],\n };\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n graph.add_vertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for edge in edges {\n graph.add_edge(edge[0], edge[1])\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n pub fn size(&self) -> usize {\n self.vertices.len()\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, val: i32) {\n let n = self.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n self.adj_mat.push(vec![0; n]);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in &mut self.adj_mat {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n pub fn remove_vertex(&mut self, index: usize) {\n if index >= self.size() {\n panic!(\"index error\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in &mut self.adj_mat {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1;\n self.adj_mat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n pub fn remove_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n self.adj_mat[i][j] = 0;\n self.adj_mat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n pub fn print(&self) {\n println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n println!(\"[\");\n for row in &self.adj_mat {\n println!(\" {:?},\", row);\n }\n println!(\"]\")\n }\n}\n
graph_adjacency_matrix.c/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n int vertices[MAX_SIZE];\n int adjMat[MAX_SIZE][MAX_SIZE];\n int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n for (int j = 0; j < MAX_SIZE; j++) {\n graph->adjMat[i][j] = 0;\n }\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n if (graph->size == MAX_SIZE) {\n fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n return;\n }\n // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n int n = graph->size;\n graph->vertices[n] = val;\n for (int i = 0; i <= n; i++) {\n graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n }\n graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n if (index < 0 || index >= graph->size) {\n fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n return;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n for (int i = index; i < graph->size - 1; i++) {\n graph->vertices[i] = graph->vertices[i + 1];\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n for (int i = index; i < graph->size - 1; i++) {\n for (int j = 0; j < graph->size; j++) {\n graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n }\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (int i = 0; i < graph->size; i++) {\n for (int j = index; j < graph->size - 1; j++) {\n graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n }\n }\n graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 1;\n graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 0;\n graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n printf(\"\u9876\u70b9\u5217\u8868 = \");\n printArray(graph->vertices, graph->size);\n printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n for (int i = 0; i < graph->size; i++) {\n printArray(graph->adjMat[i], graph->size);\n }\n}\n
graph_adjacency_matrix.kt/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n val vertices: MutableList<Int> = ArrayList() // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n val adjMat: MutableList<MutableList<Int>> = ArrayList() // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n // \u6dfb\u52a0\u9876\u70b9\n for (vertex in vertices) {\n addVertex(vertex)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (edge in edges) {\n addEdge(edge[0], edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n fun size(): Int {\n return vertices.size\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n fun addVertex(value: Int) {\n val n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(value)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n val newRow: MutableList<Int> = mutableListOf()\n for (j in 0..<n) {\n newRow.add(0)\n }\n adjMat.add(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (row in adjMat) {\n row.add(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n fun removeVertex(index: Int) {\n if (index >= size()) throw IndexOutOfBoundsException()\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (row in adjMat) {\n row.removeAt(index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n fun addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n fun removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n fun print() {\n print(\"\u9876\u70b9\u5217\u8868 = \")\n println(vertices);\n println(\"\u90bb\u63a5\u77e9\u9635 =\");\n printMatrix(adjMat)\n }\n}\n
graph_adjacency_matrix.rb[class]{GraphAdjMat}-[func]{}\n
graph_adjacency_matrix.zig[class]{GraphAdjMat}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_graph/graph_operations/#922-implementation-based-on-adjacency-list","title":"9.2.2 \u00a0 Implementation based on adjacency list","text":"Given an undirected graph with a total of \\(n\\) vertices and \\(m\\) edges, the various operations can be implemented as shown in the Figure 9-8 .
Figure 9-8 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency list
Below is the adjacency list code implementation. Compared to the above diagram, the actual code has the following differences.
key
being the vertex instance, value
being the list (linked list) of adjacent vertices of that vertex.Additionally, we use the Vertex
class to represent vertices in the adjacency list. The reason for this is: if, like with the adjacency matrix, list indexes were used to distinguish different vertices, then suppose you want to delete the vertex at index \\(i\\), you would need to traverse the entire adjacency list and decrement all indexes greater than \\(i\\) by \\(1\\), which is very inefficient. However, if each vertex is a unique Vertex
instance, then deleting a vertex does not require any changes to other vertices.
class GraphAdjList:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, edges: list[list[Vertex]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n self.adj_list = dict[Vertex, list[Vertex]]()\n # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges:\n self.add_vertex(edge[0])\n self.add_vertex(edge[1])\n self.add_edge(edge[0], edge[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.adj_list)\n\n def add_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list[vet1].append(vet2)\n self.adj_list[vet2].append(vet1)\n\n def remove_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list[vet1].remove(vet2)\n self.adj_list[vet2].remove(vet1)\n\n def add_vertex(self, vet: Vertex):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n if vet in self.adj_list:\n return\n # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list[vet] = []\n\n def remove_vertex(self, vet: Vertex):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if vet not in self.adj_list:\n raise ValueError()\n # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.pop(vet)\n # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for vertex in self.adj_list:\n if vet in self.adj_list[vertex]:\n self.adj_list[vertex].remove(vet)\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n print(\"\u90bb\u63a5\u8868 =\")\n for vertex in self.adj_list:\n tmp = [v.val for v in self.adj_list[vertex]]\n print(f\"{vertex.val}: {tmp},\")\n
graph_adjacency_list.cpp/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n public:\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n void remove(vector<Vertex *> &vec, Vertex *vet) {\n for (int i = 0; i < vec.size(); i++) {\n if (vec[i] == vet) {\n vec.erase(vec.begin() + i);\n break;\n }\n }\n }\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(const vector<vector<Vertex *>> &edges) {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const vector<Vertex *> &edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].push_back(vet2);\n adjList[vet2].push_back(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5220\u9664\u8fb9 vet1 - vet2\n remove(adjList[vet1], vet2);\n remove(adjList[vet2], vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex *vet) {\n if (adjList.count(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = vector<Vertex *>();\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex *vet) {\n if (!adjList.count(vet))\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.erase(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (auto &adj : adjList) {\n remove(adj.second, vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void print() {\n cout << \"\u90bb\u63a5\u8868 =\" << endl;\n for (auto &adj : adjList) {\n const auto &key = adj.first;\n const auto &vec = adj.second;\n cout << key->val << \": \";\n printVector(vetsToVals(vec));\n }\n }\n};\n
graph_adjacency_list.java/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjList(Vertex[][] edges) {\n this.adjList = new HashMap<>();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (Vertex[] edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList.get(vet1).add(vet2);\n adjList.get(vet2).add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList.get(vet1).remove(vet2);\n adjList.get(vet2).remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(Vertex vet) {\n if (adjList.containsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.put(vet, new ArrayList<>());\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet))\n throw new IllegalArgumentException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (List<Vertex> list : adjList.values()) {\n list.remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void print() {\n System.out.println(\"\u90bb\u63a5\u8868 =\");\n for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n List<Integer> tmp = new ArrayList<>();\n for (Vertex vertex : pair.getValue())\n tmp.add(vertex.val);\n System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n }\n }\n}\n
graph_adjacency_list.cs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public Dictionary<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjList(Vertex[][] edges) {\n adjList = [];\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n foreach (Vertex[] edge in edges) {\n AddVertex(edge[0]);\n AddVertex(edge[1]);\n AddEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return adjList.Count;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void AddEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].Add(vet2);\n adjList[vet2].Add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void RemoveEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1].Remove(vet2);\n adjList[vet2].Remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(Vertex vet) {\n if (adjList.ContainsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.Add(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(Vertex vet) {\n if (!adjList.ContainsKey(vet))\n throw new InvalidOperationException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.Remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n foreach (List<Vertex> list in adjList.Values) {\n list.Remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void Print() {\n Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n List<int> tmp = [];\n foreach (Vertex vertex in pair.Value)\n tmp.Add(vertex.val);\n Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n }\n }\n}\n
graph_adjacency_list.go/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n g := &graphAdjList{\n adjList: make(map[Vertex][]Vertex),\n }\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for _, edge := range edges {\n g.addVertex(edge[0])\n g.addVertex(edge[1])\n g.addEdge(edge[0], edge[1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n g.adjList[vet1] = append(g.adjList[vet1], vet2)\n g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if ok {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if !ok {\n panic(\"error\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n delete(g.adjList, vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for v, list := range g.adjList {\n g.adjList[v] = DeleteSliceElms(list, vet)\n }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n var builder strings.Builder\n fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n for k, v := range g.adjList {\n builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n for _, vet := range v {\n builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n }\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
graph_adjacency_list.swift/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public private(set) var adjList: [Vertex: [Vertex]]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public init(edges: [[Vertex]]) {\n adjList = [:]\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n addVertex(vet: edge[0])\n addVertex(vet: edge[1])\n addEdge(vet1: edge[0], vet2: edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public func size() -> Int {\n adjList.count\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public func addEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.append(vet2)\n adjList[vet2]?.append(vet1)\n }\n\n /* \u5220\u9664\u8fb9 */\n public func removeEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.removeAll { $0 == vet2 }\n adjList[vet2]?.removeAll { $0 == vet1 }\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public func addVertex(vet: Vertex) {\n if adjList[vet] != nil {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = []\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public func removeVertex(vet: Vertex) {\n if adjList[vet] == nil {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.removeValue(forKey: vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for key in adjList.keys {\n adjList[key]?.removeAll { $0 == vet }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public func print() {\n Swift.print(\"\u90bb\u63a5\u8868 =\")\n for (vertex, list) in adjList {\n let list = list.map { $0.val }\n Swift.print(\"\\(vertex.val): \\(list),\")\n }\n }\n}\n
graph_adjacency_list.js/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet) {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet) {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print() {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.ts/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList: Map<Vertex, Vertex[]>;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges: Vertex[][]) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet: Vertex): void {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet: Vertex): void {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index: number = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print(): void {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList.entries()) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.dart/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList = {};\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(List<List<Vertex>> edges) {\n for (List<Vertex> edge in edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.length;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]!.add(vet2);\n adjList[vet2]!.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]!.remove(vet2);\n adjList[vet2]!.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex vet) {\n if (adjList.containsKey(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = [];\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet)) {\n throw ArgumentError;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n adjList.forEach((key, value) {\n value.remove(vet);\n });\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void printAdjList() {\n print(\"\u90bb\u63a5\u8868 =\");\n adjList.forEach((key, value) {\n List<int> tmp = [];\n for (Vertex vertex in value) {\n tmp.add(vertex.val);\n }\n print(\"${key.val}: $tmp,\");\n });\n }\n}\n
graph_adjacency_list.rs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n pub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\n\nimpl GraphAdjList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n let mut graph = GraphAdjList {\n adj_list: HashMap::new(),\n };\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n graph.add_vertex(edge[0]);\n graph.add_vertex(edge[1]);\n graph.add_edge(edge[0], edge[1]);\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n #[allow(unused)]\n pub fn size(&self) -> usize {\n self.adj_list.len()\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list.get_mut(&vet1).unwrap().push(vet2);\n self.adj_list.get_mut(&vet2).unwrap().push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n #[allow(unused)]\n pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list\n .get_mut(&vet1)\n .unwrap()\n .retain(|&vet| vet != vet2);\n self.adj_list\n .get_mut(&vet2)\n .unwrap()\n .retain(|&vet| vet != vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, vet: Vertex) {\n if self.adj_list.contains_key(&vet) {\n return;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list.insert(vet, vec![]);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n #[allow(unused)]\n pub fn remove_vertex(&mut self, vet: Vertex) {\n if !self.adj_list.contains_key(&vet) {\n panic!(\"value error\");\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.remove(&vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for list in self.adj_list.values_mut() {\n list.retain(|&v| v != vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n pub fn print(&self) {\n println!(\"\u90bb\u63a5\u8868 =\");\n for (vertex, list) in &self.adj_list {\n let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n println!(\"{}: {:?},\", vertex.val, list);\n }\n }\n}\n
graph_adjacency_list.c/* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n Vertex *vertex; // \u9876\u70b9\n struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n node->vertex = vet;\n // \u5934\u63d2\u6cd5\n node->next = head->next;\n head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *pre = head;\n AdjListNode *cur = head->next;\n // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n while (cur != NULL && cur->vertex != vet) {\n pre = cur;\n cur = cur->next;\n }\n if (cur == NULL)\n return;\n // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n pre->next = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n int size; // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n if (!graph) {\n return NULL;\n }\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n graph->heads[i] = NULL;\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n for (int i = 0; i < graph->size; i++) {\n AdjListNode *cur = graph->heads[i];\n while (cur != NULL) {\n AdjListNode *next = cur->next;\n if (cur != graph->heads[i]) {\n free(cur);\n }\n cur = next;\n }\n free(graph->heads[i]->vertex);\n free(graph->heads[i]);\n }\n free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL && head1 != head2);\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n addEdgeHelper(head1, vet2);\n addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL);\n // \u5220\u9664\u8fb9 vet1 - vet2\n removeEdgeHelper(head1, head2->vertex);\n removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n assert(graph != NULL && graph->size < MAX_SIZE);\n AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n head->vertex = vet;\n head->next = NULL;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n AdjListNode *node = findNode(graph, vet);\n assert(node != NULL);\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n AdjListNode *cur = node, *pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n free(pre);\n }\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (int i = 0; i < graph->size; i++) {\n cur = graph->heads[i];\n pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n if (cur && cur->vertex == vet) {\n pre->next = cur->next;\n free(cur);\n break;\n }\n }\n }\n // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n int i;\n for (i = 0; i < graph->size; i++) {\n if (graph->heads[i] == node)\n break;\n }\n for (int j = i; j < graph->size - 1; j++) {\n graph->heads[j] = graph->heads[j + 1];\n }\n graph->size--;\n free(vet);\n}\n
graph_adjacency_list.kt/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (edge in edges) {\n addVertex(edge[0]!!);\n addVertex(edge[1]!!);\n addEdge(edge[0]!!, edge[1]!!);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n fun size(): Int {\n return adjList.size\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n fun addEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.add(vet2)\n adjList[vet2]?.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n fun removeEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.remove(vet2);\n adjList[vet2]?.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n fun addVertex(vet: Vertex) {\n if (adjList.containsKey(vet))\n return\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = mutableListOf()\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n fun removeVertex(vet: Vertex) {\n if (!adjList.containsKey(vet))\n throw IllegalArgumentException()\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (list in adjList.values) {\n list.remove(vet)\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n fun print() {\n println(\"\u90bb\u63a5\u8868 =\")\n for (pair in adjList.entries) {\n val tmp = ArrayList<Int>()\n for (vertex in pair.value) {\n tmp.add(vertex.value)\n }\n println(\"${pair.key.value}: $tmp,\")\n }\n }\n}\n
graph_adjacency_list.rb[class]{GraphAdjList}-[func]{}\n
graph_adjacency_list.zig[class]{GraphAdjList}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_graph/graph_operations/#923-efficiency-comparison","title":"9.2.3 \u00a0 Efficiency comparison","text":"Assuming there are \\(n\\) vertices and \\(m\\) edges in the graph, the Table 9-2 compares the time efficiency and space efficiency of the adjacency matrix and adjacency list.
Table 9-2 \u00a0 Comparison of adjacency matrix and adjacency list
Adjacency matrix Adjacency list (Linked list) Adjacency list (Hash table) Determine adjacency \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) Add an edge \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Remove an edge \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) Add a vertex \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) Remove a vertex \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) Memory space usage \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)Observing the Table 9-2 , it seems that the adjacency list (hash table) has the best time efficiency and space efficiency. However, in practice, operating on edges in the adjacency matrix is more efficient, requiring only a single array access or assignment operation. Overall, the adjacency matrix exemplifies the principle of \"space for time\", while the adjacency list exemplifies \"time for space\".
"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 Graph traversal","text":"Trees represent a \"one-to-many\" relationship, while graphs have a higher degree of freedom and can represent any \"many-to-many\" relationship. Therefore, we can consider trees as a special case of graphs. Clearly, tree traversal operations are also a special case of graph traversal operations.
Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal can be divided into two types: \"Breadth-First Search (BFS)\" and \"Depth-First Search (DFS)\".
"},{"location":"chapter_graph/graph_traversal/#931-breadth-first-search","title":"9.3.1 \u00a0 Breadth-first search","text":"Breadth-first search is a near-to-far traversal method, starting from a certain node, always prioritizing the visit to the nearest vertices and expanding outwards layer by layer. As shown in the Figure 9-9 , starting from the top left vertex, first traverse all adjacent vertices of that vertex, then traverse all adjacent vertices of the next vertex, and so on, until all vertices have been visited.
Figure 9-9 \u00a0 Breadth-first traversal of a graph
"},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation","title":"1. \u00a0 Algorithm implementation","text":"BFS is usually implemented with the help of a queue, as shown in the code below. The queue has a \"first in, first out\" property, which aligns with the BFS idea of traversing \"from near to far\".
startVet
to the queue and start the loop.2.
until all vertices have been visited.To prevent revisiting vertices, we use a hash table visited
to record which nodes have been visited.
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]([start_vet])\n # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n que = deque[Vertex]([start_vet])\n # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while len(que) > 0:\n vet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adj_vet in graph.adj_list[vet]:\n if adj_vet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n
graph_bfs.cpp/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited = {startVet};\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n queue<Vertex *> que;\n que.push(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.empty()) {\n Vertex *vet = que.front();\n que.pop(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (auto adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.java/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new LinkedList<>();\n que.offer(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.cs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [startVet];\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new();\n que.Enqueue(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.Count > 0) {\n Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.Enqueue(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.Add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.go/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n visited[startVet] = struct{}{}\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n queue := make([]Vertex, 0)\n queue = append(queue, startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n for len(queue) > 0 {\n // \u961f\u9996\u9876\u70b9\u51fa\u961f\n vet := queue[0]\n queue = queue[1:]\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res = append(res, vet)\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n if !isExist {\n queue = append(queue, adjVet)\n visited[adjVet] = struct{}{}\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.swift/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = [startVet]\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n var que: [Vertex] = [startVet]\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.isEmpty {\n let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.js/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.ts/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.dart/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = Queue();\n que.add(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.isNotEmpty) {\n Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.rs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n visited.insert(start_vet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n let mut que = VecDeque::new();\n que.push_back(start_vet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.is_empty() {\n let vet = que.pop_front().unwrap(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n res\n}\n
graph_bfs.c/* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n Vertex *vertices[MAX_SIZE];\n int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n Queue *q = (Queue *)malloc(sizeof(Queue));\n q->front = q->rear = q->size = 0;\n return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n q->vertices[q->rear] = vet;\n q->rear = (q->rear + 1) % MAX_SIZE;\n q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n Vertex *vet = q->vertices[q->front];\n q->front = (q->front + 1) % MAX_SIZE;\n q->size--;\n return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (visited[i] == vet)\n return 1;\n }\n return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue *queue = newQueue();\n enqueue(queue, startVet);\n visited[(*visitedSize)++] = startVet;\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!isEmpty(queue)) {\n Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res[(*resSize)++] = vet; // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(visited, *visitedSize, node->vertex)) {\n enqueue(queue, node->vertex); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n node = node->next;\n }\n }\n // \u91ca\u653e\u5185\u5b58\n free(queue);\n}\n
graph_bfs.kt/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n val res: MutableList<Vertex> = ArrayList()\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n val visited: MutableSet<Vertex> = HashSet()\n visited.add(startVet)\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n val que: Queue<Vertex> = LinkedList()\n que.offer(startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n val vet = que.poll() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet)) continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n\n que.offer(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.rb[class]{}-[func]{graph_bfs}\n
graph_bfs.zig[class]{}-[func]{graphBFS}\n
Code Visualization Full Screen >
The code is relatively abstract, it is suggested to compare with the following figure to deepen the understanding.
<1><2><3><4><5><6><7><8><9><10><11>Figure 9-10 \u00a0 Steps of breadth-first search of a graph
Is the sequence of breadth-first traversal unique?
Not unique. Breadth-first traversal only requires traversing in a \"from near to far\" order, and the traversal order of multiple vertices at the same distance can be arbitrarily shuffled. For example, in the above figure, the visitation order of vertices \\(1\\) and \\(3\\) can be switched, as can the order of vertices \\(2\\), \\(4\\), and \\(6\\).
"},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity analysis","text":"Time complexity: All vertices will be enqueued and dequeued once, using \\(O(|V|)\\) time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.
Space complexity: The maximum number of vertices in list res
, hash table visited
, and queue que
is \\(|V|\\), using \\(O(|V|)\\) space.
Depth-first search is a traversal method that prioritizes going as far as possible and then backtracks when no further paths are available. As shown in the Figure 9-11 , starting from the top left vertex, visit some adjacent vertex of the current vertex until no further path is available, then return and continue until all vertices are traversed.
Figure 9-11 \u00a0 Depth-first traversal of a graph
"},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation_1","title":"1. \u00a0 Algorithm implementation","text":"This \"go as far as possible and then return\" algorithm paradigm is usually implemented based on recursion. Similar to breadth-first search, in depth-first search, we also need the help of a hash table visited
to record the visited vertices to avoid revisiting.
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570\"\"\"\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adj_list[vet]:\n if adjVet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]()\n dfs(graph, visited, res, start_vet)\n return res\n
graph_dfs.cpp/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex *adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited;\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.java/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.cs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9 \n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n DFS(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [];\n DFS(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.go/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n *res = append(*res, vet)\n visited[vet] = struct{}{}\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n if !isExist {\n dfs(g, visited, res, adjVet)\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n dfs(g, visited, &res, startVet)\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_dfs.swift/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = []\n dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n return res\n}\n
graph_dfs.js/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.ts/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n graph: GraphAdjList,\n visited: Set<Vertex>,\n res: Vertex[],\n vet: Vertex\n): void {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.dart/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n GraphAdjList graph,\n Set<Vertex> visited,\n List<Vertex> res,\n Vertex vet,\n) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.rs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adj_vet);\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n dfs(&graph, &mut visited, &mut res, start_vet);\n\n res\n}\n
graph_dfs.c/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (res[i] == vet) {\n return 1;\n }\n }\n return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res[(*resSize)++] = vet;\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(res, *resSize, node->vertex)) {\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, res, resSize, node->vertex);\n }\n node = node->next;\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n dfs(graph, res, resSize, startVet);\n}\n
graph_dfs.kt/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfun dfs(\n graph: GraphAdjList,\n visited: MutableSet<Vertex?>,\n res: MutableList<Vertex?>,\n vet: Vertex?\n) {\n res.add(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet)) continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphDFS(\n graph: GraphAdjList,\n startVet: Vertex?\n): List<Vertex?> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n val res: MutableList<Vertex?> = ArrayList()\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n val visited: MutableSet<Vertex?> = HashSet()\n dfs(graph, visited, res, startVet)\n return res\n}\n
graph_dfs.rb[class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n
graph_dfs.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
Code Visualization Full Screen >
The algorithm process of depth-first search is shown in the following figure.
To deepen the understanding, it is suggested to combine the following figure with the code to simulate (or draw) the entire DFS process in your mind, including when each recursive method is initiated and when it returns.
<1><2><3><4><5><6><7><8><9><10><11>Figure 9-12 \u00a0 Steps of depth-first search of a graph
Is the sequence of depth-first traversal unique?
Similar to breadth-first traversal, the order of the depth-first traversal sequence is also not unique. Given a certain vertex, exploring in any direction first is possible, that is, the order of adjacent vertices can be arbitrarily shuffled, all being part of depth-first traversal.
Taking tree traversal as an example, \"root \\(\\rightarrow\\) left \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) right \\(\\rightarrow\\) root\" correspond to preorder, inorder, and postorder traversals, respectively. They showcase three types of traversal priorities, yet all three are considered depth-first traversal.
"},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity analysis","text":"Time complexity: All vertices will be visited once, using \\(O(|V|)\\) time; all edges will be visited twice, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.
Space complexity: The maximum number of vertices in list res
, hash table visited
is \\(|V|\\), and the maximum recursion depth is \\(|V|\\), therefore using \\(O(|V|)\\) space.
Q: Is a path defined as a sequence of vertices or a sequence of edges?
Definitions vary between different language versions on Wikipedia: the English version defines a path as \"a sequence of edges,\" while the Chinese version defines it as \"a sequence of vertices.\" Here is the original text from the English version: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.
In this document, a path is considered a sequence of edges, rather than a sequence of vertices. This is because there might be multiple edges connecting two vertices, in which case each edge corresponds to a path.
Q: In a disconnected graph, are there points that cannot be traversed to?
In a disconnected graph, starting from a certain vertex, there is at least one vertex that cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.
Q: In an adjacency list, does the order of \"all vertices connected to that vertex\" matter?
It can be in any order. However, in practical applications, it might be necessary to sort according to certain rules, such as the order in which vertices are added, or the order of vertex values, etc., to facilitate the quick search for vertices with certain extremal values.
"},{"location":"chapter_hashing/","title":"Chapter 6. \u00a0 Hash table","text":"Abstract
In the world of computing, a hash table is akin to an intelligent librarian.
It understands how to compute index numbers, enabling swift retrieval of the desired book.
"},{"location":"chapter_hashing/#chapter-contents","title":"Chapter Contents","text":"The previous two sections introduced the working principle of hash tables and the methods to handle hash collisions. However, both open addressing and chaining can only ensure that the hash table functions normally when collisions occur, but cannot reduce the frequency of hash collisions.
If hash collisions occur too frequently, the performance of the hash table will deteriorate drastically. As shown in the Figure 6-8 , for a chaining hash table, in the ideal case, the key-value pairs are evenly distributed across the buckets, achieving optimal query efficiency; in the worst case, all key-value pairs are stored in the same bucket, degrading the time complexity to \\(O(n)\\).
Figure 6-8 \u00a0 Ideal and worst cases of hash collisions
The distribution of key-value pairs is determined by the hash function. Recalling the steps of calculating a hash function, first compute the hash value, then modulo it by the array length:
index = hash(key) % capacity\n
Observing the above formula, when the hash table capacity capacity
is fixed, the hash algorithm hash()
determines the output value, thereby determining the distribution of key-value pairs in the hash table.
This means that, to reduce the probability of hash collisions, we should focus on the design of the hash algorithm hash()
.
To achieve a \"fast and stable\" hash table data structure, hash algorithms should have the following characteristics:
In fact, hash algorithms are not only used to implement hash tables but are also widely applied in other fields.
For cryptographic applications, to prevent reverse engineering such as deducing the original password from the hash value, hash algorithms need higher-level security features.
Note that \"Uniform Distribution\" and \"Collision Resistance\" are two separate concepts. Satisfying uniform distribution does not necessarily mean collision resistance. For example, under random input key
, the hash function key % 100
can produce a uniformly distributed output. However, this hash algorithm is too simple, and all key
with the same last two digits will have the same output, making it easy to deduce a usable key
from the hash value, thereby cracking the password.
The design of hash algorithms is a complex issue that requires consideration of many factors. However, for some less demanding scenarios, we can also design some simple hash algorithms.
def add_hash(key: str) -> int:\n \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash += ord(c)\n return hash % modulus\n\ndef mul_hash(key: str) -> int:\n \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = 31 * hash + ord(c)\n return hash % modulus\n\ndef xor_hash(key: str) -> int:\n \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash ^= ord(c)\n return hash % modulus\n\ndef rot_hash(key: str) -> int:\n \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n return hash % modulus\n
simple_hash.cpp/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (31 * hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash ^= (int)c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.java/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (31 * hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash ^= (int) c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n }\n return (int) hash;\n}\n
simple_hash.cs/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (31 * hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash ^= c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.go/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (31*hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n hash := 0\n modulus := 1000000007\n for _, b := range []byte(key) {\n fmt.Println(int(b))\n hash ^= int(b)\n hash = (31*hash + int(b)) % modulus\n }\n return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n }\n return int(hash)\n}\n
simple_hash.swift/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (31 * hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash ^= Int(scalar.value)\n }\n }\n return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n
simple_hash.js/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.ts/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.dart/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash ^= key.codeUnitAt(i);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.rs/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (31 * hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash ^= c as i64;\n }\n\n (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n }\n\n hash as i32\n}\n
simple_hash.c/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n\n for (int i = 0; i < strlen(key); i++) {\n hash ^= (unsigned char)key[i];\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n }\n\n return (int)hash;\n}\n
simple_hash.kt/* \u52a0\u6cd5\u54c8\u5e0c */\nfun addHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = (hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfun mulHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = (31 * hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfun xorHash(key: String): Int {\n var hash = 0\n for (c in key.toCharArray()) {\n hash = hash xor c.code\n }\n return hash and MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfun rotHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n }\n return hash.toInt()\n}\n
simple_hash.rb[class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n
simple_hash.zig[class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
Code Visualization Full Screen >
It is observed that the last step of each hash algorithm is to take the modulus of the large prime number \\(1000000007\\) to ensure that the hash value is within an appropriate range. It is worth pondering why emphasis is placed on modulo a prime number, or what are the disadvantages of modulo a composite number? This is an interesting question.
To conclude: Using a large prime number as the modulus can maximize the uniform distribution of hash values. Since a prime number does not share common factors with other numbers, it can reduce the periodic patterns caused by the modulo operation, thus avoiding hash collisions.
For example, suppose we choose the composite number \\(9\\) as the modulus, which can be divided by \\(3\\), then all key
divisible by \\(3\\) will be mapped to hash values \\(0\\), \\(3\\), \\(6\\).
If the input key
happens to have this kind of arithmetic sequence distribution, then the hash values will cluster, thereby exacerbating hash collisions. Now, suppose we replace modulus
with the prime number \\(13\\), since there are no common factors between key
and modulus
, the uniformity of the output hash values will be significantly improved.
It is worth noting that if the key
is guaranteed to be randomly and uniformly distributed, then choosing a prime number or a composite number as the modulus can both produce uniformly distributed hash values. However, when the distribution of key
has some periodicity, modulo a composite number is more likely to result in clustering.
In summary, we usually choose a prime number as the modulus, and this prime number should be large enough to eliminate periodic patterns as much as possible, enhancing the robustness of the hash algorithm.
"},{"location":"chapter_hashing/hash_algorithm/#633-common-hash-algorithms","title":"6.3.3 \u00a0 Common hash algorithms","text":"It is not hard to see that the simple hash algorithms mentioned above are quite \"fragile\" and far from reaching the design goals of hash algorithms. For example, since addition and XOR obey the commutative law, additive hash and XOR hash cannot distinguish strings with the same content but in different order, which may exacerbate hash collisions and cause security issues.
In practice, we usually use some standard hash algorithms, such as MD5, SHA-1, SHA-2, and SHA-3. They can map input data of any length to a fixed-length hash value.
Over the past century, hash algorithms have been in a continuous process of upgrading and optimization. Some researchers strive to improve the performance of hash algorithms, while others, including hackers, are dedicated to finding security issues in hash algorithms. The Table 6-2 shows hash algorithms commonly used in practical applications.
Table 6-2 \u00a0 Common hash algorithms
MD5 SHA-1 SHA-2 SHA-3 Release Year 1992 1995 2002 2008 Output Length 128 bit 160 bit 256/512 bit 224/256/384/512 bit Hash Collisions Frequent Frequent Rare Rare Security Level Low, has been successfully attacked Low, has been successfully attacked High High Applications Abandoned, still used for data integrity checks Abandoned Cryptocurrency transaction verification, digital signatures, etc. Can be used to replace SHA-2"},{"location":"chapter_hashing/hash_algorithm/#hash-values-in-data-structures","title":"Hash values in data structures","text":"We know that the keys in a hash table can be of various data types such as integers, decimals, or strings. Programming languages usually provide built-in hash algorithms for these data types to calculate the bucket indices in the hash table. Taking Python as an example, we can use the hash()
function to compute the hash values for various data types.
Tip
Be aware that the definition and methods of the built-in hash value calculation functions in different programming languages vary.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig built_in_hash.pynum = 3\nhash_num = hash(num)\n# Hash value of integer 3 is 3\n\nbol = True\nhash_bol = hash(bol)\n# Hash value of boolean True is 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# Hash value of decimal 3.14159 is 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# Hash value of string \"Hello \u7b97\u6cd5\" is 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# Hash value of ListNode object at 0x1058fd810 is 274267521\n
built_in_hash.cppint num = 3;\nsize_t hashNum = hash<int>()(num);\n// Hash value of integer 3 is 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// Hash value of boolean 1 is 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// Hash value of decimal 3.14159 is 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// Hash value of string \"Hello \u7b97\u6cd5\" is 15466937326284535026\n\n// In C++, built-in std::hash() only provides hash values for basic data types\n// Hash values for arrays and objects need to be implemented separately\n
built_in_hash.javaint num = 3;\nint hashNum = Integer.hashCode(num);\n// Hash value of integer 3 is 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// Hash value of decimal 3.14159 is -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// Hash value of array [12836, \u5c0f\u54c8] is 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// Hash value of ListNode object utils.ListNode@7dc5e7b4 is 2110121908\n
built_in_hash.csint num = 3;\nint hashNum = num.GetHashCode();\n// Hash value of integer 3 is 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// Hash value of boolean true is 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// Hash value of decimal 3.14159 is -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// Hash value of array [12836, \u5c0f\u54c8] is 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// Hash value of ListNode object 0 is 39053774;\n
built_in_hash.go// Go does not provide built-in hash code functions\n
built_in_hash.swiftlet num = 3\nlet hashNum = num.hashValue\n// Hash value of integer 3 is 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// Hash value of boolean true is -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// Hash value of decimal 3.14159 is -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// Hash value of string \"Hello \u7b97\u6cd5\" is -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// Hash value of array [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] is -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// Hash value of ListNode object utils.ListNode is -2434780518035996159\n
built_in_hash.js// JavaScript does not provide built-in hash code functions\n
built_in_hash.ts// TypeScript does not provide built-in hash code functions\n
built_in_hash.dartint num = 3;\nint hashNum = num.hashCode;\n// Hash value of integer 3 is 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// Hash value of decimal 3.14159 is 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// Hash value of string \"Hello \u7b97\u6cd5\" is 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// Hash value of array [12836, \u5c0f\u54c8] is 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// Hash value of ListNode object Instance of 'ListNode' is 1033450432\n
built_in_hash.rsuse std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// Hash value of integer 3 is 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// Hash value of boolean true is 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// Hash value of decimal 3.14159 is 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// Hash value of string \"Hello \u7b97\u6cd5\" is 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// Hash value of tuple (12836, \"\u5c0f\u54c8\") is 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// Hash value of ListNode object RefCell { value: ListNode { val: 42, next: None } } is 15387811073369036852\n
built_in_hash.c// C does not provide built-in hash code functions\n
built_in_hash.kt\n
built_in_hash.zig\n
Code Visualization Full Screen >
In many programming languages, only immutable objects can serve as the key
in a hash table. If we use a list (dynamic array) as a key
, when the contents of the list change, its hash value also changes, and we would no longer be able to find the original value
in the hash table.
Although the member variables of a custom object (such as a linked list node) are mutable, it is hashable. This is because the hash value of an object is usually generated based on its memory address, and even if the contents of the object change, the memory address remains the same, so the hash value remains unchanged.
You might have noticed that the hash values output in different consoles are different. This is because the Python interpreter adds a random salt to the string hash function each time it starts up. This approach effectively prevents HashDoS attacks and enhances the security of the hash algorithm.
"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 Hash collision","text":"As mentioned in the previous section, usually the input space of a hash function is much larger than its output space, making hash collisions theoretically inevitable. For example, if the input space consists of all integers and the output space is the size of the array capacity, multiple integers will inevitably map to the same bucket index.
Hash collisions can lead to incorrect query results, severely affecting the usability of hash tables. To solve this problem, we expand the hash table whenever a hash collision occurs, until the collision is resolved. This method is simple and effective but inefficient due to the extensive data transfer and hash value computation involved in resizing the hash table. To improve efficiency, we can adopt the following strategies:
There are mainly two methods for improving the structure of hash tables: \"Separate Chaining\" and \"Open Addressing\".
"},{"location":"chapter_hashing/hash_collision/#621-separate-chaining","title":"6.2.1 \u00a0 Separate chaining","text":"In the original hash table, each bucket can store only one key-value pair. \"Separate chaining\" transforms individual elements into a linked list, with key-value pairs as list nodes, storing all colliding key-value pairs in the same list. The Figure 6-5 shows an example of a hash table with separate chaining.
Figure 6-5 \u00a0 Separate chaining hash table
The operations of a hash table implemented with separate chaining have changed as follows:
key
, pass through the hash function to obtain the bucket index, access the head node of the list, then traverse the list and compare key
to find the target key-value pair.Separate chaining has the following limitations:
The code below provides a simple implementation of a separate chaining hash table, with two things to note:
class HashMapChaining:\n \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets = [[] for _ in range(self.capacity)] # \u6876\u6570\u7ec4\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def get(self, key: int) -> str | None:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket:\n if pair.key == key:\n return pair.val\n # \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket:\n if pair.key == key:\n pair.val = val\n return\n # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n pair = Pair(key, val)\n bucket.append(pair)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for pair in bucket:\n if pair.key == key:\n bucket.remove(pair)\n self.size -= 1\n break\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [[] for _ in range(self.capacity)]\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets:\n for pair in bucket:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for bucket in self.buckets:\n res = []\n for pair in bucket:\n res.append(str(pair.key) + \" -> \" + pair.val)\n print(res)\n
hash_map_chaining.cpp/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n buckets.resize(capacity);\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapChaining() {\n for (auto &bucket : buckets) {\n for (Pair *pair : bucket) {\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / (double)capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n return pair->val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n pair->val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].push_back(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n auto &bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (int i = 0; i < bucket.size(); i++) {\n if (bucket[i]->key == key) {\n Pair *tmp = bucket[i];\n bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n delete tmp; // \u91ca\u653e\u5185\u5b58\n size--;\n return;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<vector<Pair *>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets.clear();\n buckets.resize(capacity);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (auto &bucket : bucketsTmp) {\n for (Pair *pair : bucket) {\n put(pair->key, pair->val);\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (auto &bucket : buckets) {\n cout << \"[\";\n for (Pair *pair : bucket) {\n cout << pair->key << \" -> \" << pair->val << \", \";\n }\n cout << \"]\\n\";\n }\n }\n};\n
hash_map_chaining.java/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair : bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair : bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = new Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair : bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket : bucketsTmp) {\n for (Pair pair : bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (List<Pair> bucket : buckets) {\n List<String> res = new ArrayList<>();\n for (Pair pair : bucket) {\n res.add(pair.key + \" -> \" + pair.val);\n }\n System.out.println(res);\n }\n }\n}\n
hash_map_chaining.cs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].Add(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n foreach (Pair pair in buckets[index].ToList()) {\n if (pair.key == key) {\n buckets[index].Remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (List<Pair> bucket in bucketsTmp) {\n foreach (Pair pair in bucket) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (List<Pair> bucket in buckets) {\n List<string> res = [];\n foreach (Pair pair in bucket) {\n res.Add(pair.key + \" -> \" + pair.val);\n }\n foreach (string kv in res) {\n Console.WriteLine(kv);\n }\n }\n }\n}\n
hash_map_chaining.go/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n buckets := make([][]pair, 4)\n for i := 0; i < 4; i++ {\n buckets[i] = make([]pair, 0)\n }\n return &hashMapChaining{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n idx := m.hashFunc(key)\n bucket := m.buckets[idx]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for _, p := range bucket {\n if p.key == key {\n return p.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for i := range m.buckets[idx] {\n if m.buckets[idx][i].key == key {\n m.buckets[idx][i].val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n p := pair{\n key: key,\n val: val,\n }\n m.buckets[idx] = append(m.buckets[idx], p)\n m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i, p := range m.buckets[idx] {\n if p.key == key {\n // \u5207\u7247\u5220\u9664\n m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n m.size -= 1\n break\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([][]pair, len(m.buckets))\n for i := 0; i < len(m.buckets); i++ {\n tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n copy(tmpBuckets[i], m.buckets[i])\n }\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([][]pair, m.capacity)\n for i := 0; i < m.capacity; i++ {\n m.buckets[i] = make([]pair, 0)\n }\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, bucket := range tmpBuckets {\n for _, p := range bucket {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n var builder strings.Builder\n\n for _, bucket := range m.buckets {\n builder.WriteString(\"[\")\n for _, p := range bucket {\n builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n }\n builder.WriteString(\"]\")\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
hash_map_chaining.swift/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: [], count: capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size) / Double(capacity)\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return pair.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de nil\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair(key: key, val: val)\n buckets[index].append(pair)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pairIndex, pair) in bucket.enumerated() {\n if pair.key == key {\n buckets[index].remove(at: pairIndex)\n size -= 1\n break\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: [], count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in bucketsTmp {\n for pair in bucket {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for bucket in buckets {\n let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n Swift.print(res)\n }\n }\n}\n
hash_map_chaining.js/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.ts/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key: number): number {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor(): number {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.dart/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n late int extendRatio; // \u6269\u5bb9\u500d\u6570\n late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = List.generate(capacity, (_) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair in bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair in bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = List.generate(capacity, (_) => []);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket in bucketsTmp) {\n for (Pair pair in bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (List<Pair> bucket in buckets) {\n List<String> res = [];\n for (Pair pair in bucket) {\n res.add(\"${pair.key} -> ${pair.val}\");\n }\n print(res);\n }\n }\n}\n
hash_map_chaining.rs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n size: i32,\n capacity: i32,\n load_thres: f32,\n extend_ratio: i32,\n buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![vec![]; 4],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % self.capacity as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f32 {\n self.size as f32 / self.capacity as f32\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) -> Option<String> {\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i in 0..bucket.len() {\n if bucket[i].key == key {\n let pair = bucket.remove(i);\n self.size -= 1;\n return Some(pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n None\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![Vec::new(); self.capacity as usize];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets_tmp {\n for pair in bucket {\n self.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for bucket in &self.buckets {\n let mut res = Vec::new();\n for pair in bucket {\n res.push(format!(\"{} -> {}\", pair.key, pair.val));\n }\n println!(\"{:?}\", res);\n }\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val.clone();\n return;\n }\n }\n let bucket = &mut self.buckets[index];\n\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair {\n key,\n val: val.clone(),\n };\n bucket.push(pair);\n self.size += 1;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&self, key: i32) -> Option<&str> {\n let index = self.hash_func(key);\n let bucket = &self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return Some(&pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n None\n }\n}\n
hash_map_chaining.c/* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n Pair *pair;\n struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Node **buckets; // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n while (cur) {\n Node *tmp = cur;\n cur = cur->next;\n free(tmp->pair);\n free(tmp);\n }\n }\n free(hashMap->buckets);\n free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n return cur->pair->val;\n }\n cur = cur->next;\n }\n return \"\"; // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n return;\n }\n cur = cur->next;\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Pair *newPair = (Pair *)malloc(sizeof(Pair));\n newPair->key = key;\n strcpy(newPair->val, val);\n Node *newNode = (Node *)malloc(sizeof(Node));\n newNode->pair = newPair;\n newNode->next = hashMap->buckets[index];\n hashMap->buckets[index] = newNode;\n hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n int oldCapacity = hashMap->capacity;\n Node **oldBuckets = hashMap->buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Node *cur = oldBuckets[i];\n while (cur) {\n put(hashMap, cur->pair->key, cur->pair->val);\n Node *temp = cur;\n cur = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(temp->pair);\n free(temp);\n }\n }\n\n free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n Node *cur = hashMap->buckets[index];\n Node *pre = NULL;\n while (cur) {\n if (cur->pair->key == key) {\n // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n if (pre) {\n pre->next = cur->next;\n } else {\n hashMap->buckets[index] = cur->next;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur->pair);\n free(cur);\n hashMap->size--;\n return;\n }\n pre = cur;\n cur = cur->next;\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n printf(\"[\");\n while (cur) {\n printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n cur = cur->next;\n }\n printf(\"]\\n\");\n }\n}\n
hash_map_chaining.kt/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining() {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n val loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n val extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: MutableList<MutableList<Pair>> // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = ArrayList(capacity)\n for (i in 0..<capacity) {\n buckets.add(mutableListOf())\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (pair in bucket) {\n if (pair.key == key) return pair.value\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend()\n }\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (pair in bucket) {\n if (pair.key == key) {\n pair.value = value\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n val pair = Pair(key, value)\n bucket.add(pair)\n size++\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair)\n size--\n break\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fun extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n // mutablelist \u65e0\u56fa\u5b9a\u5927\u5c0f\n buckets = mutableListOf()\n for (i in 0..<capacity) {\n buckets.add(mutableListOf())\n }\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (bucket in bucketsTmp) {\n for (pair in bucket) {\n put(pair.key, pair.value)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (bucket in buckets) {\n val res = mutableListOf<String>()\n for (pair in bucket) {\n val k = pair.key\n val v = pair.value\n res.add(\"$k -> $v\")\n }\n println(res)\n }\n }\n}\n
hash_map_chaining.rb[class]{HashMapChaining}-[func]{}\n
hash_map_chaining.zig[class]{HashMapChaining}-[func]{}\n
Code Visualization Full Screen >
It's worth noting that when the list is very long, the query efficiency \\(O(n)\\) is poor. At this point, the list can be converted to an \"AVL tree\" or \"Red-Black tree\" to optimize the time complexity of the query operation to \\(O(\\log n)\\).
"},{"location":"chapter_hashing/hash_collision/#622-open-addressing","title":"6.2.2 \u00a0 Open addressing","text":"\"Open addressing\" does not introduce additional data structures but uses \"multiple probes\" to handle hash collisions. The probing methods mainly include linear probing, quadratic probing, and double hashing.
Let's use linear probing as an example to introduce the mechanism of open addressing hash tables.
"},{"location":"chapter_hashing/hash_collision/#1-linear-probing","title":"1. \u00a0 Linear probing","text":"Linear probing uses a fixed-step linear search for probing, differing from ordinary hash tables.
value
; if an empty bucket is encountered, it means the target element is not in the hash table, so return None
.The Figure 6-6 shows the distribution of key-value pairs in an open addressing (linear probing) hash table. According to this hash function, keys with the same last two digits will be mapped to the same bucket. Through linear probing, they are stored consecutively in that bucket and the buckets below it.
Figure 6-6 \u00a0 Distribution of key-value pairs in open addressing (linear probing) hash table
However, linear probing tends to create \"clustering\". Specifically, the longer a continuous position in the array is occupied, the more likely these positions are to encounter hash collisions, further promoting the growth of these clusters and eventually leading to deterioration in the efficiency of operations.
It's important to note that we cannot directly delete elements in an open addressing hash table. Deleting an element creates an empty bucket None
in the array. When searching for elements, if linear probing encounters this empty bucket, it will return, making the elements below this bucket inaccessible. The program may incorrectly assume these elements do not exist, as shown in the Figure 6-7 .
Figure 6-7 \u00a0 Query issues caused by deletion in open addressing
To solve this problem, we can use a \"lazy deletion\" mechanism: instead of directly removing elements from the hash table, use a constant TOMBSTONE
to mark the bucket. In this mechanism, both None
and TOMBSTONE
represent empty buckets and can hold key-value pairs. However, when linear probing encounters TOMBSTONE
, it should continue traversing since there may still be key-value pairs below it.
However, lazy deletion may accelerate the degradation of hash table performance. Every deletion operation produces a delete mark, and as TOMBSTONE
increases, so does the search time, as linear probing may have to skip multiple TOMBSTONE
to find the target element.
Therefore, consider recording the index of the first TOMBSTONE
encountered during linear probing and swapping the target element found with this TOMBSTONE
. The advantage of this is that each time a query or addition is performed, the element is moved to a bucket closer to the ideal position (starting point of probing), thereby optimizing the query efficiency.
The code below implements an open addressing (linear probing) hash table with lazy deletion. To make fuller use of the hash table space, we treat the hash table as a \"circular array,\" continuing to traverse from the beginning when the end of the array is passed.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map_open_addressing.pyclass HashMapOpenAddressing:\n \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u6570\u7ec4\n self.TOMBSTONE = Pair(-1, \"-1\") # \u5220\u9664\u6807\u8bb0\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def find_bucket(self, key: int) -> int:\n \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n index = self.hash_func(key)\n first_tombstone = -1\n # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index] is not None:\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].key == key:\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if first_tombstone != -1:\n self.buckets[first_tombstone] = self.buckets[index]\n self.buckets[index] = self.TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n first_tombstone = index\n # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return index if first_tombstone == -1 else first_tombstone\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n return self.buckets[index].val\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index].val = val\n return\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Pair(key, val)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index] = self.TOMBSTONE\n self.size -= 1\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets_tmp = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [None] * self.capacity\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp:\n if pair not in [None, self.TOMBSTONE]:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is None:\n print(\"None\")\n elif pair is self.TOMBSTONE:\n print(\"TOMBSTONE\")\n else:\n print(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n const double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n const int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n vector<Pair *> buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapOpenAddressing() {\n for (Pair *pair : buckets) {\n if (pair != nullptr && pair != TOMBSTONE) {\n delete pair;\n }\n }\n delete TOMBSTONE;\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != nullptr) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n return buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n buckets[index]->val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n delete buckets[index];\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<Pair *> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = vector<Pair *>(capacity, nullptr);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair *pair : bucketsTmp) {\n if (pair != nullptr && pair != TOMBSTONE) {\n put(pair->key, pair->val);\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *pair : buckets) {\n if (pair == nullptr) {\n cout << \"nullptr\" << endl;\n } else if (pair == TOMBSTONE) {\n cout << \"TOMBSTONE\" << endl;\n } else {\n cout << pair->key << \" -> \" << pair->val << endl;\n }\n }\n }\n};\n
hash_map_open_addressing.java/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n private Pair[] buckets; // \u6876\u6570\u7ec4\n private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair pair : bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair pair : buckets) {\n if (pair == null) {\n System.out.println(\"null\");\n } else if (pair == TOMBSTONE) {\n System.out.println(\"TOMBSTONE\");\n } else {\n System.out.println(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.cs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n Pair[] buckets; // \u6876\u6570\u7ec4\n Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int FindBucket(int key) {\n int index = HashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (Pair pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair pair in buckets) {\n if (pair == null) {\n Console.WriteLine(\"null\");\n } else if (pair == TOMBSTONE) {\n Console.WriteLine(\"TOMBSTONE\");\n } else {\n Console.WriteLine(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.go/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets []*pair // \u6876\u6570\u7ec4\n TOMBSTONE *pair // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n return &hashMapOpenAddressing{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: make([]*pair, 4),\n TOMBSTONE: &pair{-1, \"-1\"},\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n return key % h.capacity // \u6839\u636e\u952e\u8ba1\u7b97\u54c8\u5e0c\u503c\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n return float64(h.size) / float64(h.capacity) // \u8ba1\u7b97\u5f53\u524d\u8d1f\u8f7d\u56e0\u5b50\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n index := h.hashFunc(key) // \u83b7\u53d6\u521d\u59cb\u7d22\u5f15\n firstTombstone := -1 // \u8bb0\u5f55\u9047\u5230\u7684\u7b2c\u4e00\u4e2aTOMBSTONE\u7684\u4f4d\u7f6e\n for h.buckets[index] != nil {\n if h.buckets[index].key == key {\n if firstTombstone != -1 {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n h.buckets[firstTombstone] = h.buckets[index]\n h.buckets[index] = h.TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u627e\u5230\u7684\u7d22\u5f15\n }\n if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n firstTombstone = index // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\u7684\u4f4d\u7f6e\n }\n index = (index + 1) % h.capacity // \u7ebf\u6027\u63a2\u6d4b\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if firstTombstone != -1 {\n return firstTombstone\n }\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n return h.buckets[index].val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n }\n return \"\" // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n if h.loadFactor() > h.loadThres {\n h.extend() // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n }\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n h.buckets[index] = &pair{key, val} // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n h.size++\n } else {\n h.buckets[index].val = val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val\n }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n h.buckets[index] = h.TOMBSTONE // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n h.size--\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) extend() {\n oldBuckets := h.buckets // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n h.capacity *= h.extendRatio // \u66f4\u65b0\u5bb9\u91cf\n h.buckets = make([]*pair, h.capacity) // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n h.size = 0 // \u91cd\u7f6e\u5927\u5c0f\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, pair := range oldBuckets {\n if pair != nil && pair != h.TOMBSTONE {\n h.put(pair.key, pair.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) print() {\n for _, pair := range h.buckets {\n if pair == nil {\n fmt.Println(\"nil\")\n } else if pair == h.TOMBSTONE {\n fmt.Println(\"TOMBSTONE\")\n } else {\n fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n }\n }\n}\n
hash_map_open_addressing.swift/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [Pair?] // \u6876\u6570\u7ec4\n var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: nil, count: capacity)\n TOMBSTONE = Pair(key: -1, val: \"-1\")\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size) / Double(capacity)\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n func findBucket(key: Int) -> Int {\n var index = hashFunc(key: key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while buckets[index] != nil {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if buckets[index]!.key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if firstTombstone != -1 {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n return buckets[index]!.val\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index]!.val = val\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key: key, val: val)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index] = TOMBSTONE\n size -= 1\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: nil, count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in bucketsTmp {\n if let pair, pair != TOMBSTONE {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in buckets {\n if pair == nil {\n Swift.print(\"null\")\n } else if pair == TOMBSTONE {\n Swift.print(\"TOMBSTONE\")\n } else {\n Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n }\n }\n }\n}\n
hash_map_open_addressing.js/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n #findBucket(key) {\n let index = this.#hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.#buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (this.#buckets[index].key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone !== -1) {\n this.#buckets[firstTombstone] = this.#buckets[index];\n this.#buckets[index] = this.#TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.#buckets[index] === this.#TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.#capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n return this.#buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.#buckets[index] = new Pair(key, val);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index] = this.#TOMBSTONE;\n this.#size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = Array(this.#capacity).fill(null);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.#TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const pair of this.#buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.#TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.ts/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % this.capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private loadFactor(): number {\n return this.size / this.capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private findBucket(key: number): number {\n let index = this.hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (this.buckets[index]!.key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone !== -1) {\n this.buckets[firstTombstone] = this.buckets[index];\n this.buckets[index] = this.TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.buckets[index] === this.TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n return this.buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.loadFactor() > this.loadThres) {\n this.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.buckets[index] = new Pair(key, val);\n this.size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index] = this.TOMBSTONE;\n this.size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.capacity *= this.extendRatio;\n this.buckets = Array(this.capacity).fill(null);\n this.size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const pair of this.buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.dart/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() {\n _size = 0;\n _buckets = List.generate(_capacity, (index) => null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % _capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return _size / _capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (_buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (_buckets[index]!.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n _buckets[firstTombstone] = _buckets[index];\n _buckets[index] = _TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % _capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n return _buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > _loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n _buckets[index] = new Pair(key, val);\n _size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index] = _TOMBSTONE;\n _size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<Pair?> bucketsTmp = _buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n _capacity *= _extendRatio;\n _buckets = List.generate(_capacity, (index) => null);\n _size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair? pair in bucketsTmp) {\n if (pair != null && pair != _TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (Pair? pair in _buckets) {\n if (pair == null) {\n print(\"null\");\n } else if (pair == _TOMBSTONE) {\n print(\"TOMBSTONE\");\n } else {\n print(\"${pair.key} -> ${pair.val}\");\n }\n }\n }\n}\n
hash_map_open_addressing.rs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n size: usize, // \u952e\u503c\u5bf9\u6570\u91cf\n capacity: usize, // \u54c8\u5e0c\u8868\u5bb9\u91cf\n load_thres: f64, // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extend_ratio: usize, // \u6269\u5bb9\u500d\u6570\n buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n TOMBSTONE: Option<Pair>, // \u5220\u9664\u6807\u8bb0\n}\n\nimpl HashMapOpenAddressing {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![None; 4],\n TOMBSTONE: Some(Pair {\n key: -1,\n val: \"-1\".to_string(),\n }),\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n (key % self.capacity as i32) as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f64 {\n self.size as f64 / self.capacity as f64\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fn find_bucket(&mut self, key: i32) -> usize {\n let mut index = self.hash_func(key);\n let mut first_tombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index].is_some() {\n // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].as_ref().unwrap().key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1 {\n self.buckets[first_tombstone as usize] = self.buckets[index].take();\n self.buckets[index] = self.TOMBSTONE.clone();\n return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n first_tombstone = index as i32;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if first_tombstone == -1 {\n index\n } else {\n first_tombstone as usize\n }\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&mut self, key: i32) -> Option<&str> {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n None\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index].as_mut().unwrap().val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Some(Pair { key, val });\n self.size += 1;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index] = self.TOMBSTONE.clone();\n self.size -= 1;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = self.buckets.clone();\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![None; self.capacity];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp {\n if pair.is_none() || pair == self.TOMBSTONE {\n continue;\n }\n let pair = pair.unwrap();\n\n self.put(pair.key, pair.val);\n }\n }\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for pair in &self.buckets {\n if pair.is_none() {\n println!(\"null\");\n } else if pair == &self.TOMBSTONE {\n println!(\"TOMBSTONE\");\n } else {\n let pair = pair.as_ref().unwrap();\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.c/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Pair **buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n hashMap->TOMBSTONE->key = -1;\n hashMap->TOMBSTONE->val = \"-1\";\n\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n free(pair->val);\n free(pair);\n }\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (hashMap->buckets[index] != NULL) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (hashMap->buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % hashMap->capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n return hashMap->buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n free(hashMap->buckets[index]->val);\n hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(hashMap->buckets[index]->val, val);\n hashMap->buckets[index]->val[strlen(val)] = '\\0';\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n Pair *pair = (Pair *)malloc(sizeof(Pair));\n pair->key = key;\n pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(pair->val, val);\n pair->val[strlen(val)] = '\\0';\n\n hashMap->buckets[index] = pair;\n hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n Pair *pair = hashMap->buckets[index];\n free(pair->val);\n free(pair);\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n hashMap->size--;\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair **bucketsTmp = hashMap->buckets;\n int oldCapacity = hashMap->capacity;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Pair *pair = bucketsTmp[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n put(hashMap, pair->key, pair->val);\n free(pair->val);\n free(pair);\n }\n }\n free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair == NULL) {\n printf(\"NULL\\n\");\n } else if (pair == hashMap->TOMBSTONE) {\n printf(\"TOMBSTONE\\n\");\n } else {\n printf(\"%d -> %s\\n\", pair->key, pair->val);\n }\n }\n}\n
hash_map_open_addressing.kt/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private var size: Int = 0 // \u952e\u503c\u5bf9\u6570\u91cf\n private var capacity = 4 // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private val loadThres: Double = 2.0 / 3.0 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private val extendRatio = 2 // \u6269\u5bb9\u500d\u6570\n private var buckets: Array<Pair?> // \u6876\u6570\u7ec4\n private val TOMBSTONE = Pair(-1, \"-1\") // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init {\n buckets = arrayOfNulls(capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fun findBucket(key: Int): Int {\n var index = hashFunc(key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index]?.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return if (firstTombstone == -1) index else firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index]?.value\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index]!!.value = value\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key, value)\n size++\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE\n size--\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fun extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = arrayOfNulls(capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.value)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (pair in buckets) {\n if (pair == null) {\n println(\"null\")\n } else if (pair == TOMBSTONE) {\n println(\"TOMESTOME\")\n } else {\n println(\"${pair.key} -> ${pair.value}\")\n }\n }\n }\n}\n
hash_map_open_addressing.rb[class]{HashMapOpenAddressing}-[func]{}\n
hash_map_open_addressing.zig[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2-quadratic-probing","title":"2. \u00a0 Quadratic probing","text":"Quadratic probing is similar to linear probing and is one of the common strategies of open addressing. When a collision occurs, quadratic probing does not simply skip a fixed number of steps but skips \"the square of the number of probes,\" i.e., \\(1, 4, 9, \\dots\\) steps.
Quadratic probing has the following advantages:
However, quadratic probing is not perfect:
As the name suggests, the double hashing method uses multiple hash functions \\(f_1(x)\\), \\(f_2(x)\\), \\(f_3(x)\\), \\(\\dots\\) for probing.
None
.Compared to linear probing, double hashing is less prone to clustering but involves additional computation for multiple hash functions.
Tip
Please note that open addressing (linear probing, quadratic probing, and double hashing) hash tables all have the issue of \"not being able to directly delete elements.\"
"},{"location":"chapter_hashing/hash_collision/#623-choice-of-programming-languages","title":"6.2.3 \u00a0 Choice of programming languages","text":"Various programming languages have adopted different hash table implementation strategies, here are a few examples:
dict
dictionary uses pseudo-random numbers for probing.HashMap
reaches 64 and the length of a linked list reaches 8, the linked list is converted to a red-black tree to improve search performance.A \"hash table\", also known as a \"hash map\", achieves efficient element querying by establishing a mapping between keys and values. Specifically, when we input a key
into the hash table, we can retrieve the corresponding value
in \\(O(1)\\) time.
As shown in the Figure 6-1 , given \\(n\\) students, each with two pieces of data: \"name\" and \"student number\". If we want to implement a query feature that returns the corresponding name when given a student number, we can use the hash table shown in the Figure 6-1 .
Figure 6-1 \u00a0 Abstract representation of a hash table
Apart from hash tables, arrays and linked lists can also be used to implement querying functions. Their efficiency is compared in the Table 6-1 .
Table 6-1 \u00a0 Comparison of element query efficiency
Array Linked List Hash Table Find Element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) Add Element \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Delete Element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)Observations reveal that the time complexity for adding, deleting, and querying in a hash table is \\(O(1)\\), which is highly efficient.
"},{"location":"chapter_hashing/hash_map/#611-common-operations-of-hash-table","title":"6.1.1 \u00a0 Common operations of hash table","text":"Common operations of a hash table include initialization, querying, adding key-value pairs, and deleting key-value pairs, etc. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig hash_map.py# Initialize hash table\nhmap: dict = {}\n\n# Add operation\n# Add key-value pair (key, value) to the hash table\nhmap[12836] = \"Xiao Ha\"\nhmap[15937] = \"Xiao Luo\"\nhmap[16750] = \"Xiao Suan\"\nhmap[13276] = \"Xiao Fa\"\nhmap[10583] = \"Xiao Ya\"\n\n# Query operation\n# Input key into hash table, get value\nname: str = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.pop(10583)\n
hash_map.cpp/* Initialize hash table */\nunordered_map<int, string> map;\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\";\nmap[15937] = \"Xiao Luo\";\nmap[16750] = \"Xiao Suan\";\nmap[13276] = \"Xiao Fa\";\nmap[10583] = \"Xiao Ya\";\n\n/* Query operation */\n// Input key into hash table, get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.erase(10583);\n
hash_map.java/* Initialize hash table */\nMap<Integer, String> map = new HashMap<>();\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.put(12836, \"Xiao Ha\"); \nmap.put(15937, \"Xiao Luo\"); \nmap.put(16750, \"Xiao Suan\"); \nmap.put(13276, \"Xiao Fa\");\nmap.put(10583, \"Xiao Ya\");\n\n/* Query operation */\n// Input key into hash table, get value\nString name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
hash_map.cs/* Initialize hash table */\nDictionary<int, string> map = new() {\n /* Add operation */\n // Add key-value pair (key, value) to the hash table\n { 12836, \"Xiao Ha\" },\n { 15937, \"Xiao Luo\" },\n { 16750, \"Xiao Suan\" },\n { 13276, \"Xiao Fa\" },\n { 10583, \"Xiao Ya\" }\n};\n\n/* Query operation */\n// Input key into hash table, get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.Remove(10583);\n
hash_map_test.go/* Initialize hash table */\nhmap := make(map[int]string)\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nhmap[12836] = \"Xiao Ha\"\nhmap[15937] = \"Xiao Luo\"\nhmap[16750] = \"Xiao Suan\"\nhmap[13276] = \"Xiao Fa\"\nhmap[10583] = \"Xiao Ya\"\n\n/* Query operation */\n// Input key into hash table, get value\nname := hmap[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\ndelete(hmap, 10583)\n
hash_map.swift/* Initialize hash table */\nvar map: [Int: String] = [:]\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\"\nmap[15937] = \"Xiao Luo\"\nmap[16750] = \"Xiao Suan\"\nmap[13276] = \"Xiao Fa\"\nmap[10583] = \"Xiao Ya\"\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map[15937]!\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.removeValue(forKey: 10583)\n
hash_map.js/* Initialize hash table */\nconst map = new Map();\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.set(12836, 'Xiao Ha');\nmap.set(15937, 'Xiao Luo');\nmap.set(16750, 'Xiao Suan');\nmap.set(13276, 'Xiao Fa');\nmap.set(10583, 'Xiao Ya');\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\n
hash_map.ts/* Initialize hash table */\nconst map = new Map<number, string>();\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.set(12836, 'Xiao Ha');\nmap.set(15937, 'Xiao Luo');\nmap.set(16750, 'Xiao Suan');\nmap.set(13276, 'Xiao Fa');\nmap.set(10583, 'Xiao Ya');\nconsole.info('\\nAfter adding, the hash table is\\nKey -> Value');\nconsole.info(map);\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map.get(15937);\nconsole.info('\\nInput student number 15937, query name ' + name);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\nconsole.info('\\nAfter deleting 10583, the hash table is\\nKey -> Value');\nconsole.info(map);\n
hash_map.dart/* Initialize hash table */\nMap<int, String> map = {};\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\";\nmap[15937] = \"Xiao Luo\";\nmap[16750] = \"Xiao Suan\";\nmap[13276] = \"Xiao Fa\";\nmap[10583] = \"Xiao Ya\";\n\n/* Query operation */\n// Input key into hash table, get value\nString name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
hash_map.rsuse std::collections::HashMap;\n\n/* Initialize hash table */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.insert(12836, \"Xiao Ha\".to_string());\nmap.insert(15937, \"Xiao Luo\".to_string());\nmap.insert(16750, \"Xiao Suan\".to_string());\nmap.insert(13279, \"Xiao Fa\".to_string());\nmap.insert(10583, \"Xiao Ya\".to_string());\n\n/* Query operation */\n// Input key into hash table, get value\nlet _name: Option<&String> = map.get(&15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c// C does not provide a built-in hash table\n
hash_map.kt\n
hash_map.zig\n
Code Visualization Full Screen >
There are three common ways to traverse a hash table: traversing key-value pairs, keys, and values. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig hash_map.py# Traverse hash table\n# Traverse key-value pairs key->value\nfor key, value in hmap.items():\n print(key, \"->\", value)\n# Traverse keys only\nfor key in hmap.keys():\n print(key)\n# Traverse values only\nfor value in hmap.values():\n print(value)\n
hash_map.cpp/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (auto kv: map) {\n cout << kv.first << \" -> \" << kv.second << endl;\n}\n// Traverse using iterator key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n cout << iter->first << \"->\" << iter->second << endl;\n}\n
hash_map.java/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// Traverse keys only\nfor (int key: map.keySet()) {\n System.out.println(key);\n}\n// Traverse values only\nfor (String val: map.values()) {\n System.out.println(val);\n}\n
hash_map.cs/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nforeach (var kv in map) {\n Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// Traverse keys only\nforeach (int key in map.Keys) {\n Console.WriteLine(key);\n}\n// Traverse values only\nforeach (string val in map.Values) {\n Console.WriteLine(val);\n}\n
hash_map_test.go/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor key, value := range hmap {\n fmt.Println(key, \"->\", value)\n}\n// Traverse keys only\nfor key := range hmap {\n fmt.Println(key)\n}\n// Traverse values only\nfor _, value := range hmap {\n fmt.Println(value)\n}\n
hash_map.swift/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in map {\n print(\"\\(key) -> \\(value)\")\n}\n// Traverse keys only\nfor key in map.keys {\n print(key)\n}\n// Traverse values only\nfor value in map.values {\n print(value)\n}\n
hash_map.js/* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.ts/* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.dart/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n\n// Traverse keys only Key\nmap.keys.forEach((key) {\nprint(key);\n});\n\n// Traverse values only Value\nmap.values.forEach((value) {\nprint(value);\n});\n
hash_map.rs/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in &map {\n println!(\"{key} -> {value}\");\n}\n\n// Traverse keys only Key\nfor key in map.keys() {\n println!(\"{key}\"); \n}\n\n// Traverse values only Value\nfor value in map.values() {\n println!(\"{value}\");\n}\n
hash_map.c// C does not provide a built-in hash table\n
hash_map.kt\n
hash_map.zig// Zig example is not provided\n
Code Visualization Full Screen >
"},{"location":"chapter_hashing/hash_map/#612-simple-implementation-of-hash-table","title":"6.1.2 \u00a0 Simple implementation of hash table","text":"First, let's consider the simplest case: implementing a hash table using just an array. In the hash table, each empty slot in the array is called a \"bucket\", and each bucket can store one key-value pair. Therefore, the query operation involves finding the bucket corresponding to the key
and retrieving the value
from it.
So, how do we locate the appropriate bucket based on the key
? This is achieved through a \"hash function\". The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all possible keys, and the output space is all buckets (array indices). In other words, input a key
, and we can use the hash function to determine the storage location of the corresponding key-value pair in the array.
The calculation process of the hash function for a given key
is divided into the following two steps:
hash()
.capacity
to obtain the array index index
.index = hash(key) % capacity\n
Afterward, we can use index
to access the corresponding bucket in the hash table and thereby retrieve the value
.
Assuming array length capacity = 100
and hash algorithm hash(key) = key
, the hash function is key % 100
. The Figure 6-2 uses key
as the student number and value
as the name to demonstrate the working principle of the hash function.
Figure 6-2 \u00a0 Working principle of hash function
The following code implements a simple hash table. Here, we encapsulate key
and value
into a class Pair
to represent the key-value pair.
class Pair:\n \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n def __init__(self, key: int, val: str):\n self.key = key\n self.val = val\n\nclass ArrayHashMap:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n self.buckets: list[Pair | None] = [None] * 100\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n index = key % 100\n return index\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n pair: Pair = self.buckets[index]\n if pair is None:\n return None\n return pair.val\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n pair = Pair(key, val)\n index: int = self.hash_func(key)\n self.buckets[index] = pair\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None\n\n def entry_set(self) -> list[Pair]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n result: list[Pair] = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair)\n return result\n\n def key_set(self) -> list[int]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.key)\n return result\n\n def value_set(self) -> list[str]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.val)\n return result\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is not None:\n print(pair.key, \"->\", pair.val)\n
array_hash_map.cpp/* \u952e\u503c\u5bf9 */\nstruct Pair {\n public:\n int key;\n string val;\n Pair(int key, string val) {\n this->key = key;\n this->val = val;\n }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private:\n vector<Pair *> buckets;\n\n public:\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = vector<Pair *>(100);\n }\n\n ~ArrayHashMap() {\n // \u91ca\u653e\u5185\u5b58\n for (const auto &bucket : buckets) {\n delete bucket;\n }\n buckets.clear();\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n Pair *pair = buckets[index];\n if (pair == nullptr)\n return \"\";\n return pair->val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n Pair *pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n delete buckets[index];\n buckets[index] = nullptr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n vector<Pair *> pairSet() {\n vector<Pair *> pairSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n pairSet.push_back(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n vector<int> keySet() {\n vector<int> keySet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n keySet.push_back(pair->key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n vector<string> valueSet() {\n vector<string> valueSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n valueSet.push_back(pair->val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *kv : pairSet()) {\n cout << kv->key << \" -> \" << kv->val << endl;\n }\n }\n};\n
array_hash_map.java/* \u952e\u503c\u5bf9 */\nclass Pair {\n public int key;\n public String val;\n\n public Pair(int key, String val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private List<Pair> buckets;\n\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = new ArrayList<>();\n for (int i = 0; i < 100; i++) {\n buckets.add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n int index = hashFunc(key);\n Pair pair = buckets.get(index);\n if (pair == null)\n return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n Pair pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets.set(index, pair);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n int index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets.set(index, null);\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> pairSet() {\n List<Pair> pairSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n pairSet.add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<Integer> keySet() {\n List<Integer> keySet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n keySet.add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<String> valueSet() {\n List<String> valueSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n valueSet.add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair kv : pairSet()) {\n System.out.println(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.cs/* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n public int key = key;\n public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n List<Pair?> buckets;\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = [];\n for (int i = 0; i < 100; i++) {\n buckets.Add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n Pair? pair = buckets[index];\n if (pair == null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n Pair pair = new(key, val);\n int index = HashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> PairSet() {\n List<Pair> pairSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n pairSet.Add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<int> KeySet() {\n List<int> keySet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n keySet.Add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<string> ValueSet() {\n List<string> valueSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n valueSet.Add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair kv in PairSet()) {\n Console.WriteLine(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.go/* \u952e\u503c\u5bf9 */\ntype pair struct {\n key int\n val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets := make([]*pair, 100)\n return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n index := key % 100\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n index := a.hashFunc(key)\n pair := a.buckets[index]\n if pair == nil {\n return \"Not Found\"\n }\n return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n pair := &pair{key: key, val: val}\n index := a.hashFunc(key)\n a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n index := a.hashFunc(key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n var pairs []*pair\n for _, pair := range a.buckets {\n if pair != nil {\n pairs = append(pairs, pair)\n }\n }\n return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n var keys []int\n for _, pair := range a.buckets {\n if pair != nil {\n keys = append(keys, pair.key)\n }\n }\n return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n var values []string\n for _, pair := range a.buckets {\n if pair != nil {\n values = append(values, pair.val)\n }\n }\n return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n for _, pair := range a.buckets {\n if pair != nil {\n fmt.Println(pair.key, \"->\", pair.val)\n }\n }\n}\n
array_hash_map.swift/* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n public var key: Int\n public var val: String\n\n public init(key: Int, val: String) {\n self.key = key\n self.val = val\n }\n\n public static func == (lhs: Pair, rhs: Pair) -> Bool {\n lhs.key == rhs.key && lhs.val == rhs.val\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private var buckets: [Pair?]\n\n init() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = Array(repeating: nil, count: 100)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private func hashFunc(key: Int) -> Int {\n let index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let pair = buckets[index]\n return pair?.val\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n let pair = Pair(key: key, val: val)\n let index = hashFunc(key: key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = nil\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n func pairSet() -> [Pair] {\n buckets.compactMap { $0 }\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n func keySet() -> [Int] {\n buckets.compactMap { $0?.key }\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n func valueSet() -> [String] {\n buckets.compactMap { $0?.val }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in pairSet() {\n Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n }\n }\n}\n
array_hash_map.js/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n constructor(key, val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n #buckets;\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.#buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n let index = this.#hashFunc(key);\n let pair = this.#buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n set(key, val) {\n let index = this.#hashFunc(key);\n this.#buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n delete(key) {\n let index = this.#hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.#buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n entries() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n keys() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n values() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.ts/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n public key: number;\n public val: string;\n\n constructor(key: number, val: string) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private readonly buckets: (Pair | null)[];\n\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public get(key: number): string | null {\n let index = this.hashFunc(key);\n let pair = this.buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public set(key: number, val: string) {\n let index = this.hashFunc(key);\n this.buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public delete(key: number) {\n let index = this.hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public entries(): (Pair | null)[] {\n let arr: (Pair | null)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public keys(): (number | undefined)[] {\n let arr: (number | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public values(): (string | undefined)[] {\n let arr: (string | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.dart/* \u952e\u503c\u5bf9 */\nclass Pair {\n int key;\n String val;\n Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n late List<Pair?> _buckets;\n\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n _buckets = List.filled(100, null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int _hashFunc(int key) {\n final int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n final int index = _hashFunc(key);\n final Pair? pair = _buckets[index];\n if (pair == null) {\n return null;\n }\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n final Pair pair = Pair(key, val);\n final int index = _hashFunc(key);\n _buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n final int index = _hashFunc(key);\n _buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n List<Pair> pairSet() {\n List<Pair> pairSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n pairSet.add(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n List<int> keySet() {\n List<int> keySet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n keySet.add(pair.key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n List<String> values() {\n List<String> valueSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n valueSet.add(pair.val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (final Pair kv in pairSet()) {\n print(\"${kv.key} -> ${kv.val}\");\n }\n }\n}\n
array_hash_map.rs/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n pub key: i32,\n pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n pub fn new() -> ArrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n Self {\n buckets: vec![None; 100],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % 100\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n pub fn get(&self, key: i32) -> Option<&String> {\n let index = self.hash_func(key);\n self.buckets[index].as_ref().map(|pair| &pair.val)\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n pub fn put(&mut self, key: i32, val: &str) {\n let index = self.hash_func(key);\n self.buckets[index] = Some(Pair {\n key,\n val: val.to_string(),\n });\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n pub fn remove(&mut self, key: i32) {\n let index = self.hash_func(key);\n // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n pub fn entry_set(&self) -> Vec<&Pair> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref())\n .collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n pub fn key_set(&self) -> Vec<&i32> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n .collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n pub fn value_set(&self) -> Vec<&String> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n .collect()\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n pub fn print(&self) {\n for pair in self.entry_set() {\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n}\n
array_hash_map.c/* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n int key;\n char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n Pair *buckets[HASHTABLE_CAPACITY];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n for (int i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n free(hmap->buckets[i]->val);\n free(hmap->buckets[i]);\n }\n }\n free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n Pair *Pair = malloc(sizeof(Pair));\n Pair->key = key;\n Pair->val = malloc(strlen(val) + 1);\n strcpy(Pair->val, val);\n\n int index = hashFunc(key);\n hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n int index = hashFunc(key);\n free(hmap->buckets[index]->val);\n free(hmap->buckets[index]);\n hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n Pair *entries;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n entries = malloc(sizeof(Pair) * total);\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n entries[index].key = hmap->buckets[i]->key;\n entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n strcpy(entries[index].val, hmap->buckets[i]->val);\n index++;\n }\n }\n set->set = entries;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n int *keys;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n keys = malloc(total * sizeof(int));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n keys[index] = hmap->buckets[i]->key;\n index++;\n }\n }\n set->set = keys;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n char **vals;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n vals = malloc(total * sizeof(char *));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n vals[index] = hmap->buckets[i]->val;\n index++;\n }\n }\n set->set = vals;\n set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n int i;\n MapSet set;\n pairSet(hmap, &set);\n Pair *entries = (Pair *)set.set;\n for (i = 0; i < set.len; i++) {\n printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n }\n free(set.set);\n}\n
array_hash_map.kt/* \u952e\u503c\u5bf9 */\nclass Pair(\n var key: Int,\n var value: String\n)\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private val buckets = arrayOfNulls<Pair>(100)\n\n init {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for (i in 0..<100) {\n buckets[i] = null\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n val index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val pair = buckets[index] ?: return null\n return pair.value\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n val pair = Pair(key, value)\n val index = hashFunc(key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n fun pairSet(): MutableList<Pair> {\n val pairSet = ArrayList<Pair>()\n for (pair in buckets) {\n if (pair != null) pairSet.add(pair)\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n fun keySet(): MutableList<Int> {\n val keySet = ArrayList<Int>()\n for (pair in buckets) {\n if (pair != null) keySet.add(pair.key)\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n fun valueSet(): MutableList<String> {\n val valueSet = ArrayList<String>()\n for (pair in buckets) {\n pair?.let { valueSet.add(it.value) }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (kv in pairSet()) {\n val key = kv.key\n val value = kv.value\n println(\"${key}->${value}\")\n }\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private val buckets = arrayOfNulls<Pair>(100)\n\n init {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for (i in 0..<100) {\n buckets[i] = null\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n val index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val pair = buckets[index] ?: return null\n return pair.value\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n val pair = Pair(key, value)\n val index = hashFunc(key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n fun pairSet(): MutableList<Pair> {\n val pairSet = ArrayList<Pair>()\n for (pair in buckets) {\n if (pair != null) pairSet.add(pair)\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n fun keySet(): MutableList<Int> {\n val keySet = ArrayList<Int>()\n for (pair in buckets) {\n if (pair != null) keySet.add(pair.key)\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n fun valueSet(): MutableList<String> {\n val valueSet = ArrayList<String>()\n for (pair in buckets) {\n pair?.let { valueSet.add(it.value) }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (kv in pairSet()) {\n val key = kv.key\n val value = kv.value\n println(\"${key}->${value}\")\n }\n }\n}\n
array_hash_map.rb[class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
array_hash_map.zig// \u952e\u503c\u5bf9\nconst Pair = struct {\n key: usize = undefined,\n val: []const u8 = undefined,\n\n pub fn init(key: usize, val: []const u8) Pair {\n return Pair {\n .key = key,\n .val = val,\n };\n }\n};\n\n// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\n return struct {\n bucket: ?std.ArrayList(?T) = null,\n mem_allocator: std.mem.Allocator = undefined,\n\n const Self = @This();\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n self.mem_allocator = allocator;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\n self.bucket = std.ArrayList(?T).init(self.mem_allocator);\n var i: i32 = 0;\n while (i < 100) : (i += 1) {\n try self.bucket.?.append(null);\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n pub fn deinit(self: *Self) void {\n if (self.bucket != null) self.bucket.?.deinit();\n }\n\n // \u54c8\u5e0c\u51fd\u6570\n fn hashFunc(key: usize) usize {\n var index = key % 100;\n return index;\n }\n\n // \u67e5\u8be2\u64cd\u4f5c\n pub fn get(self: *Self, key: usize) []const u8 {\n var index = hashFunc(key);\n var pair = self.bucket.?.items[index];\n return pair.?.val;\n }\n\n // \u6dfb\u52a0\u64cd\u4f5c\n pub fn put(self: *Self, key: usize, val: []const u8) !void {\n var pair = Pair.init(key, val);\n var index = hashFunc(key);\n self.bucket.?.items[index] = pair;\n }\n\n // \u5220\u9664\u64cd\u4f5c\n pub fn remove(self: *Self, key: usize) !void {\n var index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n self.bucket.?.items[index] = null;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\n pub fn pairSet(self: *Self) !std.ArrayList(T) {\n var entry_set = std.ArrayList(T).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try entry_set.append(item.?);\n }\n return entry_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\n pub fn keySet(self: *Self) !std.ArrayList(usize) {\n var key_set = std.ArrayList(usize).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try key_set.append(item.?.key);\n }\n return key_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u503c\n pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\n var value_set = std.ArrayList([]const u8).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try value_set.append(item.?.val);\n }\n return value_set;\n }\n\n // \u6253\u5370\u54c8\u5e0c\u8868\n pub fn print(self: *Self) !void {\n var entry_set = try self.pairSet();\n defer entry_set.deinit();\n for (entry_set.items) |item| {\n std.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n }\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_hashing/hash_map/#613-hash-collision-and-resizing","title":"6.1.3 \u00a0 Hash collision and resizing","text":"Fundamentally, the role of the hash function is to map the entire input space of all keys to the output space of all array indices. However, the input space is often much larger than the output space. Therefore, theoretically, there must be situations where \"multiple inputs correspond to the same output\".
For the hash function in the above example, if the last two digits of the input key
are the same, the output of the hash function will also be the same. For example, when querying for students with student numbers 12836 and 20336, we find:
12836 % 100 = 36\n20336 % 100 = 36\n
As shown in the Figure 6-3 , both student numbers point to the same name, which is obviously incorrect. This situation where multiple inputs correspond to the same output is known as \"hash collision\".
Figure 6-3 \u00a0 Example of hash collision
It is easy to understand that the larger the capacity \\(n\\) of the hash table, the lower the probability of multiple keys being allocated to the same bucket, and the fewer the collisions. Therefore, expanding the capacity of the hash table can reduce hash collisions.
As shown in the Figure 6-4 , before expansion, key-value pairs (136, A)
and (236, D)
collided; after expansion, the collision is resolved.
Figure 6-4 \u00a0 Hash table expansion
Similar to array expansion, resizing a hash table requires migrating all key-value pairs from the original hash table to the new one, which is time-consuming. Furthermore, since the capacity capacity
of the hash table changes, we need to recalculate the storage positions of all key-value pairs using the hash function, which adds to the computational overhead of the resizing process. Therefore, programming languages often reserve a sufficiently large capacity for the hash table to prevent frequent resizing.
The \"load factor\" is an important concept for hash tables. It is defined as the ratio of the number of elements in the hash table to the number of buckets. It is used to measure the severity of hash collisions and is often used as a trigger for resizing the hash table. For example, in Java, when the load factor exceeds \\(0.75\\), the system will resize the hash table to twice its original size.
"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 Summary","text":""},{"location":"chapter_hashing/summary/#1-key-review","title":"1. \u00a0 Key review","text":"key
, a hash table can retrieve the corresponding value
in \\(O(1)\\) time, which is highly efficient.key
to an array index, allowing access to the corresponding bucket to retrieve the value
.HashMap
uses chaining, while Python's dict
employs open addressing.Q: When does the time complexity of a hash table degrade to \\(O(n)\\)?
The time complexity of a hash table can degrade to \\(O(n)\\) when hash collisions are severe. When the hash function is well-designed, the capacity is set appropriately, and collisions are evenly distributed, the time complexity is \\(O(1)\\). We usually consider the time complexity to be \\(O(1)\\) when using built-in hash tables in programming languages.
Q: Why not use the hash function \\(f(x) = x\\)? This would eliminate collisions.
Under the hash function \\(f(x) = x\\), each element corresponds to a unique bucket index, which is equivalent to an array. However, the input space is usually much larger than the output space (array length), so the last step of a hash function is often to take the modulo of the array length. In other words, the goal of a hash table is to map a larger state space to a smaller one while providing \\(O(1)\\) query efficiency.
Q: Why can hash tables be more efficient than arrays, linked lists, or binary trees, even though they are implemented using these structures?
Firstly, hash tables have higher time efficiency but lower space efficiency. A significant portion of memory in hash tables remains unused.
Secondly, they are only more efficient in specific use cases. If a feature can be implemented with the same time complexity using an array or a linked list, it's usually faster than using a hash table. This is because the computation of the hash function incurs overhead, making the constant factor in the time complexity larger.
Lastly, the time complexity of hash tables can degrade. For example, in chaining, we perform search operations in a linked list or red-black tree, which still risks degrading to \\(O(n)\\) time.
Q: Does multiple hashing also have the flaw of not being able to delete elements directly? Can space marked as deleted be reused?
Multiple hashing is a form of open addressing, and all open addressing methods have the drawback of not being able to delete elements directly; they require marking elements as deleted. Marked spaces can be reused. When inserting new elements into the hash table, and the hash function points to a position marked as deleted, that position can be used by the new element. This maintains the probing sequence of the hash table while ensuring efficient use of space.
Q: Why do hash collisions occur during the search process in linear probing?
During the search process, the hash function points to the corresponding bucket and key-value pair. If the key
doesn't match, it indicates a hash collision. Therefore, linear probing will search downwards at a predetermined step size until the correct key-value pair is found or the search fails.
Q: Why can resizing a hash table alleviate hash collisions?
The last step of a hash function often involves taking the modulo of the array length \\(n\\), to keep the output within the array index range. When resizing, the array length \\(n\\) changes, and the indices corresponding to the keys may also change. Keys that were previously mapped to the same bucket might be distributed across multiple buckets after resizing, thereby mitigating hash collisions.
"},{"location":"chapter_heap/","title":"Chapter 8. \u00a0 Heap","text":"Abstract
The heap is like mountain peaks, stacked and undulating, each with its unique shape.
Among these peaks, the highest one always catches the eye first.
"},{"location":"chapter_heap/#chapter-contents","title":"Chapter Contents","text":"In some cases, we want to build a heap using all elements of a list, and this process is known as \"heap construction operation.\"
"},{"location":"chapter_heap/build_heap/#821-implementing-with-heap-insertion-operation","title":"8.2.1 \u00a0 Implementing with heap insertion operation","text":"First, we create an empty heap and then iterate through the list, performing the \"heap insertion operation\" on each element in turn. This means adding the element to the end of the heap and then \"heapifying\" it from bottom to top.
Each time an element is added to the heap, the length of the heap increases by one. Since nodes are added to the binary tree from top to bottom, the heap is constructed \"from top to bottom.\"
Let the number of elements be \\(n\\), and each element's insertion operation takes \\(O(\\log{n})\\) time, thus the time complexity of this heap construction method is \\(O(n \\log n)\\).
"},{"location":"chapter_heap/build_heap/#822-implementing-by-heapifying-through-traversal","title":"8.2.2 \u00a0 Implementing by heapifying through traversal","text":"In fact, we can implement a more efficient method of heap construction in two steps.
After heapifying a node, the subtree with that node as the root becomes a valid sub-heap. Since the traversal is in reverse order, the heap is built \"from bottom to top.\"
The reason for choosing reverse traversal is that it ensures the subtree below the current node is already a valid sub-heap, making the heapification of the current node effective.
It's worth mentioning that since leaf nodes have no children, they naturally form valid sub-heaps and do not need to be heapified. As shown in the following code, the last non-leaf node is the parent of the last node; we start from it and traverse in reverse order to perform heapification:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef __init__(self, nums: list[int]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n self.max_heap = nums\n # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(self.parent(self.size() - 1), -1, -1):\n self.sift_down(i)\n
my_heap.cpp/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.java/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new ArrayList<>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.cs/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new List<int>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var size = Parent(this.Size() - 1);\n for (int i = size; i >= 0; i--) {\n SiftDown(i);\n }\n}\n
my_heap.go/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n h := &maxHeap{data: nums}\n for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n h.siftDown(i)\n }\n return h\n}\n
my_heap.swift/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0 ... parent(i: size() - 1)).reversed() {\n siftDown(i: i)\n }\n}\n
my_heap.js/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.#maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n this.#siftDown(i);\n }\n}\n
my_heap.ts/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n this.siftDown(i);\n }\n}\n
my_heap.dart/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n _maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = _parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.rs/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n let mut heap = MaxHeap { max_heap: nums };\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=Self::parent(heap.size() - 1)).rev() {\n heap.sift_down(i);\n }\n heap\n}\n
my_heap.c/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n // \u6240\u6709\u5143\u7d20\u5165\u5806\n MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n maxHeap->size = size;\n memcpy(maxHeap->data, nums, size * sizeof(int));\n for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n siftDown(maxHeap, i);\n }\n return maxHeap;\n}\n
my_heap.kt/* \u5927\u9876\u5806 */\nclass MaxHeap(nums: List<Int>?) {\n // \u4f7f\u7528\u5217\u8868\u800c\u975e\u6570\u7ec4\uff0c\u8fd9\u6837\u65e0\u987b\u8003\u8651\u6269\u5bb9\u95ee\u9898\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n private val maxHeap = ArrayList(nums!!)\n\n /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\n init {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (i in parent(size() - 1) downTo 0) {\n siftDown(i)\n }\n }\n\n /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u4ea4\u6362\u5143\u7d20 */\n private fun swap(i: Int, j: Int) {\n maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] }\n }\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n fun size(): Int {\n return maxHeap.size\n }\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n return size() == 0\n }\n\n /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\n fun peek(): Int {\n return maxHeap[0]\n }\n\n /* \u5143\u7d20\u5165\u5806 */\n fun push(value: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(value)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1)\n }\n\n /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n private fun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n val p = parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n }\n\n /* \u5143\u7d20\u51fa\u5806 */\n fun pop(): Int {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u5220\u9664\u8282\u70b9\n val value = maxHeap.removeAt(size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return value\n }\n\n /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n private fun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n }\n\n /* \u6253\u5370\u5806\uff08\u4e8c\u53c9\u6811\uff09 */\n fun print() {\n val queue = PriorityQueue { a: Int, b: Int -> b - a }\n queue.addAll(maxHeap)\n printHeap(queue)\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{__init__}\n
my_heap.zig// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\n if (self.max_heap != null) return;\n self.max_heap = std.ArrayList(T).init(allocator);\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n try self.max_heap.?.appendSlice(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var i: usize = parent(self.size() - 1) + 1;\n while (i > 0) : (i -= 1) {\n try self.siftDown(i - 1);\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/build_heap/#823-complexity-analysis","title":"8.2.3 \u00a0 Complexity analysis","text":"Next, let's attempt to calculate the time complexity of this second method of heap construction.
Multiplying the two, we get the time complexity of the heap construction process as \\(O(n \\log n)\\). But this estimate is not accurate, because it does not take into account the nature of the binary tree having far more nodes at the lower levels than at the top.
Let's perform a more accurate calculation. To simplify the calculation, assume a \"perfect binary tree\" with \\(n\\) nodes and height \\(h\\); this assumption does not affect the correctness of the result.
Figure 8-5 \u00a0 Node counts at each level of a perfect binary tree
As shown in the Figure 8-5 , the maximum number of iterations for a node \"to be heapified from top to bottom\" is equal to the distance from that node to the leaf nodes, which is precisely \"node height.\" Therefore, we can sum the \"number of nodes \\(\\times\\) node height\" at each level, to get the total number of heapification iterations for all nodes.
\\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]To simplify the above equation, we need to use knowledge of sequences from high school, first multiply \\(T(h)\\) by \\(2\\), to get:
\\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^h\\times1 \\newline \\end{aligned} \\]By subtracting \\(T(h)\\) from \\(2T(h)\\) using the method of displacement, we get:
\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]Observing the equation, \\(T(h)\\) is an geometric series, which can be directly calculated using the sum formula, resulting in a time complexity of:
\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]Further, a perfect binary tree with height \\(h\\) has \\(n = 2^{h+1} - 1\\) nodes, thus the complexity is \\(O(2^h) = O(n)\\). This calculation shows that the time complexity of inputting a list and constructing a heap is \\(O(n)\\), which is very efficient.
"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 Heap","text":"A \"heap\" is a complete binary tree that satisfies specific conditions and can be mainly divided into two types, as shown in the Figure 8-1 .
Figure 8-1 \u00a0 Min heap and max heap
As a special case of a complete binary tree, heaps have the following characteristics:
It should be noted that many programming languages provide a \"priority queue,\" which is an abstract data structure defined as a queue with priority sorting.
In fact, heaps are often used to implement priority queues, with max heaps equivalent to priority queues where elements are dequeued in descending order. From a usage perspective, we can consider \"priority queue\" and \"heap\" as equivalent data structures. Therefore, this book does not make a special distinction between the two, uniformly referring to them as \"heap.\"
Common operations on heaps are shown in the Table 8-1 , and the method names depend on the programming language.
Table 8-1 \u00a0 Efficiency of Heap Operations
Method name Description Time complexitypush()
Add an element to the heap \\(O(\\log n)\\) pop()
Remove the top element from the heap \\(O(\\log n)\\) peek()
Access the top element (for max/min heap, the max/min value) \\(O(1)\\) size()
Get the number of elements in the heap \\(O(1)\\) isEmpty()
Check if the heap is empty \\(O(1)\\) In practice, we can directly use the heap class (or priority queue class) provided by programming languages.
Similar to sorting algorithms where we have \"ascending order\" and \"descending order,\" we can switch between \"min heap\" and \"max heap\" by setting a flag
or modifying the Comparator
. The code is as follows:
# \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
heap.cpp/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
heap.java/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
heap.cs/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
heap.go// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n last := (*h)[len(*h)-1]\n *h = (*h)[:len(*h)-1]\n return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n /* \u521d\u59cb\u5316\u5806 */\n // \u521d\u59cb\u5316\u5927\u9876\u5806\n maxHeap := &intHeap{}\n heap.Init(maxHeap)\n /* \u5143\u7d20\u5165\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n heap.Push(maxHeap, 1)\n heap.Push(maxHeap, 3)\n heap.Push(maxHeap, 2)\n heap.Push(maxHeap, 4)\n heap.Push(maxHeap, 5)\n\n /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n top := maxHeap.Top()\n fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n heap.Pop(maxHeap) // 5\n heap.Pop(maxHeap) // 4\n heap.Pop(maxHeap) // 3\n heap.Pop(maxHeap) // 2\n heap.Pop(maxHeap) // 1\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n size := len(*maxHeap)\n fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n isEmpty := len(*maxHeap) == 0\n fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
heap.swift/* \u521d\u59cb\u5316\u5806 */\n// Swift \u7684 Heap \u7c7b\u578b\u540c\u65f6\u652f\u6301\u6700\u5927\u5806\u548c\u6700\u5c0f\u5806\uff0c\u4e14\u9700\u8981\u5f15\u5165 swift-collections\nvar heap = Heap<Int>()\n\n/* \u5143\u7d20\u5165\u5806 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = heap.max()!\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = heap.count\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = heap.isEmpty\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
heap.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rsuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.kt/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nvar minHeap = PriorityQueue<Int>()\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = maxHeap.peek() // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nval size = maxHeap.size\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
heap.rb\n
heap.zig\n
\u53ef\u89c6\u5316\u8fd0\u884c https://pythontutor.com/render.html#code=import%20heapq%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20min_heap,%20flag%20%3D%20%5B%5D,%201%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20max_heap,%20flag%20%3D%20%5B%5D,%20-1%0A%20%20%20%20%0A%20%20%20%20%23%20Python%20%E7%9A%84%20heapq%20%E6%A8%A1%E5%9D%97%E9%BB%98%E8%AE%A4%E5%AE%9E%E7%8E%B0%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E8%80%83%E8%99%91%E5%B0%86%E2%80%9C%E5%85%83%E7%B4%A0%E5%8F%96%E8%B4%9F%E2%80%9D%E5%90%8E%E5%86%8D%E5%85%A5%E5%A0%86%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%B0%86%E5%A4%A7%E5%B0%8F%E5%85%B3%E7%B3%BB%E9%A2%A0%E5%80%92%EF%BC%8C%E4%BB%8E%E8%80%8C%E5%AE%9E%E7%8E%B0%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E5%9C%A8%E6%9C%AC%E7%A4%BA%E4%BE%8B%E4%B8%AD%EF%BC%8Cflag%20%3D%201%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%B0%8F%E9%A1%B6%E5%A0%86%EF%BC%8Cflag%20%3D%20-1%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%201%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%203%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%202%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%205%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%204%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%0A%20%20%20%20peek%20%3D%20flag%20*%20max_heap%5B0%5D%20%23%205%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%0A%20%20%20%20%23%20%E5%87%BA%E5%A0%86%E5%85%83%E7%B4%A0%E4%BC%9A%E5%BD%A2%E6%88%90%E4%B8%80%E4%B8%AA%E4%BB%8E%E5%A4%A7%E5%88%B0%E5%B0%8F%E7%9A%84%E5%BA%8F%E5%88%97%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%205%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%204%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%203%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%202%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%201%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E5%A4%A7%E5%B0%8F%0A%20%20%20%20size%20%3D%20len%28max_heap%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%A0%86%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20not%20max_heap%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%BE%93%E5%85%A5%E5%88%97%E8%A1%A8%E5%B9%B6%E5%BB%BA%E5%A0%86%0A%20%20%20%20min_heap%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20heapq.heapify%28min_heap%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_heap/heap/#812-implementation-of-heaps","title":"8.1.2 \u00a0 Implementation of heaps","text":"The following implementation is of a max heap. To convert it into a min heap, simply invert all size logic comparisons (for example, replace \\(\\geq\\) with \\(\\leq\\)). Interested readers are encouraged to implement it on their own.
"},{"location":"chapter_heap/heap/#1-storage-and-representation-of-heaps","title":"1. \u00a0 Storage and representation of heaps","text":"As mentioned in the \"Binary Trees\" section, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, we will use arrays to store heaps.
When using an array to represent a binary tree, elements represent node values, and indexes represent node positions in the binary tree. Node pointers are implemented through an index mapping formula.
As shown in the Figure 8-2 , given an index \\(i\\), the index of its left child is \\(2i + 1\\), the index of its right child is \\(2i + 2\\), and the index of its parent is \\((i - 1) / 2\\) (floor division). When the index is out of bounds, it signifies a null node or the node does not exist.
Figure 8-2 \u00a0 Representation and storage of heaps
We can encapsulate the index mapping formula into functions for convenient later use:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef left(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\ndef right(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n
my_heap.cpp/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.java/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.cs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint Parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.go/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n // \u5411\u4e0b\u6574\u9664\n return (i - 1) / 2\n}\n
my_heap.swift/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.js/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#left(i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#right(i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n#parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.ts/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.dart/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint _parent(int i) {\n return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n return (i - 1) / 2;\n}\n
my_heap.kt/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun left(i: Int): Int {\n return 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun right(i: Int): Int {\n return 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rb[class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
my_heap.zig// \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\nfn left(i: usize) usize {\n return 2 * i + 1;\n}\n\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\nfn right(i: usize) usize {\n return 2 * i + 2;\n}\n\n// \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\nfn parent(i: usize) usize {\n // return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n return @divFloor(i - 1, 2);\n}\n
"},{"location":"chapter_heap/heap/#2-accessing-the-top-element-of-the-heap","title":"2. \u00a0 Accessing the top element of the heap","text":"The top element of the heap is the root node of the binary tree, which is also the first element of the list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n return self.max_heap[0]\n
my_heap.cpp/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap[0];\n}\n
my_heap.java/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap.get(0);\n}\n
my_heap.cs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n return maxHeap[0];\n}\n
my_heap.go/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n return h.data[0]\n}\n
my_heap.swift/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n maxHeap[0]\n}\n
my_heap.js/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n return this.#maxHeap[0];\n}\n
my_heap.ts/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n return this.maxHeap[0];\n}\n
my_heap.dart/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return _maxHeap[0];\n}\n
my_heap.rs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n self.max_heap.first().copied()\n}\n
my_heap.c/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n return maxHeap->data[0];\n}\n
my_heap.kt/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfun peek(): Int {\n return maxHeap[0]\n}\n
my_heap.rb[class]{MaxHeap}-[func]{peek}\n
my_heap.zig// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\n return self.max_heap.?.items[0];\n} \n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#3-inserting-an-element-into-the-heap","title":"3. \u00a0 Inserting an element into the heap","text":"Given an element val
, we first add it to the bottom of the heap. After addition, since val
may be larger than other elements in the heap, the heap's integrity might be compromised, thus it's necessary to repair the path from the inserted node to the root node. This operation is called \"heapifying\".
Considering starting from the node inserted, perform heapify from bottom to top. As shown in the Figure 8-3 , we compare the value of the inserted node with its parent node, and if the inserted node is larger, we swap them. Then continue this operation, repairing each node in the heap from bottom to top until passing the root node or encountering a node that does not need to be swapped.
<1><2><3><4><5><6><7><8><9>Figure 8-3 \u00a0 Steps of element insertion into the heap
Given a total of \\(n\\) nodes, the height of the tree is \\(O(\\log n)\\). Hence, the loop iterations for the heapify operation are at most \\(O(\\log n)\\), making the time complexity of the element insertion operation \\(O(\\log n)\\). The code is as shown:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef push(self, val: int):\n \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n # \u6dfb\u52a0\u8282\u70b9\n self.max_heap.append(val)\n # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n while True:\n # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p = self.parent(i)\n # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p)\n # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n
my_heap.cpp/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.push_back(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap[i], maxHeap[p]);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.java/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.Add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = Parent(i);\n // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.go/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n // \u6dfb\u52a0\u8282\u70b9\n h.data = append(h.data, val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n for true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p := h.parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.swift/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.append(val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n var i = i\n while true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = parent(i: i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || maxHeap[i] <= maxHeap[p] {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.js/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n // \u6dfb\u52a0\u8282\u70b9\n this.#maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.#parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n // \u6dfb\u52a0\u8282\u70b9\n this.maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n _maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = _parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n // \u6dfb\u52a0\u8282\u70b9\n self.max_heap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n loop {\n // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n if i == 0 {\n break;\n }\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = Self::parent(i);\n // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if self.max_heap[i] <= self.max_heap[p] {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.c/* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n if (maxHeap->size == MAX_SIZE) {\n printf(\"heap is full!\");\n return;\n }\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap->data[maxHeap->size] = val;\n maxHeap->size++;\n\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(maxHeap, i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.kt/* \u5143\u7d20\u5165\u5806 */\nfun push(value: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(value)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n val p = parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
my_heap.zig// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n // \u6dfb\u52a0\u8282\u70b9\n try self.max_heap.?.append(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n try self.siftUp(self.size() - 1);\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n var p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#4-removing-the-top-element-from-the-heap","title":"4. \u00a0 Removing the top element from the heap","text":"The top element of the heap is the root node of the binary tree, that is, the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree would change, making it difficult to use heapify for repairs subsequently. To minimize changes in element indexes, we use the following steps.
As shown in the Figure 8-4 , the direction of \"heapify from top to bottom\" is opposite to \"heapify from bottom to top\". We compare the value of the root node with its two children and swap it with the largest child. Then repeat this operation until passing the leaf node or encountering a node that does not need to be swapped.
<1><2><3><4><5><6><7><8><9><10>Figure 8-4 \u00a0 Steps of removing the top element from the heap
Similar to the element insertion operation, the time complexity of the top element removal operation is also \\(O(\\log n)\\). The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef pop(self) -> int:\n \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n # \u5224\u7a7a\u5904\u7406\n if self.is_empty():\n raise IndexError(\"\u5806\u4e3a\u7a7a\")\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1)\n # \u5220\u9664\u8282\u70b9\n val = self.max_heap.pop()\n # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0)\n # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n\ndef sift_down(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l, r, ma = self.left(i), self.right(i), i\n if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n ma = l\n if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma)\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n
my_heap.cpp/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) {\n throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap[0], maxHeap[size() - 1]);\n // \u5220\u9664\u8282\u70b9\n maxHeap.pop_back();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n swap(maxHeap[i], maxHeap[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.java/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.remove(size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n ma = l;\n if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n // \u5224\u7a7a\u5904\u7406\n if (IsEmpty())\n throw new IndexOutOfRangeException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n Swap(0, Size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.Last();\n maxHeap.RemoveAt(Size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n SiftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = Left(i), r = Right(i), ma = i;\n if (l < Size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < Size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.go/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n // \u5224\u7a7a\u5904\u7406\n if h.isEmpty() {\n fmt.Println(\"error\")\n return nil\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n h.swap(0, h.size()-1)\n // \u5220\u9664\u8282\u70b9\n val := h.data[len(h.data)-1]\n h.data = h.data[:len(h.data)-1]\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n h.siftDown(0)\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n l, r, max := h.left(i), h.right(i), i\n if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n max = l\n }\n if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n max = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if max == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, max)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max\n }\n}\n
my_heap.swift/* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n // \u5224\u7a7a\u5904\u7406\n if isEmpty() {\n fatalError(\"\u5806\u4e3a\u7a7a\")\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(i: 0, j: size() - 1)\n // \u5220\u9664\u8282\u70b9\n let val = maxHeap.remove(at: size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(i: 0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = left(i: i)\n let r = right(i: i)\n var ma = i\n if l < size(), maxHeap[l] > maxHeap[ma] {\n ma = l\n }\n if r < size(), maxHeap[r] > maxHeap[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.js/* \u5143\u7d20\u51fa\u5806 */\npop() {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.#swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.#maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.#siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.#left(i),\n r = this.#right(i);\n let ma = i;\n if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new RangeError('Heap is empty.');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.left(i),\n r = this.right(i);\n let ma = i;\n if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n _swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = _maxHeap.removeLast();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = _left(i);\n int r = _right(i);\n int ma = i;\n if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n // \u5224\u7a7a\u5904\u7406\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n let val = self.max_heap.remove(self.size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n ma = l;\n }\n if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.c/* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty(maxHeap)) {\n printf(\"heap is empty!\");\n return INT_MAX;\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap, 0, size(maxHeap) - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap->data[maxHeap->size - 1];\n maxHeap->size--;\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(maxHeap, 0);\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n int l = left(maxHeap, i);\n int r = right(maxHeap, i);\n int max = i;\n if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n max = l;\n }\n if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n max = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (max == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, max);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max;\n }\n}\n
my_heap.kt/* \u5143\u7d20\u51fa\u5806 */\nfun pop(): Int {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u5220\u9664\u8282\u70b9\n val value = maxHeap.removeAt(size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return value\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
my_heap.zig// \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n // \u5224\u65ad\u5904\u7406\n if (self.isEmpty()) unreachable;\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n try self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n var val = self.max_heap.?.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n try self.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n var l = left(i);\n var r = right(i);\n var ma = i;\n if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\n if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#813-common-applications-of-heaps","title":"8.1.3 \u00a0 Common applications of heaps","text":"Q: Is the \"heap\" in data structures the same concept as the \"heap\" in memory management?
The two are not the same concept, even though they are both referred to as \"heap\". The heap in computer system memory is part of dynamic memory allocation, where the program can use it to store data during execution. The program can request a certain amount of heap memory to store complex structures like objects and arrays. When these data are no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, the management and usage of heap memory need to be more cautious, as improper use may lead to memory leaks and dangling pointers.
"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k problem","text":"Question
Given an unordered array nums
of length \\(n\\), return the largest \\(k\\) elements in the array.
For this problem, we will first introduce two straightforward solutions, then explain a more efficient heap-based method.
"},{"location":"chapter_heap/top_k/#831-method-1-iterative-selection","title":"8.3.1 \u00a0 Method 1: Iterative selection","text":"We can perform \\(k\\) rounds of iterations as shown in the Figure 8-6 , extracting the \\(1^{st}\\), \\(2^{nd}\\), \\(\\dots\\), \\(k^{th}\\) largest elements in each round, with a time complexity of \\(O(nk)\\).
This method is only suitable when \\(k \\ll n\\), as the time complexity approaches \\(O(n^2)\\) when \\(k\\) is close to \\(n\\), which is very time-consuming.
Figure 8-6 \u00a0 Iteratively finding the largest k elements
Tip
When \\(k = n\\), we can obtain a complete ordered sequence, which is equivalent to the \"selection sort\" algorithm.
"},{"location":"chapter_heap/top_k/#832-method-2-sorting","title":"8.3.2 \u00a0 Method 2: Sorting","text":"As shown in the Figure 8-7 , we can first sort the array nums
and then return the last \\(k\\) elements, with a time complexity of \\(O(n \\log n)\\).
Clearly, this method \"overachieves\" the task, as we only need to find the largest \\(k\\) elements, without the need to sort the other elements.
Figure 8-7 \u00a0 Sorting to find the largest k elements
"},{"location":"chapter_heap/top_k/#833-method-3-heap","title":"8.3.3 \u00a0 Method 3: Heap","text":"We can solve the Top-k problem more efficiently based on heaps, as shown in the following process.
Figure 8-8 \u00a0 Find the largest k elements based on heap
Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig top_k.pydef top_k_heap(nums: list[int], k: int) -> list[int]:\n \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n heap = []\n # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i in range(k):\n heapq.heappush(heap, nums[i])\n # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in range(k, len(nums)):\n # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap[0]:\n heapq.heappop(heap)\n heapq.heappush(heap, nums[i])\n return heap\n
top_k.cpp/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n priority_queue<int, vector<int>, greater<int>> heap;\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.push(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.size(); i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.top()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.java/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n Queue<Integer> heap = new PriorityQueue<Integer>();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.offer(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll();\n heap.offer(nums[i]);\n }\n }\n return heap;\n}\n
top_k.cs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n PriorityQueue<int, int> heap = new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.Enqueue(nums[i], nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.Length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.Peek()) {\n heap.Dequeue();\n heap.Enqueue(nums[i], nums[i]);\n }\n }\n return heap;\n}\n
top_k.go/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n h := &minHeap{}\n heap.Init(h)\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i := 0; i < k; i++ {\n heap.Push(h, nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i := k; i < len(nums); i++ {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > h.Top().(int) {\n heap.Pop(h)\n heap.Push(h, nums[i])\n }\n }\n return h\n}\n
top_k.swift/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n var heap = Heap(nums.prefix(k))\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in nums.indices.dropFirst(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap.min()! {\n _ = heap.removeMin()\n heap.insert(nums[i])\n }\n }\n return heap.unordered\n}\n
top_k.js/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.ts/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.dart/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n MinHeap heap = MinHeap(nums.sublist(0, k));\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.rs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n let mut heap = BinaryHeap::<Reverse<i32>>::new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for &num in nums.iter().take(k) {\n heap.push(Reverse(num));\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for &num in nums.iter().skip(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if num > heap.peek().unwrap().0 {\n heap.pop();\n heap.push(Reverse(num));\n }\n }\n heap\n}\n
top_k.c/* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n // \u5143\u7d20\u53d6\u53cd\n push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n int *empty = (int *)malloc(0);\n MaxHeap *maxHeap = newMaxHeap(empty, 0);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < sizeNums; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n int *res = getMinHeap(maxHeap);\n // \u91ca\u653e\u5185\u5b58\n delMaxHeap(maxHeap);\n return res;\n}\n
top_k.kt/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n val heap = PriorityQueue<Int>()\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (i in 0..<k) {\n heap.offer(nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (i in k..<nums.size) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll()\n heap.offer(nums[i])\n }\n }\n return heap\n}\n
top_k.rb[class]{}-[func]{top_k_heap}\n
top_k.zig[class]{}-[func]{topKHeap}\n
Code Visualization Full Screen >
A total of \\(n\\) rounds of heap insertions and deletions are performed, with the maximum heap size being \\(k\\), hence the time complexity is \\(O(n \\log k)\\). This method is very efficient; when \\(k\\) is small, the time complexity tends towards \\(O(n)\\); when \\(k\\) is large, the time complexity will not exceed \\(O(n \\log n)\\).
Additionally, this method is suitable for scenarios with dynamic data streams. By continuously adding data, we can maintain the elements within the heap, thereby achieving dynamic updates of the largest \\(k\\) elements.
"},{"location":"chapter_introduction/","title":"Chapter 1. \u00a0 Introduction to algorithms","text":"Abstract
A graceful maiden dances, intertwined with the data, her skirt swaying to the melody of algorithms.
She invites you to a dance, follow her steps, and enter the world of algorithms full of logic and beauty.
"},{"location":"chapter_introduction/#chapter-contents","title":"Chapter Contents","text":"When we hear the word \"algorithm,\" we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which can be seen everywhere in our daily lives.
Before formally discussing algorithms, there's an interesting fact worth sharing: you have already unconsciously learned many algorithms and have become accustomed to applying them in your daily life. Here, I will give a few specific examples to prove this point.
Example 1: Looking Up a Dictionary. In an English dictionary, words are listed alphabetically. Suppose we're searching for a word that starts with the letter \\(r\\). This is typically done in the following way:
1.
and 2.
until you find the page where the word starts with \\(r\\).Figure 1-1 \u00a0 Process of Looking Up a Dictionary
This essential skill for elementary students, looking up a dictionary, is actually the famous \"Binary Search\" algorithm. From a data structure perspective, we can consider the dictionary as a sorted \"array\"; from an algorithmic perspective, the series of actions taken to look up a word in the dictionary can be viewed as \"Binary Search.\"
Example 2: Organizing Playing Cards. When playing cards, we need to arrange the cards in our hand in ascending order, as shown in the following process.
2.
until all cards are in order.Figure 1-2 \u00a0 Playing cards sorting process
The above method of organizing playing cards is essentially the \"Insertion Sort\" algorithm, which is very efficient for small datasets. Many programming languages' sorting functions include the insertion sort.
Example 3: Making Change. Suppose we buy goods worth \\(69\\) yuan at a supermarket and give the cashier \\(100\\) yuan, then the cashier needs to give us \\(31\\) yuan in change. They would naturally complete the thought process as shown below.
Figure 1-3 \u00a0 Change making process
In the above steps, we make the best choice at each step (using the largest denomination possible), ultimately resulting in a feasible change-making plan. From the perspective of data structures and algorithms, this method is essentially a \"Greedy\" algorithm.
From cooking a meal to interstellar travel, almost all problem-solving involves algorithms. The advent of computers allows us to store data structures in memory and write code to call the CPU and GPU to execute algorithms. In this way, we can transfer real-life problems to computers, solving various complex issues more efficiently.
Tip
If concepts such as data structures, algorithms, arrays, and binary search still seem somewhat obsecure, I encourage you to continue reading. This book will gently guide you into the realm of understanding data structures and algorithms.
"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 Summary","text":"An \"algorithm\" is a set of instructions or steps to solve a specific problem within a finite amount of time. It has the following characteristics:
A \"data structure\" is a way of organizing and storing data in a computer, with the following design goals:
Designing data structures is a balancing act, often requiring trade-offs. If you want to improve in one aspect, you often need to compromise in another. Here are two examples:
As shown in the Figure 1-4 , data structures and algorithms are highly related and closely integrated, specifically in the following three aspects:
Figure 1-4 \u00a0 Relationship between data structures and algorithms
Data structures and algorithms can be likened to a set of building blocks, as illustrated in the Figure 1-5 . A building block set includes numerous pieces, accompanied by detailed assembly instructions. Following these instructions step by step allows us to construct an intricate block model.
Figure 1-5 \u00a0 Assembling blocks
The detailed correspondence between the two is shown in the Table 1-1 .
Table 1-1 \u00a0 Comparing data structures and algorithms to building blocks
Data Structures and Algorithms Building Blocks Input data Unassembled blocks Data structure Organization of blocks, including shape, size, connections, etc Algorithm A series of steps to assemble the blocks into the desired shape Output data Completed Block modelIt's worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations in multiple programming languages.
Conventional Abbreviation
In real-life discussions, we often refer to \"Data Structures and Algorithms\" simply as \"Algorithms\". For example, the well-known LeetCode algorithm problems actually test both data structure and algorithm knowledge.
"},{"location":"chapter_preface/","title":"Chapter 0. \u00a0 Preface","text":"Abstract
Algorithms are like a beautiful symphony, with each line of code flowing like a rhythm.
May this book ring softly in your mind, leaving a unique and profound melody.
"},{"location":"chapter_preface/#chapter-contents","title":"Chapter Contents","text":"This open-source project aims to create a free, and beginner-friendly crash course on data structures and algorithms.
If you are new to algorithms with limited exposure, or you have accumulated some experience in algorithms, but you only have a vague understanding of data structures and algorithms, and you are constantly jumping between \"yep\" and \"hmm\", then this book is for you!
If you have already accumulated a certain amount of problem-solving experience, and are familiar with most types of problems, then this book can help you review and organize your algorithm knowledge system. The repository's source code can be used as a \"problem-solving toolkit\" or an \"algorithm cheat sheet\".
If you are an algorithm expert, we look forward to receiving your valuable suggestions, or join us and collaborate.
Prerequisites
You should know how to write and read simple code in at least one programming language.
"},{"location":"chapter_preface/about_the_book/#012-content-structure","title":"0.1.2 \u00a0 Content structure","text":"The main content of the book is shown in the following figure.
Figure 0-1 \u00a0 Main content of the book
"},{"location":"chapter_preface/about_the_book/#013-acknowledgements","title":"0.1.3 \u00a0 Acknowledgements","text":"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.
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, 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.
Throughout the creation of this book, numerous individuals provided invaluable assistance, including but not limited to:
Throughout the writing journey, I delved into numerous textbooks and articles on data structures and algorithms. These works served as exemplary models, ensuring the accuracy and quality of this book's content. I extend my gratitude to all who preceded me for their invaluable contributions!
This book advocates a combination of hands-on and minds-on learning, inspired in this regard by \"Dive into Deep Learning\". I highly recommend this excellent book to all readers.
Heartfelt thanks to my parents, whose ongoing support and encouragement have allowed me to do this interesting work.
"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 How to read","text":"Tip
For the best reading experience, it is recommended that you read through this section.
"},{"location":"chapter_preface/suggestions/#021-writing-conventions","title":"0.2.1 \u00a0 Writing conventions","text":"None
to mean null
.\"\"\"Header comments for labeling functions, classes, test samples, etc\"\"\"\n\n# Comments for explaining details\n\n\"\"\"\nMultiline\ncomments\n\"\"\"\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
// Header comments for labeling functions, classes, test samples, etc\n\n// Comments for explaining details.\n\n// Multiline\n// comments\n
"},{"location":"chapter_preface/suggestions/#022-efficient-learning-via-animated-illustrations","title":"0.2.2 \u00a0 Efficient learning via animated illustrations","text":"Compared with text, videos and pictures have a higher density of information and are more structured, making them easier to understand. In this book, key and difficult concepts are mainly presented through animations and illustrations, with text serving as explanations and supplements.
When encountering content with animations or illustrations as shown in the Figure 0-2 , prioritize understanding the figure, with text as supplementary, integrating both for a comprehensive understanding.
Figure 0-2 \u00a0 Animated illustration example
"},{"location":"chapter_preface/suggestions/#023-deepen-understanding-through-coding-practice","title":"0.2.3 \u00a0 Deepen understanding through coding practice","text":"The source code of this book is hosted on the GitHub Repository. As shown in the Figure 0-3 , the source code comes with test examples and can be executed with just a single click.
If time permits, it's recommended to type out the code yourself. If pressed for time, at least read and run all the codes.
Compared to just reading code, writing code often yields more learning. Learning by doing is the real way to learn.
Figure 0-3 \u00a0 Running code example
Setting up to run the code involves three main steps.
Step 1: Install a local programming environment. Follow the tutorial in the appendix for installation, or skip this step if already installed.
Step 2: Clone or download the code repository. Visit the GitHub Repository.
If Git is installed, use the following command to clone the repository:
git clone https://github.com/krahets/hello-algo.git\n
Alternatively, you can also click the \"Download ZIP\" button at the location shown in the Figure 0-4 to directly download the code as a compressed ZIP file. Then, you can simply extract it locally.
Figure 0-4 \u00a0 Cloning repository and downloading code
Step 3: Run the source code. As shown in the Figure 0-5 , for the code block labeled with the file name at the top, we can find the corresponding source code file in the codes
folder of the repository. These files can be executed with a single click, which will help you save unnecessary debugging time and allow you to focus on learning.
Figure 0-5 \u00a0 Code block and corresponding source code file
"},{"location":"chapter_preface/suggestions/#024-learning-together-in-discussion","title":"0.2.4 \u00a0 Learning together in discussion","text":"While reading this book, please don't skip over the points that you didn't learn. Feel free to post your questions in the comment section. We will be happy to answer them and can usually respond within two days.
As illustrated in the Figure 0-6 , each chapter features a comment section at the bottom. I encourage you to pay attention to these comments. They not only expose you to others' encountered problems, aiding in identifying knowledge gaps and sparking deeper contemplation, but also invite you to generously contribute by answering fellow readers' inquiries, sharing insights, and fostering mutual improvement.
Figure 0-6 \u00a0 Comment section example
"},{"location":"chapter_preface/suggestions/#025-algorithm-learning-path","title":"0.2.5 \u00a0 Algorithm learning path","text":"Overall, the journey of mastering data structures and algorithms can be divided into three stages:
As shown in the Figure 0-7 , this book mainly covers \u201cStage 1,\u201d aiming to help you more efficiently embark on Stages 2 and 3.
Figure 0-7 \u00a0 Algorithm learning path
"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 Summary","text":"Abstract
A stack is like cats placed on top of each other, while a queue is like cats lined up one by one.
They represent the logical relationships of Last-In-First-Out (LIFO) and First-In-First-Out (FIFO), respectively.
"},{"location":"chapter_stack_and_queue/#chapter-contents","title":"Chapter Contents","text":"In a queue, we can only delete elements from the head or add elements to the tail. As shown in the following diagram, a \"double-ended queue (deque)\" offers more flexibility, allowing the addition or removal of elements at both the head and the tail.
Figure 5-7 \u00a0 Operations in double-ended queue
"},{"location":"chapter_stack_and_queue/deque/#531-common-operations-in-double-ended-queue","title":"5.3.1 \u00a0 Common operations in double-ended queue","text":"The common operations in a double-ended queue are listed below, and the names of specific methods depend on the programming language used.
Table 5-3 \u00a0 Efficiency of double-ended queue operations
Method Name Description Time ComplexitypushFirst()
Add an element to the head \\(O(1)\\) pushLast()
Add an element to the tail \\(O(1)\\) popFirst()
Remove the first element \\(O(1)\\) popLast()
Remove the last element \\(O(1)\\) peekFirst()
Access the first element \\(O(1)\\) peekLast()
Access the last element \\(O(1)\\) Similarly, we can directly use the double-ended queue classes implemented in programming languages:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig deque.pyfrom collections import deque\n\n# Initialize the deque\ndeque: deque[int] = deque()\n\n# Enqueue elements\ndeque.append(2) # Add to the tail\ndeque.append(5)\ndeque.append(4)\ndeque.appendleft(3) # Add to the head\ndeque.appendleft(1)\n\n# Access elements\nfront: int = deque[0] # The first element\nrear: int = deque[-1] # The last element\n\n# Dequeue elements\npop_front: int = deque.popleft() # The first element dequeued\npop_rear: int = deque.pop() # The last element dequeued\n\n# Get the length of the deque\nsize: int = len(deque)\n\n# Check if the deque is empty\nis_empty: bool = len(deque) == 0\n
deque.cpp/* Initialize the deque */\ndeque<int> deque;\n\n/* Enqueue elements */\ndeque.push_back(2); // Add to the tail\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to the head\ndeque.push_front(1);\n\n/* Access elements */\nint front = deque.front(); // The first element\nint back = deque.back(); // The last element\n\n/* Dequeue elements */\ndeque.pop_front(); // The first element dequeued\ndeque.pop_back(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.size();\n\n/* Check if the deque is empty */\nbool empty = deque.empty();\n
deque.java/* Initialize the deque */\nDeque<Integer> deque = new LinkedList<>();\n\n/* Enqueue elements */\ndeque.offerLast(2); // Add to the tail\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // Add to the head\ndeque.offerFirst(1);\n\n/* Access elements */\nint peekFirst = deque.peekFirst(); // The first element\nint peekLast = deque.peekLast(); // The last element\n\n/* Dequeue elements */\nint popFirst = deque.pollFirst(); // The first element dequeued\nint popLast = deque.pollLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.size();\n\n/* Check if the deque is empty */\nboolean isEmpty = deque.isEmpty();\n
deque.cs/* Initialize the deque */\n// In C#, LinkedList is used as a deque\nLinkedList<int> deque = new();\n\n/* Enqueue elements */\ndeque.AddLast(2); // Add to the tail\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // Add to the head\ndeque.AddFirst(1);\n\n/* Access elements */\nint peekFirst = deque.First.Value; // The first element\nint peekLast = deque.Last.Value; // The last element\n\n/* Dequeue elements */\ndeque.RemoveFirst(); // The first element dequeued\ndeque.RemoveLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.Count;\n\n/* Check if the deque is empty */\nbool isEmpty = deque.Count == 0;\n
deque_test.go/* Initialize the deque */\n// In Go, use list as a deque\ndeque := list.New()\n\n/* Enqueue elements */\ndeque.PushBack(2) // Add to the tail\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // Add to the head\ndeque.PushFront(1)\n\n/* Access elements */\nfront := deque.Front() // The first element\nrear := deque.Back() // The last element\n\n/* Dequeue elements */\ndeque.Remove(front) // The first element dequeued\ndeque.Remove(rear) // The last element dequeued\n\n/* Get the length of the deque */\nsize := deque.Len()\n\n/* Check if the deque is empty */\nisEmpty := deque.Len() == 0\n
deque.swift/* Initialize the deque */\n// Swift does not have a built-in deque class, so Array can be used as a deque\nvar deque: [Int] = []\n\n/* Enqueue elements */\ndeque.append(2) // Add to the tail\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // Add to the head\ndeque.insert(1, at: 0)\n\n/* Access elements */\nlet peekFirst = deque.first! // The first element\nlet peekLast = deque.last! // The last element\n\n/* Dequeue elements */\n// Using Array, popFirst has a complexity of O(n)\nlet popFirst = deque.removeFirst() // The first element dequeued\nlet popLast = deque.removeLast() // The last element dequeued\n\n/* Get the length of the deque */\nlet size = deque.count\n\n/* Check if the deque is empty */\nlet isEmpty = deque.isEmpty\n
deque.js/* Initialize the deque */\n// JavaScript does not have a built-in deque, so Array is used as a deque\nconst deque = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Note that unshift() has a time complexity of O(n) as it's an array\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst = deque[0]; // The first element\nconst peekLast = deque[deque.length - 1]; // The last element\n\n/* Dequeue elements */\n// Note that shift() has a time complexity of O(n) as it's an array\nconst popFront = deque.shift(); // The first element dequeued\nconst popBack = deque.pop(); // The last element dequeued\n\n/* Get the length of the deque */\nconst size = deque.length;\n\n/* Check if the deque is empty */\nconst isEmpty = size === 0;\n
deque.ts/* Initialize the deque */\n// TypeScript does not have a built-in deque, so Array is used as a deque\nconst deque: number[] = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Note that unshift() has a time complexity of O(n) as it's an array\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst: number = deque[0]; // The first element\nconst peekLast: number = deque[deque.length - 1]; // The last element\n\n/* Dequeue elements */\n// Note that shift() has a time complexity of O(n) as it's an array\nconst popFront: number = deque.shift() as number; // The first element dequeued\nconst popBack: number = deque.pop() as number; // The last element dequeued\n\n/* Get the length of the deque */\nconst size: number = deque.length;\n\n/* Check if the deque is empty */\nconst isEmpty: boolean = size === 0;\n
deque.dart/* Initialize the deque */\n// In Dart, Queue is defined as a deque\nQueue<int> deque = Queue<int>();\n\n/* Enqueue elements */\ndeque.addLast(2); // Add to the tail\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // Add to the head\ndeque.addFirst(1);\n\n/* Access elements */\nint peekFirst = deque.first; // The first element\nint peekLast = deque.last; // The last element\n\n/* Dequeue elements */\nint popFirst = deque.removeFirst(); // The first element dequeued\nint popLast = deque.removeLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.length;\n\n/* Check if the deque is empty */\nbool isEmpty = deque.isEmpty;\n
deque.rs/* Initialize the deque */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(2); // Add to the tail\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to the head\ndeque.push_front(1);\n\n/* Access elements */\nif let Some(front) = deque.front() { // The first element\n}\nif let Some(rear) = deque.back() { // The last element\n}\n\n/* Dequeue elements */\nif let Some(pop_front) = deque.pop_front() { // The first element dequeued\n}\nif let Some(pop_rear) = deque.pop_back() { // The last element dequeued\n}\n\n/* Get the length of the deque */\nlet size = deque.len();\n\n/* Check if the deque is empty */\nlet is_empty = deque.is_empty();\n
deque.c// C does not provide a built-in deque\n
deque.kt\n
deque.zig\n
Visualizing Code https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%0A%20%20%20%20deq%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E9%98%9F%0A%20%20%20%20deq.append%282%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E5%B0%BE%0A%20%20%20%20deq.append%285%29%0A%20%20%20%20deq.append%284%29%0A%20%20%20%20deq.appendleft%283%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E9%A6%96%0A%20%20%20%20deq.appendleft%281%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20front%20%3D%20deq%5B0%5D%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%20front%20%3D%22,%20front%29%0A%20%20%20%20rear%20%3D%20deq%5B-1%5D%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%20rear%20%3D%22,%20rear%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20pop_front%20%3D%20deq.popleft%28%29%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_front%20%3D%22,%20pop_front%29%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%20%20%20%20pop_rear%20%3D%20deq.pop%28%29%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_rear%20%3D%22,%20pop_rear%29%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E7%9A%84%E9%95%BF%E5%BA%A6%0A%20%20%20%20size%20%3D%20len%28deq%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E9%95%BF%E5%BA%A6%20size%20%3D%22,%20size%29%0A%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20len%28deq%29%20%3D%3D%200%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%20%3D%22,%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_stack_and_queue/deque/#532-implementing-a-double-ended-queue","title":"5.3.2 \u00a0 Implementing a double-ended queue *","text":"The implementation of a double-ended queue is similar to that of a regular queue, it can be based on either a linked list or an array as the underlying data structure.
"},{"location":"chapter_stack_and_queue/deque/#1-implementation-based-on-doubly-linked-list","title":"1. \u00a0 Implementation based on doubly linked list","text":"Recall from the previous section that we used a regular singly linked list to implement a queue, as it conveniently allows for deleting from the head (corresponding to the dequeue operation) and adding new elements after the tail (corresponding to the enqueue operation).
For a double-ended queue, both the head and the tail can perform enqueue and dequeue operations. In other words, a double-ended queue needs to implement operations in the opposite direction as well. For this, we use a \"doubly linked list\" as the underlying data structure of the double-ended queue.
As shown in the Figure 5-8 , we treat the head and tail nodes of the doubly linked list as the front and rear of the double-ended queue, respectively, and implement the functionality to add and remove nodes at both ends.
LinkedListDequepushLast()pushFirst()popLast()popFirst()Figure 5-8 \u00a0 Implementing Double-Ended Queue with Doubly Linked List for Enqueue and Dequeue Operations
The implementation code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_deque.pyclass ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n def __init__(self, val: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.val: int = val\n self.next: ListNode | None = None # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n self.prev: ListNode | None = None # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0 # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self.size() == 0\n\n def push(self, num: int, is_front: bool):\n \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n node = ListNode(num)\n # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if self.is_empty():\n self._front = self._rear = node\n # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n elif is_front:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self._front.prev = node\n node.next = self._front\n self._front = node # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self._rear.next = node\n node.prev = self._rear\n self._rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n self.push(num, True)\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n self.push(num, False)\n\n def pop(self, is_front: bool) -> int:\n \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front:\n val: int = self._front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n # \u5220\u9664\u5934\u8282\u70b9\n fnext: ListNode | None = self._front.next\n if fnext != None:\n fnext.prev = None\n self._front.next = None\n self._front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else:\n val: int = self._rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n # \u5220\u9664\u5c3e\u8282\u70b9\n rprev: ListNode | None = self._rear.prev\n if rprev != None:\n rprev.next = None\n self._rear.prev = None\n self._rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n return self.pop(True)\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n return self.pop(False)\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._rear.val\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n node = self._front\n res = [0] * self.size()\n for i in range(self.size()):\n res[i] = node.val\n node = node.next\n return res\n
linkedlist_deque.cpp/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private:\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n LinkedListDeque() : front(nullptr), rear(nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~LinkedListDeque() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n DoublyListNode *pre, *cur = front;\n while (cur != nullptr) {\n pre = cur;\n cur = cur->next;\n delete pre;\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int num, bool isFront) {\n DoublyListNode *node = new DoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front->prev = node;\n node->next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear->next = node;\n node->prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int pop(bool isFront) {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n DoublyListNode *fNext = front->next;\n if (fNext != nullptr) {\n fNext->prev = nullptr;\n front->next = nullptr;\n }\n delete front;\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n DoublyListNode *rPrev = rear->prev;\n if (rPrev != nullptr) {\n rPrev->next = nullptr;\n rear->prev = nullptr;\n }\n delete rear;\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return rear->val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n DoublyListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_deque.java/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(int val) {\n this.val = val;\n prev = next = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private void push(int num, boolean isFront) {\n ListNode node = new ListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private int pop(boolean isFront) {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode fNext = front.next;\n if (fNext != null) {\n fNext.prev = null;\n front.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode rPrev = rear.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return rear.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.cs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n public int val = val; // \u8282\u70b9\u503c\n public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void Push(int num, bool isFront) {\n ListNode node = new(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (IsEmpty()) {\n front = node;\n rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front!.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9 \n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear!.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n Push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n Push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? Pop(bool isFront) {\n if (IsEmpty())\n throw new Exception();\n int? val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = front?.next;\n if (fNext != null) {\n fNext.prev = null;\n front!.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = rear?.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear!.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int? PopFirst() {\n return Pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int? PopLast() {\n return Pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int? PeekFirst() {\n if (IsEmpty())\n throw new Exception();\n return front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int? PeekLast() {\n if (IsEmpty())\n throw new Exception();\n return rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int?[] ToArray() {\n ListNode? node = front;\n int?[] res = new int?[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node?.val;\n node = node?.next;\n }\n\n return res;\n }\n}\n
linkedlist_deque.go/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n return &linkedListDeque{\n data: list.New(),\n }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n return s.data\n}\n
linkedlist_deque.swift/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n init(val: Int) {\n self.val = val\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? // \u5934\u8282\u70b9 front\n private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n private var _size: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private func push(num: Int, isFront: Bool) {\n let node = ListNode(val: num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if isEmpty() {\n front = node\n rear = node\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if isFront {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _size += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n push(num: num, isFront: true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n push(num: num, isFront: false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private func pop(isFront: Bool) -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n let val: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if isFront {\n val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let fNext = front?.next\n if fNext != nil {\n fNext?.prev = nil\n front?.next = nil\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let rPrev = rear?.prev\n if rPrev != nil {\n rPrev?.next = nil\n rear?.prev = nil\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _size -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n pop(isFront: true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n pop(isFront: false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return rear!.val\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_deque.js/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val; // \u8282\u70b9\u503c\n\n constructor(val) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n #front; // \u5934\u8282\u70b9 front\n #rear; // \u5c3e\u8282\u70b9 rear\n #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n this.#queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.#rear.next = node;\n node.prev = this.#rear;\n this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.#front.prev = node;\n node.next = this.#front;\n this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp = this.#rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.#rear.prev = null;\n }\n this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp = this.#front.next;\n if (temp !== null) {\n temp.prev = null;\n this.#front.next = null;\n }\n this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n return this.#queSize === 0 ? null : this.#rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n return this.#queSize === 0 ? null : this.#front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print() {\n const arr = [];\n let temp = this.#front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.ts/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val: number; // \u8282\u70b9\u503c\n\n constructor(val: number) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private front: ListNode; // \u5934\u8282\u70b9 front\n private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.front = null;\n this.rear = null;\n this.queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.rear.next = node;\n node.prev = this.rear;\n this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.front.prev = node;\n node.next = this.front;\n this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp: ListNode = this.rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.rear.prev = null;\n }\n this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp: ListNode = this.front.next;\n if (temp !== null) {\n temp.prev = null;\n this.front.next = null;\n }\n this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n return this.queSize === 0 ? null : this.rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n return this.queSize === 0 ? null : this.front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print(): void {\n const arr: number[] = [];\n let temp: ListNode = this.front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.dart/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n late ListNode? _front; // \u5934\u8282\u70b9 _front\n late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n LinkedListDeque() {\n this._front = null;\n this._rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n int size() {\n return this._queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int _num, bool isFront) {\n final ListNode node = ListNode(_num);\n if (isEmpty()) {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n _front = _rear = node;\n } else if (isFront) {\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n _front!.prev = node;\n node.next = _front;\n _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n _rear!.next = node;\n node.prev = _rear;\n _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n push(_num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n push(_num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? pop(bool isFront) {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n if (isEmpty()) {\n return null;\n }\n final int val;\n if (isFront) {\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = _front!.next;\n if (fNext != null) {\n fNext.prev = null;\n _front!.next = null;\n }\n _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = _rear!.prev;\n if (rPrev != null) {\n rPrev.next = null;\n _rear!.prev = null;\n }\n _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int? popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int? popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int? peekFirst() {\n return _front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int? peekLast() {\n return _rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> res = [];\n for (int i = 0; i < _queSize; i++) {\n res.add(node!.val);\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.rs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n pub val: T, // \u8282\u70b9\u503c\n pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n Rc::new(RefCell::new(ListNode {\n val,\n next: None,\n prev: None,\n }))\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear\n que_size: usize, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n pub fn push(&mut self, num: T, is_front: bool) {\n let node = ListNode::new(num);\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n if is_front {\n match self.front.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.rear = Some(node.clone());\n self.front = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Some(old_front) => {\n old_front.borrow_mut().prev = Some(node.clone());\n node.borrow_mut().next = Some(old_front);\n self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n }\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n match self.rear.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.front = Some(node.clone());\n self.rear = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(node.clone());\n node.borrow_mut().prev = Some(old_rear);\n self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n }\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: T) {\n self.push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: T) {\n self.push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n pub fn pop(&mut self, is_front: bool) -> Option<T> {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n if self.is_empty() {\n return None;\n };\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n new_front.borrow_mut().prev.take();\n self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n self.rear.take().map(|old_rear| {\n match old_rear.borrow_mut().prev.take() {\n Some(new_rear) => {\n new_rear.borrow_mut().next.take();\n self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n None => {\n self.front.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n })\n }\n }\n\n /* \u961f\u9996\u51fa\u961f */\n pub fn pop_first(&mut self) -> Option<T> {\n return self.pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n pub fn pop_last(&mut self) -> Option<T> {\n return self.pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.rear.as_ref()\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_deque.c/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n new->val = num;\n new->next = NULL;\n new->prev = NULL;\n return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n deque->front = NULL;\n deque->rear = NULL;\n deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n DoublyListNode *tmp = deque->front;\n deque->front = deque->front->next;\n free(tmp);\n }\n // \u91ca\u653e deque \u7ed3\u6784\u4f53\n free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n DoublyListNode *node = newDoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n if (empty(deque)) {\n deque->front = deque->rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n deque->front->prev = node;\n node->next = deque->front;\n deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n deque->rear->next = node;\n node->prev = deque->rear;\n deque->rear = node;\n }\n deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n assert(size(deque) && deque->front);\n return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n assert(size(deque) && deque->rear);\n return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n if (empty(deque))\n return -1;\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n DoublyListNode *fNext = deque->front->next;\n if (fNext) {\n fNext->prev = NULL;\n deque->front->next = NULL;\n delDoublyListNode(deque->front);\n }\n deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n DoublyListNode *rPrev = deque->rear->prev;\n if (rPrev) {\n rPrev->next = NULL;\n deque->rear->prev = NULL;\n delDoublyListNode(deque->rear);\n }\n deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n int *arr = malloc(sizeof(int) * deque->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n DoublyListNode *node;\n for (i = 0, node = deque->front; i < deque->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, deque->queSize);\n free(arr);\n}\n
linkedlist_deque.kt/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(var value: Int) {\n // \u8282\u70b9\u503c\n var next: ListNode? = null // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n var prev: ListNode? = null // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? = null // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private var rear: ListNode? = null\n private var queSize = 0 // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n fun push(num: Int, isFront: Boolean) {\n val node = ListNode(num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty()) {\n rear = node\n front = rear\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++ // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n fun pushFirst(num: Int) {\n push(num, true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n fun pushLast(num: Int) {\n push(num, false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n fun pop(isFront: Boolean): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n\n val value: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n value = front!!.value // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n val fNext = front!!.next\n if (fNext != null) {\n fNext.prev = null\n front!!.next = null\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n value = rear!!.value // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n val rPrev = rear!!.prev\n if (rPrev != null) {\n rPrev.next = null\n rear!!.prev = null\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize-- // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return value\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fun popFirst(): Int {\n return pop(true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fun popLast(): Int {\n return pop(false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) {\n throw IndexOutOfBoundsException()\n\n }\n return front!!.value\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return rear!!.value\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!.value\n node = node.next\n }\n return res\n }\n}\n
linkedlist_deque.rb[class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
linkedlist_deque.zig// \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = undefined, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n prev: ?*Self = null, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n\n // Initialize a list node with specific value\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u5165\u961f\u64cd\u4f5c\n pub fn push(self: *Self, num: T, is_front: bool) !void {\n var node = try self.mem_allocator.create(ListNode(T));\n node.init(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (self.isEmpty()) {\n self.front = node;\n self.rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (is_front) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self.front.?.prev = node;\n node.next = self.front;\n self.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self.rear.?.next = node;\n node.prev = self.rear;\n self.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n } \n\n // \u961f\u9996\u5165\u961f\n pub fn pushFirst(self: *Self, num: T) !void {\n try self.push(num, true);\n } \n\n // \u961f\u5c3e\u5165\u961f\n pub fn pushLast(self: *Self, num: T) !void {\n try self.push(num, false);\n } \n\n // \u51fa\u961f\u64cd\u4f5c\n pub fn pop(self: *Self, is_front: bool) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n var val: T = undefined;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (is_front) {\n val = self.front.?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n var fNext = self.front.?.next;\n if (fNext != null) {\n fNext.?.prev = null;\n self.front.?.next = null;\n }\n self.front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = self.rear.?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n var rPrev = self.rear.?.prev;\n if (rPrev != null) {\n rPrev.?.next = null;\n self.rear.?.prev = null;\n }\n self.rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n } \n\n // \u961f\u9996\u51fa\u961f\n pub fn popFirst(self: *Self) T {\n return self.pop(true);\n } \n\n // \u961f\u5c3e\u51fa\u961f\n pub fn popLast(self: *Self) T {\n return self.pop(false);\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peekFirst(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\n pub fn peekLast(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.rear.?.val;\n }\n\n // \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/deque/#2-implementation-based-on-array","title":"2. \u00a0 Implementation based on array","text":"As shown in the Figure 5-9 , similar to implementing a queue with an array, we can also use a circular array to implement a double-ended queue.
ArrayDequepushLast()pushFirst()popLast()popFirst()Figure 5-9 \u00a0 Implementing Double-Ended Queue with Array for Enqueue and Dequeue Operations
The implementation only needs to add methods for \"front enqueue\" and \"rear dequeue\":
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_deque.pyclass ArrayDeque:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self, capacity: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * capacity\n self._front: int = 0\n self._size: int = 0\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def index(self, i: int) -> int:\n \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + self.capacity()) % self.capacity()\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self._front = self.index(self._front - 1)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self._nums[self._front] = num\n self._size += 1\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear = self.index(self._front + self._size)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n num = self.peek_first()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self._front = self.index(self._front + 1)\n self._size -= 1\n return num\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n num = self.peek_last()\n self._size -= 1\n return num\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = self.index(self._front + self._size - 1)\n return self._nums[last]\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res = []\n for i in range(self._size):\n res.append(self._nums[self.index(self._front + i)])\n return res\n
array_deque.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private:\n vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n nums.resize(capacity);\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return nums.size();\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> res(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n};\n
array_deque.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n this.nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int Index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + Capacity()) % Capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = Index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = Index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int PopFirst() {\n int num = PeekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = Index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int PopLast() {\n int num = PeekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int PeekFirst() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int PeekLast() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = Index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[Index(j)];\n }\n return res;\n }\n}\n
array_deque.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n return &arrayDeque{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n q.front = q.index(q.front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[q.front] = num\n q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear := q.index(q.front + q.queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n num := q.peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n q.front = q.index(q.front + 1)\n q.queSize--\n return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n num := q.peekLast()\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n if q.isEmpty() {\n return nil\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last := q.index(q.front + q.queSize - 1)\n return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res := make([]int, q.queSize)\n for i, j := 0, q.front; i < q.queSize; i++ {\n res[i] = q.nums[q.index(j)]\n j++\n }\n return res\n}\n
array_deque.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var _size: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(capacity: Int) {\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private func index(i: Int) -> Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(i: front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n _size += 1\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = index(i: front + size())\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n let num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(i: front + 1)\n _size -= 1\n return num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n let num = peekLast()\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = index(i: front + size() - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n (front ..< front + size()).map { nums[index(i: $0)] }\n }\n}\n
array_deque.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity) {\n this.#nums = new Array(capacity);\n this.#front = 0;\n this.#queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.#front = this.index(this.#front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.#nums[this.#front] = num;\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear = this.index(this.#front + this.#queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst() {\n const num = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.#front = this.index(this.#front + 1);\n this.#queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast() {\n const num = this.peekLast();\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.#nums[this.#front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.#front + this.#queSize - 1);\n return this.#nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res = [];\n for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n res[i] = this.#nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = 0;\n this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i: number): number {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.front = this.index(this.front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.nums[this.front] = num;\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear: number = this.index(this.front + this.queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst(): number {\n const num: number = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.front = this.index(this.front + 1);\n this.queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast(): number {\n const num: number = this.peekLast();\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.nums[this.front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.front + this.queSize - 1);\n return this.nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res: number[] = [];\n for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n res[i] = this.nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n this._nums = List.filled(capacity, 0);\n this._front = this._queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n _front = index(_front - 1);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n _nums[_front] = _num;\n _queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(_front + _queSize);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int _num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n _front = index(_front + 1);\n _queSize--;\n return _num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int _num = peekLast();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(_front + _queSize - 1);\n return _nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: usize, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl ArrayDeque {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n Self {\n nums: vec![0; capacity],\n front: 0,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n self.nums.len()\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n self.que_size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n fn index(&self, i: i32) -> usize {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self.front = self.index(self.front as i32 - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self.nums[self.front] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = self.index(self.front as i32 + self.que_size as i32);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fn pop_first(&mut self) -> i32 {\n let num = self.peek_first();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self.front = self.index(self.front as i32 + 1);\n self.que_size -= 1;\n num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fn pop_last(&mut self) -> i32 {\n let num = self.peek_last();\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek_first(&self) -> i32 {\n if self.is_empty() {\n panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n };\n self.nums[self.front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fn peek_last(&self) -> i32 {\n if self.is_empty() {\n panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n };\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n self.nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fn to_array(&self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut res = vec![0; self.que_size];\n let mut j = self.front;\n for i in 0..self.que_size {\n res[i] = self.nums[self.index(j as i32)];\n j += 1;\n }\n res\n }\n}\n
array_deque.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n // \u521d\u59cb\u5316\u6570\u7ec4\n deque->queCapacity = capacity;\n deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n deque->front = deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n free(deque->nums);\n free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n deque->front = dequeIndex(deque, deque->front - 1);\n // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n deque->nums[deque->front] = num;\n deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = dequeIndex(deque, deque->front + deque->queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n deque->nums[rear] = num;\n deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n int num = peekFirst(deque);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n deque->front = dequeIndex(deque, deque->front + 1);\n deque->queSize--;\n return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n int num = peekLast(deque);\n deque->queSize--;\n return num;\n}\n
array_deque.kt/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque(capacity: Int) {\n private var nums = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private fun index(i: Int): Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n fun pushFirst(num: Int) {\n if (queSize == capacity()) {\n println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n queSize++\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n fun pushLast(num: Int) {\n if (queSize == capacity()) {\n println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n val rear = index(front + queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fun popFirst(): Int {\n val num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1)\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun popLast(): Int {\n val num = peekLast()\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n val last = index(front + queSize - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fun toArray(): IntArray {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i < queSize) {\n res[i] = nums[index(j)]\n i++\n j++\n }\n return res\n }\n}\n
array_deque.rb[class]{ArrayDeque}-[func]{}\n
array_deque.zig[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533-applications-of-double-ended-queue","title":"5.3.3 \u00a0 Applications of double-ended queue","text":"The double-ended queue combines the logic of both stacks and queues, thus, it can implement all their respective use cases while offering greater flexibility.
We know that software's \"undo\" feature is typically implemented using a stack: the system pushes
each change operation onto the stack and then pops
to implement undoing. However, considering the limitations of system resources, software often restricts the number of undo steps (for example, only allowing the last 50 steps). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (the front of the queue). But a regular stack cannot perform this function, where a double-ended queue becomes necessary. Note that the core logic of \"undo\" still follows the Last-In-First-Out principle of a stack, but a double-ended queue can more flexibly implement some additional logic.
\"Queue\" is a linear data structure that follows the First-In-First-Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers join the queue at the rear, and the person at the front leaves the queue first.
As shown in the Figure 5-4 , we call the front of the queue the \"head\" and the back the \"tail.\" The operation of adding elements to the rear of the queue is termed \"enqueue,\" and the operation of removing elements from the front is termed \"dequeue.\"
Figure 5-4 \u00a0 Queue's first-in-first-out rule
"},{"location":"chapter_stack_and_queue/queue/#521-common-operations-on-queue","title":"5.2.1 \u00a0 Common operations on queue","text":"The common operations on a queue are shown in the Table 5-2 . Note that method names may vary across different programming languages. Here, we use the same naming convention as that used for stacks.
Table 5-2 \u00a0 Efficiency of queue operations
Method Name Description Time Complexitypush()
Enqueue an element, add it to the tail \\(O(1)\\) pop()
Dequeue the head element \\(O(1)\\) peek()
Access the head element \\(O(1)\\) We can directly use the ready-made queue classes in programming languages:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig queue.pyfrom collections import deque\n\n# Initialize the queue\n# In Python, we generally use the deque class as a queue\n# Although queue.Queue() is a pure queue class, it's not very user-friendly, so it's not recommended\nque: deque[int] = deque()\n\n# Enqueue elements\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# Access the first element\nfront: int = que[0]\n\n# Dequeue an element\npop: int = que.popleft()\n\n# Get the length of the queue\nsize: int = len(que)\n\n# Check if the queue is empty\nis_empty: bool = len(que) == 0\n
queue.cpp/* Initialize the queue */\nqueue<int> queue;\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element*/\nint front = queue.front();\n\n/* Dequeue an element */\nqueue.pop();\n\n/* Get the length of the queue */\nint size = queue.size();\n\n/* Check if the queue is empty */\nbool empty = queue.empty();\n
queue.java/* Initialize the queue */\nQueue<Integer> queue = new LinkedList<>();\n\n/* Enqueue elements */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* Access the first element */\nint peek = queue.peek();\n\n/* Dequeue an element */\nint pop = queue.poll();\n\n/* Get the length of the queue */\nint size = queue.size();\n\n/* Check if the queue is empty */\nboolean isEmpty = queue.isEmpty();\n
queue.cs/* Initialize the queue */\nQueue<int> queue = new();\n\n/* Enqueue elements */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* Access the first element */\nint peek = queue.Peek();\n\n/* Dequeue an element */\nint pop = queue.Dequeue();\n\n/* Get the length of the queue */\nint size = queue.Count;\n\n/* Check if the queue is empty */\nbool isEmpty = queue.Count == 0;\n
queue_test.go/* Initialize the queue */\n// In Go, use list as a queue\nqueue := list.New()\n\n/* Enqueue elements */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* Access the first element */\npeek := queue.Front()\n\n/* Dequeue an element */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* Get the length of the queue */\nsize := queue.Len()\n\n/* Check if the queue is empty */\nisEmpty := queue.Len() == 0\n
queue.swift/* Initialize the queue */\n// Swift does not have a built-in queue class, so Array can be used as a queue\nvar queue: [Int] = []\n\n/* Enqueue elements */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* Access the first element */\nlet peek = queue.first!\n\n/* Dequeue an element */\n// Since it's an array, removeFirst has a complexity of O(n)\nlet pool = queue.removeFirst()\n\n/* Get the length of the queue */\nlet size = queue.count\n\n/* Check if the queue is empty */\nlet isEmpty = queue.isEmpty\n
queue.js/* Initialize the queue */\n// JavaScript does not have a built-in queue, so Array can be used as a queue\nconst queue = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element */\nconst peek = queue[0];\n\n/* Dequeue an element */\n// Since the underlying structure is an array, shift() method has a time complexity of O(n)\nconst pop = queue.shift();\n\n/* Get the length of the queue */\nconst size = queue.length;\n\n/* Check if the queue is empty */\nconst empty = queue.length === 0;\n
queue.ts/* Initialize the queue */\n// TypeScript does not have a built-in queue, so Array can be used as a queue \nconst queue: number[] = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element */\nconst peek = queue[0];\n\n/* Dequeue an element */\n// Since the underlying structure is an array, shift() method has a time complexity of O(n)\nconst pop = queue.shift();\n\n/* Get the length of the queue */\nconst size = queue.length;\n\n/* Check if the queue is empty */\nconst empty = queue.length === 0;\n
queue.dart/* Initialize the queue */\n// In Dart, the Queue class is a double-ended queue but can be used as a queue\nQueue<int> queue = Queue();\n\n/* Enqueue elements */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* Access the first element */\nint peek = queue.first;\n\n/* Dequeue an element */\nint pop = queue.removeFirst();\n\n/* Get the length of the queue */\nint size = queue.length;\n\n/* Check if the queue is empty */\nbool isEmpty = queue.isEmpty;\n
queue.rs/* Initialize the double-ended queue */\n// In Rust, use a double-ended queue as a regular queue\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* Access the first element */\nif let Some(front) = deque.front() {\n}\n\n/* Dequeue an element */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* Get the length of the queue */\nlet size = deque.len();\n\n/* Check if the queue is empty */\nlet is_empty = deque.is_empty();\n
queue.c// C does not provide a built-in queue\n
queue.kt\n
queue.zig\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/queue/#522-implementing-a-queue","title":"5.2.2 \u00a0 Implementing a queue","text":"To implement a queue, we need a data structure that allows adding elements at one end and removing them at the other. Both linked lists and arrays meet this requirement.
"},{"location":"chapter_stack_and_queue/queue/#1-implementation-based-on-a-linked-list","title":"1. \u00a0 Implementation based on a linked list","text":"As shown in the Figure 5-5 , we can consider the \"head node\" and \"tail node\" of a linked list as the \"front\" and \"rear\" of the queue, respectively. It is stipulated that nodes can only be added at the rear and removed at the front.
LinkedListQueuepush()pop()Figure 5-5 \u00a0 Implementing Queue with Linked List for Enqueue and Dequeue Operations
Below is the code for implementing a queue using a linked list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_queue.pyclass LinkedListQueue:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._front\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n node = ListNode(num)\n # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if self._front is None:\n self._front = node\n self._rear = node\n # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else:\n self._rear.next = node\n self._rear = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num = self.peek()\n # \u5220\u9664\u5934\u8282\u70b9\n self._front = self._front.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n queue = []\n temp = self._front\n while temp:\n queue.append(temp.val)\n temp = temp.next\n return queue\n
linkedlist_queue.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private:\n ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize;\n\n public:\n LinkedListQueue() {\n front = nullptr;\n rear = nullptr;\n queSize = 0;\n }\n\n ~LinkedListQueue() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(front);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode *node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == nullptr) {\n front = node;\n rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear->next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode *tmp = front;\n front = front->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (size() == 0)\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_queue.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else if (rear != null) {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (front == null)\n return [];\n\n ListNode? node = front;\n int[] res = new int[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n return &linkedListQueue{\n data: list.New(),\n }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n return s.data\n}\n
linkedlist_queue.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private var front: ListNode? // \u5934\u8282\u70b9\n private var rear: ListNode? // \u5c3e\u8282\u70b9\n private var _size: Int\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let node = ListNode(x: num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if front == nil {\n front = node\n rear = node\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear?.next = node\n rear = node\n }\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_queue.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n #front; // \u5934\u8282\u70b9 #front\n #rear; // \u5c3e\u8282\u70b9 #rear\n #queSize = 0;\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.#front) {\n this.#front = node;\n this.#rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.#rear.next = node;\n this.#rear = node;\n }\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n this.#front = this.#front.next;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#front;\n const res = new Array(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private front: ListNode | null; // \u5934\u8282\u70b9 front\n private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n private queSize: number = 0;\n\n constructor() {\n this.front = null;\n this.rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.front) {\n this.front = node;\n this.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.rear!.next = node;\n this.rear = node;\n }\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n // \u5220\u9664\u5934\u8282\u70b9\n this.front = this.front.next;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.front;\n const res = new Array<number>(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_queue.dart/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? _front; // \u5934\u8282\u70b9 _front\n ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n LinkedListQueue() {\n _front = null;\n _rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n final node = ListNode(_num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (_front == null) {\n _front = node;\n _rear = node;\n } else {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n _rear!.next = node;\n _rear = node;\n }\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n final int _num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n _front = _front!.next;\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (_queSize == 0) {\n throw Exception('\u961f\u5217\u4e3a\u7a7a');\n }\n return _front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> queue = [];\n while (node != null) {\n queue.add(node.val);\n node = node.next;\n }\n return queue;\n }\n}\n
linkedlist_queue.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear\n que_size: usize, // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f */\n pub fn push(&mut self, num: T) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let new_rear = ListNode::new(num);\n match self.rear.take() {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n None => {\n self.front = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n }\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n pub fn pop(&mut self) -> Option<T> {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n self.front = Some(new_front);\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1;\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_queue.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n ListNode *front, *rear;\n int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n queue->front = NULL;\n queue->rear = NULL;\n queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n while (queue->front != NULL) {\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n }\n // \u91ca\u653e queue \u7ed3\u6784\u4f53\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n ListNode *node = newListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (queue->front == NULL) {\n queue->front = node;\n queue->rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n queue->rear->next = node;\n queue->rear = node;\n }\n queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n assert(size(queue) && queue->front);\n return queue->front->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListQueue *queue) {\n int num = peek(queue);\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n queue->queSize--;\n return num;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n int *arr = malloc(sizeof(int) * queue->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n ListNode *node;\n for (i = 0, node = queue->front; i < queue->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, queue->queSize);\n free(arr);\n}\n
linkedlist_queue.kt/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue(\n // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private var front: ListNode? = null,\n private var rear: ListNode? = null,\n private var queSize: Int = 0\n) {\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u961f */\n fun push(num: Int) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n val node = ListNode(num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node\n rear = node\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear?.next = node\n rear = node\n }\n queSize++\n }\n\n /* \u51fa\u961f */\n fun pop(): Int {\n val num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return front!!.value\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!.value\n node = node.next\n }\n return res\n }\n}\n
linkedlist_queue.rb[class]{LinkedListQueue}-[func]{}\n
linkedlist_queue.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*inc.ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*inc.ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (self.front == null) {\n self.front = node;\n self.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n self.rear.?.next = node;\n self.rear = node;\n }\n self.que_size += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n self.front = self.front.?.next;\n self.que_size -= 1;\n return num;\n } \n\n // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/queue/#2-implementation-based-on-an-array","title":"2. \u00a0 Implementation based on an array","text":"Deleting the first element in an array has a time complexity of \\(O(n)\\), which would make the dequeue operation inefficient. However, this problem can be cleverly avoided as follows.
We use a variable front
to indicate the index of the front element and maintain a variable size
to record the queue's length. Define rear = front + size
, which points to the position immediately following the tail element.
With this design, the effective interval of elements in the array is [front, rear - 1]
. The implementation methods for various operations are shown in the Figure 5-6 .
rear
index and increase size
by 1.front
by 1 and decrease size
by 1.Both enqueue and dequeue operations only require a single operation, each with a time complexity of \\(O(1)\\).
ArrayQueuepush()pop()Figure 5-6 \u00a0 Implementing Queue with Array for Enqueue and Dequeue Operations
You might notice a problem: as enqueue and dequeue operations are continuously performed, both front
and rear
move to the right and will eventually reach the end of the array and can't move further. To resolve this, we can treat the array as a \"circular array\" where connecting the end of the array back to its beginning.
In a circular array, front
or rear
needs to loop back to the start of the array upon reaching the end. This cyclical pattern can be achieved with a \"modulo operation\" as shown in the code below:
class ArrayQueue:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self, size: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n self._front: int = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n self._size: int = 0 # \u961f\u5217\u957f\u5ea6\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n if self._size == self.capacity():\n raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear: int = (self._front + self._size) % self.capacity()\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num: int = self.peek()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self._front = (self._front + 1) % self.capacity()\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n res = [0] * self.size()\n j: int = self._front\n for i in range(self.size()):\n res[i] = self._nums[(j % self.capacity())]\n j += 1\n return res\n
array_queue.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private:\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n public:\n ArrayQueue(int capacity) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = new int[capacity];\n queCapacity = capacity;\n front = queSize = 0;\n }\n\n ~ArrayQueue() {\n delete[] nums;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return queCapacity;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n if (queSize == queCapacity) {\n cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % queCapacity;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> arr(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n arr[i] = nums[j % queCapacity];\n }\n return arr;\n }\n};\n
array_queue.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % capacity()];\n }\n return res;\n }\n}\n
array_queue.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % Capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % Capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % this.Capacity()];\n }\n return res;\n }\n}\n
array_queue.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n return &arrayQueue{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n if q.queSize == q.queCapacity {\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear := (q.front + q.queSize) % q.queCapacity\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n num := q.peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n q.front = (q.front + 1) % q.queCapacity\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n rear := (q.front + q.queSize)\n if rear >= q.queCapacity {\n rear %= q.queCapacity\n return append(q.nums[q.front:], q.nums[:rear]...)\n }\n return q.nums[q.front:rear]\n}\n
array_queue.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var _size: Int // \u961f\u5217\u957f\u5ea6\n\n init(capacity: Int) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n if size() == capacity() {\n print(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (front + size()) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n (front ..< front + size()).map { nums[$0 % capacity()] }\n }\n}\n
array_queue.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity) {\n this.#nums = new Array(capacity);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.#front + this.size) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.#front = (this.#front + 1) % this.capacity;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#nums[this.#front];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n arr[i] = this.#nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.front + this.queSize) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.front = (this.front + 1) % this.capacity;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.nums[this.front];\n }\n\n /* \u8fd4\u56de Array */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.front; i < this.size; i++, j++) {\n arr[i] = this.nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n ArrayQueue(int capacity) {\n _nums = List.filled(capacity, 0);\n _front = _queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capaCity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n if (_queSize == capaCity()) {\n throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (_front + _queSize) % capaCity();\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int _num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n _front = (_front + 1) % capaCity();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8fd4\u56de Array */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n final List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[j % capaCity()];\n }\n return res;\n }\n}\n
array_queue.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: i32, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: i32, // \u961f\u5217\u957f\u5ea6\n que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl ArrayQueue {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(capacity: i32) -> ArrayQueue {\n ArrayQueue {\n nums: vec![0; capacity as usize],\n front: 0,\n que_size: 0,\n que_capacity: capacity,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fn capacity(&self) -> i32 {\n self.que_capacity\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fn size(&self) -> i32 {\n self.que_size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u5165\u961f */\n fn push(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (self.front + self.que_size) % self.que_capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear as usize] = num;\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n fn pop(&mut self) -> i32 {\n let num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.que_capacity;\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek(&self) -> i32 {\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n self.nums[self.front as usize]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fn to_vector(&self) -> Vec<i32> {\n let cap = self.que_capacity;\n let mut j = self.front;\n let mut arr = vec![0; self.que_size as usize];\n for i in 0..self.que_size {\n arr[i as usize] = self.nums[(j % cap) as usize];\n j += 1;\n }\n arr\n }\n}\n
array_queue.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n // \u521d\u59cb\u5316\u6570\u7ec4\n queue->queCapacity = capacity;\n queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n queue->front = queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n free(queue->nums);\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n assert(size(queue) != 0);\n return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n if (size(queue) == capacity(queue)) {\n printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (queue->front + queue->queSize) % queue->queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n queue->nums[rear] = num;\n queue->queSize++;\n}\n\n/* \u51fa\u961f */\nint pop(ArrayQueue *queue) {\n int num = peek(queue);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n queue->front = (queue->front + 1) % queue->queCapacity;\n queue->queSize--;\n return num;\n}\n
array_queue.kt/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue(capacity: Int) {\n private val nums = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u961f\u5217\u957f\u5ea6\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u5165\u961f */\n fun push(num: Int) {\n if (queSize == capacity()) {\n println(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n val rear = (front + queSize) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u51fa\u961f */\n fun pop(): Int {\n val num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fun toArray(): IntArray {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i < queSize) {\n res[i] = nums[j % capacity()]\n i++\n j++\n }\n return res\n }\n}\n
array_queue.rb[class]{ArrayQueue}-[func]{}\n
array_queue.zig// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n nums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \n cap: usize = 0, // \u961f\u5217\u5bb9\u91cf\n front: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.cap = cap;\n self.nums = try self.mem_allocator.alloc(T, self.cap);\n @memset(self.nums, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.cap;\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.queSize;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.queSize == 0;\n }\n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n if (self.size() == self.capacity()) {\n std.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n var rear = (self.front + self.queSize) % self.capacity();\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n self.nums[rear] = num;\n self.queSize += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.capacity();\n self.queSize -= 1;\n return num;\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.nums[self.front];\n } \n\n // \u8fd4\u56de\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n var j: usize = self.front;\n while (i < self.size()) : ({ i += 1; j += 1; }) {\n res[i] = self.nums[j % self.capacity()];\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
The above implementation of the queue still has its limitations: its length is fixed. However, this issue is not difficult to resolve. We can replace the array with a dynamic array that can expand itself if needed. Interested readers can try to implement this themselves.
The comparison of the two implementations is consistent with that of the stack and is not repeated here.
"},{"location":"chapter_stack_and_queue/queue/#523-typical-applications-of-queue","title":"5.2.3 \u00a0 Typical applications of queue","text":"A \"Stack\" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).
We can compare a stack to a pile of plates on a table. To access the bottom plate, one must first remove the plates on top. By replacing the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.
As shown in the Figure 5-1 , we refer to the top of the pile of elements as the \"top of the stack\" and the bottom as the \"bottom of the stack.\" The operation of adding elements to the top of the stack is called \"push,\" and the operation of removing the top element is called \"pop.\"
Figure 5-1 \u00a0 Stack's last-in-first-out rule
"},{"location":"chapter_stack_and_queue/stack/#511-common-operations-on-stack","title":"5.1.1 \u00a0 Common operations on stack","text":"The common operations on a stack are shown in the Table 5-1 . The specific method names depend on the programming language used. Here, we use push()
, pop()
, and peek()
as examples.
Table 5-1 \u00a0 Efficiency of stack operations
Method Description Time Complexitypush()
Push an element onto the stack (add to the top) \\(O(1)\\) pop()
Pop the top element from the stack \\(O(1)\\) peek()
Access the top element of the stack \\(O(1)\\) Typically, we can directly use the stack class built into the programming language. However, some languages may not specifically provide a stack class. In these cases, we can use the language's \"array\" or \"linked list\" as a stack and ignore operations that are not related to stack logic in the program.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig stack.py# Initialize the stack\n# Python does not have a built-in stack class, so a list can be used as a stack\nstack: list[int] = []\n\n# Push elements onto the stack\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# Access the top element of the stack\npeek: int = stack[-1]\n\n# Pop an element from the stack\npop: int = stack.pop()\n\n# Get the length of the stack\nsize: int = len(stack)\n\n# Check if the stack is empty\nis_empty: bool = len(stack) == 0\n
stack.cpp/* Initialize the stack */\nstack<int> stack;\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nint top = stack.top();\n\n/* Pop an element from the stack */\nstack.pop(); // No return value\n\n/* Get the length of the stack */\nint size = stack.size();\n\n/* Check if the stack is empty */\nbool empty = stack.empty();\n
stack.java/* Initialize the stack */\nStack<Integer> stack = new Stack<>();\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nint peek = stack.peek();\n\n/* Pop an element from the stack */\nint pop = stack.pop();\n\n/* Get the length of the stack */\nint size = stack.size();\n\n/* Check if the stack is empty */\nboolean isEmpty = stack.isEmpty();\n
stack.cs/* Initialize the stack */\nStack<int> stack = new();\n\n/* Push elements onto the stack */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* Access the top element of the stack */\nint peek = stack.Peek();\n\n/* Pop an element from the stack */\nint pop = stack.Pop();\n\n/* Get the length of the stack */\nint size = stack.Count;\n\n/* Check if the stack is empty */\nbool isEmpty = stack.Count == 0;\n
stack_test.go/* Initialize the stack */\n// In Go, it is recommended to use a Slice as a stack\nvar stack []int\n\n/* Push elements onto the stack */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* Access the top element of the stack */\npeek := stack[len(stack)-1]\n\n/* Pop an element from the stack */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* Get the length of the stack */\nsize := len(stack)\n\n/* Check if the stack is empty */\nisEmpty := len(stack) == 0\n
stack.swift/* Initialize the stack */\n// Swift does not have a built-in stack class, so Array can be used as a stack\nvar stack: [Int] = []\n\n/* Push elements onto the stack */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* Access the top element of the stack */\nlet peek = stack.last!\n\n/* Pop an element from the stack */\nlet pop = stack.removeLast()\n\n/* Get the length of the stack */\nlet size = stack.count\n\n/* Check if the stack is empty */\nlet isEmpty = stack.isEmpty\n
stack.js/* Initialize the stack */\n// JavaScript does not have a built-in stack class, so Array can be used as a stack\nconst stack = [];\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nconst peek = stack[stack.length-1];\n\n/* Pop an element from the stack */\nconst pop = stack.pop();\n\n/* Get the length of the stack */\nconst size = stack.length;\n\n/* Check if the stack is empty */\nconst is_empty = stack.length === 0;\n
stack.ts/* Initialize the stack */\n// TypeScript does not have a built-in stack class, so Array can be used as a stack\nconst stack: number[] = [];\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nconst peek = stack[stack.length - 1];\n\n/* Pop an element from the stack */\nconst pop = stack.pop();\n\n/* Get the length of the stack */\nconst size = stack.length;\n\n/* Check if the stack is empty */\nconst is_empty = stack.length === 0;\n
stack.dart/* Initialize the stack */\n// Dart does not have a built-in stack class, so List can be used as a stack\nList<int> stack = [];\n\n/* Push elements onto the stack */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* Access the top element of the stack */\nint peek = stack.last;\n\n/* Pop an element from the stack */\nint pop = stack.removeLast();\n\n/* Get the length of the stack */\nint size = stack.length;\n\n/* Check if the stack is empty */\nbool isEmpty = stack.isEmpty;\n
stack.rs/* Initialize the stack */\n// Use Vec as a stack\nlet mut stack: Vec<i32> = Vec::new();\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nlet top = stack.last().unwrap();\n\n/* Pop an element from the stack */\nlet pop = stack.pop().unwrap();\n\n/* Get the length of the stack */\nlet size = stack.len();\n\n/* Check if the stack is empty */\nlet is_empty = stack.is_empty();\n
stack.c// C does not provide a built-in stack\n
stack.kt\n
stack.zig\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#512-implementing-a-stack","title":"5.1.2 \u00a0 Implementing a stack","text":"To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.
A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, therefore a stack can be seen as a restricted array or linked list. In other words, we can \"shield\" certain irrelevant operations of an array or linked list, aligning their external behavior with the characteristics of a stack.
"},{"location":"chapter_stack_and_queue/stack/#1-implementation-based-on-a-linked-list","title":"1. \u00a0 Implementation based on a linked list","text":"When implementing a stack using a linked list, we can consider the head node of the list as the top of the stack and the tail node as the bottom of the stack.
As shown in the Figure 5-2 , for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as \"head insertion.\" For the pop operation, we just need to remove the head node from the list.
LinkedListStackpush()pop()Figure 5-2 \u00a0 Implementing Stack with Linked List for Push and Pop Operations
Below is an example code for implementing a stack based on a linked list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_stack.pyclass LinkedListStack:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._peek: ListNode | None = None\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._peek\n\n def push(self, val: int):\n \"\"\"\u5165\u6808\"\"\"\n node = ListNode(val)\n node.next = self._peek\n self._peek = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n num = self.peek()\n self._peek = self._peek.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._peek.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n arr = []\n node = self._peek\n while node:\n arr.append(node.val)\n node = node.next\n arr.reverse()\n return arr\n
linkedlist_stack.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private:\n ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize; // \u6808\u7684\u957f\u5ea6\n\n public:\n LinkedListStack() {\n stackTop = nullptr;\n stkSize = 0;\n }\n\n ~LinkedListStack() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(stackTop);\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n ListNode *node = new ListNode(num);\n node->next = stackTop;\n stackTop = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n int num = top();\n ListNode *tmp = stackTop;\n stackTop = stackTop->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stackTop->val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = stackTop;\n vector<int> res(size());\n for (int i = res.size() - 1; i >= 0; i--) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_stack.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n ListNode node = new ListNode(num);\n node.next = stackPeek;\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n int num = peek();\n stackPeek = stackPeek.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stackPeek.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = stackPeek;\n int[] res = new int[size()];\n for (int i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n ListNode node = new(num) {\n next = stackPeek\n };\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n int num = Peek();\n stackPeek = stackPeek!.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stackPeek!.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (stackPeek == null)\n return [];\n\n ListNode? node = stackPeek;\n int[] res = new int[Size()];\n for (int i = res.Length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n return &linkedListStack{\n data: list.New(),\n }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n return s.data\n}\n
linkedlist_stack.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var _size: Int // \u6808\u7684\u957f\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n let node = ListNode(x: num)\n node.next = _peek\n _peek = node\n _size += 1\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n _peek = _peek?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return _peek!.val\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = _peek\n var res = Array(repeating: 0, count: size())\n for i in res.indices.reversed() {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_stack.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.#stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n const node = new ListNode(num);\n node.next = this.#stackPeek;\n this.#stackPeek = node;\n this.#stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop() {\n const num = this.peek();\n this.#stackPeek = this.#stackPeek.next;\n this.#stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek() {\n if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#stackPeek;\n const res = new Array(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n const node = new ListNode(num);\n node.next = this.stackPeek;\n this.stackPeek = node;\n this.stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop(): number {\n const num = this.peek();\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n this.stackPeek = this.stackPeek.next;\n this.stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek(): number {\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.stackPeek;\n const res = new Array<number>(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_stack.dart/* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n LinkedListStack() {\n _stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stkSize == 0;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n final ListNode node = ListNode(_num);\n node.next = _stackPeek;\n _stackPeek = node;\n _stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n final int _num = peek();\n _stackPeek = _stackPeek!.next;\n _stkSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (_stackPeek == null) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stackPeek!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n List<int> toList() {\n ListNode? node = _stackPeek;\n List<int> list = [];\n while (node != null) {\n list.add(node.val);\n node = node.next;\n }\n list = list.reversed.toList();\n return list;\n }\n}\n
linkedlist_stack.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize, // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n pub fn new() -> Self {\n Self {\n stack_peek: None,\n stk_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.stk_size;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u6808 */\n pub fn push(&mut self, num: T) {\n let node = ListNode::new(num);\n node.borrow_mut().next = self.stack_peek.take();\n self.stack_peek = Some(node);\n self.stk_size += 1;\n }\n\n /* \u51fa\u6808 */\n pub fn pop(&mut self) -> Option<T> {\n self.stack_peek.take().map(|old_head| {\n match old_head.borrow_mut().next.take() {\n Some(new_head) => {\n self.stack_peek = Some(new_head);\n }\n None => {\n self.stack_peek = None;\n }\n }\n self.stk_size -= 1;\n Rc::try_unwrap(old_head).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.stack_peek.as_ref()\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.push(node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_stack.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int size; // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n LinkedListStack *s = malloc(sizeof(LinkedListStack));\n s->top = NULL;\n s->size = 0;\n return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n while (s->top) {\n ListNode *n = s->top->next;\n free(s->top);\n s->top = n;\n }\n free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n return size(s) == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n node->val = num; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n s->top = node; // \u66f4\u65b0\u6808\u9876\n s->size++; // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n if (s->size == 0) {\n printf(\"\u6808\u4e3a\u7a7a\\n\");\n return INT_MAX;\n }\n return s->top->val;\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n int val = peek(s);\n ListNode *tmp = s->top;\n s->top = s->top->next;\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n s->size--;\n return val;\n}\n
linkedlist_stack.kt/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack(\n private var stackPeek: ListNode? = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var stkSize: Int = 0 // \u6808\u7684\u957f\u5ea6\n) {\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fun size(): Int {\n return stkSize\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u6808 */\n fun push(num: Int) {\n val node = ListNode(num)\n node.next = stackPeek\n stackPeek = node\n stkSize++\n }\n\n /* \u51fa\u6808 */\n fun pop(): Int? {\n val num = peek()\n stackPeek = stackPeek?.next\n stkSize--;\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fun peek(): Int? {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stackPeek?.value\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = stackPeek\n val res = IntArray(size())\n for (i in res.size - 1 downTo 0) {\n res[i] = node?.value!!\n node = node.next\n }\n return res\n }\n}\n
linkedlist_stack.rb[class]{LinkedListStack}-[func]{}\n
linkedlist_stack.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack_top: ?*inc.ListNode(T) = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize = 0, // \u6808\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.stack_top = null;\n self.stk_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stk_size;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack_top.?.val;\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n node.next = self.stack_top;\n self.stack_top = node;\n self.stk_size += 1;\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.peek();\n self.stack_top = self.stack_top.?.next;\n self.stk_size -= 1;\n return num;\n } \n\n // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.stack_top;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[res.len - i - 1] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#2-implementation-based-on-an-array","title":"2. \u00a0 Implementation based on an array","text":"When implementing a stack using an array, we can consider the end of the array as the top of the stack. As shown in the Figure 5-3 , push and pop operations correspond to adding and removing elements at the end of the array, respectively, both with a time complexity of \\(O(1)\\).
ArrayStackpush()pop()Figure 5-3 \u00a0 Implementing Stack with Array for Push and Pop Operations
Since the elements to be pushed onto the stack may continuously increase, we can use a dynamic array, thus avoiding the need to handle array expansion ourselves. Here is an example code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_stack.pyclass ArrayStack:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._stack: list[int] = []\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return len(self._stack)\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._stack == []\n\n def push(self, item: int):\n \"\"\"\u5165\u6808\"\"\"\n self._stack.append(item)\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack.pop()\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack[-1]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n return self._stack\n
array_stack.cpp/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private:\n vector<int> stack;\n\n public:\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return stack.size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n stack.push_back(num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n int num = top();\n stack.pop_back();\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stack.back();\n }\n\n /* \u8fd4\u56de Vector */\n vector<int> toVector() {\n return stack;\n }\n};\n
array_stack.java/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private ArrayList<Integer> stack;\n\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = new ArrayList<>();\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n stack.add(num);\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.remove(size() - 1);\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.get(size() - 1);\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public Object[] toArray() {\n return stack.toArray();\n }\n}\n
array_stack.cs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n List<int> stack;\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stack.Count;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n stack.Add(num);\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n if (IsEmpty())\n throw new Exception();\n var val = Peek();\n stack.RemoveAt(Size() - 1);\n return val;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stack[Size() - 1];\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n return [.. stack];\n }\n}\n
array_stack.go/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n return &arrayStack{\n // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n data: make([]int, 0, 16),\n }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n val := s.peek()\n s.data = s.data[:len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n val := s.data[len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n return s.data\n}\n
array_stack.swift/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private var stack: [Int]\n\n init() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = []\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n stack.count\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n stack.isEmpty\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n stack.append(num)\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.removeLast()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.last!\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n stack\n }\n}\n
array_stack.js/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n #stack;\n constructor() {\n this.#stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n this.#stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack[this.#stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.#stack;\n }\n}\n
array_stack.ts/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private stack: number[];\n constructor() {\n this.stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n this.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack[this.stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.stack;\n }\n}\n
array_stack.dart/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n late List<int> _stack;\n ArrayStack() {\n _stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stack.isEmpty;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n _stack.add(_num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.removeLast();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.last;\n }\n\n /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() => _stack;\n}\n
array_stack.rs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n /* \u521d\u59cb\u5316\u6808 */\n fn new() -> ArrayStack<T> {\n ArrayStack::<T> {\n stack: Vec::<T>::new(),\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fn size(&self) -> usize {\n self.stack.len()\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.size() == 0\n }\n\n /* \u5165\u6808 */\n fn push(&mut self, num: T) {\n self.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n fn pop(&mut self) -> Option<T> {\n self.stack.pop()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fn peek(&self) -> Option<&T> {\n if self.is_empty() {\n panic!(\"\u6808\u4e3a\u7a7a\")\n };\n self.stack.last()\n }\n\n /* \u8fd4\u56de &Vec */\n fn to_array(&self) -> &Vec<T> {\n &self.stack\n }\n}\n
array_stack.c/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n int *data;\n int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n ArrayStack *stack = malloc(sizeof(ArrayStack));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n stack->data = malloc(sizeof(int) * MAX_SIZE);\n stack->size = 0;\n return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n free(stack->data);\n free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n if (stack->size == MAX_SIZE) {\n printf(\"\u6808\u5df2\u6ee1\\n\");\n return;\n }\n stack->data[stack->size] = num;\n stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"\u6808\u4e3a\u7a7a\\n\");\n return INT_MAX;\n }\n return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n int val = peek(stack);\n stack->size--;\n return val;\n}\n
array_stack.kt/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n private val stack = ArrayList<Int>()\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fun size(): Int {\n return stack.size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u6808 */\n fun push(num: Int) {\n stack.add(num)\n }\n\n /* \u51fa\u6808 */\n fun pop(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack.removeAt(size() - 1)\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack[size() - 1]\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): Array<Any> {\n return stack.toArray()\n }\n}\n
array_stack.rb[class]{ArrayStack}-[func]{}\n
array_stack.zig// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack: ?std.ArrayList(T) = null, \n\n // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) void {\n if (self.stack == null) {\n self.stack = std.ArrayList(T).init(allocator);\n }\n }\n\n // \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.stack == null) return;\n self.stack.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stack.?.items.len;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack.?.items[self.size() - 1];\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n try self.stack.?.append(num);\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.stack.?.pop();\n return num;\n } \n\n // \u8fd4\u56de ArrayList\n pub fn toList(self: *Self) std.ArrayList(T) {\n return self.stack.?;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#513-comparison-of-the-two-implementations","title":"5.1.3 \u00a0 Comparison of the two implementations","text":"Supported Operations
Both implementations support all the operations defined in a stack. The array implementation additionally supports random access, but this is beyond the scope of a stack definition and is generally not used.
Time Efficiency
In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation \\(O(n)\\).
In the linked list implementation, list expansion is very flexible, and there is no efficiency decrease issue as in array expansion. However, the push operation requires initializing a node object and modifying pointers, so its efficiency is relatively lower. If the elements being pushed are already node objects, then the initialization step can be skipped, improving efficiency.
Thus, when the elements for push and pop operations are basic data types like int
or double
, we can draw the following conclusions:
Space Efficiency
When initializing a list, the system allocates an \"initial capacity,\" which might exceed the actual need; moreover, the expansion mechanism usually increases capacity by a specific factor (like doubling), which may also exceed the actual need. Therefore, the array-based stack might waste some space.
However, since linked list nodes require extra space for storing pointers, the space occupied by linked list nodes is relatively larger.
In summary, we cannot simply determine which implementation is more memory-efficient. It requires analysis based on specific circumstances.
"},{"location":"chapter_stack_and_queue/stack/#514-typical-applications-of-stack","title":"5.1.4 \u00a0 Typical applications of stack","text":"Q: Is the browser's forward and backward functionality implemented with a doubly linked list?
A browser's forward and backward navigation is essentially a manifestation of the \"stack\" concept. When a user visits a new page, the page is added to the top of the stack; when they click the back button, the page is popped from the top of the stack. A double-ended queue (deque) can conveniently implement some additional operations, as mentioned in the \"Double-Ended Queue\" section.
Q: After popping from a stack, is it necessary to free the memory of the popped node?
If the popped node will still be used later, it's not necessary to free its memory. In languages like Java and Python that have automatic garbage collection, manual memory release is not necessary; in C and C++, manual memory release is required.
Q: A double-ended queue seems like two stacks joined together. What are its uses?
A double-ended queue, which is a combination of a stack and a queue or two stacks joined together, exhibits both stack and queue logic. Thus, it can implement all applications of stacks and queues while offering more flexibility.
Q: How exactly are undo and redo implemented?
Undo and redo operations are implemented using two stacks: Stack A for undo and Stack B for redo.
Abstract
The towering tree, full of vitality with its roots deep and leaves lush, branches spreading wide.
It vividly demonstrates the form of data divide-and-conquer.
"},{"location":"chapter_tree/#chapter-contents","title":"Chapter Contents","text":"Under the linked list representation, the storage unit of a binary tree is a node TreeNode
, with nodes connected by pointers. The basic operations of binary trees under the linked list representation were introduced in the previous section.
So, can we use an array to represent a binary tree? The answer is yes.
"},{"location":"chapter_tree/array_representation_of_tree/#731-representing-perfect-binary-trees","title":"7.3.1 \u00a0 Representing perfect binary trees","text":"Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.
Based on the characteristics of level-order traversal, we can deduce a \"mapping formula\" between the index of a parent node and its children: If a node's index is \\(i\\), then the index of its left child is \\(2i + 1\\) and the right child is \\(2i + 2\\). The Figure 7-12 shows the mapping relationship between the indices of various nodes.
Figure 7-12 \u00a0 Array representation of a perfect binary tree
The mapping formula plays a role similar to the node references (pointers) in linked lists. Given any node in the array, we can access its left (right) child node using the mapping formula.
"},{"location":"chapter_tree/array_representation_of_tree/#732-representing-any-binary-tree","title":"7.3.2 \u00a0 Representing any binary tree","text":"Perfect binary trees are a special case; there are often many None
values in the middle levels of a binary tree. Since the sequence of level-order traversal does not include these None
values, we cannot solely rely on this sequence to deduce the number and distribution of None
values. This means that multiple binary tree structures can match the same level-order traversal sequence.
As shown in the Figure 7-13 , given a non-perfect binary tree, the above method of array representation fails.
Figure 7-13 \u00a0 Level-order traversal sequence corresponds to multiple binary tree possibilities
To solve this problem, we can consider explicitly writing out all None
values in the level-order traversal sequence. As shown in the following figure, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:
# Array representation of a binary tree\n# Using None to represent empty slots\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* Array representation of a binary tree */\n// Using the maximum integer value INT_MAX to mark empty slots\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* Array representation of a binary tree */\n// Using the Integer wrapper class allows for using null to mark empty slots\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using an any type slice, allowing for nil to mark empty slots\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* Array representation of a binary tree */\n// Using optional Int (Int?) allows for using nil to mark empty slots\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using None to mark empty slots\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* Array representation of a binary tree */\n// Using the maximum int value to mark empty slots, therefore, node values must not be INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nval tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
\n
\n
Figure 7-14 \u00a0 Array representation of any type of binary tree
It's worth noting that complete binary trees are very suitable for array representation. Recalling the definition of a complete binary tree, None
appears only at the bottom level and towards the right, meaning all None
values definitely appear at the end of the level-order traversal sequence.
This means that when using an array to represent a complete binary tree, it's possible to omit storing all None
values, which is very convenient. The Figure 7-15 gives an example.
Figure 7-15 \u00a0 Array representation of a complete binary tree
The following code implements a binary tree based on array representation, including the following operations:
class ArrayBinaryTree:\n \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n def __init__(self, arr: list[int | None]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._tree = list(arr)\n\n def size(self):\n \"\"\"\u5217\u8868\u5bb9\u91cf\"\"\"\n return len(self._tree)\n\n def val(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 or i >= self.size():\n return None\n return self._tree[i]\n\n def left(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\n def right(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\n def parent(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2\n\n def level_order(self) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n self.res = []\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in range(self.size()):\n if self.val(i) is not None:\n self.res.append(self.val(i))\n return self.res\n\n def dfs(self, i: int, order: str):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n if self.val(i) is None:\n return\n # \u524d\u5e8f\u904d\u5386\n if order == \"pre\":\n self.res.append(self.val(i))\n self.dfs(self.left(i), order)\n # \u4e2d\u5e8f\u904d\u5386\n if order == \"in\":\n self.res.append(self.val(i))\n self.dfs(self.right(i), order)\n # \u540e\u5e8f\u904d\u5386\n if order == \"post\":\n self.res.append(self.val(i))\n\n def pre_order(self) -> list[int]:\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"pre\")\n return self.res\n\n def in_order(self) -> list[int]:\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"in\")\n return self.res\n\n def post_order(self) -> list[int]:\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"post\")\n return self.res\n
array_binary_tree.cpp/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(vector<int> arr) {\n tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return INT_MAX;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n vector<int> levelOrder() {\n vector<int> res;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != INT_MAX)\n res.push_back(val(i));\n }\n return res;\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n vector<int> preOrder() {\n vector<int> res;\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n vector<int> inOrder() {\n vector<int> res;\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n vector<int> postOrder() {\n vector<int> res;\n dfs(0, \"post\", res);\n return res;\n }\n\n private:\n vector<int> tree;\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, string order, vector<int> &res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.push_back(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.push_back(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.push_back(val(i));\n }\n};\n
array_binary_tree.java/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private List<Integer> tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayBinaryTree(List<Integer> arr) {\n tree = new ArrayList<>(arr);\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n public int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public Integer val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return null;\n return tree.get(i);\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<Integer> levelOrder() {\n List<Integer> res = new ArrayList<>();\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != null)\n res.add(val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private void dfs(Integer i, String order, List<Integer> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\".equals(order))\n res.add(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\".equals(order))\n res.add(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (\"post\".equals(order))\n res.add(val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<Integer> preOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<Integer> inOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<Integer> postOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.cs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n List<int?> tree = new(arr);\n\n /* \u5217\u8868\u5bb9\u91cf */\n public int Size() {\n return tree.Count;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public int? Val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= Size())\n return null;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<int> LevelOrder() {\n List<int> res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < Size(); i++) {\n if (Val(i).HasValue)\n res.Add(Val(i)!.Value);\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void DFS(int i, string order, List<int> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (!Val(i).HasValue)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.Add(Val(i)!.Value);\n DFS(Left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.Add(Val(i)!.Value);\n DFS(Right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.Add(Val(i)!.Value);\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<int> PreOrder() {\n List<int> res = [];\n DFS(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<int> InOrder() {\n List<int> res = [];\n DFS(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<int> PostOrder() {\n List<int> res = [];\n DFS(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.go/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n return &arrayBinaryTree{\n tree: arr,\n }\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= abt.size() {\n return nil\n }\n return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n var res []any\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i := 0; i < abt.size(); i++ {\n if abt.val(i) != nil {\n res = append(res, abt.val(i))\n }\n }\n return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if abt.val(i) == nil {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n *res = append(*res, abt.val(i))\n }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n var res []any\n abt.dfs(0, \"pre\", &res)\n return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n var res []any\n abt.dfs(0, \"in\", &res)\n return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n var res []any\n abt.dfs(0, \"post\", &res)\n return res\n}\n
array_binary_tree.swift/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private var tree: [Int?]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(arr: [Int?]) {\n tree = arr\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n func size() -> Int {\n tree.count\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n func val(i: Int) -> Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= size() {\n return nil\n }\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func left(i: Int) -> Int {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func right(i: Int) -> Int {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n func parent(i: Int) -> Int {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n func levelOrder() -> [Int] {\n var res: [Int] = []\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0 ..< size() {\n if let val = val(i: i) {\n res.append(val)\n }\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private func dfs(i: Int, order: String, res: inout [Int]) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n guard let val = val(i: i) else {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.append(val)\n }\n dfs(i: left(i: i), order: order, res: &res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.append(val)\n }\n dfs(i: right(i: i), order: order, res: &res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.append(val)\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n func preOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"pre\", res: &res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n func inOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"in\", res: &res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n func postOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"post\", res: &res)\n return res\n }\n}\n
array_binary_tree.js/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr) {\n this.#tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n size() {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder() {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i, order, res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder() {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder() {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder() {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.ts/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree: (number | null)[];\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr: (number | null)[]) {\n this.#tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n size(): number {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i: number): number | null {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i: number): number {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i: number): number {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder(): number[] {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i: number, order: Order, res: (number | null)[]): void {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.dart/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n late List<int?> _tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(this._tree);\n\n /* \u5217\u8868\u5bb9\u91cf */\n int size() {\n return _tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int? val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) {\n return null;\n }\n return _tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int? parent(int i) {\n return (i - 1) ~/ 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n List<int> levelOrder() {\n List<int> res = [];\n for (int i = 0; i < size(); i++) {\n if (val(i) != null) {\n res.add(val(i)!);\n }\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, String order, List<int?> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null) {\n return;\n }\n // \u524d\u5e8f\u904d\u5386\n if (order == 'pre') {\n res.add(val(i));\n }\n dfs(left(i)!, order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == 'in') {\n res.add(val(i));\n }\n dfs(right(i)!, order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == 'post') {\n res.add(val(i));\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n List<int?> preOrder() {\n List<int?> res = [];\n dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n List<int?> inOrder() {\n List<int?> res = [];\n dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n List<int?> postOrder() {\n List<int?> res = [];\n dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.rs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(arr: Vec<Option<i32>>) -> Self {\n Self { tree: arr }\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n fn size(&self) -> i32 {\n self.tree.len() as i32\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fn val(&self, i: i32) -> Option<i32> {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= self.size() {\n None\n } else {\n self.tree[i as usize]\n }\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn left(&self, i: i32) -> i32 {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn right(&self, i: i32) -> i32 {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fn parent(&self, i: i32) -> i32 {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fn level_order(&self) -> Vec<i32> {\n let mut res = vec![];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0..self.size() {\n if let Some(val) = self.val(i) {\n res.push(val)\n }\n }\n res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\n if self.val(i).is_none() {\n return;\n }\n let val = self.val(i).unwrap();\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.push(val);\n }\n self.dfs(self.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.push(val);\n }\n self.dfs(self.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.push(val);\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fn pre_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"pre\", &mut res);\n res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fn in_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"in\", &mut res);\n res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fn post_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"post\", &mut res);\n res\n }\n}\n
array_binary_tree.c/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n int *tree;\n int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n abt->tree = malloc(sizeof(int) * arrSize);\n memcpy(abt->tree, arr, sizeof(int) * arrSize);\n abt->size = arrSize;\n return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n free(abt->tree);\n free(abt);\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nint size(ArrayBinaryTree *abt) {\n return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size(abt))\n return INT_MAX;\n return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(abt); i++) {\n if (val(abt, i) != INT_MAX)\n res[index++] = val(abt, i);\n }\n *returnSize = index;\n return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(abt, i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (strcmp(order, \"pre\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, left(i), order, res, index);\n // \u4e2d\u5e8f\u904d\u5386\n if (strcmp(order, \"in\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, right(i), order, res, index);\n // \u540e\u5e8f\u904d\u5386\n if (strcmp(order, \"post\") == 0)\n res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"pre\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"in\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"post\", res, &index);\n *returnSize = index;\n return res;\n}\n
array_binary_tree.kt/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(val tree: List<Int?>) {\n /* \u5217\u8868\u5bb9\u91cf */\n fun size(): Int {\n return tree.size\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fun value(i: Int): Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) return null\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fun parent(i: Int): Int {\n return (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fun levelOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (i in 0..<size()) {\n if (value(i) != null) res.add(value(i))\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (value(i) == null) return\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\" == order) res.add(value(i))\n dfs(left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\" == order) res.add(value(i))\n dfs(right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if (\"post\" == order) res.add(value(i))\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fun preOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"pre\", res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fun inOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"in\", res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fun postOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"post\", res)\n return res\n }\n}\n
array_binary_tree.rb[class]{ArrayBinaryTree}-[func]{}\n
array_binary_tree.zig[class]{ArrayBinaryTree}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/array_representation_of_tree/#733-advantages-and-limitations","title":"7.3.3 \u00a0 Advantages and limitations","text":"The array representation of binary trees has the following advantages:
However, the array representation also has some limitations:
None
values in the binary tree, the proportion of node data contained in the array is low, leading to lower space utilization.In the \"Binary Search Tree\" section, we mentioned that after multiple insertions and removals, a binary search tree might degrade to a linked list. In such cases, the time complexity of all operations degrades from \\(O(\\log n)\\) to \\(O(n)\\).
As shown in the Figure 7-24 , after two node removal operations, this binary search tree will degrade into a linked list.
Figure 7-24 \u00a0 Degradation of an AVL tree after removing nodes
For example, in the perfect binary tree shown in the Figure 7-25 , after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.
Figure 7-25 \u00a0 Degradation of an AVL tree after inserting nodes
In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the \"AVL Tree\" in their paper \"An algorithm for the organization of information\". The paper detailed a series of operations to ensure that after continuously adding and removing nodes, the AVL tree would not degrade, thus maintaining the time complexity of various operations at \\(O(\\log n)\\) level. In other words, in scenarios where frequent additions, removals, searches, and modifications are needed, the AVL tree can always maintain efficient data operation performance, which has great application value.
"},{"location":"chapter_tree/avl_tree/#751-common-terminology-in-avl-trees","title":"7.5.1 \u00a0 Common terminology in AVL trees","text":"An AVL tree is both a binary search tree and a balanced binary tree, satisfying all properties of these two types of binary trees, hence it is a \"balanced binary search tree\".
"},{"location":"chapter_tree/avl_tree/#1-node-height","title":"1. \u00a0 Node height","text":"Since the operations related to AVL trees require obtaining node heights, we need to add a height
variable to the node class:
class TreeNode:\n \"\"\"AVL tree node\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.height: int = 0 # Node height\n self.left: TreeNode | None = None # Left child reference\n self.right: TreeNode | None = None # Right child reference\n
/* AVL tree node */\nstruct TreeNode {\n int val{}; // Node value\n int height = 0; // Node height\n TreeNode *left{}; // Left child\n TreeNode *right{}; // Right child\n TreeNode() = default;\n explicit TreeNode(int x) : val(x){}\n};\n
/* AVL tree node */\nclass TreeNode {\n public int val; // Node value\n public int height; // Node height\n public TreeNode left; // Left child\n public TreeNode right; // Right child\n public TreeNode(int x) { val = x; }\n}\n
/* AVL tree node */\nclass TreeNode(int? x) {\n public int? val = x; // Node value\n public int height; // Node height\n public TreeNode? left; // Left child reference\n public TreeNode? right; // Right child reference\n}\n
/* AVL tree node */\ntype TreeNode struct {\n Val int // Node value\n Height int // Node height\n Left *TreeNode // Left child reference\n Right *TreeNode // Right child reference\n}\n
/* AVL tree node */\nclass TreeNode {\n var val: Int // Node value\n var height: Int // Node height\n var left: TreeNode? // Left child\n var right: TreeNode? // Right child\n\n init(x: Int) {\n val = x\n height = 0\n }\n}\n
/* AVL tree node */\nclass TreeNode {\n val; // Node value\n height; // Node height\n left; // Left child pointer\n right; // Right child pointer\n constructor(val, left, right, height) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* AVL tree node */\nclass TreeNode {\n val: number; // Node value\n height: number; // Node height\n left: TreeNode | null; // Left child pointer\n right: TreeNode | null; // Right child pointer\n constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height; \n this.left = left === undefined ? null : left; \n this.right = right === undefined ? null : right; \n }\n}\n
/* AVL tree node */\nclass TreeNode {\n int val; // Node value\n int height; // Node height\n TreeNode? left; // Left child\n TreeNode? right; // Right child\n TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL tree node */\nstruct TreeNode {\n val: i32, // Node value\n height: i32, // Node height\n left: Option<Rc<RefCell<TreeNode>>>, // Left child\n right: Option<Rc<RefCell<TreeNode>>>, // Right child\n}\n\nimpl TreeNode {\n /* Constructor */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n height: 0,\n left: None,\n right: None\n }))\n }\n}\n
/* AVL tree node */\nTreeNode struct TreeNode {\n int val;\n int height;\n struct TreeNode *left;\n struct TreeNode *right;\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
/* AVL tree node */\nclass TreeNode(val _val: Int) { // Node value\n val height: Int = 0 // Node height\n val left: TreeNode? = null // Left child\n val right: TreeNode? = null // Right child\n}\n
\n
\n
The \"node height\" refers to the distance from that node to its farthest leaf node, i.e., the number of \"edges\" passed. It is important to note that the height of a leaf node is \\(0\\), and the height of a null node is \\(-1\\). We will create two utility functions for getting and updating the height of a node:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef height(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node is not None:\n return node.height\n return -1\n\ndef update_height(self, node: TreeNode | None):\n \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
avl_tree.cpp/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node->height = max(height(node->left), height(node->right)) + 1;\n}\n
avl_tree.java/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.cs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
avl_tree.go/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node != nil {\n return node.Height\n }\n return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n lh := t.height(node.Left)\n rh := t.height(node.Right)\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if lh > rh {\n node.Height = lh + 1\n } else {\n node.Height = rh + 1\n }\n}\n
avl_tree.swift/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n node?.height ?? -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
avl_tree.js/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.ts/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.dart/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.rs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n match node {\n Some(node) => node.borrow().height,\n None => -1,\n }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n if let Some(node) = node {\n let left = node.borrow().left.clone();\n let right = node.borrow().right.clone();\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if (node != NULL) {\n return node->height;\n }\n return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n int lh = height(node->left);\n int rh = height(node->right);\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if (lh > rh) {\n node->height = lh + 1;\n } else {\n node->height = rh + 1;\n }\n}\n
avl_tree.kt/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfun height(node: TreeNode?): Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node?.height ?: -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfun updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt()\n}\n
avl_tree.rb[class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
avl_tree.zig// \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n _ = self;\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return if (node == null) -1 else node.?.height;\n}\n\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-node-balance-factor","title":"2. \u00a0 Node balance factor","text":"The \"balance factor\" of a node is defined as the height of the node's left subtree minus the height of its right subtree, with the balance factor of a null node defined as \\(0\\). We will also encapsulate the functionality of obtaining the node balance factor into a function for easy use later on:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef balance_factor(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node is None:\n return 0\n # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.left) - self.height(node.right)\n
avl_tree.cpp/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == nullptr)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.java/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.cs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return Height(node.left) - Height(node.right);\n}\n
avl_tree.go/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node == nil {\n return 0\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return t.height(node.Left) - t.height(node.Right)\n}\n
avl_tree.swift/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n guard let node = node else { return 0 }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node: node.left) - height(node: node.right)\n}\n
avl_tree.js/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.ts/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.dart/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.rs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n match node {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n None => 0,\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n Some(node) => {\n Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n }\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == NULL) {\n return 0;\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.kt/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfun balanceFactor(node: TreeNode?): Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right)\n}\n
avl_tree.rb[class]{AVLTree}-[func]{balance_factor}\n
avl_tree.zig// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.?.left) - self.height(node.?.right);\n}\n
Note
Let the balance factor be \\(f\\), then the balance factor of any node in an AVL tree satisfies \\(-1 \\le f \\le 1\\).
"},{"location":"chapter_tree/avl_tree/#752-rotations-in-avl-trees","title":"7.5.2 \u00a0 Rotations in AVL trees","text":"The characteristic feature of an AVL tree is the \"rotation\" operation, which can restore balance to an unbalanced node without affecting the in-order traversal sequence of the binary tree. In other words, the rotation operation can maintain the property of a \"binary search tree\" while also turning the tree back into a \"balanced binary tree\".
We call nodes with an absolute balance factor \\(> 1\\) \"unbalanced nodes\". Depending on the type of imbalance, there are four kinds of rotations: right rotation, left rotation, right-left rotation, and left-right rotation. Below, we detail these rotation operations.
"},{"location":"chapter_tree/avl_tree/#1-right-rotation","title":"1. \u00a0 Right rotation","text":"As shown in the Figure 7-26 , the first unbalanced node from the bottom up in the binary tree is \"node 3\". Focusing on the subtree with this unbalanced node as the root, denoted as node
, and its left child as child
, perform a \"right rotation\". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.
Figure 7-26 \u00a0 Steps of right rotation
As shown in the Figure 7-27 , when the child
node has a right child (denoted as grand_child
), a step needs to be added in the right rotation: set grand_child
as the left child of node
.
Figure 7-27 \u00a0 Right rotation with grand_child
\"Right rotation\" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child = node->left;\n TreeNode *grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n TreeNode child = node.left;\n TreeNode grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n TreeNode? child = node?.left;\n TreeNode? grandChild = child?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n child := node.Left\n grandChild := child.Right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.Right = node\n node.Left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.left\n let grandChild = child?.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child?.right = node\n node?.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n TreeNode? child = node!.left;\n TreeNode? grandChild = child!.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().left.clone().unwrap();\n let grand_child = child.borrow().right.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.borrow_mut().right = Some(node.clone());\n node.borrow_mut().left = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->left;\n grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.kt/* \u53f3\u65cb\u64cd\u4f5c */\nfun rightRotate(node: TreeNode?): TreeNode {\n val child = node!!.left\n val grandChild = child!!.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.rb[class]{AVLTree}-[func]{right_rotate}\n
avl_tree.zig// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.left;\n var grandChild = child.?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.?.right = node;\n node.?.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-left-rotation","title":"2. \u00a0 Left rotation","text":"Correspondingly, if considering the \"mirror\" of the above unbalanced binary tree, the \"left rotation\" operation shown in the Figure 7-28 needs to be performed.
Figure 7-28 \u00a0 Left rotation operation
Similarly, as shown in the Figure 7-29 , when the child
node has a left child (denoted as grand_child
), a step needs to be added in the left rotation: set grand_child
as the right child of node
.
Figure 7-29 \u00a0 Left rotation with grand_child
It can be observed that the right and left rotation operations are logically symmetrical, and they solve two symmetrical types of imbalance. Based on symmetry, by replacing all left
with right
, and all right
with left
in the implementation code of right rotation, we can get the implementation code for left rotation:
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child = node->right;\n TreeNode *grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n TreeNode child = node.right;\n TreeNode grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n TreeNode? child = node?.right;\n TreeNode? grandChild = child?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n child := node.Right\n grandChild := child.Left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.Left = node\n node.Right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.right\n let grandChild = child?.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child?.left = node\n node?.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n TreeNode? child = node!.right;\n TreeNode? grandChild = child!.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().right.clone().unwrap();\n let grand_child = child.borrow().left.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.borrow_mut().left = Some(node.clone());\n node.borrow_mut().right = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->right;\n grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.kt/* \u5de6\u65cb\u64cd\u4f5c */\nfun leftRotate(node: TreeNode?): TreeNode {\n val child = node!!.right\n val grandChild = child!!.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.rb[class]{AVLTree}-[func]{left_rotate}\n
avl_tree.zig// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.right;\n var grandChild = child.?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.?.left = node;\n node.?.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#3-right-left-rotation","title":"3. \u00a0 Right-left rotation","text":"For the unbalanced node 3 shown in the Figure 7-30 , using either left or right rotation alone cannot restore balance to the subtree. In this case, a \"left rotation\" needs to be performed on child
first, followed by a \"right rotation\" on node
.
Figure 7-30 \u00a0 Right-left rotation
"},{"location":"chapter_tree/avl_tree/#4-left-right-rotation","title":"4. \u00a0 Left-right rotation","text":"As shown in the Figure 7-31 , for the mirror case of the above unbalanced binary tree, a \"right rotation\" needs to be performed on child
first, followed by a \"left rotation\" on node
.
Figure 7-31 \u00a0 Left-right rotation
"},{"location":"chapter_tree/avl_tree/#5-choice-of-rotation","title":"5. \u00a0 Choice of rotation","text":"The four kinds of imbalances shown in the Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.
Figure 7-32 \u00a0 The four rotation cases of AVL tree
As shown in the Table 7-3 , we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.
Table 7-3 \u00a0 Conditions for Choosing Among the Four Rotation Cases
Balance factor of unbalanced node Balance factor of child node Rotation method to use \\(> 1\\) (Left-leaning tree) \\(\\geq 0\\) Right rotation \\(> 1\\) (Left-leaning tree) \\(<0\\) Left rotation then right rotation \\(< -1\\) (Right-leaning tree) \\(\\leq 0\\) Left rotation \\(< -1\\) (Right-leaning tree) \\(>0\\) Right rotation then left rotationFor convenience, we encapsulate the rotation operations into a function. With this function, we can perform rotations on various kinds of imbalances, restoring balance to unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = self.balance_factor(node)\n # \u5de6\u504f\u6811\n if balance_factor > 1:\n if self.balance_factor(node.left) >= 0:\n # \u53f3\u65cb\n return self.right_rotate(node)\n else:\n # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = self.left_rotate(node.left)\n return self.right_rotate(node)\n # \u53f3\u504f\u6811\n elif balance_factor < -1:\n if self.balance_factor(node.right) <= 0:\n # \u5de6\u65cb\n return self.left_rotate(node)\n else:\n # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = self.right_rotate(node.right)\n return self.left_rotate(node)\n # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n
avl_tree.cpp/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int _balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (_balanceFactor > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (_balanceFactor < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.java/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.cs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactorInt = BalanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactorInt > 1) {\n if (BalanceFactor(node?.left) >= 0) {\n // \u53f3\u65cb\n return RightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node!.left = LeftRotate(node!.left);\n return RightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactorInt < -1) {\n if (BalanceFactor(node?.right) <= 0) {\n // \u5de6\u65cb\n return LeftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node!.right = RightRotate(node!.right);\n return LeftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.go/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n bf := t.balanceFactor(node)\n // \u5de6\u504f\u6811\n if bf > 1 {\n if t.balanceFactor(node.Left) >= 0 {\n // \u53f3\u65cb\n return t.rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.Left = t.leftRotate(node.Left)\n return t.rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if bf < -1 {\n if t.balanceFactor(node.Right) <= 0 {\n // \u5de6\u65cb\n return t.leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.Right = t.rightRotate(node.Right)\n return t.leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.swift/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balanceFactor = balanceFactor(node: node)\n // \u5de6\u504f\u6811\n if balanceFactor > 1 {\n if self.balanceFactor(node: node?.left) >= 0 {\n // \u53f3\u65cb\n return rightRotate(node: node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node?.left = leftRotate(node: node?.left)\n return rightRotate(node: node)\n }\n }\n // \u53f3\u504f\u6811\n if balanceFactor < -1 {\n if self.balanceFactor(node: node?.right) <= 0 {\n // \u5de6\u65cb\n return leftRotate(node: node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node?.right = rightRotate(node: node?.right)\n return leftRotate(node: node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.js/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.#rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.#leftRotate(node.left);\n return this.#rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.#leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.#rightRotate(node.right);\n return this.#leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.ts/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.leftRotate(node.left);\n return this.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.rightRotate(node.right);\n return this.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.dart/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int factor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (factor > 1) {\n if (balanceFactor(node!.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (factor < -1) {\n if (balanceFactor(node!.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.rs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balance_factor = Self::balance_factor(node.clone());\n // \u5de6\u504f\u6811\n if balance_factor > 1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n // \u53f3\u65cb\n Self::right_rotate(Some(node))\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::left_rotate(left);\n Self::right_rotate(Some(node))\n }\n }\n // \u53f3\u504f\u6811\n else if balance_factor < -1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n // \u5de6\u65cb\n Self::left_rotate(Some(node))\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::right_rotate(right);\n Self::left_rotate(Some(node))\n }\n } else {\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n node\n }\n}\n
avl_tree.c/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int bf = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (bf > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (bf < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.kt/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfun rotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n val balanceFactor = balanceFactor(node)\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left)\n return rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right)\n return leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{rotate}\n
avl_tree.zig// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n var balance_factor = self.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balance_factor > 1) {\n if (self.balanceFactor(node.?.left) >= 0) {\n // \u53f3\u65cb\n return self.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.?.left = self.leftRotate(node.?.left);\n return self.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balance_factor < -1) {\n if (self.balanceFactor(node.?.right) <= 0) {\n // \u5de6\u65cb\n return self.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.?.right = self.rightRotate(node.?.right);\n return self.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#753-common-operations-in-avl-trees","title":"7.5.3 \u00a0 Common operations in AVL trees","text":""},{"location":"chapter_tree/avl_tree/#1-node-insertion","title":"1. \u00a0 Node insertion","text":"The node insertion operation in AVL trees is similar to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear along the path from that node to the root node. Therefore, we need to start from this node and perform rotation operations upwards to restore balance to all unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef insert(self, val):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return TreeNode(val)\n # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n if val < node.val:\n node.left = self.insert_helper(node.left, val)\n elif val > node.val:\n node.right = self.insert_helper(node.right, val)\n else:\n # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val)\n node->left = insertHelper(node->left, val);\n else if (val > node->val)\n node->right = insertHelper(node->right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n if (node == null)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n if (node == null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = InsertHelper(node.left, val);\n else if (val > node.val)\n node.right = InsertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return NewTreeNode(val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node.Val.(int) {\n node.Left = t.insertHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.insertHelper(node.Right, val)\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return TreeNode(x: val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node!.val {\n node?.left = insertHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = insertHelper(node: node?.right, val: val)\n } else {\n return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) node.left = this.#insertHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#insertHelper(node.right, val);\n else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) {\n node.left = this.insertHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.insertHelper(node.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n if (node == null) return TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n match {\n let node_val = node.borrow().val;\n node_val\n }\n .cmp(&val)\n {\n Ordering::Greater => {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::insert_helper(left, val);\n }\n Ordering::Less => {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::insert_helper(right, val);\n }\n Ordering::Equal => {\n return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => Some(TreeNode::new(val)),\n }\n}\n
avl_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == NULL) {\n return newTreeNode(val);\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val) {\n node->left = insertHelper(node->left, val);\n } else if (val > node->val) {\n node->right = insertHelper(node->right, val);\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.kt/* \u63d2\u5165\u8282\u70b9 */\nfun insert(value: Int) {\n root = insertHelper(root, value)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun insertHelper(n: TreeNode?, value: Int): TreeNode {\n if (n == null)\n return TreeNode(value)\n var node = n\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (value < node.value) node.left = insertHelper(node.left, value)\n else if (value > node.value) node.right = insertHelper(node.right, value)\n else return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n\n updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
avl_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) !void {\n self.root = (try self.insertHelper(self.root, val)).?;\n}\n\n// \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) {\n var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\n tmp_node.init(val);\n return tmp_node;\n }\n // 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n if (val < node.?.val) {\n node.?.left = try self.insertHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = try self.insertHelper(node.?.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-node-removal","title":"2. \u00a0 Node removal","text":"Similarly, based on the method of removing nodes in binary search trees, rotation operations need to be performed from the bottom up to restore balance to all unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef remove(self, val: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return None\n # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n if val < node.val:\n node.left = self.remove_helper(node.left, val)\n elif val > node.val:\n node.right = self.remove_helper(node.right, val)\n else:\n if node.left is None or node.right is None:\n child = node.left or node.right\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child is None:\n return None\n # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else:\n node = child\n else:\n # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp = node.right\n while temp.left is not None:\n temp = temp.left\n node.right = self.remove_helper(node.right, temp.val)\n node.val = temp.val\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return nullptr;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node->val)\n node->left = removeHelper(node->left, val);\n else if (val > node->val)\n node->right = removeHelper(node->right, val);\n else {\n if (node->left == nullptr || node->right == nullptr) {\n TreeNode *child = node->left != nullptr ? node->left : node->right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == nullptr) {\n delete node;\n return nullptr;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n delete node;\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != nullptr) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n if (node == null)\n return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode child = node.left != null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = RemoveHelper(node.left, val);\n else if (val > node.val)\n node.right = RemoveHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = RemoveHelper(node.right, temp.val!.Value);\n node.val = temp.val;\n }\n }\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node.Val.(int) {\n node.Left = t.removeHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.removeHelper(node.Right, val)\n } else {\n if node.Left == nil || node.Right == nil {\n child := node.Left\n if node.Right != nil {\n child = node.Right\n }\n if child == nil {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n return nil\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp := node.Right\n for temp.Left != nil {\n temp = temp.Left\n }\n node.Right = t.removeHelper(node.Right, temp.Val.(int))\n node.Val = temp.Val\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node!.val {\n node?.left = removeHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = removeHelper(node: node?.right, val: val)\n } else {\n if node?.left == nil || node?.right == nil {\n let child = node?.left ?? node?.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child == nil {\n return nil\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node?.right\n while temp?.left != nil {\n temp = temp?.left\n }\n node?.right = removeHelper(node: node?.right, val: temp!.val)\n node?.val = temp!.val\n }\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val) node.left = this.#removeHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#removeHelper(node.right, val);\n else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.#removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val) {\n node.left = this.removeHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.removeHelper(node.right, val);\n } else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) {\n return null;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp!.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node.borrow().val {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::remove_helper(left, val);\n } else if val > node.borrow().val {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, val);\n } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n let child = if node.borrow().left.is_some() {\n node.borrow().left.clone()\n } else {\n node.borrow().right.clone()\n };\n match child {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n None => {\n return None;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n Some(child) => node = child,\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let mut temp = node.borrow().right.clone().unwrap();\n loop {\n let temp_left = temp.borrow().left.clone();\n if temp_left.is_none() {\n break;\n }\n temp = temp_left.unwrap();\n }\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n node.borrow_mut().val = temp.borrow().val;\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n TreeNode *child, *grandChild;\n if (node == NULL) {\n return NULL;\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node->val) {\n node->left = removeHelper(node->left, val);\n } else if (val > node->val) {\n node->right = removeHelper(node->right, val);\n } else {\n if (node->left == NULL || node->right == NULL) {\n child = node->left;\n if (node->right != NULL) {\n child = node->right;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == NULL) {\n return NULL;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != NULL) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.kt/* \u5220\u9664\u8282\u70b9 */\nfun remove(value: Int) {\n root = removeHelper(root, value)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun removeHelper(n: TreeNode?, value: Int): TreeNode? {\n var node = n ?: return null\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (value < node.value) node.left = removeHelper(node.left, value)\n else if (value > node.value) node.right = removeHelper(node.right, value)\n else {\n if (node.left == null || node.right == null) {\n val child = if (node.left != null) node.left else node.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) return null\n else node = child\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.right\n while (temp!!.left != null) {\n temp = temp.left\n }\n node.right = removeHelper(node.right, temp.value)\n node.value = temp.value\n }\n }\n updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
avl_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\n self.root = self.removeHelper(self.root, val).?;\n}\n\n// \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) return null;\n // 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n if (val < node.?.val) {\n node.?.left = self.removeHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = self.removeHelper(node.?.right, val);\n } else {\n if (node.?.left == null or node.?.right == null) {\n var child = if (node.?.left != null) node.?.left else node.?.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) {\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n } else {\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.?.right;\n while (temp.?.left != null) {\n temp = temp.?.left;\n }\n node.?.right = self.removeHelper(node.?.right, temp.?.val);\n node.?.val = temp.?.val;\n }\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#3-node-search","title":"3. \u00a0 Node search","text":"The node search operation in AVL trees is consistent with that in binary search trees and will not be detailed here.
"},{"location":"chapter_tree/avl_tree/#754-typical-applications-of-avl-trees","title":"7.5.4 \u00a0 Typical applications of AVL trees","text":"As shown in the Figure 7-16 , a \"binary search tree\" satisfies the following conditions.
1.
as well.Figure 7-16 \u00a0 Binary search tree
"},{"location":"chapter_tree/binary_search_tree/#741-operations-on-a-binary-search-tree","title":"7.4.1 \u00a0 Operations on a binary search tree","text":"We encapsulate the binary search tree as a class BinarySearchTree
and declare a member variable root
, pointing to the tree's root node.
Given a target node value num
, one can search according to the properties of the binary search tree. As shown in the Figure 7-17 , we declare a node cur
and start from the binary tree's root node root
, looping to compare the size relationship between the node value cur.val
and num
.
cur.val < num
, it means the target node is in cur
's right subtree, thus execute cur = cur.right
.cur.val > num
, it means the target node is in cur
's left subtree, thus execute cur = cur.left
.cur.val = num
, it means the target node is found, exit the loop and return the node.Figure 7-17 \u00a0 Example of searching for a node in a binary search tree
The search operation in a binary search tree works on the same principle as the binary search algorithm, eliminating half of the possibilities in each round. The number of loops is at most the height of the binary tree. When the binary tree is balanced, it uses \\(O(\\log n)\\) time. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.pydef search(self, num: int) -> TreeNode | None:\n \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n cur = self._root\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur is not None:\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n elif cur.val > num:\n cur = cur.left\n # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else:\n break\n return cur\n
binary_search_tree.cpp/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n TreeNode *cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur->val > num)\n cur = cur->left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.java/* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n TreeNode cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.cs/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n TreeNode? cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur =\n cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.go/* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n node := bst.root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for node != nil {\n if node.Val.(int) < num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n node = node.Right\n } else if node.Val.(int) > num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n node = node.Left\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return node\n}\n
binary_search_tree.swift/* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if cur!.val > num {\n cur = cur?.left\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.js/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.ts/* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.dart/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n TreeNode? cur = _root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > _num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.rs/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n let mut cur = self.root.clone();\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => cur = node.borrow().right.clone(),\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => cur = node.borrow().left.clone(),\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n Ordering::Equal => break,\n }\n }\n\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n cur\n}\n
binary_search_tree.c/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n TreeNode *cur = bst->root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n if (cur->val < num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else if (cur->val > num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.kt/* \u67e5\u627e\u8282\u70b9 */\nfun search(num: Int): TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.value > num) cur.left\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{search}\n
binary_search_tree.zig// \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\n var cur = self.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else if (cur.?.val > num) {\n cur = cur.?.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n } else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_search_tree/#2-inserting-a-node","title":"2. \u00a0 Inserting a node","text":"Given an element num
to be inserted, to maintain the property of the binary search tree \"left subtree < root node < right subtree,\" the insertion operation proceeds as shown in the Figure 7-18 .
num
until passing through the leaf node (traversing to None
) then exit the loop.num
and place it where None
was.Figure 7-18 \u00a0 Inserting a node into a binary search tree
In the code implementation, note the following two points.
pre
to save the node from the last loop. This way, when traversing to None
, we can get its parent node, thus completing the node insertion operation.def insert(self, num: int):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self._root is None:\n self._root = TreeNode(num)\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur.val == num:\n return\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u63d2\u5165\u8282\u70b9\n node = TreeNode(num)\n if pre.val < num:\n pre.right = node\n else:\n pre.left = node\n
binary_search_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == nullptr) {\n root = new TreeNode(num);\n return;\n }\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = new TreeNode(num);\n if (pre->val < num)\n pre->right = node;\n else\n pre->left = node;\n}\n
binary_search_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new TreeNode(num);\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new(num);\n if (pre != null) {\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n }\n}\n
binary_search_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if cur == nil {\n bst.root = NewTreeNode(num)\n return\n }\n // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n return\n }\n pre = cur\n if cur.Val.(int) < num {\n cur = cur.Right\n } else {\n cur = cur.Left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n node := NewTreeNode(num)\n if pre.Val.(int) < num {\n pre.Right = node\n } else {\n pre.Left = node\n }\n}\n
binary_search_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if root == nil {\n root = TreeNode(x: num)\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur!.val == num {\n return\n }\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode(x: num)\n if pre!.val < num {\n pre?.right = node\n } else {\n pre?.left = node\n }\n}\n
binary_search_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre.val < num) pre.right = node;\n else pre.left = node;\n}\n
binary_search_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre!.val < num) pre!.right = node;\n else pre!.left = node;\n}\n
binary_search_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (_root == null) {\n _root = TreeNode(_num);\n return;\n }\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == _num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode? node = TreeNode(_num);\n if (pre!.val < _num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.rs/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self.root.is_none() {\n self.root = Some(TreeNode::new(num));\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n Ordering::Equal => return,\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let pre = pre.unwrap();\n let node = Some(TreeNode::new(num));\n if num > pre.borrow().val {\n pre.borrow_mut().right = node;\n } else {\n pre.borrow_mut().left = node;\n }\n}\n
binary_search_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (bst->root == NULL) {\n bst->root = newTreeNode(num);\n return;\n }\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num) {\n return;\n }\n pre = cur;\n if (cur->val < num) {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = newTreeNode(num);\n if (pre->val < num) {\n pre->right = node;\n } else {\n pre->left = node;\n }\n}\n
binary_search_tree.kt/* \u63d2\u5165\u8282\u70b9 */\nfun insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = TreeNode(num)\n return\n }\n var cur = root\n var pre: TreeNode? = null\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.value == num) return\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur.left\n }\n // \u63d2\u5165\u8282\u70b9\n val node = TreeNode(num)\n if (pre?.value!! < num) pre.right = node\n else pre.left = node\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{insert}\n
binary_search_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (self.root == null) {\n self.root = try self.mem_allocator.create(inc.TreeNode(T));\n return;\n }\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.?.val == num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n var node = try self.mem_allocator.create(inc.TreeNode(T));\n node.init(num);\n if (pre.?.val < num) {\n pre.?.right = node;\n } else {\n pre.?.left = node;\n }\n}\n
Code Visualization Full Screen >
Similar to searching for a node, inserting a node uses \\(O(\\log n)\\) time.
"},{"location":"chapter_tree/binary_search_tree/#3-removing-a-node","title":"3. \u00a0 Removing a node","text":"First, find the target node in the binary tree, then remove it. Similar to inserting a node, we need to ensure that after the removal operation is completed, the property of the binary search tree \"left subtree < root node < right subtree\" is still satisfied. Therefore, based on the number of child nodes of the target node, we divide it into 0, 1, and 2 cases, performing the corresponding node removal operations.
As shown in the Figure 7-19 , when the degree of the node to be removed is \\(0\\), it means the node is a leaf node, and it can be directly removed.
Figure 7-19 \u00a0 Removing a node in a binary search tree (degree 0)
As shown in the Figure 7-20 , when the degree of the node to be removed is \\(1\\), replacing the node to be removed with its child node is sufficient.
Figure 7-20 \u00a0 Removing a node in a binary search tree (degree 1)
When the degree of the node to be removed is \\(2\\), we cannot remove it directly, but need to use a node to replace it. To maintain the property of the binary search tree \"left subtree < root node < right subtree,\" this node can be either the smallest node of the right subtree or the largest node of the left subtree.
Assuming we choose the smallest node of the right subtree (the next node in in-order traversal), then the removal operation proceeds as shown in the Figure 7-21 .
tmp
.tmp
's value, and recursively remove the node tmp
in the tree.Figure 7-21 \u00a0 Removing a node in a binary search tree (degree 2)
The operation of removing a node also uses \\(O(\\log n)\\) time, where finding the node to be removed requires \\(O(\\log n)\\) time, and obtaining the in-order traversal successor node requires \\(O(\\log n)\\) time. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.pydef remove(self, num: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self._root is None:\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur.val == num:\n break\n pre = cur\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur is None:\n return\n\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.left is None or cur.right is None:\n # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n child = cur.left or cur.right\n # \u5220\u9664\u8282\u70b9 cur\n if cur != self._root:\n if pre.left == cur:\n pre.left = child\n else:\n pre.right = child\n else:\n # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self._root = child\n # \u5b50\u8282\u70b9\u6570\u91cf = 2\n else:\n # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp: TreeNode = cur.right\n while tmp.left is not None:\n tmp = tmp.left\n # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.val)\n # \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val\n
binary_search_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == nullptr)\n return;\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == nullptr)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur->left == nullptr || cur->right == nullptr) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre->left == cur)\n pre->left = child;\n else\n pre->right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n // \u91ca\u653e\u5185\u5b58\n delete cur;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != nullptr) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode child = cur.left != null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n Remove(tmp.val!.Value);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n break\n }\n pre = cur\n if cur.Val.(int) < num {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n cur = cur.Right\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n cur = cur.Left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n if cur.Left == nil || cur.Right == nil {\n var child *TreeNode = nil\n // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n if cur.Left != nil {\n child = cur.Left\n } else {\n child = cur.Right\n }\n // \u5220\u9664\u8282\u70b9 cur\n if cur != bst.root {\n if pre.Left == cur {\n pre.Left = child\n } else {\n pre.Right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n bst.root = child\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp := cur.Right\n for tmp.Left != nil {\n tmp = tmp.Left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n bst.remove(tmp.Val.(int))\n // \u7528 tmp \u8986\u76d6 cur\n cur.Val = tmp.Val\n }\n}\n
binary_search_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if root == nil {\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur!.val == num {\n break\n }\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur?.left == nil || cur?.right == nil {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n let child = cur?.left ?? cur?.right\n // \u5220\u9664\u8282\u70b9 cur\n if cur !== root {\n if pre?.left === cur {\n pre?.left = child\n } else {\n pre?.right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur?.right\n while tmp?.left != nil {\n tmp = tmp?.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(num: tmp!.val)\n // \u7528 tmp \u8986\u76d6 cur\n cur?.val = tmp!.val\n }\n}\n
binary_search_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child = cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre.left === cur) pre.left = child;\n else pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp = cur.right;\n while (tmp.left !== null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child: TreeNode | null =\n cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre!.left === cur) pre!.left = child;\n else pre!.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp: TreeNode | null = cur.right;\n while (tmp!.left !== null) {\n tmp = tmp!.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp!.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp!.val;\n }\n}\n
binary_search_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (_root == null) return;\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == _num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n _root = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.rs/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self.root.is_none() {\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n Ordering::Equal => break,\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur.is_none() {\n return;\n }\n let cur = cur.unwrap();\n let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n match (left_child.clone(), right_child.clone()) {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n (None, None) | (Some(_), None) | (None, Some(_)) => {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n let child = left_child.or(right_child);\n let pre = pre.unwrap();\n // \u5220\u9664\u8282\u70b9 cur\n if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n let left = pre.borrow().left.clone();\n if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {\n pre.borrow_mut().left = child;\n } else {\n pre.borrow_mut().right = child;\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n (Some(_), Some(_)) => {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let mut tmp = cur.borrow().right.clone();\n while let Some(node) = tmp.clone() {\n if node.borrow().left.is_some() {\n tmp = node.borrow().left.clone();\n } else {\n break;\n }\n }\n let tmpval = tmp.unwrap().borrow().val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmpval);\n // \u7528 tmp \u8986\u76d6 cur\n cur.borrow_mut().val = tmpval;\n }\n }\n}\n
binary_search_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (bst->root == NULL)\n return;\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n if (cur->val < num) {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == NULL)\n return;\n // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n if (cur->left == NULL || cur->right == NULL) {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre->left == cur) {\n pre->left = child;\n } else {\n pre->right = child;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n } else {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != NULL) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n removeItem(bst, tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.kt/* \u5220\u9664\u8282\u70b9 */\nfun remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null) return\n var cur = root\n var pre: TreeNode? = null\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.value == num) break\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur.left\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n val child = if (cur.left != null) cur.left else cur.right\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!!.left == cur) pre.left = child\n else pre.right = child\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.right\n while (tmp!!.left != null) {\n tmp = tmp.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.value)\n // \u7528 tmp \u8986\u76d6 cur\n cur.value = tmp.value\n }\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{remove}\n
binary_search_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (self.root == null) return;\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.?.val == num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.?.left == null or cur.?.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n var child = if (cur.?.left != null) cur.?.left else cur.?.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre.?.left == cur) {\n pre.?.left = child;\n } else {\n pre.?.right = child;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.?.right;\n while (tmp.?.left != null) {\n tmp = tmp.?.left;\n }\n var tmp_val = tmp.?.val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.?.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.?.val = tmp_val;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_search_tree/#4-in-order-traversal-is-ordered","title":"4. \u00a0 In-order traversal is ordered","text":"As shown in the Figure 7-22 , the in-order traversal of a binary tree follows the \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" traversal order, and a binary search tree satisfies the size relationship \"left child node < root node < right child node\".
This means that in-order traversal in a binary search tree always traverses the next smallest node first, thus deriving an important property: The in-order traversal sequence of a binary search tree is ascending.
Using the ascending property of in-order traversal, obtaining ordered data in a binary search tree requires only \\(O(n)\\) time, without the need for additional sorting operations, which is very efficient.
Figure 7-22 \u00a0 In-order traversal sequence of a binary search tree
"},{"location":"chapter_tree/binary_search_tree/#742-efficiency-of-binary-search-trees","title":"7.4.2 \u00a0 Efficiency of binary search trees","text":"Given a set of data, we consider using an array or a binary search tree for storage. Observing the Table 7-2 , the operations on a binary search tree all have logarithmic time complexity, which is stable and efficient. Only in scenarios of high-frequency addition and low-frequency search and removal, arrays are more efficient than binary search trees.
Table 7-2 \u00a0 Efficiency comparison between arrays and search trees
Unsorted array Binary search tree Search element \\(O(n)\\) \\(O(\\log n)\\) Insert element \\(O(1)\\) \\(O(\\log n)\\) Remove element \\(O(n)\\) \\(O(\\log n)\\)In ideal conditions, the binary search tree is \"balanced,\" thus any node can be found within \\(\\log n\\) loops.
However, continuously inserting and removing nodes in a binary search tree may lead to the binary tree degenerating into a chain list as shown in the Figure 7-23 , at which point the time complexity of various operations also degrades to \\(O(n)\\).
Figure 7-23 \u00a0 Degradation of a binary search tree
"},{"location":"chapter_tree/binary_search_tree/#743-common-applications-of-binary-search-trees","title":"7.4.3 \u00a0 Common applications of binary search trees","text":"A \"binary tree\" is a non-linear data structure that represents the ancestral and descendent relationships, embodying the \"divide and conquer\" logic. Similar to a linked list, the basic unit of a binary tree is a node, each containing a value, a reference to the left child node, and a reference to the right child node.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZigclass TreeNode:\n \"\"\"Binary tree node\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.left: TreeNode | None = None # Reference to left child node\n self.right: TreeNode | None = None # Reference to right child node\n
/* Binary tree node */\nstruct TreeNode {\n int val; // Node value\n TreeNode *left; // Pointer to left child node\n TreeNode *right; // Pointer to right child node\n TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
/* Binary tree node */\nclass TreeNode {\n int val; // Node value\n TreeNode left; // Reference to left child node\n TreeNode right; // Reference to right child node\n TreeNode(int x) { val = x; }\n}\n
/* Binary tree node */\nclass TreeNode(int? x) {\n public int? val = x; // Node value\n public TreeNode? left; // Reference to left child node\n public TreeNode? right; // Reference to right child node\n}\n
/* Binary tree node */\ntype TreeNode struct {\n Val int\n Left *TreeNode\n Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n return &TreeNode{\n Left: nil, // Pointer to left child node\n Right: nil, // Pointer to right child node\n Val: v, // Node value\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n var val: Int // Node value\n var left: TreeNode? // Reference to left child node\n var right: TreeNode? // Reference to right child node\n\n init(x: Int) {\n val = x\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n val; // Node value\n left; // Pointer to left child node\n right; // Pointer to right child node\n constructor(val, left, right) {\n this.val = val === undefined ? 0 : val;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n val: number;\n left: TreeNode | null;\n right: TreeNode | null;\n\n constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.left = left === undefined ? null : left; // Reference to left child node\n this.right = right === undefined ? null : right; // Reference to right child node\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n int val; // Node value\n TreeNode? left; // Reference to left child node\n TreeNode? right; // Reference to right child node\n TreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Binary tree node */\nstruct TreeNode {\n val: i32, // Node value\n left: Option<Rc<RefCell<TreeNode>>>, // Reference to left child node\n right: Option<Rc<RefCell<TreeNode>>>, // Reference to right child node\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n left: None,\n right: None\n }))\n }\n}\n
/* Binary tree node */\ntypedef struct TreeNode {\n int val; // Node value\n int height; // \u8282\u70b9\u9ad8\u5ea6\n struct TreeNode *left; // Pointer to left child node\n struct TreeNode *right; // Pointer to right child node\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
/* Binary tree node */\nclass TreeNode(val _val: Int) { // Node value\n val left: TreeNode? = null // Reference to left child node\n val right: TreeNode? = null // Reference to right child node\n}\n
\n
\n
Each node has two references (pointers), pointing to the \"left-child node\" and \"right-child node,\" respectively. This node is called the \"parent node\" of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes under it the \"left subtree\" of this node. Similarly, the \"right subtree\" can be defined.
In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees. As shown in the Figure 7-1 , if \"Node 2\" is considered as the parent node, then its left and right child nodes are \"Node 4\" and \"Node 5,\" respectively. The left subtree is \"the tree formed by Node 4 and all nodes under it,\" and the right subtree is \"the tree formed by Node 5 and all nodes under it.\"
Figure 7-1 \u00a0 Parent Node, child Node, subtree
"},{"location":"chapter_tree/binary_tree/#711-common-terminology-of-binary-trees","title":"7.1.1 \u00a0 Common terminology of binary trees","text":"The commonly used terminology of binary trees is shown in the following figure.
None
.Figure 7-2 \u00a0 Common Terminology of Binary Trees
Tip
Please note that we usually define \"height\" and \"depth\" as \"the number of edges passed,\" but some problems or textbooks may define them as \"the number of nodes passed.\" In this case, both height and depth need to be incremented by 1.
"},{"location":"chapter_tree/binary_tree/#712-basic-operations-of-binary-trees","title":"7.1.2 \u00a0 Basic operations of binary trees","text":""},{"location":"chapter_tree/binary_tree/#1-initializing-a-binary-tree","title":"1. \u00a0 Initializing a binary tree","text":"Similar to a linked list, initialize nodes first, then construct references (pointers).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py# Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.cpp/* Initializing a binary tree */\n// Initializing nodes\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.java// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.cs/* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.go/* Initializing a binary tree */\n// Initializing nodes\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// Linking references (pointers) between nodes\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
binary_tree.swift// Initializing nodes\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.js/* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.ts/* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.dart/* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.rs// Initializing nodes\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// Linking references (pointers) between nodes\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c/* Initializing a binary tree */\n// Initializing nodes\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.kt// Initializing nodes\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.rb\n
binary_tree.zig\n
Code visualization https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_tree/binary_tree/#2-inserting-and-removing-nodes","title":"2. \u00a0 Inserting and removing nodes","text":"Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The Figure 7-3 provides an example.
Figure 7-3 \u00a0 Inserting and removing nodes in a binary tree
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py# Inserting and removing nodes\np = TreeNode(0)\n# Inserting node P between n1 -> n2\nn1.left = p\np.left = n2\n# Removing node P\nn1.left = n2\n
binary_tree.cpp/* Inserting and removing nodes */\nTreeNode* P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
binary_tree.javaTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.cs/* Inserting and removing nodes */\nTreeNode P = new(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.go/* Inserting and removing nodes */\n// Inserting node P between n1 and n2\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// Removing node P\nn1.Left = n2\n
binary_tree.swiftlet P = TreeNode(x: 0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
binary_tree.js/* Inserting and removing nodes */\nlet P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.ts/* Inserting and removing nodes */\nconst P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.dart/* Inserting and removing nodes */\nTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.rslet p = TreeNode::new(0);\n// Inserting node P between n1 and n2\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// Removing node P\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c/* Inserting and removing nodes */\nTreeNode *P = newTreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
binary_tree.ktval P = TreeNode(0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
binary_tree.rb\n
binary_tree.zig\n
Code visualization https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
Note
It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes usually means removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful actions.
"},{"location":"chapter_tree/binary_tree/#713-common-types-of-binary-trees","title":"7.1.3 \u00a0 Common types of binary trees","text":""},{"location":"chapter_tree/binary_tree/#1-perfect-binary-tree","title":"1. \u00a0 Perfect binary tree","text":"As shown in the Figure 7-4 , in a \"perfect binary tree,\" all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is \\(0\\), and the degree of all other nodes is \\(2\\); if the tree's height is \\(h\\), then the total number of nodes is \\(2^{h+1} - 1\\), showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
Tip
Please note that in the Chinese community, a perfect binary tree is often referred to as a \"full binary tree.\"
Figure 7-4 \u00a0 Perfect binary tree
"},{"location":"chapter_tree/binary_tree/#2-complete-binary-tree","title":"2. \u00a0 Complete binary tree","text":"As shown in the Figure 7-5 , a \"complete binary tree\" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
Figure 7-5 \u00a0 Complete binary tree
"},{"location":"chapter_tree/binary_tree/#3-full-binary-tree","title":"3. \u00a0 Full binary tree","text":"As shown in the Figure 7-6 , a \"full binary tree\" has all nodes except leaf nodes having two children.
Figure 7-6 \u00a0 Full binary tree
"},{"location":"chapter_tree/binary_tree/#4-balanced-binary-tree","title":"4. \u00a0 Balanced binary tree","text":"As shown in the Figure 7-7 , in a \"balanced binary tree,\" the absolute difference in height between the left and right subtrees of any node does not exceed 1.
Figure 7-7 \u00a0 Balanced binary tree
"},{"location":"chapter_tree/binary_tree/#714-degeneration-of-binary-trees","title":"7.1.4 \u00a0 Degeneration of binary trees","text":"The Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the \"perfect binary tree\"; when all nodes are biased towards one side, the binary tree degenerates into a \"linked list\".
Figure 7-8 \u00a0 The Best and Worst Structures of Binary Trees
As shown in the Table 7-1 , in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.
Table 7-1 \u00a0 The Best and Worst Structures of Binary Trees
Perfect binary tree Linked list Number of nodes at level \\(i\\) \\(2^{i-1}\\) \\(1\\) Number of leaf nodes in a tree with height \\(h\\) \\(2^h\\) \\(1\\) Total number of nodes in a tree with height \\(h\\) \\(2^{h+1} - 1\\) \\(h + 1\\) Height of a tree with \\(n\\) total nodes \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 Binary tree traversal","text":"From the perspective of physical structure, a tree is a data structure based on linked lists, hence its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms to achieve.
Common traversal methods for binary trees include level-order traversal, preorder traversal, inorder traversal, and postorder traversal, among others.
"},{"location":"chapter_tree/binary_tree_traversal/#721-level-order-traversal","title":"7.2.1 \u00a0 Level-order traversal","text":"As shown in the Figure 7-9 , \"level-order traversal\" traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.
Level-order traversal essentially belongs to \"breadth-first traversal\", also known as \"breadth-first search (BFS)\", which embodies a \"circumferentially outward expanding\" layer-by-layer traversal method.
Figure 7-9 \u00a0 Level-order traversal of a binary tree
"},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation","title":"1. \u00a0 Code implementation","text":"Breadth-first traversal is usually implemented with the help of a \"queue\". The queue follows the \"first in, first out\" rule, while breadth-first traversal follows the \"layer-by-layer progression\" rule, the underlying ideas of the two are consistent. The implementation code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_bfs.pydef level_order(root: TreeNode | None) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue: deque[TreeNode] = deque()\n queue.append(root)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n res = []\n while queue:\n node: TreeNode = queue.popleft() # \u961f\u5217\u51fa\u961f\n res.append(node.val) # \u4fdd\u5b58\u8282\u70b9\u503c\n if node.left is not None:\n queue.append(node.left) # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if node.right is not None:\n queue.append(node.right) # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n return res\n
binary_tree_bfs.cpp/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue<TreeNode *> queue;\n queue.push(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n vector<int> vec;\n while (!queue.empty()) {\n TreeNode *node = queue.front();\n queue.pop(); // \u961f\u5217\u51fa\u961f\n vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node->left != nullptr)\n queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node->right != nullptr)\n queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return vec;\n}\n
binary_tree_bfs.java/* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new LinkedList<>();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<Integer> list = new ArrayList<>();\n while (!queue.isEmpty()) {\n TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n list.add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.offer(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.offer(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.cs/* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new();\n queue.Enqueue(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> list = [];\n while (queue.Count != 0) {\n TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n list.Add(node.val!.Value); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.Enqueue(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.Enqueue(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.go/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue := list.New()\n queue.PushBack(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n nums := make([]any, 0)\n for queue.Len() > 0 {\n // \u961f\u5217\u51fa\u961f\n node := queue.Remove(queue.Front()).(*TreeNode)\n // \u4fdd\u5b58\u8282\u70b9\u503c\n nums = append(nums, node.Val)\n if node.Left != nil {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Left)\n }\n if node.Right != nil {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Right)\n }\n }\n return nums\n}\n
binary_tree_bfs.swift/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n var queue: [TreeNode] = [root]\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list: [Int] = []\n while !queue.isEmpty {\n let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n if let left = node.left {\n queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let right = node.right {\n queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list\n}\n
binary_tree_bfs.js/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list = [];\n while (queue.length) {\n let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.ts/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list: number[] = [];\n while (queue.length) {\n let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) {\n queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right) {\n queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list;\n}\n
binary_tree_bfs.dart/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode?> queue = Queue();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> res = [];\n while (queue.isNotEmpty) {\n TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return res;\n}\n
binary_tree_bfs.rs/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n let mut que = VecDeque::new();\n que.push_back(Rc::clone(&root));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n let mut vec = Vec::new();\n\n while let Some(node) = que.pop_front() {\n // \u961f\u5217\u51fa\u961f\n vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if let Some(left) = node.borrow().left.as_ref() {\n que.push_back(Rc::clone(left)); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let Some(right) = node.borrow().right.as_ref() {\n que.push_back(Rc::clone(right)); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n };\n }\n vec\n}\n
binary_tree_bfs.c/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n /* \u8f85\u52a9\u961f\u5217 */\n int front, rear;\n int index, *arr;\n TreeNode *node;\n TreeNode **queue;\n\n /* \u8f85\u52a9\u961f\u5217 */\n queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n // \u961f\u5217\u6307\u9488\n front = 0, rear = 0;\n // \u52a0\u5165\u6839\u8282\u70b9\n queue[rear++] = root;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n /* \u8f85\u52a9\u6570\u7ec4 */\n arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n // \u6570\u7ec4\u6307\u9488\n index = 0;\n while (front < rear) {\n // \u961f\u5217\u51fa\u961f\n node = queue[front++];\n // \u4fdd\u5b58\u8282\u70b9\u503c\n arr[index++] = node->val;\n if (node->left != NULL) {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->left;\n }\n if (node->right != NULL) {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->right;\n }\n }\n // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n *size = index;\n arr = realloc(arr, sizeof(int) * (*size));\n\n // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n free(queue);\n return arr;\n}\n
binary_tree_bfs.kt/* \u5c42\u5e8f\u904d\u5386 */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n val queue = LinkedList<TreeNode?>()\n queue.add(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n val list = ArrayList<Int>()\n while (!queue.isEmpty()) {\n val node = queue.poll() // \u961f\u5217\u51fa\u961f\n list.add(node?.value!!) // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.offer(node.left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n\n if (node.right != null) queue.offer(node.right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list\n}\n
binary_tree_bfs.rb[class]{}-[func]{level_order}\n
binary_tree_bfs.zig// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const L = std.TailQueue(*inc.TreeNode(T));\n var queue = L{};\n var root_node = try mem_allocator.create(L.Node);\n root_node.data = root;\n queue.append(root_node); \n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list = std.ArrayList(T).init(std.heap.page_allocator);\n while (queue.len > 0) {\n var queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\n var node = queue_node.data;\n try list.append(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.left.?;\n queue.append(tmp_node); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.right.?;\n queue.append(tmp_node); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n } \n }\n return list;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity analysis","text":"Correspondingly, preorder, inorder, and postorder traversal all belong to \"depth-first traversal\", also known as \"depth-first search (DFS)\", which embodies a \"proceed to the end first, then backtrack and continue\" traversal method.
The Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. Depth-first traversal is like walking around the perimeter of the entire binary tree, encountering three positions at each node, corresponding to preorder traversal, inorder traversal, and postorder traversal.
Figure 7-10 \u00a0 Preorder, inorder, and postorder traversal of a binary search tree
"},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation_1","title":"1. \u00a0 Code implementation","text":"Depth-first search is usually implemented based on recursion:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_dfs.pydef pre_order(root: TreeNode | None):\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n res.append(root.val)\n pre_order(root=root.left)\n pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n in_order(root=root.left)\n res.append(root.val)\n in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n post_order(root=root.left)\n post_order(root=root.right)\n res.append(root.val)\n
binary_tree_dfs.cpp/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n vec.push_back(root->val);\n preOrder(root->left);\n preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left);\n vec.push_back(root->val);\n inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left);\n postOrder(root->right);\n vec.push_back(root->val);\n}\n
binary_tree_dfs.java/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.add(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.add(root.val);\n}\n
binary_tree_dfs.cs/* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.Add(root.val!.Value);\n PreOrder(root.left);\n PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n InOrder(root.left);\n list.Add(root.val!.Value);\n InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n PostOrder(root.left);\n PostOrder(root.right);\n list.Add(root.val!.Value);\n}\n
binary_tree_dfs.go/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n nums = append(nums, node.Val)\n preOrder(node.Left)\n preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.Left)\n nums = append(nums, node.Val)\n inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.Left)\n postOrder(node.Right)\n nums = append(nums, node.Val)\n}\n
binary_tree_dfs.swift/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.append(root.val)\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root: root.left)\n list.append(root.val)\n inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root: root.left)\n postOrder(root: root.right)\n list.append(root.val)\n}\n
binary_tree_dfs.js/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.ts/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.dart/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(node.val);\n preOrder(node.left);\n preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.left);\n list.add(node.val);\n inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.left);\n postOrder(node.right);\n list.add(node.val);\n}\n
binary_tree_dfs.rs/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n result.push(node.borrow().val);\n result.append(&mut pre_order(node.borrow().left.as_ref()));\n result.append(&mut pre_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n result.append(&mut in_order(node.borrow().left.as_ref()));\n result.push(node.borrow().val);\n result.append(&mut in_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n result.append(&mut post_order(node.borrow().left.as_ref()));\n result.append(&mut post_order(node.borrow().right.as_ref()));\n result.push(node.borrow().val);\n }\n result\n}\n
binary_tree_dfs.c/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n arr[(*size)++] = root->val;\n preOrder(root->left, size);\n preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left, size);\n arr[(*size)++] = root->val;\n inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left, size);\n postOrder(root->right, size);\n arr[(*size)++] = root->val;\n}\n
binary_tree_dfs.kt/* \u524d\u5e8f\u904d\u5386 */\nfun preOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.value)\n preOrder(root.left)\n preOrder(root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfun inOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left)\n list.add(root.value)\n inOrder(root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfun postOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left)\n postOrder(root.right)\n list.add(root.value)\n}\n
binary_tree_dfs.rb[class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n
binary_tree_dfs.zig// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n try list.append(root.?.val);\n try preOrder(T, root.?.left);\n try preOrder(T, root.?.right);\n}\n\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n try inOrder(T, root.?.left);\n try list.append(root.?.val);\n try inOrder(T, root.?.right);\n}\n\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n try postOrder(T, root.?.left);\n try postOrder(T, root.?.right);\n try list.append(root.?.val);\n}\n
Code Visualization Full Screen >
Tip
Depth-first search can also be implemented based on iteration, interested readers can study this on their own.
The Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: \"recursion\" and \"return\".
Figure 7-11 \u00a0 The recursive process of preorder traversal
"},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity analysis","text":"Q: For a binary tree with only one node, are both the height of the tree and the depth of the root node \\(0\\)?
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?
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.
Q: Why are there three sequences: pre-order, in-order, and post-order for DFS traversal of a binary tree, and what are their uses?
Similar to sequential and reverse traversal of arrays, pre-order, in-order, and post-order traversals are three methods of traversing a binary tree, allowing us to obtain a traversal result in a specific order. For example, in a binary search tree, since the node sizes satisfy left child node value < root node value < right child node value
, we can obtain an ordered node sequence by traversing the tree in the \"left \u2192 root \u2192 right\" priority.
Q: In a right rotation operation that deals with the relationship between the imbalance nodes node
, child
, grand_child
, isn't the connection between node
and its parent node and the original link of node
lost after the right rotation?
We need to view this problem from a recursive perspective. The right_rotate(root)
operation passes the root node of the subtree and eventually returns the root node of the rotated subtree with return child
. The connection between the subtree's root node and its parent node is established after this function returns, which is outside the scope of the right rotation operation's maintenance.
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.
Q: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
Yes, the method for building the tree is provided in the build_tree()
method in the binary search tree code. As for the choice of the root node, we usually sort the input data and then select the middle element as the root node, recursively building the left and right subtrees. This approach maximizes the balance of the tree.
Q: In Java, do you always have to use the equals()
method for string comparison?
In Java, for primitive data types, ==
is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.
==
: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.equals()
: Used to compare whether the values of two objects are equal.Therefore, to compare values, we should use equals()
. However, strings initialized with String a = \"hi\"; String b = \"hi\";
are stored in the string constant pool and point to the same object, so a == b
can also be used to compare the contents of two strings.
Q: Before reaching the bottom level, is the number of nodes in the queue \\(2^h\\) in breadth-first traversal?
Yes, for example, a full binary tree with height \\(h = 2\\) has a total of \\(n = 7\\) nodes, then the bottom level has \\(4 = 2^h = (n + 1) / 2\\) nodes.
"}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Home","text":"Hello AlgoData Structures and Algorithms Crash Course with Animated Illustrations and Off-the-Shelf Code
Dive In Clone Repo Get PDF
The English version is brewing...
Feel free to engage in Chinese-to-English translation and pull request review! For guidelines, please see #914.
Quote
\"An easy-to-understand book on data structures and algorithms, which guides readers to learn by minds-on and hands-on. Strongly recommended for algorithm beginners!\"
\u2014\u2014 Junhui Deng, Professor of Computer Science, Tsinghua University
Quote
\"If I had 'Hello Algo' when I was learning data structures and algorithms, it would have been 10 times easier!\"
\u2014\u2014 Mu Li, Senior Principal Scientist, Amazon
Animated illustrationsEasy to understandSmooth learning curve
\"A picture is worth a thousand words.\"
Off-the-Shelf CodeMulti programming languagesRun with one click
\"Talk is cheap. Show me the code.\"
Learning TogetherDiscussion and questions welcomeReaders progress together
\"Learning by teaching.\"
PrefaceTwo years ago, I shared the \"Sword Offer\" series of problem solutions on LeetCode, which received much love and support from many students. During my interactions with readers, the most common question I encountered was \"How to get started with algorithms.\" Gradually, I developed a deep interest in this question.
Blindly solving problems seems to be the most popular method, being simple, direct, and effective. However, problem-solving is like playing a \"Minesweeper\" game, where students with strong self-learning abilities can successfully clear the mines one by one, but those with insufficient foundations may end up bruised from explosions, retreating step by step in frustration. Thoroughly reading textbooks is also common, but for students aiming for job applications, the energy consumed by graduation, resume submissions, and preparing for written tests and interviews makes tackling thick books a daunting challenge.
If you are facing similar troubles, then you are lucky to have found this book. This book is my answer to this question, not necessarily the best solution, but at least an active attempt. Although this book won't directly land you an Offer, it will guide you through the \"knowledge map\" of data structures and algorithms, help you understand the shape, size, and distribution of different \"mines,\" and equip you with various \"demining methods.\" With these skills, I believe you can more comfortably solve problems and read literature, gradually building a complete knowledge system.
I deeply agree with Professor Feynman's saying: \"Knowledge isn't free. You have to pay attention.\" In this sense, this book is not entirely \"free.\" To not disappoint the precious \"attention\" you pay to this book, I will do my utmost, investing the greatest \"attention\" to complete the creation of this book.
AuthorYudong Jin(krahets), Senior Algorithm Engineer in a top tech company, Master's degree from Shanghai Jiao Tong University. The highest-read blogger across the entire LeetCode, his published \"Illustration of Algorithm Data Structures\" has been subscribed to by over 300k.
ContributionThis 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:
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, 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.
codingonionRust, Zig GongljaC, C++ gvenusleoDart hpstoryC# justin-tseJS, TS krahetsJava, Python night-cruiseRust nuomi1Swift ReanonGo, C"},{"location":"chapter_array_and_linkedlist/","title":"Chapter 4. \u00a0 Arrays and linked lists","text":"Abstract
The world of data structures resembles a sturdy brick wall.
In arrays, envision bricks snugly aligned, each resting seamlessly beside the next, creating a unified formation. Meanwhile, in linked lists, these bricks disperse freely, embraced by vines gracefully knitting connections between them.
"},{"location":"chapter_array_and_linkedlist/#chapter-contents","title":"Chapter contents","text":"An \"array\" is a linear data structure that operates as a lineup of similar items, stored together in a computer's memory in contiguous spaces. It's like a sequence that maintains organized storage. Each item in this lineup has its unique 'spot' known as an \"index\". Please refer to the Figure 4-1 to observe how arrays work and grasp these key terms.
Figure 4-1 \u00a0 Array definition and storage method
"},{"location":"chapter_array_and_linkedlist/array/#411-common-operations-on-arrays","title":"4.1.1 \u00a0 Common operations on arrays","text":""},{"location":"chapter_array_and_linkedlist/array/#1-initializing-arrays","title":"1. \u00a0 Initializing arrays","text":"Arrays can be initialized in two ways depending on the needs: either without initial values or with specified initial values. When initial values are not specified, most programming languages will set the array elements to \\(0\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig array.py# Initialize array\narr: list[int] = [0] * 5 # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
array.cpp/* Initialize array */\n// Stored on stack\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// Stored on heap (manual memory release needed)\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java/* Initialize array */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs/* Initialize array */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
array.go/* Initialize array */\nvar arr [5]int\n// In Go, specifying the length ([5]int) denotes an array, while not specifying it ([]int) denotes a slice.\n// Since Go's arrays are designed to have compile-time fixed length, only constants can be used to specify the length.\n// For convenience in implementing the extend() method, the Slice will be considered as an Array here.\nnums := []int{1, 3, 2, 5, 4}\n
array.swift/* Initialize array */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js/* Initialize array */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts/* Initialize array */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart/* Initialize array */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs/* Initialize array */\nlet arr: Vec<i32> = vec![0; 5]; // [0, 0, 0, 0, 0]\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c/* Initialize array */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.kt\n
array.zig// Initialize array\nvar arr = [_]i32{0} ** 5; // { 0, 0, 0, 0, 0 }\nvar nums = [_]i32{ 1, 3, 2, 5, 4 };\n
"},{"location":"chapter_array_and_linkedlist/array/#2-accessing-elements","title":"2. \u00a0 Accessing elements","text":"Elements in an array are stored in contiguous memory spaces, making it simpler to compute each element's memory address. The formula shown in the Figure below aids in determining an element's memory address, utilizing the array's memory address (specifically, the first element's address) and the element's index. This computation streamlines direct access to the desired element.
Figure 4-2 \u00a0 Memory address calculation for array elements
As observed in the above illustration, array indexing conventionally begins at \\(0\\). While this might appear counterintuitive, considering counting usually starts at \\(1\\), within the address calculation formula, an index is essentially an offset from the memory address. For the first element's address, this offset is \\(0\\), validating its index as \\(0\\).
Accessing elements in an array is highly efficient, allowing us to randomly access any element in \\(O(1)\\) time.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef random_access(nums: list[int]) -> int:\n \"\"\"\u968f\u673a\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u5728\u533a\u95f4 [0, len(nums)-1] \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = random.randint(0, len(nums) - 1)\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n random_num = nums[random_index]\n return random_num\n
array.cpp/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.java/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int[] nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.cs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint RandomAccess(int[] nums) {\n Random random = new();\n // \u5728\u533a\u95f4 [0, nums.Length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = random.Next(nums.Length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.go/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums []int) (randomNum int) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n randomIndex := rand.Intn(len(nums))\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n randomNum = nums[randomIndex]\n return\n}\n
array.swift/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunc randomAccess(nums: [Int]) -> Int {\n // \u5728\u533a\u95f4 [0, nums.count) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let randomIndex = nums.indices.randomElement()!\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let randomNum = nums[randomIndex]\n return randomNum\n}\n
array.js/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.ts/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfunction randomAccess(nums: number[]): number {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n const random_index = Math.floor(Math.random() * nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n const random_num = nums[random_index];\n return random_num;\n}\n
array.dart/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(List<int> nums) {\n // \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = Random().nextInt(nums.length);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.rs/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfn random_access(nums: &[i32]) -> i32 {\n // \u5728\u533a\u95f4 [0, nums.len()) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n let random_index = rand::thread_rng().gen_range(0..nums.len());\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n let random_num = nums[random_index];\n random_num\n}\n
array.c/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nint randomAccess(int *nums, int size) {\n // \u5728\u533a\u95f4 [0, size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n int randomIndex = rand() % size;\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n int randomNum = nums[randomIndex];\n return randomNum;\n}\n
array.kt/* \u968f\u673a\u8bbf\u95ee\u5143\u7d20 */\nfun randomAccess(nums: IntArray): Int {\n // \u5728\u533a\u95f4 [0, nums.size) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n val randomNum = nums[randomIndex]\n return randomNum\n}\n
array.rb### \u968f\u673a\u8bbf\u95ee\u5143\u7d20 ###\ndef random_access(nums)\n # \u5728\u533a\u95f4 [0, nums.length) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6570\u5b57\n random_index = Random.rand(0...nums.length)\n\n # \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n nums[random_index]\nend\n
array.zig// \u968f\u673a\u8bbf\u95ee\u5143\u7d20\nfn randomAccess(nums: []i32) i32 {\n // \u5728\u533a\u95f4 [0, nums.len) \u4e2d\u968f\u673a\u62bd\u53d6\u4e00\u4e2a\u6574\u6570\n var randomIndex = std.crypto.random.intRangeLessThan(usize, 0, nums.len);\n // \u83b7\u53d6\u5e76\u8fd4\u56de\u968f\u673a\u5143\u7d20\n var randomNum = nums[randomIndex];\n return randomNum;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#3-inserting-elements","title":"3. \u00a0 Inserting elements","text":"Array elements are tightly packed in memory, with no space available to accommodate additional data between them. Illustrated in Figure below, inserting an element in the middle of an array requires shifting all subsequent elements back by one position to create room for the new element.
Figure 4-3 \u00a0 Array element insertion example
It's important to note that due to the fixed length of an array, inserting an element will unavoidably result in the loss of the last element in the array. Solutions to address this issue will be explored in the \"List\" chapter.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef insert(nums: list[int], num: int, index: int):\n \"\"\"\u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\"\"\"\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in range(len(nums) - 1, index, -1):\n nums[i] = nums[i - 1]\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n
array.cpp/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.java/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.cs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid Insert(int[] nums, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = nums.Length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.go/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums []int, num int, index int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i := len(nums) - 1; i > index; i-- {\n nums[i] = nums[i-1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.swift/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).reversed() {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.js/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums, num, index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.ts/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfunction insert(nums: number[], num: number, index: number): void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.dart/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 _num */\nvoid insert(List<int> nums, int _num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var i = nums.length - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 _num \u8d4b\u7ed9 index \u5904\u5143\u7d20\n nums[index] = _num;\n}\n
array.rs/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfn insert(nums: &mut Vec<i32>, num: i32, index: usize) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (index + 1..nums.len()).rev() {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.c/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nvoid insert(int *nums, int size, int num, int index) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int i = size - 1; i > index; i--) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
array.kt/* \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num */\nfun insert(nums: IntArray, num: Int, index: Int) {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (i in nums.size - 1 downTo index + 1) {\n nums[i] = nums[i - 1]\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\n}\n
array.rb### \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num ###\ndef insert(nums, num, index)\n # \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for i in (nums.length - 1).downto(index + 1)\n nums[i] = nums[i - 1]\n end\n\n # \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num\nend\n
array.zig// \u5728\u6570\u7ec4\u7684\u7d22\u5f15 index \u5904\u63d2\u5165\u5143\u7d20 num\nfn insert(nums: []i32, num: i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var i = nums.len - 1;\n while (i > index) : (i -= 1) {\n nums[i] = nums[i - 1];\n }\n // \u5c06 num \u8d4b\u7ed9 index \u5904\u7684\u5143\u7d20\n nums[index] = num;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#4-deleting-elements","title":"4. \u00a0 Deleting elements","text":"Similarly, as depicted in the Figure 4-4 , to delete an element at index \\(i\\), all elements following index \\(i\\) must be moved forward by one position.
Figure 4-4 \u00a0 Array element deletion example
Please note that after deletion, the former last element becomes \"meaningless,\" hence requiring no specific modification.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef remove(nums: list[int], index: int):\n \"\"\"\u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\"\"\"\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in range(index, len(nums) - 1):\n nums[i] = nums[i + 1]\n
array.cpp/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.java/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.cs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid Remove(int[] nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < nums.Length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.go/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums []int, index int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i := index; i < len(nums)-1; i++ {\n nums[i] = nums[i+1]\n }\n}\n
array.swift/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunc remove(nums: inout [Int], index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in nums.indices.dropFirst(index).dropLast() {\n nums[i] = nums[i + 1]\n }\n}\n
array.js/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums, index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.ts/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfunction remove(nums: number[], index: number): void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.dart/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nvoid remove(List<int> nums, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var i = index; i < nums.length - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.rs/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfn remove(nums: &mut Vec<i32>, index: usize) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index..nums.len() - 1 {\n nums[i] = nums[i + 1];\n }\n}\n
array.c/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(int *nums, int size, int index) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int i = index; i < size - 1; i++) {\n nums[i] = nums[i + 1];\n }\n}\n
array.kt/* \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 */\nfun remove(nums: IntArray, index: Int) {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (i in index..<nums.size - 1) {\n nums[i] = nums[i + 1]\n }\n}\n
array.rb### \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20 ###\ndef remove(nums, index)\n # \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for i in index...nums.length\n nums[i] = nums[i + 1] || 0\n end\nend\n
array.zig// \u5220\u9664\u7d22\u5f15 index \u5904\u7684\u5143\u7d20\nfn remove(nums: []i32, index: usize) void {\n // \u628a\u7d22\u5f15 index \u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var i = index;\n while (i < nums.len - 1) : (i += 1) {\n nums[i] = nums[i + 1];\n }\n}\n
Code Visualization Full Screen >
In summary, the insertion and deletion operations in arrays present the following disadvantages:
In most programming languages, we can traverse an array either by using indices or by directly iterating over each element:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef traverse(nums: list[int]):\n \"\"\"\u904d\u5386\u6570\u7ec4\"\"\"\n count = 0\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in range(len(nums)):\n count += nums[i]\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums:\n count += num\n # \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num in enumerate(nums):\n count += nums[i]\n count += num\n
array.cpp/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.java/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int num : nums) {\n count += num;\n }\n}\n
array.cs/* \u904d\u5386\u6570\u7ec4 */\nvoid Traverse(int[] nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n foreach (int num in nums) {\n count += num;\n }\n}\n
array.go/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums []int) {\n count := 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i := 0; i < len(nums); i++ {\n count += nums[i]\n }\n count = 0\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for _, num := range nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for i, num := range nums {\n count += nums[i]\n count += num\n }\n}\n
array.swift/* \u904d\u5386\u6570\u7ec4 */\nfunc traverse(nums: [Int]) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in nums.indices {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n count += num\n }\n // \u540c\u65f6\u904d\u5386\u6570\u636e\u7d22\u5f15\u548c\u5143\u7d20\n for (i, num) in nums.enumerated() {\n count += nums[i]\n count += num\n }\n}\n
array.js/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums) {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.ts/* \u904d\u5386\u6570\u7ec4 */\nfunction traverse(nums: number[]): void {\n let count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (const num of nums) {\n count += num;\n }\n}\n
array.dart/* \u904d\u5386\u6570\u7ec4\u5143\u7d20 */\nvoid traverse(List<int> nums) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (int _num in nums) {\n count += _num;\n }\n // \u901a\u8fc7 forEach \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\n nums.forEach((_num) {\n count += _num;\n });\n}\n
array.rs/* \u904d\u5386\u6570\u7ec4 */\nfn traverse(nums: &[i32]) {\n let mut _count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0..nums.len() {\n _count += nums[i];\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums {\n _count += num;\n }\n}\n
array.c/* \u904d\u5386\u6570\u7ec4 */\nvoid traverse(int *nums, int size) {\n int count = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n count += nums[i];\n }\n}\n
array.kt/* \u904d\u5386\u6570\u7ec4 */\nfun traverse(nums: IntArray) {\n var count = 0\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for (i in nums.indices) {\n count += nums[i]\n }\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (j: Int in nums) {\n count += j\n }\n}\n
array.rb### \u904d\u5386\u6570\u7ec4 ###\ndef traverse(nums)\n count = 0\n\n # \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n for i in 0...nums.length\n count += nums[i]\n end\n\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for num in nums\n count += num\n end\nend\n
array.zig// \u904d\u5386\u6570\u7ec4\nfn traverse(nums: []i32) void {\n var count: i32 = 0;\n // \u901a\u8fc7\u7d22\u5f15\u904d\u5386\u6570\u7ec4\n var i: i32 = 0;\n while (i < nums.len) : (i += 1) {\n count += nums[i];\n }\n count = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\u5143\u7d20\n for (nums) |num| {\n count += num;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#6-finding-elements","title":"6. \u00a0 Finding elements","text":"Locating a specific element within an array involves iterating through the array, checking each element to determine if it matches the desired value.
Because arrays are linear data structures, this operation is commonly referred to as \"linear search.\"
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef find(nums: list[int], target: int) -> int:\n \"\"\"\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\"\"\"\n for i in range(len(nums)):\n if nums[i] == target:\n return i\n return -1\n
array.cpp/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.java/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int[] nums, int target) {\n for (int i = 0; i < nums.length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.cs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint Find(int[] nums, int target) {\n for (int i = 0; i < nums.Length; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.go/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums []int, target int) (index int) {\n index = -1\n for i := 0; i < len(nums); i++ {\n if nums[i] == target {\n index = i\n break\n }\n }\n return\n}\n
array.swift/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunc find(nums: [Int], target: Int) -> Int {\n for i in nums.indices {\n if nums[i] == target {\n return i\n }\n }\n return -1\n}\n
array.js/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums, target) {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) return i;\n }\n return -1;\n}\n
array.ts/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfunction find(nums: number[], target: number): number {\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] === target) {\n return i;\n }\n }\n return -1;\n}\n
array.dart/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(List<int> nums, int target) {\n for (var i = 0; i < nums.length; i++) {\n if (nums[i] == target) return i;\n }\n return -1;\n}\n
array.rs/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n for i in 0..nums.len() {\n if nums[i] == target {\n return Some(i);\n }\n }\n None\n}\n
array.c/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nint find(int *nums, int size, int target) {\n for (int i = 0; i < size; i++) {\n if (nums[i] == target)\n return i;\n }\n return -1;\n}\n
array.kt/* \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 */\nfun find(nums: IntArray, target: Int): Int {\n for (i in nums.indices) {\n if (nums[i] == target) return i\n }\n return -1\n}\n
array.rb### \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20 ###\ndef find(nums, target)\n for i in 0...nums.length\n return i if nums[i] == target\n end\n\n -1\nend\n
array.zig// \u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\nfn find(nums: []i32, target: i32) i32 {\n for (nums, 0..) |num, i| {\n if (num == target) return @intCast(i);\n }\n return -1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#7-expanding-arrays","title":"7. \u00a0 Expanding arrays","text":"In complex system environments, ensuring the availability of memory space after an array for safe capacity extension becomes challenging. Consequently, in most programming languages, the length of an array is immutable.
To expand an array, it's necessary to create a larger array and then copy the elements from the original array. This operation has a time complexity of \\(O(n)\\) and can be time-consuming for large arrays. The code are as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array.pydef extend(nums: list[int], enlarge: int) -> list[int]:\n \"\"\"\u6269\u5c55\u6570\u7ec4\u957f\u5ea6\"\"\"\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = [0] * (len(nums) + enlarge)\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in range(len(nums)):\n res[i] = nums[i]\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n
array.cpp/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = new int[size + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] nums;\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.java/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.cs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint[] Extend(int[] nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int[] res = new int[nums.Length + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < nums.Length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.go/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums []int, enlarge int) []int {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res := make([]int, len(nums)+enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i, num := range nums {\n res[i] = num\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.swift/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = Array(repeating: 0, count: nums.count + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in nums.indices {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.js/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cJavaScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums, enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.ts/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\n// \u8bf7\u6ce8\u610f\uff0cTypeScript \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n// \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\nfunction extend(nums: number[], enlarge: number): number[] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n const res = new Array(nums.length + enlarge).fill(0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (let i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.dart/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nList<int> extend(List<int> nums, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n List<int> res = List.filled(nums.length + enlarge, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (var i = 0; i < nums.length; i++) {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.rs/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfn extend(nums: Vec<i32>, enlarge: usize) -> Vec<i32> {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\n for i in 0..nums.len() {\n res[i] = nums[i];\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\n}\n
array.c/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nint *extend(int *nums, int size, int enlarge) {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size; i++) {\n res[i] = nums[i];\n }\n // \u521d\u59cb\u5316\u6269\u5c55\u540e\u7684\u7a7a\u95f4\n for (int i = size; i < size + enlarge; i++) {\n res[i] = 0;\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
array.kt/* \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n val res = IntArray(nums.size + enlarge)\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (i in nums.indices) {\n res[i] = nums[i]\n }\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res\n}\n
array.rb### \u6269\u5c55\u6570\u7ec4\u957f\u5ea6 ###\n# \u8bf7\u6ce8\u610f\uff0cRuby \u7684 Array \u662f\u52a8\u6001\u6570\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u6269\u5c55\n# \u4e3a\u4e86\u65b9\u4fbf\u5b66\u4e60\uff0c\u672c\u51fd\u6570\u5c06 Array \u770b\u4f5c\u957f\u5ea6\u4e0d\u53ef\u53d8\u7684\u6570\u7ec4\ndef extend(nums, enlarge)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n res = Array.new(nums.length + enlarge, 0)\n\n # \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for i in 0...nums.length\n res[i] = nums[i]\n end\n\n # \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n res\nend\n
array.zig// \u6269\u5c55\u6570\u7ec4\u957f\u5ea6\nfn extend(mem_allocator: std.mem.Allocator, nums: []i32, enlarge: usize) ![]i32 {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u6269\u5c55\u957f\u5ea6\u540e\u7684\u6570\u7ec4\n var res = try mem_allocator.alloc(i32, nums.len + enlarge);\n @memset(res, 0);\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(i32, res, nums);\n // \u8fd4\u56de\u6269\u5c55\u540e\u7684\u65b0\u6570\u7ec4\n return res;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/array/#412-advantages-and-limitations-of-arrays","title":"4.1.2 \u00a0 Advantages and limitations of arrays","text":"Arrays are stored in contiguous memory spaces and consist of elements of the same type. This approach provides substantial prior information that systems can leverage to optimize the efficiency of data structure operations.
However, continuous space storage is a double-edged sword, with the following limitations:
Arrays are fundamental and widely used data structures. They find frequent application in various algorithms and serve in the implementation of complex data structures.
Memory space is a shared resource among all programs. In a complex system environment, available memory can be dispersed throughout the memory space. We understand that the memory allocated for an array must be continuous. However, for very large arrays, finding a sufficiently large contiguous memory space might be challenging. This is where the flexible advantage of linked lists becomes evident.
A \"linked list\" is a linear data structure in which each element is a node object, and the nodes are interconnected through \"references\". These references hold the memory addresses of subsequent nodes, enabling navigation from one node to the next.
The design of linked lists allows for their nodes to be distributed across memory locations without requiring contiguous memory addresses.
Figure 4-5 \u00a0 Linked list definition and storage method
As shown in the figure, we see that the basic building block of a linked list is the \"node\" object. Each node comprises two key components: the node's \"value\" and a \"reference\" to the next node.
null
in Java, nullptr
in C++, and None
in Python.As the code below illustrates, a ListNode
in a linked list, besides holding a value, must also maintain an additional reference (or pointer). Therefore, a linked list occupies more memory space than an array when storing the same quantity of data..
class ListNode:\n \"\"\"Linked list node class\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.next: ListNode | None = None # Reference to the next node\n
/* Linked list node structure */\nstruct ListNode {\n int val; // Node value\n ListNode *next; // Pointer to the next node\n ListNode(int x) : val(x), next(nullptr) {} // Constructor\n};\n
/* Linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode(int x) { val = x; } // Constructor\n}\n
/* Linked list node class */\nclass ListNode(int x) { // Constructor\n int val = x; // Node value\n ListNode? next; // Reference to the next node\n}\n
/* Linked list node structure */\ntype ListNode struct {\n Val int // Node value\n Next *ListNode // Pointer to the next node\n}\n\n// NewListNode Constructor, creates a new linked list\nfunc NewListNode(val int) *ListNode {\n return &ListNode{\n Val: val,\n Next: nil,\n }\n}\n
/* Linked list node class */\nclass ListNode {\n var val: Int // Node value\n var next: ListNode? // Reference to the next node\n\n init(x: Int) { // Constructor\n val = x\n }\n}\n
/* Linked list node class */\nclass ListNode {\n constructor(val, next) {\n this.val = (val === undefined ? 0 : val); // Node value\n this.next = (next === undefined ? null : next); // Reference to the next node\n }\n}\n
/* Linked list node class */\nclass ListNode {\n val: number;\n next: ListNode | null;\n constructor(val?: number, next?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the next node\n }\n}\n
/* Linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode? next; // Reference to the next node\n ListNode(this.val, [this.next]); // Constructor\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* Linked list node class */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // Node value\n next: Option<Rc<RefCell<ListNode>>>, // Pointer to the next node\n}\n
/* Linked list node structure */\ntypedef struct ListNode {\n int val; // Node value\n struct ListNode *next; // Pointer to the next node\n} ListNode;\n\n/* Constructor */\nListNode *newListNode(int val) {\n ListNode *node;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n return node;\n}\n
\n
// Linked list node class\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // Node value\n next: ?*Self = null, // Pointer to the next node\n\n // Constructor\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n }\n };\n}\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421-common-operations-on-linked-lists","title":"4.2.1 \u00a0 Common operations on linked lists","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1-initializing-a-linked-list","title":"1. \u00a0 Initializing a linked list","text":"Constructing a linked list is a two-step process: first, initializing each node object, and second, forming the reference links between the nodes. After initialization, we can traverse all nodes sequentially from the head node by following the next
reference.
# Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4\n# Initialize each node\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.cpp/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.java/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.cs/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.go/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// Build references between nodes\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
linked_list.swift/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// Build references between nodes\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.js/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.ts/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.dart/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// Build references between nodes\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rs/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// Build references between nodes\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c/* Initialize linked list: 1 -> 3 -> 2 -> 5 -> 4 */\n// Initialize each node\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// Build references between nodes\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.kt\n
linked_list.zig// Initialize linked list\n// Initialize each node\nvar n0 = inc.ListNode(i32){.val = 1};\nvar n1 = inc.ListNode(i32){.val = 3};\nvar n2 = inc.ListNode(i32){.val = 2};\nvar n3 = inc.ListNode(i32){.val = 5};\nvar n4 = inc.ListNode(i32){.val = 4};\n// Build references between nodes\nn0.next = &n1;\nn1.next = &n2;\nn2.next = &n3;\nn3.next = &n4;\n
The array as a whole is a variable, for instance, the array nums
includes elements like nums[0]
, nums[1]
, and so on, whereas a linked list is made up of several distinct node objects. We typically refer to a linked list by its head node, for example, the linked list in the previous code snippet is referred to as n0
.
Inserting a node into a linked list is very easy. As shown in the figure, let's assume we aim to insert a new node P
between two adjacent nodes n0
and n1
. This can be achieved by simply modifying two node references (pointers), with a time complexity of \\(O(1)\\).
By comparison, inserting an element into an array has a time complexity of \\(O(n)\\), which becomes less efficient when dealing with large data volumes.
Figure 4-6 \u00a0 Linked list node insertion example
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef insert(n0: ListNode, P: ListNode):\n \"\"\"\u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\"\"\"\n n1 = n0.next\n P.next = n1\n n0.next = P\n
linked_list.cpp/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid Insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n n1 := n0.Next\n P.Next = n1\n n0.Next = P\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunc insert(n0: ListNode, P: ListNode) {\n let n1 = n0.next\n P.next = n1\n n0.next = P\n}\n
linked_list.js/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0, P) {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nfunction insert(n0: ListNode, P: ListNode): void {\n const n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode n0, ListNode P) {\n ListNode? n1 = n0.next;\n P.next = n1;\n n0.next = P;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n let n1 = n0.borrow_mut().next.take();\n P.borrow_mut().next = n1;\n n0.borrow_mut().next = Some(P);\n}\n
linked_list.c/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P */\nvoid insert(ListNode *n0, ListNode *P) {\n ListNode *n1 = n0->next;\n P->next = n1;\n n0->next = P;\n}\n
linked_list.kt/* \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9p */\nfun insert(n0: ListNode?, p: ListNode?) {\n val n1 = n0?.next\n p?.next = n1\n n0?.next = p\n}\n
linked_list.rb### \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 _p ###\n# Ruby \u7684 `p` \u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c `P` \u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528 `_p` \u4ee3\u66ff\ndef insert(n0, _p)\n n1 = n0.next\n _p.next = n1\n n0.next = _p\nend\n
linked_list.zig// \u5728\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u63d2\u5165\u8282\u70b9 P\nfn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {\n var n1 = n0.?.next;\n P.?.next = n1;\n n0.?.next = P;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#3-deleting-nodes","title":"3. \u00a0 Deleting nodes","text":"As shown in the figure, deleting a node from a linked list is also very easy, involving only the modification of a single node's reference (pointer).
It's important to note that even though node P
continues to point to n1
after being deleted, it becomes inaccessible during linked list traversal. This effectively means that P
is no longer a part of the linked list.
Figure 4-7 \u00a0 Linked list node deletion
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef remove(n0: ListNode):\n \"\"\"\u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n if not n0.next:\n return\n # n0 -> P -> n1\n P = n0.next\n n1 = P.next\n n0.next = n1\n
linked_list.cpp/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode *n0) {\n if (n0->next == nullptr)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n delete P;\n}\n
linked_list.java/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode n1 = P.next;\n n0.next = n1;\n}\n
linked_list.cs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid Remove(ListNode n0) {\n if (n0.next == null)\n return;\n // n0 -> P -> n1\n ListNode P = n0.next;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.go/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc removeItem(n0 *ListNode) {\n if n0.Next == nil {\n return\n }\n // n0 -> P -> n1\n P := n0.Next\n n1 := P.Next\n n0.Next = n1\n}\n
linked_list.swift/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunc remove(n0: ListNode) {\n if n0.next == nil {\n return\n }\n // n0 -> P -> n1\n let P = n0.next\n let n1 = P?.next\n n0.next = n1\n}\n
linked_list.js/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0) {\n if (!n0.next) return;\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.ts/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfunction remove(n0: ListNode): void {\n if (!n0.next) {\n return;\n }\n // n0 -> P -> n1\n const P = n0.next;\n const n1 = P.next;\n n0.next = n1;\n}\n
linked_list.dart/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nvoid remove(ListNode n0) {\n if (n0.next == null) return;\n // n0 -> P -> n1\n ListNode P = n0.next!;\n ListNode? n1 = P.next;\n n0.next = n1;\n}\n
linked_list.rs/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n if n0.borrow().next.is_none() {\n return;\n };\n // n0 -> P -> n1\n let P = n0.borrow_mut().next.take();\n if let Some(node) = P {\n let n1 = node.borrow_mut().next.take();\n n0.borrow_mut().next = n1;\n }\n}\n
linked_list.c/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nvoid removeItem(ListNode *n0) {\n if (!n0->next)\n return;\n // n0 -> P -> n1\n ListNode *P = n0->next;\n ListNode *n1 = P->next;\n n0->next = n1;\n // \u91ca\u653e\u5185\u5b58\n free(P);\n}\n
linked_list.kt/* \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 */\nfun remove(n0: ListNode?) {\n val p = n0?.next\n val n1 = p?.next\n n0?.next = n1\n}\n
linked_list.rb### \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9 ###\ndef remove(n0)\n return if n0.next.nil?\n\n # n0 -> remove_node -> n1\n remove_node = n0.next\n n1 = remove_node.next\n n0.next = n1\nend\n
linked_list.zig// \u5220\u9664\u94fe\u8868\u7684\u8282\u70b9 n0 \u4e4b\u540e\u7684\u9996\u4e2a\u8282\u70b9\nfn remove(n0: ?*inc.ListNode(i32)) void {\n if (n0.?.next == null) return;\n // n0 -> P -> n1\n var P = n0.?.next;\n var n1 = P.?.next;\n n0.?.next = n1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#4-accessing-nodes","title":"4. \u00a0 Accessing nodes","text":"Accessing nodes in a linked list is less efficient. As previously mentioned, any element in an array can be accessed in \\(O(1)\\) time. In contrast, with a linked list, the program involves starting from the head node and sequentially traversing through the nodes until the desired node is found. In other words, to access the \\(i\\)-th node in a linked list, the program must iterate through \\(i - 1\\) nodes, resulting in a time complexity of \\(O(n)\\).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linked_list.pydef access(head: ListNode, index: int) -> ListNode | None:\n \"\"\"\u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\"\"\"\n for _ in range(index):\n if not head:\n return None\n head = head.next\n return head\n
linked_list.cpp/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == nullptr)\n return nullptr;\n head = head->next;\n }\n return head;\n}\n
linked_list.java/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode access(ListNode head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.cs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? Access(ListNode? head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == null)\n return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.go/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head *ListNode, index int) *ListNode {\n for i := 0; i < index; i++ {\n if head == nil {\n return nil\n }\n head = head.Next\n }\n return head\n}\n
linked_list.swift/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n var head: ListNode? = head\n for _ in 0 ..< index {\n if head == nil {\n return nil\n }\n head = head?.next\n }\n return head\n}\n
linked_list.js/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head, index) {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.ts/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n for (let i = 0; i < index; i++) {\n if (!head) {\n return null;\n }\n head = head.next;\n }\n return head;\n}\n
linked_list.dart/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode? access(ListNode? head, int index) {\n for (var i = 0; i < index; i++) {\n if (head == null) return null;\n head = head.next;\n }\n return head;\n}\n
linked_list.rs/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {\n if index <= 0 {\n return head;\n };\n if let Some(node) = &head.borrow().next {\n return access(node.clone(), index - 1);\n }\n\n return head;\n}\n
linked_list.c/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nListNode *access(ListNode *head, int index) {\n for (int i = 0; i < index; i++) {\n if (head == NULL)\n return NULL;\n head = head->next;\n }\n return head;\n}\n
linked_list.kt/* \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 */\nfun access(head: ListNode?, index: Int): ListNode? {\n var h = head\n for (i in 0..<index) {\n h = h?.next\n }\n return h\n}\n
linked_list.rb### \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9 ###\ndef access(head, index)\n for i in 0...index\n return nil if head.nil?\n head = head.next\n end\n\n head\nend\n
linked_list.zig// \u8bbf\u95ee\u94fe\u8868\u4e2d\u7d22\u5f15\u4e3a index \u7684\u8282\u70b9\nfn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {\n var head = node;\n var i: i32 = 0;\n while (i < index) : (i += 1) {\n head = head.?.next;\n if (head == null) return null;\n }\n return head;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#5-finding-nodes","title":"5. \u00a0 Finding nodes","text":"Traverse the linked list to locate a node whose value matches target
, and then output the index of that node within the linked list. This procedure is also an example of linear search. The corresponding code is provided below:
def find(head: ListNode, target: int) -> int:\n \"\"\"\u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\"\"\"\n index = 0\n while head:\n if head.val == target:\n return index\n head = head.next\n index += 1\n return -1\n
linked_list.cpp/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head != nullptr) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.java/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.cs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint Find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target)\n return index;\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.go/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc findNode(head *ListNode, target int) int {\n index := 0\n for head != nil {\n if head.Val == target {\n return index\n }\n head = head.Next\n index++\n }\n return -1\n}\n
linked_list.swift/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunc find(head: ListNode, target: Int) -> Int {\n var head: ListNode? = head\n var index = 0\n while head != nil {\n if head?.val == target {\n return index\n }\n head = head?.next\n index += 1\n }\n return -1\n}\n
linked_list.js/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head, target) {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.ts/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfunction find(head: ListNode | null, target: number): number {\n let index = 0;\n while (head !== null) {\n if (head.val === target) {\n return index;\n }\n head = head.next;\n index += 1;\n }\n return -1;\n}\n
linked_list.dart/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode? head, int target) {\n int index = 0;\n while (head != null) {\n if (head.val == target) {\n return index;\n }\n head = head.next;\n index++;\n }\n return -1;\n}\n
linked_list.rs/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {\n if head.borrow().val == target {\n return index;\n };\n if let Some(node) = &head.borrow_mut().next {\n return find(node.clone(), target, index + 1);\n }\n return -1;\n}\n
linked_list.c/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nint find(ListNode *head, int target) {\n int index = 0;\n while (head) {\n if (head->val == target)\n return index;\n head = head->next;\n index++;\n }\n return -1;\n}\n
linked_list.kt/* \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 */\nfun find(head: ListNode?, target: Int): Int {\n var index = 0\n var h = head\n while (h != null) {\n if (h.value == target) return index\n h = h.next\n index++\n }\n return -1\n}\n
linked_list.rb### \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9 ###\ndef find(head, target)\n index = 0\n while head\n return index if head.val == target\n head = head.next\n index += 1\n end\n\n -1\nend\n
linked_list.zig// \u5728\u94fe\u8868\u4e2d\u67e5\u627e\u503c\u4e3a target \u7684\u9996\u4e2a\u8282\u70b9\nfn find(node: ?*inc.ListNode(i32), target: i32) i32 {\n var head = node;\n var index: i32 = 0;\n while (head != null) {\n if (head.?.val == target) return index;\n head = head.?.next;\n index += 1;\n }\n return -1;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/linked_list/#422-arrays-vs-linked-lists","title":"4.2.2 \u00a0 Arrays vs. linked lists","text":"The Table 4-1 summarizes the characteristics of arrays and linked lists, and it also compares their efficiencies in various operations. Because they utilize opposing storage strategies, their respective properties and operational efficiencies exhibit distinct contrasts.
Table 4-1 \u00a0 Efficiency comparison of arrays and linked lists
Arrays Linked Lists Storage Contiguous Memory Space Dispersed Memory Space Capacity Expansion Fixed Length Flexible Expansion Memory Efficiency Less Memory per Element, Potential Space Wastage More Memory per Element Accessing Elements \\(O(1)\\) \\(O(n)\\) Adding Elements \\(O(n)\\) \\(O(1)\\) Deleting Elements \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423-common-types-of-linked-lists","title":"4.2.3 \u00a0 Common types of linked lists","text":"As shown in the figure, there are three common types of linked lists.
None
), is the tail node.class ListNode:\n \"\"\"Bidirectional linked list node class\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.next: ListNode | None = None # Reference to the successor node\n self.prev: ListNode | None = None # Reference to a predecessor node\n
/* Bidirectional linked list node structure */\nstruct ListNode {\n int val; // Node value\n ListNode *next; // Pointer to the successor node\n ListNode *prev; // Pointer to the predecessor node\n ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // Constructor\n};\n
/* Bidirectional linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n ListNode(int x) { val = x; } // Constructor\n}\n
/* Bidirectional linked list node class */\nclass ListNode(int x) { // Constructor\n int val = x; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n}\n
/* Bidirectional linked list node structure */\ntype DoublyListNode struct {\n Val int // Node value\n Next *DoublyListNode // Pointer to the successor node\n Prev *DoublyListNode // Pointer to the predecessor node\n}\n\n// NewDoublyListNode initialization\nfunc NewDoublyListNode(val int) *DoublyListNode {\n return &DoublyListNode{\n Val: val,\n Next: nil,\n Prev: nil,\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n var val: Int // Node value\n var next: ListNode? // Reference to the next node\n var prev: ListNode? // Reference to the predecessor node\n\n init(x: Int) { // Constructor\n val = x\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n constructor(val, next, prev) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the successor node\n this.prev = prev === undefined ? null : prev; // Reference to the predecessor node\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n val: number;\n next: ListNode | null;\n prev: ListNode | null;\n constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.next = next === undefined ? null : next; // Reference to the successor node\n this.prev = prev === undefined ? null : prev; // Reference to the predecessor node\n }\n}\n
/* Bidirectional linked list node class */\nclass ListNode {\n int val; // Node value\n ListNode next; // Reference to the next node\n ListNode prev; // Reference to the predecessor node\n ListNode(this.val, [this.next, this.prev]); // Constructor\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Bidirectional linked list node type */\n#[derive(Debug)]\nstruct ListNode {\n val: i32, // Node value\n next: Option<Rc<RefCell<ListNode>>>, // Pointer to successor node\n prev: Option<Rc<RefCell<ListNode>>>, // Pointer to predecessor node\n}\n\n/* Constructors */\nimpl ListNode {\n fn new(val: i32) -> Self {\n ListNode {\n val,\n next: None,\n prev: None,\n }\n }\n}\n
/* Bidirectional linked list node structure */\ntypedef struct ListNode {\n int val; // Node value\n struct ListNode *next; // Pointer to the successor node\n struct ListNode *prev; // Pointer to the predecessor node\n} ListNode;\n\n/* Constructors */\nListNode *newListNode(int val) {\n ListNode *node, *next;\n node = (ListNode *) malloc(sizeof(ListNode));\n node->val = val;\n node->next = NULL;\n node->prev = NULL;\n return node;\n}\n
\n
// Bidirectional linked list node class\npub fn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = 0, // Node value\n next: ?*Self = null, // Pointer to the successor node\n prev: ?*Self = null, // Pointer to the predecessor node\n\n // Constructor\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n
Figure 4-8 \u00a0 Common types of linked lists
"},{"location":"chapter_array_and_linkedlist/linked_list/#424-typical-applications-of-linked-lists","title":"4.2.4 \u00a0 Typical applications of linked lists","text":"Singly linked lists are frequently utilized in implementing stacks, queues, hash tables, and graphs.
Doubly linked lists are ideal for scenarios requiring rapid access to preceding and succeeding elements.
Circular linked lists are ideal for applications that require periodic operations, such as resource scheduling in operating systems.
A \"list\" is an abstract data structure concept that represents an ordered collection of elements, supporting operations such as element access, modification, addition, deletion, and traversal, without requiring users to consider capacity limitations. Lists can be implemented based on linked lists or arrays.
When implementing lists using arrays, the immutability of length reduces the practicality of the list. This is because predicting the amount of data to be stored in advance is often challenging, making it difficult to choose an appropriate list length. If the length is too small, it may not meet the requirements; if too large, it may waste memory space.
To solve this problem, we can implement lists using a \"dynamic array.\" It inherits the advantages of arrays and can dynamically expand during program execution.
In fact, many programming languages' standard libraries implement lists using dynamic arrays, such as Python's list
, Java's ArrayList
, C++'s vector
, and C#'s List
. In the following discussion, we will consider \"list\" and \"dynamic array\" as synonymous concepts.
We typically use two initialization methods: \"without initial values\" and \"with initial values\".
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Initialize list\n# Without initial values\nnums1: list[int] = []\n# With initial values\nnums: list[int] = [1, 3, 2, 5, 4]\n
list.cpp/* Initialize list */\n// Note, in C++ the vector is the equivalent of nums described here\n// Without initial values\nvector<int> nums1;\n// With initial values\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java/* Initialize list */\n// Without initial values\nList<Integer> nums1 = new ArrayList<>();\n// With initial values (note the element type should be the wrapper class Integer[] for int[])\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs/* Initialize list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
list_test.go/* Initialize list */\n// Without initial values\nnums1 := []int{}\n// With initial values\nnums := []int{1, 3, 2, 5, 4}\n
list.swift/* Initialize list */\n// Without initial values\nlet nums1: [Int] = []\n// With initial values\nvar nums = [1, 3, 2, 5, 4]\n
list.js/* Initialize list */\n// Without initial values\nconst nums1 = [];\n// With initial values\nconst nums = [1, 3, 2, 5, 4];\n
list.ts/* Initialize list */\n// Without initial values\nconst nums1: number[] = [];\n// With initial values\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart/* Initialize list */\n// Without initial values\nList<int> nums1 = [];\n// With initial values\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs/* Initialize list */\n// Without initial values\nlet nums1: Vec<i32> = Vec::new();\n// With initial values\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Initialize list\nvar nums = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums.deinit();\ntry nums.appendSlice(&[_]i32{ 1, 3, 2, 5, 4 });\n
"},{"location":"chapter_array_and_linkedlist/list/#2-accessing-elements","title":"2. \u00a0 Accessing elements","text":"Lists are essentially arrays, thus they can access and update elements in \\(O(1)\\) time, which is very efficient.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Access elements\nnum: int = nums[1] # Access the element at index 1\n\n# Update elements\nnums[1] = 0 # Update the element at index 1 to 0\n
list.cpp/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.java/* Access elements */\nint num = nums.get(1); // Access the element at index 1\n\n/* Update elements */\nnums.set(1, 0); // Update the element at index 1 to 0\n
list.cs/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list_test.go/* Access elements */\nnum := nums[1] // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0 // Update the element at index 1 to 0\n
list.swift/* Access elements */\nlet num = nums[1] // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0 // Update the element at index 1 to 0\n
list.js/* Access elements */\nconst num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.ts/* Access elements */\nconst num: number = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.dart/* Access elements */\nint num = nums[1]; // Access the element at index 1\n\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.rs/* Access elements */\nlet num: i32 = nums[1]; // Access the element at index 1\n/* Update elements */\nnums[1] = 0; // Update the element at index 1 to 0\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Access elements\nvar num = nums.items[1]; // Access the element at index 1\n\n// Update elements\nnums.items[1] = 0; // Update the element at index 1 to 0 \n
"},{"location":"chapter_array_and_linkedlist/list/#3-inserting-and-removing-elements","title":"3. \u00a0 Inserting and removing elements","text":"Compared to arrays, lists offer more flexibility in adding and removing elements. While adding elements to the end of a list is an \\(O(1)\\) operation, the efficiency of inserting and removing elements elsewhere in the list remains the same as in arrays, with a time complexity of \\(O(n)\\).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Clear list\nnums.clear()\n\n# Append elements at the end\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# Insert element in the middle\nnums.insert(3, 6) # Insert number 6 at index 3\n\n# Remove elements\nnums.pop(3) # Remove the element at index 3\n
list.cpp/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* Insert element in the middle */\nnums.insert(nums.begin() + 3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.erase(nums.begin() + 3); // Remove the element at index 3\n
list.java/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert element in the middle */\nnums.add(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(3); // Remove the element at index 3\n
list.cs/* Clear list */\nnums.Clear();\n\n/* Append elements at the end */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* Insert element in the middle */\nnums.Insert(3, 6);\n\n/* Remove elements */\nnums.RemoveAt(3);\n
list_test.go/* Clear list */\nnums = nil\n\n/* Append elements at the end */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* Insert element in the middle */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // Insert number 6 at index 3\n\n/* Remove elements */\nnums = append(nums[:3], nums[4:]...) // Remove the element at index 3\n
list.swift/* Clear list */\nnums.removeAll()\n\n/* Append elements at the end */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* Insert element in the middle */\nnums.insert(6, at: 3) // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(at: 3) // Remove the element at index 3\n
list.js/* Clear list */\nnums.length = 0;\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.splice(3, 0, 6);\n\n/* Remove elements */\nnums.splice(3, 1);\n
list.ts/* Clear list */\nnums.length = 0;\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.splice(3, 0, 6);\n\n/* Remove elements */\nnums.splice(3, 1);\n
list.dart/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* Insert element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.removeAt(3); // Remove the element at index 3\n
list.rs/* Clear list */\nnums.clear();\n\n/* Append elements at the end */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* Insert element in the middle */\nnums.insert(3, 6); // Insert number 6 at index 3\n\n/* Remove elements */\nnums.remove(3); // Remove the element at index 3\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Clear list\nnums.clearRetainingCapacity();\n\n// Append elements at the end\ntry nums.append(1);\ntry nums.append(3);\ntry nums.append(2);\ntry nums.append(5);\ntry nums.append(4);\n\n// Insert element in the middle\ntry nums.insert(3, 6); // Insert number 6 at index 3\n\n// Remove elements\n_ = nums.orderedRemove(3); // Remove the element at index 3\n
"},{"location":"chapter_array_and_linkedlist/list/#4-iterating-the-list","title":"4. \u00a0 Iterating the list","text":"Similar to arrays, lists can be iterated either by using indices or by directly iterating through each element.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Iterate through the list by index\ncount = 0\nfor i in range(len(nums)):\n count += nums[i]\n\n# Iterate directly through list elements\nfor num in nums:\n count += num\n
list.cpp/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (int num : nums) {\n count += num;\n}\n
list.java/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n count += nums.get(i);\n}\n\n/* Iterate directly through list elements */\nfor (int num : nums) {\n count += num;\n}\n
list.cs/* Iterate through the list by index */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nforeach (int num in nums) {\n count += num;\n}\n
list_test.go/* Iterate through the list by index */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n count += nums[i]\n}\n\n/* Iterate directly through list elements */\ncount = 0\nfor _, num := range nums {\n count += num\n}\n
list.swift/* Iterate through the list by index */\nvar count = 0\nfor i in nums.indices {\n count += nums[i]\n}\n\n/* Iterate directly through list elements */\ncount = 0\nfor num in nums {\n count += num\n}\n
list.js/* Iterate through the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.ts/* Iterate through the list by index */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (const num of nums) {\n count += num;\n}\n
list.dart/* Iterate through the list by index */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n count += nums[i];\n}\n\n/* Iterate directly through list elements */\ncount = 0;\nfor (var num in nums) {\n count += num;\n}\n
list.rs// Iterate through the list by index\nlet mut _count = 0;\nfor i in 0..nums.len() {\n _count += nums[i];\n}\n\n// Iterate directly through list elements\n_count = 0;\nfor num in &nums {\n _count += num;\n}\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Iterate through the list by index\nvar count: i32 = 0;\nvar i: i32 = 0;\nwhile (i < nums.items.len) : (i += 1) {\n count += nums[i];\n}\n\n// Iterate directly through list elements\ncount = 0;\nfor (nums.items) |num| {\n count += num;\n}\n
"},{"location":"chapter_array_and_linkedlist/list/#5-concatenating-lists","title":"5. \u00a0 Concatenating lists","text":"Given a new list nums1
, we can append it to the end of the original list.
# Concatenate two lists\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1 # Concatenate nums1 to the end of nums\n
list.cpp/* Concatenate two lists */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// Concatenate nums1 to the end of nums\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java/* Concatenate two lists */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1); // Concatenate nums1 to the end of nums\n
list.cs/* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1); // Concatenate nums1 to the end of nums\n
list_test.go/* Concatenate two lists */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...) // Concatenate nums1 to the end of nums\n
list.swift/* Concatenate two lists */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // Concatenate nums1 to the end of nums\n
list.js/* Concatenate two lists */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1); // Concatenate nums1 to the end of nums\n
list.ts/* Concatenate two lists */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1); // Concatenate nums1 to the end of nums\n
list.dart/* Concatenate two lists */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1); // Concatenate nums1 to the end of nums\n
list.rs/* Concatenate two lists */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Concatenate two lists\nvar nums1 = std.ArrayList(i32).init(std.heap.page_allocator);\ndefer nums1.deinit();\ntry nums1.appendSlice(&[_]i32{ 6, 8, 7, 10, 9 });\ntry nums.insertSlice(nums.items.len, nums1.items); // Concatenate nums1 to the end of nums\n
"},{"location":"chapter_array_and_linkedlist/list/#6-sorting-the-list","title":"6. \u00a0 Sorting the list","text":"Once the list is sorted, we can employ algorithms commonly used in array-related algorithm problems, such as \"binary search\" and \"two-pointer\" algorithms.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig list.py# Sort the list\nnums.sort() # After sorting, the list elements are in ascending order\n
list.cpp/* Sort the list */\nsort(nums.begin(), nums.end()); // After sorting, the list elements are in ascending order\n
list.java/* Sort the list */\nCollections.sort(nums); // After sorting, the list elements are in ascending order\n
list.cs/* Sort the list */\nnums.Sort(); // After sorting, the list elements are in ascending order\n
list_test.go/* Sort the list */\nsort.Ints(nums) // After sorting, the list elements are in ascending order\n
list.swift/* Sort the list */\nnums.sort() // After sorting, the list elements are in ascending order\n
list.js/* Sort the list */ \nnums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order\n
list.ts/* Sort the list */\nnums.sort((a, b) => a - b); // After sorting, the list elements are in ascending order\n
list.dart/* Sort the list */\nnums.sort(); // After sorting, the list elements are in ascending order\n
list.rs/* Sort the list */\nnums.sort(); // After sorting, the list elements are in ascending order\n
list.c// C does not provide built-in dynamic arrays\n
list.kt\n
list.zig// Sort the list\nstd.sort.sort(i32, nums.items, {}, comptime std.sort.asc(i32));\n
"},{"location":"chapter_array_and_linkedlist/list/#432-list-implementation","title":"4.3.2 \u00a0 List implementation","text":"Many programming languages come with built-in lists, including Java, C++, Python, etc. Their implementations tend to be intricate, featuring carefully considered settings for various parameters, like initial capacity and expansion factors. Readers who are curious can delve into the source code for further learning.
To enhance our understanding of how lists work, we will attempt to implement a simplified version of a list, focusing on three crucial design aspects:
size
to record the current number of elements in the list, updating in real-time with element insertion and deletion. With this variable, we can locate the end of the list and determine whether expansion is needed.class MyList:\n \"\"\"\u5217\u8868\u7c7b\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._capacity: int = 10 # \u5217\u8868\u5bb9\u91cf\n self._arr: list[int] = [0] * self._capacity # \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n self._size: int = 0 # \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n self._extend_ratio: int = 2 # \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\"\"\"\n return self._size\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u5217\u8868\u5bb9\u91cf\"\"\"\n return self._capacity\n\n def get(self, index: int) -> int:\n \"\"\"\u8bbf\u95ee\u5143\u7d20\"\"\"\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n return self._arr[index]\n\n def set(self, num: int, index: int):\n \"\"\"\u66f4\u65b0\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n self._arr[index] = num\n\n def add(self, num: int):\n \"\"\"\u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\"\"\"\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size() == self.capacity():\n self.extend_capacity()\n self._arr[self._size] = num\n self._size += 1\n\n def insert(self, num: int, index: int):\n \"\"\"\u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self._size == self.capacity():\n self.extend_capacity()\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in range(self._size - 1, index - 1, -1):\n self._arr[j + 1] = self._arr[j]\n self._arr[index] = num\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size += 1\n\n def remove(self, index: int) -> int:\n \"\"\"\u5220\u9664\u5143\u7d20\"\"\"\n if index < 0 or index >= self._size:\n raise IndexError(\"\u7d22\u5f15\u8d8a\u754c\")\n num = self._arr[index]\n # \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in range(index, self._size - 1):\n self._arr[j] = self._arr[j + 1]\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self._size -= 1\n # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n\n def extend_capacity(self):\n \"\"\"\u5217\u8868\u6269\u5bb9\"\"\"\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self._capacity = len(self._arr)\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868\"\"\"\n return self._arr[: self._size]\n
my_list.cpp/* \u5217\u8868\u7c7b */\nclass MyList {\n private:\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~MyList() {\n delete[] arr;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n arr[size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int num) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size() == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size() - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index < 0 || index >= size())\n throw out_of_range(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size() - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n int newCapacity = capacity() * extendRatio;\n int *tmp = arr;\n arr = new int[newCapacity];\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n arr[i] = tmp[i];\n }\n // \u91ca\u653e\u5185\u5b58\n delete[] tmp;\n arrCapacity = newCapacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Vector \u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> vec(size());\n for (int i = 0; i < size(); i++) {\n vec[i] = arr[i];\n }\n return vec;\n }\n};\n
my_list.java/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int capacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[capacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n public int size() {\n return size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int capacity() {\n return capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void set(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n arr[size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void insert(int index, int num) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = size - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int remove(int index) {\n if (index < 0 || index >= size)\n throw new IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < size - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = Arrays.copyOf(arr, capacity() * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] toArray() {\n int size = size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[size];\n for (int i = 0; i < size; i++) {\n arr[i] = get(i);\n }\n return arr;\n }\n}\n
my_list.cs/* \u5217\u8868\u7c7b */\nclass MyList {\n private int[] arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private int arrCapacity = 10; // \u5217\u8868\u5bb9\u91cf\n private int arrSize = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private readonly int extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public MyList() {\n arr = new int[arrCapacity];\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public int Size() {\n return arrSize;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public int Capacity() {\n return arrCapacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public int Get(int index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n return arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public void Set(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public void Add(int num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n arr[arrSize] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public void Insert(int index, int num) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (arrSize == arrCapacity)\n ExtendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (int j = arrSize - 1; j >= index; j--) {\n arr[j + 1] = arr[j];\n }\n arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public int Remove(int index) {\n if (index < 0 || index >= arrSize)\n throw new IndexOutOfRangeException(\"\u7d22\u5f15\u8d8a\u754c\");\n int num = arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (int j = index; j < arrSize - 1; j++) {\n arr[j] = arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n arrSize--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public void ExtendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a arrCapacity * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n Array.Resize(ref arr, arrCapacity * extendRatio);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n arrCapacity = arr.Length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] arr = new int[arrSize];\n for (int i = 0; i < arrSize; i++) {\n arr[i] = Get(i);\n }\n return arr;\n }\n}\n
my_list.go/* \u5217\u8868\u7c7b */\ntype myList struct {\n arrCapacity int\n arr []int\n arrSize int\n extendRatio int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newMyList() *myList {\n return &myList{\n arrCapacity: 10, // \u5217\u8868\u5bb9\u91cf\n arr: make([]int, 10), // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrSize: 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n }\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\nfunc (l *myList) size() int {\n return l.arrSize\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nfunc (l *myList) capacity() int {\n return l.arrCapacity\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nfunc (l *myList) get(index int) int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return l.arr[index]\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nfunc (l *myList) set(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n l.arr[index] = num\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nfunc (l *myList) add(num int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n l.arr[l.arrSize] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nfunc (l *myList) insert(num, index int) {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if l.arrSize == l.arrCapacity {\n l.extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j := l.arrSize - 1; j >= index; j-- {\n l.arr[j+1] = l.arr[j]\n }\n l.arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize++\n}\n\n/* \u5220\u9664\u5143\u7d20 */\nfunc (l *myList) remove(index int) int {\n if index < 0 || index >= l.arrSize {\n panic(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n num := l.arr[index]\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j := index; j < l.arrSize-1; j++ {\n l.arr[j] = l.arr[j+1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n l.arrSize--\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nfunc (l *myList) extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n l.arrCapacity = len(l.arr)\n}\n\n/* \u8fd4\u56de\u6709\u6548\u957f\u5ea6\u7684\u5217\u8868 */\nfunc (l *myList) toArray() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n return l.arr[:l.arrSize]\n}\n
my_list.swift/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: [Int] // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var _capacity: Int // \u5217\u8868\u5bb9\u91cf\n private var _size: Int // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private let extendRatio: Int // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n _capacity = 10\n _size = 0\n extendRatio = 2\n arr = Array(repeating: 0, count: _capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n func size() -> Int {\n _size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n func capacity() -> Int {\n _capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n func get(index: Int) -> Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\u5219\u629b\u51fa\u9519\u8bef\uff0c\u4e0b\u540c\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n func set(index: Int, num: Int) {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n func add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if size() == capacity() {\n extendCapacity()\n }\n arr[size()] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n func insert(index: Int, num: Int) {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if size() == capacity() {\n extendCapacity()\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index ..< size()).reversed() {\n arr[j + 1] = arr[j]\n }\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size += 1\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n @discardableResult\n func remove(index: Int) -> Int {\n if index < 0 || index >= size() {\n fatalError(\"\u7d22\u5f15\u8d8a\u754c\")\n }\n let num = arr[index]\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index ..< (size() - 1) {\n arr[j] = arr[j + 1]\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size -= 1\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n func extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = arr.count\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n func toArray() -> [Int] {\n Array(arr.prefix(size()))\n }\n}\n
my_list.js/* \u5217\u8868\u7c7b */\nclass MyList {\n #arr = new Array(); // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n #capacity = 10; // \u5217\u8868\u5bb9\u91cf\n #size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n #extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#arr = new Array(this.#capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n size() {\n return this.#size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n capacity() {\n return this.#capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n get(index) {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.#arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n set(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.#arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n add(num) {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.#arr[this.#size] = num;\n this.#size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n insert(index, num) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this.#size === this.#capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this.#size - 1; j >= index; j--) {\n this.#arr[j + 1] = this.#arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#arr[index] = num;\n this.#size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n remove(index) {\n if (index < 0 || index >= this.#size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.#arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this.#size - 1; j++) {\n this.#arr[j] = this.#arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.#size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n this.#arr = this.#arr.concat(\n new Array(this.capacity() * (this.#extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this.#capacity = this.#arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n toArray() {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.ts/* \u5217\u8868\u7c7b */\nclass MyList {\n private arr: Array<number>; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private _capacity: number = 10; // \u5217\u8868\u5bb9\u91cf\n private _size: number = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private extendRatio: number = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.arr = new Array(this._capacity);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n public size(): number {\n return this._size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n public capacity(): number {\n return this._capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n public get(index: number): number {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n return this.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n public set(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n this.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n public add(num: number): void {\n // \u5982\u679c\u957f\u5ea6\u7b49\u4e8e\u5bb9\u91cf\uff0c\u5219\u9700\u8981\u6269\u5bb9\n if (this._size === this._capacity) this.extendCapacity();\n // \u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u5c3e\u90e8\n this.arr[this._size] = num;\n this._size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n public insert(index: number, num: number): void {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (this._size === this._capacity) {\n this.extendCapacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (let j = this._size - 1; j >= index; j--) {\n this.arr[j + 1] = this.arr[j];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this.arr[index] = num;\n this._size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n public remove(index: number): number {\n if (index < 0 || index >= this._size) throw new Error('\u7d22\u5f15\u8d8a\u754c');\n let num = this.arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (let j = index; j < this._size - 1; j++) {\n this.arr[j] = this.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n this._size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n public extendCapacity(): void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n this.arr = this.arr.concat(\n new Array(this.capacity() * (this.extendRatio - 1))\n );\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n this._capacity = this.arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n public toArray(): number[] {\n let size = this.size();\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(size);\n for (let i = 0; i < size; i++) {\n arr[i] = this.get(i);\n }\n return arr;\n }\n}\n
my_list.dart/* \u5217\u8868\u7c7b */\nclass MyList {\n late List<int> _arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int _capacity = 10; // \u5217\u8868\u5bb9\u91cf\n int _size = 0; // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n int _extendRatio = 2; // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u65b9\u6cd5 */\n MyList() {\n _arr = List.filled(_capacity, 0);\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n int size() => _size;\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n int capacity() => _capacity;\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n int get(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n return _arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n void set(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n _arr[index] = _num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n void add(int _num) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n _arr[_size] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n void insert(int index, int _num) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (_size == _capacity) extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (var j = _size - 1; j >= index; j--) {\n _arr[j + 1] = _arr[j];\n }\n _arr[index] = _num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size++;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n int remove(int index) {\n if (index >= _size) throw RangeError('\u7d22\u5f15\u8d8a\u754c');\n int _num = _arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (var j = index; j < _size - 1; j++) {\n _arr[j] = _arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n _size--;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return _num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n void extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 _extendRatio \u500d\u7684\u65b0\u6570\u7ec4\n final _newNums = List.filled(_capacity * _extendRatio, 0);\n // \u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n List.copyRange(_newNums, 0, _arr);\n // \u66f4\u65b0 _arr \u7684\u5f15\u7528\n _arr = _newNums;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n _capacity = _arr.length;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n List<int> toArray() {\n List<int> arr = [];\n for (var i = 0; i < _size; i++) {\n arr.add(get(i));\n }\n return arr;\n }\n}\n
my_list.rs/* \u5217\u8868\u7c7b */\n#[allow(dead_code)]\nstruct MyList {\n arr: Vec<i32>, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n capacity: usize, // \u5217\u8868\u5bb9\u91cf\n size: usize, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extend_ratio: usize, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n let mut vec = Vec::new();\n vec.resize(capacity, 0);\n Self {\n arr: vec,\n capacity,\n size: 0,\n extend_ratio: 2,\n }\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09*/\n pub fn size(&self) -> usize {\n return self.size;\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n return self.capacity;\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n pub fn get(&self, index: usize) -> i32 {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if index >= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n return self.arr[index];\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n pub fn set(&mut self, index: usize, num: i32) {\n if index >= self.size {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n self.arr[index] = num;\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n pub fn add(&mut self, num: i32) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n self.arr[self.size] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n pub fn insert(&mut self, index: usize, num: i32) {\n if index >= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if self.size == self.capacity() {\n self.extend_capacity();\n }\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size).rev() {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size += 1;\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n pub fn remove(&mut self, index: usize) -> i32 {\n if index >= self.size() {\n panic!(\"\u7d22\u5f15\u8d8a\u754c\")\n };\n let num = self.arr[index];\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in (index..self.size - 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.size -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n pub fn extend_capacity(&mut self) {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n let new_capacity = self.capacity * self.extend_ratio;\n self.arr.resize(new_capacity, 0);\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.capacity = new_capacity;\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n pub fn to_array(&mut self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut arr = Vec::new();\n for i in 0..self.size {\n arr.push(self.get(i));\n }\n arr\n }\n}\n
my_list.c/* \u5217\u8868\u7c7b */\ntypedef struct {\n int *arr; // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n int capacity; // \u5217\u8868\u5bb9\u91cf\n int size; // \u5217\u8868\u5927\u5c0f\n int extendRatio; // \u5217\u8868\u6bcf\u6b21\u6269\u5bb9\u7684\u500d\u6570\n} MyList;\n\n/* \u6784\u9020\u51fd\u6570 */\nMyList *newMyList() {\n MyList *nums = malloc(sizeof(MyList));\n nums->capacity = 10;\n nums->arr = malloc(sizeof(int) * nums->capacity);\n nums->size = 0;\n nums->extendRatio = 2;\n return nums;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delMyList(MyList *nums) {\n free(nums->arr);\n free(nums);\n}\n\n/* \u83b7\u53d6\u5217\u8868\u957f\u5ea6 */\nint size(MyList *nums) {\n return nums->size;\n}\n\n/* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\nint capacity(MyList *nums) {\n return nums->capacity;\n}\n\n/* \u8bbf\u95ee\u5143\u7d20 */\nint get(MyList *nums, int index) {\n assert(index >= 0 && index < nums->size);\n return nums->arr[index];\n}\n\n/* \u66f4\u65b0\u5143\u7d20 */\nvoid set(MyList *nums, int index, int num) {\n assert(index >= 0 && index < nums->size);\n nums->arr[index] = num;\n}\n\n/* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\nvoid add(MyList *nums, int num) {\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n nums->arr[size(nums)] = num;\n nums->size++;\n}\n\n/* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\nvoid insert(MyList *nums, int index, int num) {\n assert(index >= 0 && index < size(nums));\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size(nums) == capacity(nums)) {\n extendCapacity(nums); // \u6269\u5bb9\n }\n for (int i = size(nums); i > index; --i) {\n nums->arr[i] = nums->arr[i - 1];\n }\n nums->arr[index] = num;\n nums->size++;\n}\n\n/* \u5220\u9664\u5143\u7d20 */\n// \u6ce8\u610f\uff1astdio.h \u5360\u7528\u4e86 remove \u5173\u952e\u8bcd\nint removeItem(MyList *nums, int index) {\n assert(index >= 0 && index < size(nums));\n int num = nums->arr[index];\n for (int i = index; i < size(nums) - 1; i++) {\n nums->arr[i] = nums->arr[i + 1];\n }\n nums->size--;\n return num;\n}\n\n/* \u5217\u8868\u6269\u5bb9 */\nvoid extendCapacity(MyList *nums) {\n // \u5148\u5206\u914d\u7a7a\u95f4\n int newCapacity = capacity(nums) * nums->extendRatio;\n int *extend = (int *)malloc(sizeof(int) * newCapacity);\n int *temp = nums->arr;\n\n // \u62f7\u8d1d\u65e7\u6570\u636e\u5230\u65b0\u6570\u636e\n for (int i = 0; i < size(nums); i++)\n extend[i] = nums->arr[i];\n\n // \u91ca\u653e\u65e7\u6570\u636e\n free(temp);\n\n // \u66f4\u65b0\u65b0\u6570\u636e\n nums->arr = extend;\n nums->capacity = newCapacity;\n}\n\n/* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a Array \u7528\u4e8e\u6253\u5370 */\nint *toArray(MyList *nums) {\n return nums->arr;\n}\n
my_list.kt/* \u5217\u8868\u7c7b */\nclass MyList {\n private var arr: IntArray = intArrayOf() // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n private var capacity = 10 // \u5217\u8868\u5bb9\u91cf\n private var size = 0 // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n private var extendRatio = 2 // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n arr = IntArray(capacity)\n }\n\n /* \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09 */\n fun size(): Int {\n return size\n }\n\n /* \u83b7\u53d6\u5217\u8868\u5bb9\u91cf */\n fun capacity(): Int {\n return capacity\n }\n\n /* \u8bbf\u95ee\u5143\u7d20 */\n fun get(index: Int): Int {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException()\n return arr[index]\n }\n\n /* \u66f4\u65b0\u5143\u7d20 */\n fun set(index: Int, num: Int) {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n arr[index] = num\n }\n\n /* \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 */\n fun add(num: Int) {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity()\n arr[size] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++\n }\n\n /* \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 */\n fun insert(index: Int, num: Int) {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (size == capacity())\n extendCapacity()\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for (j in size - 1 downTo index)\n arr[j + 1] = arr[j]\n arr[index] = num\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size++\n }\n\n /* \u5220\u9664\u5143\u7d20 */\n fun remove(index: Int): Int {\n if (index < 0 || index >= size)\n throw IndexOutOfBoundsException(\"\u7d22\u5f15\u8d8a\u754c\")\n val num: Int = arr[index]\n // \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for (j in index..<size - 1)\n arr[j] = arr[j + 1]\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n size--\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num\n }\n\n /* \u5217\u8868\u6269\u5bb9 */\n fun extendCapacity() {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extendRatio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = arr.copyOf(capacity() * extendRatio)\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n capacity = arr.size\n }\n\n /* \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 */\n fun toArray(): IntArray {\n val size = size()\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val arr = IntArray(size)\n for (i in 0..<size) {\n arr[i] = get(i)\n }\n return arr\n }\n}\n
my_list.rb### \u5217\u8868\u7c7b ###\nclass MyList\n attr_reader :size # \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n attr_reader :capacity # \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n\n ### \u6784\u9020\u65b9\u6cd5 ###\n def initialize\n @capacity = 10\n @size = 0\n @extend_ratio = 2\n @arr = Array.new(capacity)\n end\n\n ### \u8bbf\u95ee\u5143\u7d20 ###\n def get(index)\n # \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n @arr[index]\n end\n\n ### \u8bbf\u95ee\u5143\u7d20 ###\n def set(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n @arr[index] = num\n end\n\n ### \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20 ###\n def add(num)\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n extend_capacity if size == capacity\n @arr[size] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size += 1\n end\n\n ### \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20 ###\n def insert(index, num)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n\n # \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n extend_capacity if size == capacity\n\n # \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n for j in (size - 1).downto(index)\n @arr[j + 1] = @arr[j] \n end\n @arr[index] = num\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size += 1\n end\n\n ### \u5220\u9664\u5143\u7d20 ###\n def remove(index)\n raise IndexError, \"\u7d22\u5f15\u8d8a\u754c\" if index < 0 || index >= size\n num = @arr[index]\n\n # \u5c06\u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n for j in index...size\n @arr[j] = @arr[j + 1]\n end\n\n # \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n @size -= 1\n\n # \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n num\n end\n\n ### \u5217\u8868\u6269\u5bb9 ###\n def extend_capacity\n # \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u539f\u6570\u7ec4 extend_ratio \u500d\u7684\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n # \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n @capacity = arr.length\n end\n\n ### \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4 ###\n def to_array\n sz = size\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n arr = Array.new(sz)\n for i in 0...sz\n arr[i] = get(i)\n end\n arr\n end\nend\n
my_list.zig// \u5217\u8868\u7c7b\nfn MyList(comptime T: type) type {\n return struct {\n const Self = @This();\n\n arr: []T = undefined, // \u6570\u7ec4\uff08\u5b58\u50a8\u5217\u8868\u5143\u7d20\uff09\n arrCapacity: usize = 10, // \u5217\u8868\u5bb9\u91cf\n numSize: usize = 0, // \u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n extendRatio: usize = 2, // \u6bcf\u6b21\u5217\u8868\u6269\u5bb9\u7684\u500d\u6570\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u5217\u8868\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.arr = try self.mem_allocator.alloc(T, self.arrCapacity);\n @memset(self.arr, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u5217\u8868\u957f\u5ea6\uff08\u5f53\u524d\u5143\u7d20\u6570\u91cf\uff09\n pub fn size(self: *Self) usize {\n return self.numSize;\n }\n\n // \u83b7\u53d6\u5217\u8868\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.arrCapacity;\n }\n\n // \u8bbf\u95ee\u5143\u7d20\n pub fn get(self: *Self, index: usize) T {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n return self.arr[index];\n } \n\n // \u66f4\u65b0\u5143\u7d20\n pub fn set(self: *Self, index: usize, num: T) void {\n // \u7d22\u5f15\u5982\u679c\u8d8a\u754c\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u4e0b\u540c\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n self.arr[index] = num;\n } \n\n // \u5728\u5c3e\u90e8\u6dfb\u52a0\u5143\u7d20\n pub fn add(self: *Self, num: T) !void {\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n self.arr[self.size()] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n } \n\n // \u5728\u4e2d\u95f4\u63d2\u5165\u5143\u7d20\n pub fn insert(self: *Self, index: usize, num: T) !void {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n // \u5143\u7d20\u6570\u91cf\u8d85\u51fa\u5bb9\u91cf\u65f6\uff0c\u89e6\u53d1\u6269\u5bb9\u673a\u5236\n if (self.size() == self.capacity()) try self.extendCapacity();\n // \u5c06\u7d22\u5f15 index \u4ee5\u53ca\u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n var j = self.size() - 1;\n while (j >= index) : (j -= 1) {\n self.arr[j + 1] = self.arr[j];\n }\n self.arr[index] = num;\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize += 1;\n }\n\n // \u5220\u9664\u5143\u7d20\n pub fn remove(self: *Self, index: usize) T {\n if (index < 0 or index >= self.size()) @panic(\"\u7d22\u5f15\u8d8a\u754c\");\n var num = self.arr[index];\n // \u5c06\u7d22\u5f15 index \u4e4b\u540e\u7684\u5143\u7d20\u90fd\u5411\u524d\u79fb\u52a8\u4e00\u4f4d\n var j = index;\n while (j < self.size() - 1) : (j += 1) {\n self.arr[j] = self.arr[j + 1];\n }\n // \u66f4\u65b0\u5143\u7d20\u6570\u91cf\n self.numSize -= 1;\n // \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n return num;\n }\n\n // \u5217\u8868\u6269\u5bb9\n pub fn extendCapacity(self: *Self) !void {\n // \u65b0\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a size * extendRatio \u7684\u6570\u7ec4\uff0c\u5e76\u5c06\u539f\u6570\u7ec4\u590d\u5236\u5230\u65b0\u6570\u7ec4\n var newCapacity = self.capacity() * self.extendRatio;\n var extend = try self.mem_allocator.alloc(T, newCapacity);\n @memset(extend, @as(T, 0));\n // \u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u65b0\u6570\u7ec4\n std.mem.copy(T, extend, self.arr);\n self.arr = extend;\n // \u66f4\u65b0\u5217\u8868\u5bb9\u91cf\n self.arrCapacity = newCapacity;\n }\n\n // \u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var arr = try self.mem_allocator.alloc(T, self.size());\n @memset(arr, @as(T, 0));\n for (arr, 0..) |*num, i| {\n num.* = self.get(i);\n }\n return arr;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 Memory and cache *","text":"In the first two sections of this chapter, we explored arrays and linked lists, two fundamental and important data structures, representing \"continuous storage\" and \"dispersed storage\" respectively.
In fact, the physical structure largely determines the efficiency of a program's use of memory and cache, which in turn affects the overall performance of the algorithm.
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441-computer-storage-devices","title":"4.4.1 \u00a0 Computer storage devices","text":"There are three types of storage devices in computers: \"hard disk,\" \"random-access memory (RAM),\" and \"cache memory.\" The following table shows their different roles and performance characteristics in computer systems.
Table 4-2 \u00a0 Computer storage devices
Hard Disk Memory Cache Usage Long-term storage of data, including OS, programs, files, etc. Temporary storage of currently running programs and data being processed Stores frequently accessed data and instructions, reducing the number of CPU accesses to memory Volatility Data is not lost after power off Data is lost after power off Data is lost after power off Capacity Larger, TB level Smaller, GB level Very small, MB level Speed Slower, several hundred to thousands MB/s Faster, several tens of GB/s Very fast, several tens to hundreds of GB/s Price Cheaper, several cents to yuan / GB More expensive, tens to hundreds of yuan / GB Very expensive, priced with CPUWe can imagine the computer storage system as a pyramid structure shown in the Figure 4-9 . The storage devices closer to the top of the pyramid are faster, have smaller capacity, and are more costly. This multi-level design is not accidental, but the result of careful consideration by computer scientists and engineers.
Figure 4-9 \u00a0 Computer storage system
Note
The storage hierarchy of computers reflects a delicate balance between speed, capacity, and cost. In fact, this kind of trade-off is common in all industrial fields, requiring us to find the best balance between different advantages and limitations.
Overall, hard disks are used for long-term storage of large amounts of data, memory is used for temporary storage of data being processed during program execution, and cache is used to store frequently accessed data and instructions to improve program execution efficiency. Together, they ensure the efficient operation of computer systems.
As shown in the Figure 4-10 , during program execution, data is read from the hard disk into memory for CPU computation. The cache can be considered a part of the CPU, smartly loading data from memory to provide fast data access to the CPU, significantly enhancing program execution efficiency and reducing reliance on slower memory.
Figure 4-10 \u00a0 Data flow between hard disk, memory, and cache
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442-memory-efficiency-of-data-structures","title":"4.4.2 \u00a0 Memory efficiency of data structures","text":"In terms of memory space utilization, arrays and linked lists have their advantages and limitations.
On one hand, memory is limited and cannot be shared by multiple programs, so we hope that data structures can use space as efficiently as possible. The elements of an array are tightly packed without extra space for storing references (pointers) between linked list nodes, making them more space-efficient. However, arrays require allocating sufficient continuous memory space at once, which may lead to memory waste, and array expansion also requires additional time and space costs. In contrast, linked lists allocate and reclaim memory dynamically on a per-node basis, providing greater flexibility.
On the other hand, during program execution, as memory is repeatedly allocated and released, the degree of fragmentation of free memory becomes higher, leading to reduced memory utilization efficiency. Arrays, due to their continuous storage method, are relatively less likely to cause memory fragmentation. In contrast, the elements of a linked list are dispersedly stored, and frequent insertion and deletion operations make memory fragmentation more likely.
"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443-cache-efficiency-of-data-structures","title":"4.4.3 \u00a0 Cache efficiency of data structures","text":"Although caches are much smaller in space capacity than memory, they are much faster and play a crucial role in program execution speed. Since the cache's capacity is limited and can only store a small part of frequently accessed data, when the CPU tries to access data not in the cache, a \"cache miss\" occurs, forcing the CPU to load the needed data from slower memory.
Clearly, the fewer the cache misses, the higher the CPU's data read-write efficiency, and the better the program performance. The proportion of successful data retrieval from the cache by the CPU is called the \"cache hit rate,\" a metric often used to measure cache efficiency.
To achieve higher efficiency, caches adopt the following data loading mechanisms.
In fact, arrays and linked lists have different cache utilization efficiencies, mainly reflected in the following aspects.
Overall, arrays have a higher cache hit rate and are generally more efficient in operation than linked lists. This makes data structures based on arrays more popular in solving algorithmic problems.
It should be noted that high cache efficiency does not mean that arrays are always better than linked lists. Which data structure to choose in actual applications should be based on specific requirements. For example, both arrays and linked lists can implement the \"stack\" data structure (which will be detailed in the next chapter), but they are suitable for different scenarios.
Q: Does storing arrays on the stack versus the heap affect time and space efficiency?
Arrays stored on both the stack and heap are stored in contiguous memory spaces, and data operation efficiency is essentially the same. However, stacks and heaps have their own characteristics, leading to the following differences.
Q: Why do arrays require elements of the same type, while linked lists do not emphasize same-type elements?
Linked lists consist of nodes connected by references (pointers), and each node can store data of different types, such as int, double, string, object, etc.
In contrast, array elements must be of the same type, allowing the calculation of offsets to access the corresponding element positions. For example, an array containing both int and long types, with single elements occupying 4 bytes and 8 bytes respectively, cannot use the following formula to calculate offsets, as the array contains elements of two different lengths.
# Element memory address = array memory address + element length * element index\n
Q: After deleting a node, is it necessary to set P.next
to None
?
Not modifying P.next
is also acceptable. From the perspective of the linked list, traversing from the head node to the tail node will no longer encounter P
. This means that node P
has been effectively removed from the list, and where P
points no longer affects the list.
From a garbage collection perspective, for languages with automatic garbage collection mechanisms like Java, Python, and Go, whether node P
is collected depends on whether there are still references pointing to it, not on the value of P.next
. In languages like C and C++, we need to manually free the node's memory.
Q: In linked lists, the time complexity for insertion and deletion operations is O(1)
. But searching for the element before insertion or deletion takes O(n)
time, so why isn't the time complexity O(n)
?
If an element is searched first and then deleted, the time complexity is indeed O(n)
. However, the O(1)
advantage of linked lists in insertion and deletion can be realized in other applications. For example, in the implementation of double-ended queues using linked lists, we maintain pointers always pointing to the head and tail nodes, making each insertion and deletion operation O(1)
.
Q: In the image \"Linked List Definition and Storage Method\", do the light blue storage nodes occupy a single memory address, or do they share half with the node value?
The diagram is just a qualitative representation; quantitative analysis depends on specific situations.
Q: Is adding elements to the end of a list always O(1)
?
If adding an element exceeds the list length, the list needs to be expanded first. The system will request a new memory block and move all elements of the original list over, in which case the time complexity becomes O(n)
.
Q: The statement \"The emergence of lists greatly improves the practicality of arrays, but may lead to some memory space wastage\" - does this refer to the memory occupied by additional variables like capacity, length, and expansion multiplier?
The space wastage here mainly refers to two aspects: on the one hand, lists are set with an initial length, which we may not always need; on the other hand, to prevent frequent expansion, expansion usually multiplies by a coefficient, such as \\(\\times 1.5\\). This results in many empty slots, which we typically cannot fully fill.
Q: In Python, after initializing n = [1, 2, 3]
, the addresses of these 3 elements are contiguous, but initializing m = [2, 1, 3]
shows that each element's id
is not consecutive but identical to those in n
. If the addresses of these elements are not contiguous, is m
still an array?
If we replace list elements with linked list nodes n = [n1, n2, n3, n4, n5]
, these 5 node objects are also typically dispersed throughout memory. However, given a list index, we can still access the node's memory address in O(1)
time, thereby accessing the corresponding node. This is because the array stores references to the nodes, not the nodes themselves.
Unlike many languages, in Python, numbers are also wrapped as objects, and lists store references to these numbers, not the numbers themselves. Therefore, we find that the same number in two arrays has the same id
, and these numbers' memory addresses need not be contiguous.
Q: The std::list
in C++ STL has already implemented a doubly linked list, but it seems that some algorithm books don't directly use it. Is there any limitation?
On the one hand, we often prefer to use arrays to implement algorithms, only using linked lists when necessary, mainly for two reasons.
std::list
usually occupies more space than std::vector
.std::list
has a lower cache utilization rate. Generally, std::vector
performs better.On the other hand, linked lists are primarily necessary for binary trees and graphs. Stacks and queues are often implemented using the programming language's stack
and queue
classes, rather than linked lists.
Q: Does initializing a list res = [0] * self.size()
result in each element of res
referencing the same address?
No. However, this issue arises with two-dimensional arrays, for example, initializing a two-dimensional list res = [[0] * self.size()]
would reference the same list [0]
multiple times.
Q: In deleting a node, is it necessary to break the reference to its successor node?
From the perspective of data structures and algorithms (problem-solving), it's okay not to break the link, as long as the program's logic is correct. From the perspective of standard libraries, breaking the link is safer and more logically clear. If the link is not broken, and the deleted node is not properly recycled, it could affect the recycling of the successor node's memory.
"},{"location":"chapter_computational_complexity/","title":"Chapter 2. \u00a0 Complexity analysis","text":"Abstract
Complexity analysis is like a space-time navigator in the vast universe of algorithms.
It guides us in exploring deeper within the the dimensions of time and space, seeking more elegant solutions.
"},{"location":"chapter_computational_complexity/#chapter-contents","title":"Chapter contents","text":"In algorithms, the repeated execution of a task is quite common and is closely related to the analysis of complexity. Therefore, before delving into the concepts of time complexity and space complexity, let's first explore how to implement repetitive tasks in programming. This involves understanding two fundamental programming control structures: iteration and recursion.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221-iteration","title":"2.2.1 \u00a0 Iteration","text":"\"Iteration\" is a control structure for repeatedly performing a task. In iteration, a program repeats a block of code as long as a certain condition is met until this condition is no longer satisfied.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for-loops","title":"1. \u00a0 For loops","text":"The for
loop is one of the most common forms of iteration, and it's particularly suitable when the number of iterations is known in advance.
The following function uses a for
loop to perform a summation of \\(1 + 2 + \\dots + n\\), with the sum being stored in the variable res
. It's important to note that in Python, range(a, b)
creates an interval that is inclusive of a
but exclusive of b
, meaning it iterates over the range from \\(a\\) up to \\(b\u22121\\).
def for_loop(n: int) -> int:\n \"\"\"for \u5faa\u73af\"\"\"\n res = 0\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n res += i\n return res\n
iteration.cpp/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n res += i;\n }\n return res;\n}\n
iteration.java/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.cs/* for \u5faa\u73af */\nint ForLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.go/* for \u5faa\u73af */\nfunc forLoop(n int) int {\n res := 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n res += i\n }\n return res\n}\n
iteration.swift/* for \u5faa\u73af */\nfunc forLoop(n: Int) -> Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1 ... n {\n res += i\n }\n return res\n}\n
iteration.js/* for \u5faa\u73af */\nfunction forLoop(n) {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.ts/* for \u5faa\u73af */\nfunction forLoop(n: number): number {\n let res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.dart/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.rs/* for \u5faa\u73af */\nfn for_loop(n: i32) -> i32 {\n let mut res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..=n {\n res += i;\n }\n res\n}\n
iteration.c/* for \u5faa\u73af */\nint forLoop(int n) {\n int res = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n res += i;\n }\n return res;\n}\n
iteration.kt/* for \u5faa\u73af */\nfun forLoop(n: Int): Int {\n var res = 0\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (i in 1..n) {\n res += i\n }\n return res\n}\n
iteration.rb### for \u5faa\u73af ###\ndef for_loop(n)\n res = 0\n\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i in 1..n\n res += i\n end\n\n res\nend\n
iteration.zig// for \u5faa\u73af\nfn forLoop(n: usize) i32 {\n var res: i32 = 0;\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n res = res + @as(i32, @intCast(i));\n }\n return res;\n} \n
Code Visualization Full Screen >
The flowchart below represents this sum function.
Figure 2-1 \u00a0 Flowchart of the sum function
The number of operations in this summation function is proportional to the size of the input data \\(n\\), or in other words, it has a \"linear relationship.\" This \"linear relationship\" is what time complexity describes. This topic will be discussed in more detail in the next section.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while-loops","title":"2. \u00a0 While loops","text":"Similar to for
loops, while
loops are another approach for implementing iteration. In a while
loop, the program checks a condition at the beginning of each iteration; if the condition is true, the execution continues, otherwise, the loop ends.
Below we use a while
loop to implement the sum \\(1 + 2 + \\dots + n\\).
def while_loop(n: int) -> int:\n \"\"\"while \u5faa\u73af\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n:\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n return res\n
iteration.cpp/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af */\nint WhileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af */\nfunc whileLoop(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af */\nfunc whileLoop(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i\n i += 1 // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af */\nfunction whileLoop(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af */\nfunction whileLoop(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af */\nfn while_loop(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n {\n res += i;\n i += 1; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n res\n}\n
iteration.c/* while \u5faa\u73af */\nint whileLoop(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i;\n i++; // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res;\n}\n
iteration.kt/* while \u5faa\u73af */\nfun whileLoop(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += i\n i++ // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n }\n return res\n}\n
iteration.rb### while \u5faa\u73af ###\ndef while_loop(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n # \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while i <= n\n res += i\n i += 1 # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n end\n\n res\nend\n
iteration.zig// while \u5faa\u73af\nfn whileLoop(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 2, ..., n-1, n\n while (i <= n) {\n res += @intCast(i);\n i += 1;\n }\n return res;\n}\n
Code Visualization Full Screen >
While
loops provide more flexibility than for
loops, especially since they allow for custom initialization and modification of the condition variable at each step.
For example, in the following code, the condition variable \\(i\\) is updated twice each round, which would be inconvenient to implement with a for
loop.
def while_loop_ii(n: int) -> int:\n \"\"\"while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\"\"\"\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n:\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n return res\n
iteration.cpp/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.java/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.cs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint WhileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1; \n i *= 2;\n }\n return res;\n}\n
iteration.go/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n int) int {\n res := 0\n // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n i := 1\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n for i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.swift/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunc whileLoopII(n: Int) -> Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n }\n return res\n}\n
iteration.js/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n) {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.ts/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfunction whileLoopII(n: number): number {\n let res = 0;\n let i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.dart/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.rs/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfn while_loop_ii(n: i32) -> i32 {\n let mut res = 0;\n let mut i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n res\n}\n
iteration.c/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nint whileLoopII(int n) {\n int res = 0;\n int i = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i;\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++;\n i *= 2;\n }\n return res;\n}\n
iteration.kt/* while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09 */\nfun whileLoopII(n: Int): Int {\n var res = 0\n var i = 1 // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += i\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i++\n i *= 2\n }\n return res\n}\n
iteration.rb### while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09###\ndef while_loop_ii(n)\n res = 0\n i = 1 # \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n\n # \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while i <= n\n res += i\n # \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1\n i *= 2\n end\n\n res\nend\n
iteration.zig// while \u5faa\u73af\uff08\u4e24\u6b21\u66f4\u65b0\uff09\nfn whileLoopII(n: i32) i32 {\n var res: i32 = 0;\n var i: i32 = 1; // \u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cf\n // \u5faa\u73af\u6c42\u548c 1, 4, 10, ...\n while (i <= n) {\n res += @intCast(i);\n // \u66f4\u65b0\u6761\u4ef6\u53d8\u91cf\n i += 1;\n i *= 2;\n }\n return res;\n}\n
Code Visualization Full Screen >
Overall, for
loops are more concise, while while
loops are more flexible. Both can implement iterative structures. Which one to use should be determined based on the specific requirements of the problem.
We can nest one loop structure within another. Below is an example using for
loops:
def nested_for_loop(n: int) -> str:\n \"\"\"\u53cc\u5c42 for \u5faa\u73af\"\"\"\n res = \"\"\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in range(1, n + 1):\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in range(1, n + 1):\n res += f\"({i}, {j}), \"\n return res\n
iteration.cpp/* \u53cc\u5c42 for \u5faa\u73af */\nstring nestedForLoop(int n) {\n ostringstream res;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; ++i) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; ++j) {\n res << \"(\" << i << \", \" << j << \"), \";\n }\n }\n return res.str();\n}\n
iteration.java/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n StringBuilder res = new StringBuilder();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.append(\"(\" + i + \", \" + j + \"), \");\n }\n }\n return res.toString();\n}\n
iteration.cs/* \u53cc\u5c42 for \u5faa\u73af */\nstring NestedForLoop(int n) {\n StringBuilder res = new();\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res.Append($\"({i}, {j}), \");\n }\n }\n return res.ToString();\n}\n
iteration.go/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n int) string {\n res := \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i := 1; i <= n; i++ {\n for j := 1; j <= n; j++ {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n res += fmt.Sprintf(\"(%d, %d), \", i, j)\n }\n }\n return res\n}\n
iteration.swift/* \u53cc\u5c42 for \u5faa\u73af */\nfunc nestedForLoop(n: Int) -> String {\n var res = \"\"\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1 ... n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1 ... n {\n res.append(\"(\\(i), \\(j)), \")\n }\n }\n return res\n}\n
iteration.js/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n) {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.ts/* \u53cc\u5c42 for \u5faa\u73af */\nfunction nestedForLoop(n: number): string {\n let res = '';\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (let i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (let j = 1; j <= n; j++) {\n res += `(${i}, ${j}), `;\n }\n }\n return res;\n}\n
iteration.dart/* \u53cc\u5c42 for \u5faa\u73af */\nString nestedForLoop(int n) {\n String res = \"\";\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n res += \"($i, $j), \";\n }\n }\n return res;\n}\n
iteration.rs/* \u53cc\u5c42 for \u5faa\u73af */\nfn nested_for_loop(n: i32) -> String {\n let mut res = vec![];\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..=n {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..=n {\n res.push(format!(\"({}, {}), \", i, j));\n }\n }\n res.join(\"\")\n}\n
iteration.c/* \u53cc\u5c42 for \u5faa\u73af */\nchar *nestedForLoop(int n) {\n // n * n \u4e3a\u5bf9\u5e94\u70b9\u6570\u91cf\uff0c\"(i, j), \" \u5bf9\u5e94\u5b57\u7b26\u4e32\u957f\u6700\u5927\u4e3a 6+10*2\uff0c\u52a0\u4e0a\u6700\u540e\u4e00\u4e2a\u7a7a\u5b57\u7b26 \\0 \u7684\u989d\u5916\u7a7a\u95f4\n int size = n * n * 26 + 1;\n char *res = malloc(size * sizeof(char));\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (int i = 1; i <= n; i++) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (int j = 1; j <= n; j++) {\n char tmp[26];\n snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n strncat(res, tmp, size - strlen(res) - 1);\n }\n }\n return res;\n}\n
iteration.kt/* \u53cc\u5c42 for \u5faa\u73af */\nfun nestedForLoop(n: Int): String {\n val res = StringBuilder()\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (i in 1..n) {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (j in 1..n) {\n res.append(\" ($i, $j), \")\n }\n }\n return res.toString()\n}\n
iteration.rb### \u53cc\u5c42 for \u5faa\u73af ###\ndef nested_for_loop(n)\n res = \"\"\n\n # \u5faa\u73af i = 1, 2, ..., n-1, n\n for i in 1..n\n # \u5faa\u73af j = 1, 2, ..., n-1, n\n for j in 1..n\n res += \"(#{i}, #{j}), \"\n end\n end\n\n res\nend\n
iteration.zig// \u53cc\u5c42 for \u5faa\u73af\nfn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {\n var res = std.ArrayList(u8).init(allocator);\n defer res.deinit();\n var buffer: [20]u8 = undefined;\n // \u5faa\u73af i = 1, 2, ..., n-1, n\n for (1..n+1) |i| {\n // \u5faa\u73af j = 1, 2, ..., n-1, n\n for (1..n+1) |j| {\n var _str = try std.fmt.bufPrint(&buffer, \"({d}, {d}), \", .{i, j});\n try res.appendSlice(_str);\n }\n }\n return res.toOwnedSlice();\n}\n
Code Visualization Full Screen >
The flowchart below represents this nested loop.
Figure 2-2 \u00a0 Flowchart of the nested loop
In such cases, the number of operations of the function is proportional to \\(n^2\\), meaning the algorithm's runtime and the size of the input data \\(n\\) has a 'quadratic relationship.'
We can further increase the complexity by adding more nested loops, each level of nesting effectively \"increasing the dimension,\" which raises the time complexity to \"cubic,\" \"quartic,\" and so on.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222-recursion","title":"2.2.2 \u00a0 Recursion","text":"\"Recursion\" is an algorithmic strategy where a function solves a problem by calling itself. It primarily involves two phases:
From an implementation perspective, recursive code mainly includes three elements.
Observe the following code, where simply calling the function recur(n)
can compute the sum of \\(1 + 2 + \\dots + n\\):
def recur(n: int) -> int:\n \"\"\"\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 1:\n return 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n
recursion.cpp/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.java/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.cs/* \u9012\u5f52 */\nint Recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = Recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.go/* \u9012\u5f52 */\nfunc recur(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res := recur(n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.swift/* \u9012\u5f52 */\nfunc recur(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n: n - 1)\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.js/* \u9012\u5f52 */\nfunction recur(n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.ts/* \u9012\u5f52 */\nfunction recur(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n const res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.dart/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.rs/* \u9012\u5f52 */\nfn recur(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 1 {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n let res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\n}\n
recursion.c/* \u9012\u5f52 */\nint recur(int n) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n int res = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
recursion.kt/* \u9012\u5f52 */\nfun recur(n: Int): Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1)\n return 1\n // \u9012: \u9012\u5f52\u8c03\u7528\n val res = recur(n - 1)\n // \u5f52: \u8fd4\u56de\u7ed3\u679c\n return n + res\n}\n
recursion.rb### \u9012\u5f52 ###\ndef recur(n)\n # \u7ec8\u6b62\u6761\u4ef6\n return 1 if n == 1\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n res = recur(n - 1)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n n + res\nend\n
recursion.zig// \u9012\u5f52\u51fd\u6570\nfn recur(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 1) {\n return 1;\n }\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var res: i32 = recur(n - 1);\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n return n + res;\n}\n
Code Visualization Full Screen >
The Figure 2-3 shows the recursive process of this function.
Figure 2-3 \u00a0 Recursive process of the sum function
Although iteration and recursion can achieve the same results from a computational standpoint, they represent two entirely different paradigms of thinking and problem-solving.
Let's take the earlier example of the summation function, defined as \\(f(n) = 1 + 2 + \\dots + n\\).
Every time a recursive function calls itself, the system allocates memory for the newly initiated function to store local variables, the return address, and other relevant information. This leads to two primary outcomes.
As shown in the Figure 2-4 , there are \\(n\\) unreturned recursive functions before triggering the termination condition, indicating a recursion depth of \\(n\\).
Figure 2-4 \u00a0 Recursion call depth
In practice, the depth of recursion allowed by programming languages is usually limited, and excessively deep recursion can lead to stack overflow errors.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-tail-recursion","title":"2. \u00a0 Tail recursion","text":"Interestingly, if a function performs its recursive call as the very last step before returning, it can be optimized by the compiler or interpreter to be as space-efficient as iteration. This scenario is known as \"tail recursion.\"
For example, in calculating \\(1 + 2 + \\dots + n\\), we can make the result variable res
a parameter of the function, thereby achieving tail recursion:
def tail_recur(n, res):\n \"\"\"\u5c3e\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6\n if n == 0:\n return res\n # \u5c3e\u9012\u5f52\u8c03\u7528\n return tail_recur(n - 1, res + n)\n
recursion.cpp/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.java/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.cs/* \u5c3e\u9012\u5f52 */\nint TailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return TailRecur(n - 1, res + n);\n}\n
recursion.go/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n int, res int) int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n-1, res+n)\n}\n
recursion.swift/* \u5c3e\u9012\u5f52 */\nfunc tailRecur(n: Int, res: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n, res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.ts/* \u5c3e\u9012\u5f52 */\nfunction tailRecur(n: number, res: number): number {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n === 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.dart/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.rs/* \u5c3e\u9012\u5f52 */\nfn tail_recur(n: i32, res: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if n == 0 {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\n}\n
recursion.c/* \u5c3e\u9012\u5f52 */\nint tailRecur(int n, int res) {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res;\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
recursion.kt/* \u5c3e\u9012\u5f52 */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n // \u6dfb\u52a0 tailrec \u5173\u952e\u8bcd\uff0c\u4ee5\u5f00\u542f\u5c3e\u9012\u5f52\u4f18\u5316\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0)\n return res\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n)\n}\n
recursion.rb### \u5c3e\u9012\u5f52 ###\ndef tail_recur(n, res)\n # \u7ec8\u6b62\u6761\u4ef6\n return res if n == 0\n # \u5c3e\u9012\u5f52\u8c03\u7528\n tail_recur(n - 1, res + n)\nend\n
recursion.zig// \u5c3e\u9012\u5f52\u51fd\u6570\nfn tailRecur(n: i32, res: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6\n if (n == 0) {\n return res;\n }\n // \u5c3e\u9012\u5f52\u8c03\u7528\n return tailRecur(n - 1, res + n);\n}\n
Code Visualization Full Screen >
The execution process of tail recursion is shown in the following figure. Comparing regular recursion and tail recursion, the point of the summation operation is different.
Figure 2-5 \u00a0 Tail recursion process
Tip
Note that many compilers or interpreters do not support tail recursion optimization. For example, Python does not support tail recursion optimization by default, so even if the function is in the form of tail recursion, it may still encounter stack overflow issues.
"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3-recursion-tree","title":"3. \u00a0 Recursion tree","text":"When dealing with algorithms related to \"divide and conquer\", recursion often offers a more intuitive approach and more readable code than iteration. Take the \"Fibonacci sequence\" as an example.
Question
Given a Fibonacci sequence \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\), find the \\(n\\)th number in the sequence.
Let the \\(n\\)th number of the Fibonacci sequence be \\(f(n)\\), it's easy to deduce two conclusions:
Using the recursive relation, and considering the first two numbers as termination conditions, we can write the recursive code. Calling fib(n)
will yield the \\(n\\)th number of the Fibonacci sequence:
def fib(n: int) -> int:\n \"\"\"\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52\"\"\"\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 or n == 2:\n return n - 1\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n
recursion.cpp/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.java/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.cs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint Fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = Fib(n - 1) + Fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.go/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n int) int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res := fib(n-1) + fib(n-2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.swift/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunc fib(n: Int) -> Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n: n - 1) + fib(n: n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.js/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.ts/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfunction fib(n: number): number {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n === 1 || n === 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n const res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.dart/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2) return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.rs/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfn fib(n: i32) -> i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if n == 1 || n == 2 {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n let res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c\n res\n}\n
recursion.c/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nint fib(int n) {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1;\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n int res = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
recursion.kt/* \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 */\nfun fib(n: Int): Int {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 || n == 2)\n return n - 1\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n val res = fib(n - 1) + fib(n - 2)\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res\n}\n
recursion.rb### \u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a\u9012\u5f52 ###\ndef fib(n)\n # \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n return n - 1 if n == 1 || n == 2\n # \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n res = fib(n - 1) + fib(n - 2)\n # \u8fd4\u56de\u7ed3\u679c f(n)\n res\nend\n
recursion.zig// \u6590\u6ce2\u90a3\u5951\u6570\u5217\nfn fib(n: i32) i32 {\n // \u7ec8\u6b62\u6761\u4ef6 f(1) = 0, f(2) = 1\n if (n == 1 or n == 2) {\n return n - 1;\n }\n // \u9012\u5f52\u8c03\u7528 f(n) = f(n-1) + f(n-2)\n var res: i32 = fib(n - 1) + fib(n - 2);\n // \u8fd4\u56de\u7ed3\u679c f(n)\n return res;\n}\n
Code Visualization Full Screen >
Observing the above code, we see that it recursively calls two functions within itself, meaning that one call generates two branching calls. As illustrated below, this continuous recursive calling eventually creates a \"recursion tree\" with a depth of \\(n\\).
Figure 2-6 \u00a0 Fibonacci sequence recursion tree
Fundamentally, recursion embodies the paradigm of \"breaking down a problem into smaller sub-problems.\" This divide-and-conquer strategy is crucial.
Summarizing the above content, the following table shows the differences between iteration and recursion in terms of implementation, performance, and applicability.
Table: Comparison of iteration and recursion characteristics
Iteration Recursion Approach Loop structure Function calls itself Time Efficiency Generally higher efficiency, no function call overhead Each function call generates overhead Memory Usage Typically uses a fixed size of memory space Accumulative function calls can use a substantial amount of stack frame space Suitable Problems Suitable for simple loop tasks, intuitive and readable code Suitable for problem decomposition, like trees, graphs, divide-and-conquer, backtracking, etc., concise and clear code structureTip
If you find the following content difficult to understand, consider revisiting it after reading the \"Stack\" chapter.
So, what is the intrinsic connection between iteration and recursion? Taking the above recursive function as an example, the summation operation occurs during the recursion's \"return\" phase. This means that the initially called function is the last to complete its summation operation, mirroring the \"last in, first out\" principle of a stack.
Recursive terms like \"call stack\" and \"stack frame space\" hint at the close relationship between recursion and stacks.
Therefore, we can use an explicit stack to simulate the behavior of the call stack, thus transforming recursion into an iterative form:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig recursion.pydef for_loop_recur(n: int) -> int:\n \"\"\"\u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\"\"\"\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in range(n, 0, -1):\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while stack:\n # \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n # res = 1+2+3+...+n\n return res\n
recursion.cpp/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack<int> stack;\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.empty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.top();\n stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.java/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<Integer> stack = new Stack<>();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.cs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint ForLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n Stack<int> stack = new();\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.Push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.Count > 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.go/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n int) int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack := list.New()\n res := 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i := n; i > 0; i-- {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.PushBack(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n for stack.Len() != 0 {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.Back().Value.(int)\n stack.Remove(stack.Back())\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.swift/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunc forLoopRecur(n: Int) -> Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [Int] = []\n var res = 0\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1 ... n).reversed() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.append(i)\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.isEmpty {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.js/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n const stack = [];\n let res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.ts/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfunction forLoopRecur(n: number): number {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808 \n const stack: number[] = [];\n let res: number = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (let i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (stack.length) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.dart/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n List<int> stack = [];\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.add(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (!stack.isEmpty) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.removeLast();\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.rs/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfn for_loop_recur(n: i32) -> i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n let mut stack = Vec::new();\n let mut res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in (1..=n).rev() {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack.push(i);\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.is_empty() {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop().unwrap();\n }\n // res = 1+2+3+...+n\n res\n}\n
recursion.c/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nint forLoopRecur(int n) {\n int stack[1000]; // \u501f\u52a9\u4e00\u4e2a\u5927\u6570\u7ec4\u6765\u6a21\u62df\u6808\n int top = -1; // \u6808\u9876\u7d22\u5f15\n int res = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for (int i = n; i > 0; i--) {\n // \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack[1 + top++] = i;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while (top >= 0) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack[top--];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
recursion.kt/* \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 */\nfun forLoopRecur(n: Int): Int {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n val stack = Stack<Int>()\n var res = 0\n // \u9012: \u9012\u5f52\u8c03\u7528\n for (i in n downTo 0) {\n stack.push(i)\n }\n // \u5f52: \u8fd4\u56de\u7ed3\u679c\n while (stack.isNotEmpty()) {\n // \u901a\u8fc7\u201c\u51fa\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u5f52\u201d\n res += stack.pop()\n }\n // res = 1+2+3+...+n\n return res\n}\n
recursion.rb### \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52 ###\ndef for_loop_recur(n)\n # \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n stack = []\n res = 0\n\n # \u9012\uff1a\u9012\u5f52\u8c03\u7528\n for i in n.downto(0)\n # \u901a\u8fc7\u201c\u5165\u6808\u64cd\u4f5c\u201d\u6a21\u62df\u201c\u9012\u201d\n stack << i\n end\n # \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n while !stack.empty?\n res += stack.pop\n end\n\n # res = 1+2+3+...+n\n res\nend\n
recursion.zig// \u4f7f\u7528\u8fed\u4ee3\u6a21\u62df\u9012\u5f52\nfn forLoopRecur(comptime n: i32) i32 {\n // \u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u7684\u6808\u6765\u6a21\u62df\u7cfb\u7edf\u8c03\u7528\u6808\n var stack: [n]i32 = undefined;\n var res: i32 = 0;\n // \u9012\uff1a\u9012\u5f52\u8c03\u7528\n var i: usize = n;\n while (i > 0) {\n stack[i - 1] = @intCast(i);\n i -= 1;\n }\n // \u5f52\uff1a\u8fd4\u56de\u7ed3\u679c\n var index: usize = n;\n while (index > 0) {\n index -= 1;\n res += stack[index];\n }\n // res = 1+2+3+...+n\n return res;\n}\n
Code Visualization Full Screen >
Observing the above code, when recursion is transformed into iteration, the code becomes more complex. Although iteration and recursion can often be transformed into each other, it's not always advisable to do so for two reasons:
In conclusion, whether to choose iteration or recursion depends on the specific nature of the problem. In programming practice, it's crucial to weigh the pros and cons of both and choose the most suitable approach for the situation at hand.
"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 Algorithm efficiency assessment","text":"In algorithm design, we pursue the following two objectives in sequence.
In other words, under the premise of being able to solve the problem, algorithm efficiency has become the main criterion for evaluating the merits of an algorithm, which includes the following two dimensions.
In short, our goal is to design data structures and algorithms that are both fast and memory-efficient. Effectively assessing algorithm efficiency is crucial because only then can we compare various algorithms and guide the process of algorithm design and optimization.
There are mainly two methods of efficiency assessment: actual testing and theoretical estimation.
"},{"location":"chapter_computational_complexity/performance_evaluation/#211-actual-testing","title":"2.1.1 \u00a0 Actual testing","text":"Suppose we have algorithms A
and B
, both capable of solving the same problem, and we need to compare their efficiencies. The most direct method is to use a computer to run these two algorithms and monitor and record their runtime and memory usage. This assessment method reflects the actual situation but has significant limitations.
On one hand, it's difficult to eliminate interference from the testing environment. Hardware configurations can affect algorithm performance. For example, algorithm A
might run faster than B
on one computer, but the opposite result may occur on another computer with different configurations. This means we would need to test on a variety of machines to calculate average efficiency, which is impractical.
On the other hand, conducting a full test is very resource-intensive. As the volume of input data changes, the efficiency of the algorithms may vary. For example, with smaller data volumes, algorithm A
might run faster than B
, but the opposite might be true with larger data volumes. Therefore, to draw convincing conclusions, we need to test a wide range of input data sizes, which requires significant computational resources.
Due to the significant limitations of actual testing, we can consider evaluating algorithm efficiency solely through calculations. This estimation method is known as \"asymptotic complexity analysis,\" or simply \"complexity analysis.\"
Complexity analysis reflects the relationship between the time and space resources required for algorithm execution and the size of the input data. It describes the trend of growth in the time and space required by the algorithm as the size of the input data increases. This definition might sound complex, but we can break it down into three key points to understand it better.
Complexity analysis overcomes the disadvantages of actual testing methods, reflected in the following aspects:
Tip
If you're still confused about the concept of complexity, don't worry. We will introduce it in detail in subsequent chapters.
Complexity analysis provides us with a \"ruler\" to measure the time and space resources needed to execute an algorithm and compare the efficiency between different algorithms.
Complexity is a mathematical concept and may be abstract and challenging for beginners. From this perspective, complexity analysis might not be the best content to introduce first. However, when discussing the characteristics of a particular data structure or algorithm, it's hard to avoid analyzing its speed and space usage.
In summary, it's recommended that you establish a preliminary understanding of complexity analysis before diving deep into data structures and algorithms, so that you can carry out simple complexity analyses of algorithms.
"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 Space complexity","text":"\"Space complexity\" is used to measure the growth trend of the memory space occupied by an algorithm as the amount of data increases. This concept is very similar to time complexity, except that \"running time\" is replaced with \"occupied memory space\".
"},{"location":"chapter_computational_complexity/space_complexity/#241-space-related-to-algorithms","title":"2.4.1 \u00a0 Space related to algorithms","text":"The memory space used by an algorithm during its execution mainly includes the following types.
Generally, the scope of space complexity statistics includes both \"Temporary Space\" and \"Output Space\".
Temporary space can be further divided into three parts.
When analyzing the space complexity of a program, we typically count the Temporary Data, Stack Frame Space, and Output Data, as shown in the Figure 2-15 .
Figure 2-15 \u00a0 Space types used in algorithms
The relevant code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigclass Node:\n \"\"\"Classes\"\"\"\n def __init__(self, x: int):\n self.val: int = x # node value\n self.next: Node | None = None # reference to the next node\n\ndef function() -> int:\n \"\"\"Functions\"\"\"\n # Perform certain operations...\n return 0\n\ndef algorithm(n) -> int: # input data\n A = 0 # temporary data (constant, usually in uppercase)\n b = 0 # temporary data (variable)\n node = Node(0) # temporary data (object)\n c = function() # Stack frame space (call function)\n return A + b + c # output data\n
/* Structures */\nstruct Node {\n int val;\n Node *next;\n Node(int x) : val(x), next(nullptr) {}\n};\n\n/* Functions */\nint func() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node* node = new Node(0); // temporary data (object)\n int c = func(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* Functions */\nint function() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n final int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = new Node(0); // temporary data (object)\n int c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(int x) { val = x; }\n}\n\n/* Functions */\nint Function() {\n // Perform certain operations...\n return 0;\n}\n\nint Algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = new(0); // temporary data (object)\n int c = Function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Structures */\ntype node struct {\n val int\n next *node\n}\n\n/* Create node structure */\nfunc newNode(val int) *node {\n return &node{val: val}\n}\n\n/* Functions */\nfunc function() int {\n // Perform certain operations...\n return 0\n}\n\nfunc algorithm(n int) int { // input data\n const a = 0 // temporary data (constant)\n b := 0 // temporary storage of data (variable)\n newNode(0) // temporary data (object)\n c := function() // stack frame space (call function)\n return a + b + c // output data\n}\n
/* Classes */\nclass Node {\n var val: Int\n var next: Node?\n\n init(x: Int) {\n val = x\n }\n}\n\n/* Functions */\nfunc function() -> Int {\n // Perform certain operations...\n return 0\n}\n\nfunc algorithm(n: Int) -> Int { // input data\n let a = 0 // temporary data (constant)\n var b = 0 // temporary data (variable)\n let node = Node(x: 0) // temporary data (object)\n let c = function() // stack frame space (call function)\n return a + b + c // output data\n}\n
/* Classes */\nclass Node {\n val;\n next;\n constructor(val) {\n this.val = val === undefined ? 0 : val; // node value\n this.next = null; // reference to the next node\n }\n}\n\n/* Functions */\nfunction constFunc() {\n // Perform certain operations\n return 0;\n}\n\nfunction algorithm(n) { // input data\n const a = 0; // temporary data (constant)\n let b = 0; // temporary data (variable)\n const node = new Node(0); // temporary data (object)\n const c = constFunc(); // Stack frame space (calling function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n val: number;\n next: Node | null;\n constructor(val?: number) {\n this.val = val === undefined ? 0 : val; // node value\n this.next = null; // reference to the next node\n }\n}\n\n/* Functions */\nfunction constFunc(): number {\n // Perform certain operations\n return 0;\n}\n\nfunction algorithm(n: number): number { // input data\n const a = 0; // temporary data (constant)\n let b = 0; // temporary data (variable)\n const node = new Node(0); // temporary data (object)\n const c = constFunc(); // Stack frame space (calling function)\n return a + b + c; // output data\n}\n
/* Classes */\nclass Node {\n int val;\n Node next;\n Node(this.val, [this.next]);\n}\n\n/* Functions */\nint function() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n Node node = Node(0); // temporary data (object)\n int c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Structures */\nstruct Node {\n val: i32,\n next: Option<Rc<RefCell<Node>>>,\n}\n\n/* Constructor */\nimpl Node {\n fn new(val: i32) -> Self {\n Self { val: val, next: None }\n }\n}\n\n/* Functions */\nfn function() -> i32 { \n // Perform certain operations...\n return 0;\n}\n\nfn algorithm(n: i32) -> i32 { // input data\n const a: i32 = 0; // temporary data (constant)\n let mut b = 0; // temporary data (variable)\n let node = Node::new(0); // temporary data (object)\n let c = function(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
/* Functions */\nint func() {\n // Perform certain operations...\n return 0;\n}\n\nint algorithm(int n) { // input data\n const int a = 0; // temporary data (constant)\n int b = 0; // temporary data (variable)\n int c = func(); // stack frame space (call function)\n return a + b + c; // output data\n}\n
\n
\n
"},{"location":"chapter_computational_complexity/space_complexity/#242-calculation-method","title":"2.4.2 \u00a0 Calculation method","text":"The method for calculating space complexity is roughly similar to that of time complexity, with the only change being the shift of the statistical object from \"number of operations\" to \"size of used space\".
However, unlike time complexity, we usually only focus on the worst-case space complexity. This is because memory space is a hard requirement, and we must ensure that there is enough memory space reserved under all input data.
Consider the following code, the term \"worst-case\" in worst-case space complexity has two meanings.
nums
occupies \\(O(n)\\) space, thus the worst-case space complexity is \\(O(n)\\).nums
, the program occupies \\(O(n)\\) space, hence the worst-case space complexity is \\(O(n)\\).def algorithm(n: int):\n a = 0 # O(1)\n b = [0] * 10000 # O(1)\n if n > 10:\n nums = [0] * n # O(n)\n
void algorithm(int n) {\n int a = 0; // O(1)\n vector<int> b(10000); // O(1)\n if (n > 10)\n vector<int> nums(n); // O(n)\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10)\n int[] nums = new int[n]; // O(n)\n}\n
void Algorithm(int n) {\n int a = 0; // O(1)\n int[] b = new int[10000]; // O(1)\n if (n > 10) {\n int[] nums = new int[n]; // O(n)\n }\n}\n
func algorithm(n int) {\n a := 0 // O(1)\n b := make([]int, 10000) // O(1)\n var nums []int\n if n > 10 {\n nums := make([]int, n) // O(n)\n }\n fmt.Println(a, b, nums)\n}\n
func algorithm(n: Int) {\n let a = 0 // O(1)\n let b = Array(repeating: 0, count: 10000) // O(1)\n if n > 10 {\n let nums = Array(repeating: 0, count: n) // O(n)\n }\n}\n
function algorithm(n) {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
function algorithm(n: number): void {\n const a = 0; // O(1)\n const b = new Array(10000); // O(1)\n if (n > 10) {\n const nums = new Array(n); // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n List<int> b = List.filled(10000, 0); // O(1)\n if (n > 10) {\n List<int> nums = List.filled(n, 0); // O(n)\n }\n}\n
fn algorithm(n: i32) {\n let a = 0; // O(1)\n let b = [0; 10000]; // O(1)\n if n > 10 {\n let nums = vec![0; n as usize]; // O(n)\n }\n}\n
void algorithm(int n) {\n int a = 0; // O(1)\n int b[10000]; // O(1)\n if (n > 10)\n int nums[n] = {0}; // O(n)\n}\n
\n
\n
In recursive functions, stack frame space must be taken into count. Consider the following code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef function() -> int:\n # Perform certain operations\n return 0\n\ndef loop(n: int):\n \"\"\"Loop O(1)\"\"\"\n for _ in range(n):\n function()\n\ndef recur(n: int):\n \"\"\"Recursion O(n)\"\"\"\n if n == 1:\n return\n return recur(n - 1)\n
int func() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
int Function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid Loop(int n) {\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n/* Recursion O(n) */\nint Recur(int n) {\n if (n == 1) return 1;\n return Recur(n - 1);\n}\n
func function() int {\n // Perform certain operations\n return 0\n}\n\n/* Cycle O(1) */\nfunc loop(n int) {\n for i := 0; i < n; i++ {\n function()\n }\n}\n\n/* Recursion O(n) */\nfunc recur(n int) {\n if n == 1 {\n return\n }\n recur(n - 1)\n}\n
@discardableResult\nfunc function() -> Int {\n // Perform certain operations\n return 0\n}\n\n/* Cycle O(1) */\nfunc loop(n: Int) {\n for _ in 0 ..< n {\n function()\n }\n}\n\n/* Recursion O(n) */\nfunc recur(n: Int) {\n if n == 1 {\n return\n }\n recur(n: n - 1)\n}\n
function constFunc() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfunction loop(n) {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* Recursion O(n) */\nfunction recur(n) {\n if (n === 1) return;\n return recur(n - 1);\n}\n
function constFunc(): number {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfunction loop(n: number): void {\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n/* Recursion O(n) */\nfunction recur(n: number): void {\n if (n === 1) return;\n return recur(n - 1);\n}\n
int function() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
fn function() -> i32 {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nfn loop(n: i32) {\n for i in 0..n {\n function();\n }\n}\n/* Recursion O(n) */\nvoid recur(n: i32) {\n if n == 1 {\n return;\n }\n recur(n - 1);\n}\n
int func() {\n // Perform certain operations\n return 0;\n}\n/* Cycle O(1) */\nvoid loop(int n) {\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n/* Recursion O(n) */\nvoid recur(int n) {\n if (n == 1) return;\n return recur(n - 1);\n}\n
\n
\n
The time complexity of both loop()
and recur()
functions is \\(O(n)\\), but their space complexities differ.
loop()
function calls function()
\\(n\\) times in a loop, where each iteration's function()
returns and releases its stack frame space, so the space complexity remains \\(O(1)\\).recur()
will have \\(n\\) instances of unreturned recur()
existing simultaneously during its execution, thus occupying \\(O(n)\\) stack frame space.Let the size of the input data be \\(n\\), the following chart displays common types of space complexities (arranged from low to high).
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{Constant Order} < \\text{Logarithmic Order} < \\text{Linear Order} < \\text{Quadratic Order} < \\text{Exponential Order} \\end{aligned} \\]Figure 2-16 \u00a0 Common types of space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant order \\(O(1)\\)","text":"Constant order is common in constants, variables, objects that are independent of the size of input data \\(n\\).
Note that memory occupied by initializing variables or calling functions in a loop, which is released upon entering the next cycle, does not accumulate over space, thus the space complexity remains \\(O(1)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef function() -> int:\n \"\"\"\u51fd\u6570\"\"\"\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n\ndef constant(n: int):\n \"\"\"\u5e38\u6570\u9636\"\"\"\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode(0)\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n c = 0\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in range(n):\n function()\n
space_complexity.cpp/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n vector<int> nums(10000);\n ListNode node(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.java/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.cs/* \u51fd\u6570 */\nint Function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid Constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n int a = 0;\n int b = 0;\n int[] nums = new int[10000];\n ListNode node = new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n Function();\n }\n}\n
space_complexity.go/* \u51fd\u6570 */\nfunc function() int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c...\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc spaceConstant(n int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0\n b := 0\n nums := make([]int, 10000)\n node := newNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n var c int\n for i := 0; i < n; i++ {\n c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i := 0; i < n; i++ {\n function()\n }\n b += 0\n c += 0\n nums[0] = 0\n node.val = 0\n}\n
space_complexity.swift/* \u51fd\u6570 */\n@discardableResult\nfunc function() -> Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n let a = 0\n var b = 0\n let nums = Array(repeating: 0, count: 10000)\n let node = ListNode(x: 0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n let c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for _ in 0 ..< n {\n function()\n }\n}\n
space_complexity.js/* \u51fd\u6570 */\nfunction constFunc() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.ts/* \u51fd\u6570 */\nfunction constFunc(): number {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nfunction constant(n: number): void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a = 0;\n const b = 0;\n const nums = new Array(10000);\n const node = new ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n const c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (let i = 0; i < n; i++) {\n constFunc();\n }\n}\n
space_complexity.dart/* \u51fd\u6570 */\nint function() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n final int a = 0;\n int b = 0;\n List<int> nums = List.filled(10000, 0);\n ListNode node = ListNode(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (var i = 0; i < n; i++) {\n function();\n }\n}\n
space_complexity.rs/* \u51fd\u6570 */\nfn function() -> i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\n#[allow(unused)]\nfn constant(n: i32) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const A: i32 = 0;\n let b = 0;\n let nums = vec![0; 10000];\n let node = ListNode::new(0);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n let c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for i in 0..n {\n function();\n }\n}\n
space_complexity.c/* \u51fd\u6570 */\nint func() {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n/* \u5e38\u6570\u9636 */\nvoid constant(int n) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const int a = 0;\n int b = 0;\n int nums[1000];\n ListNode *node = newListNode(0);\n free(node);\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n int c = 0;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (int i = 0; i < n; i++) {\n func();\n }\n}\n
space_complexity.kt/* \u51fd\u6570 */\nfun function(): Int {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0\n}\n\n/* \u5e38\u6570\u9636 */\nfun constant(n: Int) {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n val a = 0\n var b = 0\n val nums = Array(10000) { 0 }\n val node = ListNode(0)\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n for (i in 0..<n) {\n val c = 0\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n for (i in 0..<n) {\n function()\n }\n}\n
space_complexity.rb### \u51fd\u6570 ###\ndef function\n # \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n 0\nend\n\n### \u5e38\u6570\u9636 ###\ndef constant(n)\n # \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n a = 0\n nums = [0] * 10000\n node = ListNode.new\n\n # \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n (0...n).each { c = 0 }\n # \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n (0...n).each { function }\nend\n
space_complexity.zig// \u51fd\u6570\nfn function() i32 {\n // \u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\n return 0;\n}\n\n// \u5e38\u6570\u9636\nfn constant(n: i32) void {\n // \u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5bf9\u8c61\u5360\u7528 O(1) \u7a7a\u95f4\n const a: i32 = 0;\n var b: i32 = 0;\n var nums = [_]i32{0}**10000;\n var node = inc.ListNode(i32){.val = 0};\n var i: i32 = 0;\n // \u5faa\u73af\u4e2d\u7684\u53d8\u91cf\u5360\u7528 O(1) \u7a7a\u95f4\n while (i < n) : (i += 1) {\n var c: i32 = 0;\n _ = c;\n }\n // \u5faa\u73af\u4e2d\u7684\u51fd\u6570\u5360\u7528 O(1) \u7a7a\u95f4\n i = 0;\n while (i < n) : (i += 1) {\n _ = function();\n }\n _ = a;\n _ = b;\n _ = nums;\n _ = node;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/space_complexity/#2-linear-order-on","title":"2. \u00a0 Linear order \\(O(n)\\)","text":"Linear order is common in arrays, linked lists, stacks, queues, etc., where the number of elements is proportional to \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef linear(n: int):\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = [0] * n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = dict[int, str]()\n for i in range(n):\n hmap[i] = str(i)\n
space_complexity.cpp/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n vector<int> nums(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n vector<ListNode> nodes;\n for (int i = 0; i < n; i++) {\n nodes.push_back(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n unordered_map<int, string> map;\n for (int i = 0; i < n; i++) {\n map[i] = to_string(i);\n }\n}\n
space_complexity.java/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n nodes.add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<Integer, String> map = new HashMap<>();\n for (int i = 0; i < n; i++) {\n map.put(i, String.valueOf(i));\n }\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636 */\nvoid Linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int[] nums = new int[n];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (int i = 0; i < n; i++) {\n nodes.Add(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Dictionary<int, string> map = [];\n for (int i = 0; i < n; i++) {\n map.Add(i, i.ToString());\n }\n}\n
space_complexity.go/* \u7ebf\u6027\u9636 */\nfunc spaceLinear(n int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n _ = make([]int, n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes []*node\n for i := 0; i < n; i++ {\n nodes = append(nodes, newNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n m := make(map[int]string, n)\n for i := 0; i < n; i++ {\n m[i] = strconv.Itoa(i)\n }\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let nums = Array(repeating: 0, count: n)\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let nodes = (0 ..< n).map { ListNode(x: $0) }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
space_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n const nums = new Array(n);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const nodes: ListNode[] = [];\n for (let i = 0; i < n; i++) {\n nodes.push(new ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n const map = new Map();\n for (let i = 0; i < n; i++) {\n map.set(i, i.toString());\n }\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n List<int> nums = List.filled(n, 0);\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n List<ListNode> nodes = [];\n for (var i = 0; i < n; i++) {\n nodes.add(ListNode(i));\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n Map<int, String> map = HashMap();\n for (var i = 0; i < n; i++) {\n map.putIfAbsent(i, () => i.toString());\n }\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636 */\n#[allow(unused)]\nfn linear(n: i32) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nums = vec![0; n as usize];\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut nodes = Vec::new();\n for i in 0..n {\n nodes.push(ListNode::new(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n let mut map = HashMap::new();\n for i in 0..n {\n map.insert(i, i.to_string());\n }\n}\n
space_complexity.c/* \u54c8\u5e0c\u8868 */\ntypedef struct {\n int key;\n int val;\n UT_hash_handle hh; // \u57fa\u4e8e uthash.h \u5b9e\u73b0\n} HashTable;\n\n/* \u7ebf\u6027\u9636 */\nvoid linear(int n) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n int *nums = malloc(sizeof(int) * n);\n free(nums);\n\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n ListNode **nodes = malloc(sizeof(ListNode *) * n);\n for (int i = 0; i < n; i++) {\n nodes[i] = newListNode(i);\n }\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(nodes[i]);\n }\n free(nodes);\n\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n HashTable *h = NULL;\n for (int i = 0; i < n; i++) {\n HashTable *tmp = malloc(sizeof(HashTable));\n tmp->key = i;\n tmp->val = i;\n HASH_ADD_INT(h, key, tmp);\n }\n\n // \u5185\u5b58\u91ca\u653e\n HashTable *curr, *tmp;\n HASH_ITER(hh, h, curr, tmp) {\n HASH_DEL(h, curr);\n free(curr);\n }\n}\n
space_complexity.kt/* \u7ebf\u6027\u9636 */\nfun linear(n: Int) {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n val nums = Array(n) { 0 }\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n val nodes = mutableListOf<ListNode>()\n for (i in 0..<n) {\n nodes.add(ListNode(i))\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n val map = mutableMapOf<Int, String>()\n for (i in 0..<n) {\n map[i] = i.toString()\n }\n}\n
space_complexity.rb### \u7ebf\u6027\u9636 ###\ndef linear(n)\n # \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n nums = Array.new(n, 0)\n\n # \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n hmap = {}\n for i in 0...n\n hmap[i] = i.to_s\n end\nend\n
space_complexity.zig// \u7ebf\u6027\u9636\nfn linear(comptime n: i32) !void {\n // \u957f\u5ea6\u4e3a n \u7684\u6570\u7ec4\u5360\u7528 O(n) \u7a7a\u95f4\n var nums = [_]i32{0}**n;\n // \u957f\u5ea6\u4e3a n \u7684\u5217\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var nodes = std.ArrayList(i32).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n try nodes.append(i);\n }\n // \u957f\u5ea6\u4e3a n \u7684\u54c8\u5e0c\u8868\u5360\u7528 O(n) \u7a7a\u95f4\n var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);\n defer map.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n const string = try std.fmt.allocPrint(std.heap.page_allocator, \"{d}\", .{j});\n defer std.heap.page_allocator.free(string);\n try map.put(i, string);\n }\n _ = nums;\n}\n
Code Visualization Full Screen >
As shown below, this function's recursive depth is \\(n\\), meaning there are \\(n\\) instances of unreturned linear_recur()
function, using \\(O(n)\\) size of stack frame space:
def linear_recur(n: int):\n \"\"\"\u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n print(\"\u9012\u5f52 n =\", n)\n if n == 1:\n return\n linear_recur(n - 1)\n
space_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n cout << \"\u9012\u5f52 n = \" << n << endl;\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.java/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n System.out.println(\"\u9012\u5f52 n = \" + n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.cs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid LinearRecur(int n) {\n Console.WriteLine(\"\u9012\u5f52 n = \" + n);\n if (n == 1) return;\n LinearRecur(n - 1);\n}\n
space_complexity.go/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceLinearRecur(n int) {\n fmt.Println(\"\u9012\u5f52 n =\", n)\n if n == 1 {\n return\n }\n spaceLinearRecur(n - 1)\n}\n
space_complexity.swift/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc linearRecur(n: Int) {\n print(\"\u9012\u5f52 n = \\(n)\")\n if n == 1 {\n return\n }\n linearRecur(n: n - 1)\n}\n
space_complexity.js/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n) {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.ts/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction linearRecur(n: number): void {\n console.log(`\u9012\u5f52 n = ${n}`);\n if (n === 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.dart/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n print('\u9012\u5f52 n = $n');\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
space_complexity.rs/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn linear_recur(n: i32) {\n println!(\"\u9012\u5f52 n = {}\", n);\n if n == 1 {\n return;\n };\n linear_recur(n - 1);\n}\n
space_complexity.c/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nvoid linearRecur(int n) {\n printf(\"\u9012\u5f52 n = %d\\r\\n\", n);\n if (n == 1)\n return;\n linearRecur(n - 1);\n}\n
space_complexity.kt/* \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun linearRecur(n: Int) {\n println(\"\u9012\u5f52 n = $n\")\n if (n == 1)\n return\n linearRecur(n - 1)\n}\n
space_complexity.rb### \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef linear_recur(n)\n puts \"\u9012\u5f52 n = #{n}\"\n return if n == 1\n linear_recur(n - 1)\nend\n
space_complexity.zig// \u7ebf\u6027\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn linearRecur(comptime n: i32) void {\n std.debug.print(\"\u9012\u5f52 n = {}\\n\", .{n});\n if (n == 1) return;\n linearRecur(n - 1);\n}\n
Code Visualization Full Screen >
Figure 2-17 \u00a0 Recursive function generating linear order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic order \\(O(n^2)\\)","text":"Quadratic order is common in matrices and graphs, where the number of elements is quadratic to \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef quadratic(n: int):\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n num_matrix = [[0] * n for _ in range(n)]\n
space_complexity.cpp/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n vector<vector<int>> numMatrix;\n for (int i = 0; i < n; i++) {\n vector<int> tmp;\n for (int j = 0; j < n; j++) {\n tmp.push_back(0);\n }\n numMatrix.push_back(tmp);\n }\n}\n
space_complexity.java/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[][] numMatrix = new int[n][n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<Integer>> numList = new ArrayList<>();\n for (int i = 0; i < n; i++) {\n List<Integer> tmp = new ArrayList<>();\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636 */\nvoid Quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n int[,] numMatrix = new int[n, n];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (int i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.Add(0);\n }\n numList.Add(tmp);\n }\n}\n
space_complexity.go/* \u5e73\u65b9\u9636 */\nfunc spaceQuadratic(n int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n numMatrix := make([][]int, n)\n for i := 0; i < n; i++ {\n numMatrix[i] = make([]int, n)\n }\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): void {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numMatrix = Array(n)\n .fill(null)\n .map(() => Array(n).fill(null));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n const numList = [];\n for (let i = 0; i < n; i++) {\n const tmp = [];\n for (let j = 0; j < n; j++) {\n tmp.push(0);\n }\n numList.push(tmp);\n }\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n List<List<int>> numList = [];\n for (var i = 0; i < n; i++) {\n List<int> tmp = [];\n for (int j = 0; j < n; j++) {\n tmp.add(0);\n }\n numList.add(tmp);\n }\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636 */\n#[allow(unused)]\nfn quadratic(n: i32) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n let num_matrix = vec![vec![0; n as usize]; n as usize];\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n let mut num_list = Vec::new();\n for i in 0..n {\n let mut tmp = Vec::new();\n for j in 0..n {\n tmp.push(0);\n }\n num_list.push(tmp);\n }\n}\n
space_complexity.c/* \u5e73\u65b9\u9636 */\nvoid quadratic(int n) {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n int **numMatrix = malloc(sizeof(int *) * n);\n for (int i = 0; i < n; i++) {\n int *tmp = malloc(sizeof(int) * n);\n for (int j = 0; j < n; j++) {\n tmp[j] = 0;\n }\n numMatrix[i] = tmp;\n }\n\n // \u5185\u5b58\u91ca\u653e\n for (int i = 0; i < n; i++) {\n free(numMatrix[i]);\n }\n free(numMatrix);\n}\n
space_complexity.kt/* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int) {\n // \u77e9\u9635\u5360\u7528 O(n^2) \u7a7a\u95f4\n val numMatrix: Array<Array<Int>?> = arrayOfNulls(n)\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n val numList: MutableList<MutableList<Int>> = arrayListOf()\n for (i in 0..<n) {\n val tmp = mutableListOf<Int>()\n for (j in 0..<n) {\n tmp.add(0)\n }\n numList.add(tmp)\n }\n}\n
space_complexity.rb### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n # \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n Array.new(n) { Array.new(n, 0) }\nend\n
space_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) !void {\n // \u4e8c\u7ef4\u5217\u8868\u5360\u7528 O(n^2) \u7a7a\u95f4\n var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);\n defer nodes.deinit();\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n var tmp = std.ArrayList(i32).init(std.heap.page_allocator);\n defer tmp.deinit();\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n try tmp.append(0);\n }\n try nodes.append(tmp);\n }\n}\n
Code Visualization Full Screen >
As shown below, the recursive depth of this function is \\(n\\), and in each recursive call, an array is initialized with lengths \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\), averaging \\(n/2\\), thus overall occupying \\(O(n^2)\\) space:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef quadratic_recur(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 0:\n return 0\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = [0] * n\n return quadratic_recur(n - 1)\n
space_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n vector<int> nums(n);\n cout << \"\u9012\u5f52 n = \" << n << \" \u4e2d\u7684 nums \u957f\u5ea6 = \" << nums.size() << endl;\n return quadraticRecur(n - 1);\n}\n
space_complexity.java/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n int[] nums = new int[n];\n System.out.println(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.length);\n return quadraticRecur(n - 1);\n}\n
space_complexity.cs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint QuadraticRecur(int n) {\n if (n <= 0) return 0;\n int[] nums = new int[n];\n Console.WriteLine(\"\u9012\u5f52 n = \" + n + \" \u4e2d\u7684 nums \u957f\u5ea6 = \" + nums.Length);\n return QuadraticRecur(n - 1);\n}\n
space_complexity.go/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc spaceQuadraticRecur(n int) int {\n if n <= 0 {\n return 0\n }\n nums := make([]int, n)\n fmt.Printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d \\n\", n, len(nums))\n return spaceQuadraticRecur(n - 1)\n}\n
space_complexity.swift/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n if n <= 0 {\n return 0\n }\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = Array(repeating: 0, count: n)\n print(\"\u9012\u5f52 n = \\(n) \u4e2d\u7684 nums \u957f\u5ea6 = \\(nums.count)\")\n return quadraticRecur(n: n - 1)\n}\n
space_complexity.js/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n) {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.ts/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction quadraticRecur(n: number): number {\n if (n <= 0) return 0;\n const nums = new Array(n);\n console.log(`\u9012\u5f52 n = ${n} \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}`);\n return quadraticRecur(n - 1);\n}\n
space_complexity.dart/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0) return 0;\n List<int> nums = List.filled(n, 0);\n print('\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.length}');\n return quadraticRecur(n - 1);\n}\n
space_complexity.rs/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn quadratic_recur(n: i32) -> i32 {\n if n <= 0 {\n return 0;\n };\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n let nums = vec![0; n as usize];\n println!(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\", n, nums.len());\n return quadratic_recur(n - 1);\n}\n
space_complexity.c/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint quadraticRecur(int n) {\n if (n <= 0)\n return 0;\n int *nums = malloc(sizeof(int) * n);\n printf(\"\u9012\u5f52 n = %d \u4e2d\u7684 nums \u957f\u5ea6 = %d\\r\\n\", n, n);\n int res = quadraticRecur(n - 1);\n free(nums);\n return res;\n}\n
space_complexity.kt/* \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\ntailrec fun quadraticRecur(n: Int): Int {\n if (n <= 0)\n return 0\n // \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n val nums = Array(n) { 0 }\n println(\"\u9012\u5f52 n = $n \u4e2d\u7684 nums \u957f\u5ea6 = ${nums.size}\")\n return quadraticRecur(n - 1)\n}\n
space_complexity.rb### \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef quadratic_recur(n)\n return 0 unless n > 0\n\n # \u6570\u7ec4 nums \u957f\u5ea6\u4e3a n, n-1, ..., 2, 1\n nums = Array.new(n, 0)\n quadratic_recur(n - 1)\nend\n
space_complexity.zig// \u5e73\u65b9\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn quadraticRecur(comptime n: i32) i32 {\n if (n <= 0) return 0;\n var nums = [_]i32{0}**n;\n std.debug.print(\"\u9012\u5f52 n = {} \u4e2d\u7684 nums \u957f\u5ea6 = {}\\n\", .{n, nums.len});\n return quadraticRecur(n - 1);\n}\n
Code Visualization Full Screen >
Figure 2-18 \u00a0 Recursive function generating quadratic order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential order \\(O(2^n)\\)","text":"Exponential order is common in binary trees. Observe the below image, a \"full binary tree\" with \\(n\\) levels has \\(2^n - 1\\) nodes, occupying \\(O(2^n)\\) space:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig space_complexity.pydef build_tree(n: int) -> TreeNode | None:\n \"\"\"\u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\"\"\"\n if n == 0:\n return None\n root = TreeNode(0)\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n return root\n
space_complexity.cpp/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return nullptr;\n TreeNode *root = new TreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.java/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode buildTree(int n) {\n if (n == 0)\n return null;\n TreeNode root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.cs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? BuildTree(int n) {\n if (n == 0) return null;\n TreeNode root = new(0) {\n left = BuildTree(n - 1),\n right = BuildTree(n - 1)\n };\n return root;\n}\n
space_complexity.go/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n int) *TreeNode {\n if n == 0 {\n return nil\n }\n root := NewTreeNode(0)\n root.Left = buildTree(n - 1)\n root.Right = buildTree(n - 1)\n return root\n}\n
space_complexity.swift/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunc buildTree(n: Int) -> TreeNode? {\n if n == 0 {\n return nil\n }\n let root = TreeNode(x: 0)\n root.left = buildTree(n: n - 1)\n root.right = buildTree(n: n - 1)\n return root\n}\n
space_complexity.js/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n) {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.ts/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfunction buildTree(n: number): TreeNode | null {\n if (n === 0) return null;\n const root = new TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.dart/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode? buildTree(int n) {\n if (n == 0) return null;\n TreeNode root = TreeNode(0);\n root.left = buildTree(n - 1);\n root.right = buildTree(n - 1);\n return root;\n}\n
space_complexity.rs/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n if n == 0 {\n return None;\n };\n let root = TreeNode::new(0);\n root.borrow_mut().left = build_tree(n - 1);\n root.borrow_mut().right = build_tree(n - 1);\n return Some(root);\n}\n
space_complexity.c/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nTreeNode *buildTree(int n) {\n if (n == 0)\n return NULL;\n TreeNode *root = newTreeNode(0);\n root->left = buildTree(n - 1);\n root->right = buildTree(n - 1);\n return root;\n}\n
space_complexity.kt/* \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09 */\nfun buildTree(n: Int): TreeNode? {\n if (n == 0)\n return null\n val root = TreeNode(0)\n root.left = buildTree(n - 1)\n root.right = buildTree(n - 1)\n return root\n}\n
space_complexity.rb### \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09###\ndef build_tree(n)\n return if n == 0\n\n TreeNode.new.tap do |root|\n root.left = build_tree(n - 1)\n root.right = build_tree(n - 1)\n end\nend\n
space_complexity.zig// \u6307\u6570\u9636\uff08\u5efa\u7acb\u6ee1\u4e8c\u53c9\u6811\uff09\nfn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {\n if (n == 0) return null;\n const root = try mem_allocator.create(inc.TreeNode(i32));\n root.init(0);\n root.left = try buildTree(mem_allocator, n - 1);\n root.right = try buildTree(mem_allocator, n - 1);\n return root;\n}\n
Code Visualization Full Screen >
Figure 2-19 \u00a0 Full binary tree generating exponential order space complexity
"},{"location":"chapter_computational_complexity/space_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic order \\(O(\\log n)\\)","text":"Logarithmic order is common in divide-and-conquer algorithms. For example, in merge sort, an array of length \\(n\\) is recursively divided in half each round, forming a recursion tree of height \\(\\log n\\), using \\(O(\\log n)\\) stack frame space.
Another example is converting a number to a string. Given a positive integer \\(n\\), its number of digits is \\(\\log_{10} n + 1\\), corresponding to the length of the string, thus the space complexity is \\(O(\\log_{10} n + 1) = O(\\log n)\\).
"},{"location":"chapter_computational_complexity/space_complexity/#244-balancing-time-and-space","title":"2.4.4 \u00a0 Balancing time and space","text":"Ideally, we aim for both time complexity and space complexity to be optimal. However, in practice, optimizing both simultaneously is often difficult.
Lowering time complexity usually comes at the cost of increased space complexity, and vice versa. The approach of sacrificing memory space to improve algorithm speed is known as \"space-time tradeoff\"; the reverse is known as \"time-space tradeoff\".
The choice depends on which aspect we value more. In most cases, time is more precious than space, so \"space-time tradeoff\" is often the more common strategy. Of course, controlling space complexity is also very important when dealing with large volumes of data.
"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 Summary","text":""},{"location":"chapter_computational_complexity/summary/#1-key-review","title":"1. \u00a0 Key review","text":"Algorithm Efficiency Assessment
Time Complexity
Space Complexity
Q: Is the space complexity of tail recursion \\(O(1)\\)?
Theoretically, the space complexity of a tail-recursive function can be optimized to \\(O(1)\\). However, most programming languages (such as Java, Python, C++, Go, C#) do not support automatic optimization of tail recursion, so it's generally considered to have a space complexity of \\(O(n)\\).
Q: What is the difference between the terms \"function\" and \"method\"?
A \"function\" can be executed independently, with all parameters passed explicitly. A \"method\" is associated with an object and is implicitly passed to the object calling it, able to operate on the data contained within an instance of a class.
Here are some examples from common programming languages:
Q: Does the \"Common Types of Space Complexity\" figure reflect the absolute size of occupied space?
No, the figure shows space complexities, which reflect growth trends, not the absolute size of the occupied space.
If you take \\(n = 8\\), you might find that the values of each curve don't correspond to their functions. This is because each curve includes a constant term, intended to compress the value range into a visually comfortable range.
In practice, since we usually don't know the \"constant term\" complexity of each method, it's generally not possible to choose the best solution for \\(n = 8\\) based solely on complexity. However, for \\(n = 8^5\\), it's much easier to choose, as the growth trend becomes dominant.
"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 Time complexity","text":"Time complexity is a concept used to measure how the run time of an algorithm increases with the size of the input data. Understanding time complexity is crucial for accurately assessing the efficiency of an algorithm.
+
might take 1 ns, a multiplication operation *
might take 10 ns, a print operation print()
might take 5 ns, etc.For example, consider the following code with an input size of \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig# Under an operating platform\ndef algorithm(n: int):\n a = 2 # 1 ns\n a = a + 1 # 1 ns\n a = a * 2 # 10 ns\n # Cycle n times\n for _ in range(n): # 1 ns\n print(0) # 5 ns\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n cout << 0 << endl; // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n System.out.println(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid Algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n Console.WriteLine(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfunc algorithm(n int) {\n a := 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // Loop n times\n for i := 0; i < n; i++ { // 1 ns\n fmt.Println(a) // 5 ns\n }\n}\n
// Under a particular operating platform\nfunc algorithm(n: Int) {\n var a = 2 // 1 ns\n a = a + 1 // 1 ns\n a = a * 2 // 10 ns\n // Loop n times\n for _ in 0 ..< n { // 1 ns\n print(0) // 5 ns\n }\n}\n
// Under a particular operating platform\nfunction algorithm(n) {\n var a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for(let i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n console.log(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfunction algorithm(n: number): void {\n var a: number = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for(let i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n console.log(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n print(0); // 5 ns\n }\n}\n
// Under a particular operating platform\nfn algorithm(n: i32) {\n let mut a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for _ in 0..n { // 1 ns for each round i++\n println!(\"{}\", 0); // 5 ns\n }\n}\n
// Under a particular operating platform\nvoid algorithm(int n) {\n int a = 2; // 1 ns\n a = a + 1; // 1 ns\n a = a * 2; // 10 ns\n // Loop n times\n for (int i = 0; i < n; i++) { // 1 ns , every round i++ is executed\n printf(\"%d\", 0); // 5 ns\n }\n}\n
\n
// Under a particular operating platform\nfn algorithm(n: usize) void {\n var a: i32 = 2; // 1 ns\n a += 1; // 1 ns\n a *= 2; // 10 ns\n // Loop n times\n for (0..n) |_| { // 1 ns\n std.debug.print(\"{}\\n\", .{0}); // 5 ns\n }\n}\n
Using the above method, the run time of the algorithm can be calculated as \\((6n + 12)\\) ns:
\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]However, in practice, counting the run time of an algorithm is neither practical nor reasonable. First, we don't want to tie the estimated time to the running platform, as algorithms need to run on various platforms. Second, it's challenging to know the run time for each type of operation, making the estimation process difficult.
"},{"location":"chapter_computational_complexity/time_complexity/#231-assessing-time-growth-trend","title":"2.3.1 \u00a0 Assessing time growth trend","text":"Time complexity analysis does not count the algorithm's run time, but rather the growth trend of the run time as the data volume increases.
Let's understand this concept of \"time growth trend\" with an example. Assume the input data size is \\(n\\), and consider three algorithms A
, B
, and C
:
# Time complexity of algorithm A: constant order\ndef algorithm_A(n: int):\n print(0)\n# Time complexity of algorithm B: linear order\ndef algorithm_B(n: int):\n for _ in range(n):\n print(0)\n# Time complexity of algorithm C: constant order\ndef algorithm_C(n: int):\n for _ in range(1000000):\n print(0)\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n cout << 0 << endl;\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n cout << 0 << endl;\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n cout << 0 << endl;\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n System.out.println(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n System.out.println(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n System.out.println(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid AlgorithmA(int n) {\n Console.WriteLine(0);\n}\n// Time complexity of algorithm B: linear order\nvoid AlgorithmB(int n) {\n for (int i = 0; i < n; i++) {\n Console.WriteLine(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid AlgorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n Console.WriteLine(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfunc algorithm_A(n int) {\n fmt.Println(0)\n}\n// Time complexity of algorithm B: linear order\nfunc algorithm_B(n int) {\n for i := 0; i < n; i++ {\n fmt.Println(0)\n }\n}\n// Time complexity of algorithm C: constant order\nfunc algorithm_C(n int) {\n for i := 0; i < 1000000; i++ {\n fmt.Println(0)\n }\n}\n
// Time complexity of algorithm A: constant order\nfunc algorithmA(n: Int) {\n print(0)\n}\n\n// Time complexity of algorithm B: linear order\nfunc algorithmB(n: Int) {\n for _ in 0 ..< n {\n print(0)\n }\n}\n\n// Time complexity of algorithm C: constant order\nfunc algorithmC(n: Int) {\n for _ in 0 ..< 1_000_000 {\n print(0)\n }\n}\n
// Time complexity of algorithm A: constant order\nfunction algorithm_A(n) {\n console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n) {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n) {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfunction algorithm_A(n: number): void {\n console.log(0);\n}\n// Time complexity of algorithm B: linear order\nfunction algorithm_B(n: number): void {\n for (let i = 0; i < n; i++) {\n console.log(0);\n }\n}\n// Time complexity of algorithm C: constant order\nfunction algorithm_C(n: number): void {\n for (let i = 0; i < 1000000; i++) {\n console.log(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithmA(int n) {\n print(0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithmB(int n) {\n for (int i = 0; i < n; i++) {\n print(0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithmC(int n) {\n for (int i = 0; i < 1000000; i++) {\n print(0);\n }\n}\n
// Time complexity of algorithm A: constant order\nfn algorithm_A(n: i32) {\n println!(\"{}\", 0);\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) {\n for _ in 0..n {\n println!(\"{}\", 0);\n }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) {\n for _ in 0..1000000 {\n println!(\"{}\", 0);\n }\n}\n
// Time complexity of algorithm A: constant order\nvoid algorithm_A(int n) {\n printf(\"%d\", 0);\n}\n// Time complexity of algorithm B: linear order\nvoid algorithm_B(int n) {\n for (int i = 0; i < n; i++) {\n printf(\"%d\", 0);\n }\n}\n// Time complexity of algorithm C: constant order\nvoid algorithm_C(int n) {\n for (int i = 0; i < 1000000; i++) {\n printf(\"%d\", 0);\n }\n}\n
\n
// Time complexity of algorithm A: constant order\nfn algorithm_A(n: usize) void {\n _ = n;\n std.debug.print(\"{}\\n\", .{0});\n}\n// Time complexity of algorithm B: linear order\nfn algorithm_B(n: i32) void {\n for (0..n) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n// Time complexity of algorithm C: constant order\nfn algorithm_C(n: i32) void {\n _ = n;\n for (0..1000000) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n}\n
The following figure shows the time complexities of these three algorithms.
A
has just one print operation, and its run time does not grow with \\(n\\). Its time complexity is considered \"constant order.\"B
involves a print operation looping \\(n\\) times, and its run time grows linearly with \\(n\\). Its time complexity is \"linear order.\"C
has a print operation looping 1,000,000 times. Although it takes a long time, it is independent of the input data size \\(n\\). Therefore, the time complexity of C
is the same as A
, which is \"constant order.\"Figure 2-7 \u00a0 Time growth trend of algorithms a, b, and c
Compared to directly counting the run time of an algorithm, what are the characteristics of time complexity analysis?
B
has linearly growing run time, which is slower than algorithm A
when \\(n > 1\\) and slower than C
when \\(n > 1,000,000\\). In fact, as long as the input data size \\(n\\) is sufficiently large, a \"constant order\" complexity algorithm will always be better than a \"linear order\" one, demonstrating the essence of time growth trend.A
and C
have the same time complexity, their actual run times can be quite different. Similarly, even though algorithm B
has a higher time complexity than C
, it is clearly superior when the input data size \\(n\\) is small. In these cases, it's difficult to judge the efficiency of algorithms based solely on time complexity. Nonetheless, despite these issues, complexity analysis remains the most effective and commonly used method for evaluating algorithm efficiency.Consider a function with an input size of \\(n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef algorithm(n: int):\n a = 1 # +1\n a = a + 1 # +1\n a = a * 2 # +1\n # Cycle n times\n for i in range(n): # +1\n print(0) # +1\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n cout << 0 << endl; // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n System.out.println(0); // +1\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n Console.WriteLine(0); // +1\n }\n}\n
func algorithm(n int) {\n a := 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // Loop n times\n for i := 0; i < n; i++ { // +1\n fmt.Println(a) // +1\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +1\n a = a + 1 // +1\n a = a * 2 // +1\n // Loop n times\n for _ in 0 ..< n { // +1\n print(0) // +1\n }\n}\n
function algorithm(n) {\n var a = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for(let i = 0; i < n; i++){ // +1 (execute i ++ every round)\n console.log(0); // +1\n }\n}\n
function algorithm(n: number): void{\n var a: number = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for(let i = 0; i < n; i++){ // +1 (execute i ++ every round)\n console.log(0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n print(0); // +1\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n\n // Loop n times\n for _ in 0..n { // +1 (execute i ++ every round)\n println!(\"{}\", 0); // +1\n }\n}\n
void algorithm(int n) {\n int a = 1; // +1\n a = a + 1; // +1\n a = a * 2; // +1\n // Loop n times\n for (int i = 0; i < n; i++) { // +1 (execute i ++ every round)\n printf(\"%d\", 0); // +1\n }\n} \n
\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +1\n a += 1; // +1\n a *= 2; // +1\n // Loop n times\n for (0..n) |_| { // +1 (execute i ++ every round)\n std.debug.print(\"{}\\n\", .{0}); // +1\n }\n}\n
Given a function that represents the number of operations of an algorithm as a function of the input size \\(n\\), denoted as \\(T(n)\\), consider the following example:
\\[ T(n) = 3 + 2n \\]Since \\(T(n)\\) is a linear function, its growth trend is linear, and therefore, its time complexity is of linear order, denoted as \\(O(n)\\). This mathematical notation, known as \"big-O notation,\" represents the \"asymptotic upper bound\" of the function \\(T(n)\\).
In essence, time complexity analysis is about finding the asymptotic upper bound of the \"number of operations \\(T(n)\\)\". It has a precise mathematical definition.
Asymptotic Upper Bound
If there exist positive real numbers \\(c\\) and \\(n_0\\) such that for all \\(n > n_0\\), \\(T(n) \\leq c \\cdot f(n)\\), then \\(f(n)\\) is considered an asymptotic upper bound of \\(T(n)\\), denoted as \\(T(n) = O(f(n))\\).
As illustrated below, calculating the asymptotic upper bound involves finding a function \\(f(n)\\) such that, as \\(n\\) approaches infinity, \\(T(n)\\) and \\(f(n)\\) have the same growth order, differing only by a constant factor \\(c\\).
Figure 2-8 \u00a0 Asymptotic upper bound of a function
"},{"location":"chapter_computational_complexity/time_complexity/#233-calculation-method","title":"2.3.3 \u00a0 Calculation method","text":"While the concept of asymptotic upper bound might seem mathematically dense, you don't need to fully grasp it right away. Let's first understand the method of calculation, which can be practiced and comprehended over time.
Once \\(f(n)\\) is determined, we obtain the time complexity \\(O(f(n))\\). But how do we determine the asymptotic upper bound \\(f(n)\\)? This process generally involves two steps: counting the number of operations and determining the asymptotic upper bound.
"},{"location":"chapter_computational_complexity/time_complexity/#1-step-1-counting-the-number-of-operations","title":"1. \u00a0 Step 1: counting the number of operations","text":"This step involves going through the code line by line. However, due to the presence of the constant \\(c\\) in \\(c \\cdot f(n)\\), all coefficients and constant terms in \\(T(n)\\) can be ignored. This principle allows for simplification techniques in counting operations.
Given a function, we can use these techniques to count operations:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZigdef algorithm(n: int):\n a = 1 # +0 (trick 1)\n a = a + n # +0 (trick 1)\n # +n (technique 2)\n for i in range(5 * n + 1):\n print(0)\n # +n*n (technique 3)\n for i in range(2 * n):\n for j in range(n + 1):\n print(0)\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n cout << 0 << endl;\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n cout << 0 << endl;\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n System.out.println(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n System.out.println(0);\n }\n }\n}\n
void Algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n Console.WriteLine(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n Console.WriteLine(0);\n }\n }\n}\n
func algorithm(n int) {\n a := 1 // +0 (trick 1)\n a = a + n // +0 (trick 1)\n // +n (technique 2)\n for i := 0; i < 5 * n + 1; i++ {\n fmt.Println(0)\n }\n // +n*n (technique 3)\n for i := 0; i < 2 * n; i++ {\n for j := 0; j < n + 1; j++ {\n fmt.Println(0)\n }\n }\n}\n
func algorithm(n: Int) {\n var a = 1 // +0 (trick 1)\n a = a + n // +0 (trick 1)\n // +n (technique 2)\n for _ in 0 ..< (5 * n + 1) {\n print(0)\n }\n // +n*n (technique 3)\n for _ in 0 ..< (2 * n) {\n for _ in 0 ..< (n + 1) {\n print(0)\n }\n }\n}\n
function algorithm(n) {\n let a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n (technique 3)\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
function algorithm(n: number): void {\n let a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (let i = 0; i < 5 * n + 1; i++) {\n console.log(0);\n }\n // +n*n (technique 3)\n for (let i = 0; i < 2 * n; i++) {\n for (let j = 0; j < n + 1; j++) {\n console.log(0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n print(0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n print(0);\n }\n }\n}\n
fn algorithm(n: i32) {\n let mut a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n\n // +n (technique 2)\n for i in 0..(5 * n + 1) {\n println!(\"{}\", 0);\n }\n\n // +n*n (technique 3)\n for i in 0..(2 * n) {\n for j in 0..(n + 1) {\n println!(\"{}\", 0);\n }\n }\n}\n
void algorithm(int n) {\n int a = 1; // +0 (trick 1)\n a = a + n; // +0 (trick 1)\n // +n (technique 2)\n for (int i = 0; i < 5 * n + 1; i++) {\n printf(\"%d\", 0);\n }\n // +n*n (technique 3)\n for (int i = 0; i < 2 * n; i++) {\n for (int j = 0; j < n + 1; j++) {\n printf(\"%d\", 0);\n }\n }\n}\n
\n
fn algorithm(n: usize) void {\n var a: i32 = 1; // +0 (trick 1)\n a = a + @as(i32, @intCast(n)); // +0 (trick 1)\n\n // +n (technique 2)\n for(0..(5 * n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n\n // +n*n (technique 3)\n for(0..(2 * n)) |_| {\n for(0..(n + 1)) |_| {\n std.debug.print(\"{}\\n\", .{0});\n }\n }\n}\n
The formula below shows the counting results before and after simplification, both leading to a time complexity of \\(O(n^2)\\):
\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{Complete Count (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{Simplified Count (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-step-2-determining-the-asymptotic-upper-bound","title":"2. \u00a0 Step 2: determining the asymptotic upper bound","text":"The time complexity is determined by the highest order term in \\(T(n)\\). This is because, as \\(n\\) approaches infinity, the highest order term dominates, rendering the influence of other terms negligible.
The following table illustrates examples of different operation counts and their corresponding time complexities. Some exaggerated values are used to emphasize that coefficients cannot alter the order of growth. When \\(n\\) becomes very large, these constants become insignificant.
Table: Time complexity for different operation counts
Operation Count \\(T(n)\\) Time Complexity \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234-common-types-of-time-complexity","title":"2.3.4 \u00a0 Common types of time complexity","text":"Let's consider the input data size as \\(n\\). The common types of time complexities are illustrated below, arranged from lowest to highest:
\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{Constant Order} < \\text{Logarithmic Order} < \\text{Linear Order} < \\text{Linear-Logarithmic Order} < \\text{Quadratic Order} < \\text{Exponential Order} < \\text{Factorial Order} \\end{aligned} \\]Figure 2-9 \u00a0 Common types of time complexity
"},{"location":"chapter_computational_complexity/time_complexity/#1-constant-order-o1","title":"1. \u00a0 Constant order \\(O(1)\\)","text":"Constant order means the number of operations is independent of the input data size \\(n\\). In the following function, although the number of operations size
might be large, the time complexity remains \\(O(1)\\) as it's unrelated to \\(n\\):
def constant(n: int) -> int:\n \"\"\"\u5e38\u6570\u9636\"\"\"\n count = 0\n size = 100000\n for _ in range(size):\n count += 1\n return count\n
time_complexity.cpp/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u5e38\u6570\u9636 */\nint Constant(int n) {\n int count = 0;\n int size = 100000;\n for (int i = 0; i < size; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u5e38\u6570\u9636 */\nfunc constant(n int) int {\n count := 0\n size := 100000\n for i := 0; i < size; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5e38\u6570\u9636 */\nfunc constant(n: Int) -> Int {\n var count = 0\n let size = 100_000\n for _ in 0 ..< size {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5e38\u6570\u9636 */\nfunction constant(n) {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u5e38\u6570\u9636 */\nfunction constant(n: number): number {\n let count = 0;\n const size = 100000;\n for (let i = 0; i < size; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n for (var i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5e38\u6570\u9636 */\nfn constant(n: i32) -> i32 {\n _ = n;\n let mut count = 0;\n let size = 100_000;\n for _ in 0..size {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5e38\u6570\u9636 */\nint constant(int n) {\n int count = 0;\n int size = 100000;\n int i = 0;\n for (int i = 0; i < size; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u5e38\u6570\u9636 */\nfun constant(n: Int): Int {\n var count = 0\n val size = 10_0000\n for (i in 0..<size)\n count++\n return count\n}\n
time_complexity.rb### \u5e38\u6570\u9636 ###\ndef constant(n)\n count = 0\n size = 100000\n\n (0...size).each { count += 1 }\n\n count\nend\n
time_complexity.zig// \u5e38\u6570\u9636\nfn constant(n: i32) i32 {\n _ = n;\n var count: i32 = 0;\n const size: i32 = 100_000;\n var i: i32 = 0;\n while(i<size) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/time_complexity/#2-linear-order-on","title":"2. \u00a0 Linear order \\(O(n)\\)","text":"Linear order indicates the number of operations grows linearly with the input data size \\(n\\). Linear order commonly appears in single-loop structures:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef linear(n: int) -> int:\n \"\"\"\u7ebf\u6027\u9636\"\"\"\n count = 0\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636 */\nint Linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++)\n count++;\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636 */\nfunc linear(n int) int {\n count := 0\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636 */\nfunc linear(n: Int) -> Int {\n var count = 0\n for _ in 0 ..< n {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636 */\nfunction linear(n) {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636 */\nfunction linear(n: number): number {\n let count = 0;\n for (let i = 0; i < n; i++) count++;\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636 */\nfn linear(n: i32) -> i32 {\n let mut count = 0;\n for _ in 0..n {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636 */\nint linear(int n) {\n int count = 0;\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u9636 */\nfun linear(n: Int): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (i in 0..<n)\n count++\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u9636 ###\ndef linear(n)\n count = 0\n (0...n).each { count += 1 }\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u9636\nfn linear(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
Operations like array traversal and linked list traversal have a time complexity of \\(O(n)\\), where \\(n\\) is the length of the array or list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef array_traversal(nums: list[int]) -> int:\n \"\"\"\u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums:\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(vector<int> &nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int num : nums) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint ArrayTraversal(int[] nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n foreach (int num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums []int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for range nums {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunc arrayTraversal(nums: [Int]) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfunction arrayTraversal(nums: number[]): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (let i = 0; i < nums.length; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(List<int> nums) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (var _num in nums) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfn array_traversal(nums: &[i32]) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for _ in nums {\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nint arrayTraversal(int *nums, int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09 */\nfun arrayTraversal(nums: IntArray): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (num in nums) {\n count++\n }\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09###\ndef array_traversal(nums)\n count = 0\n\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for num in nums\n count += 1\n end\n\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u9636\uff08\u904d\u5386\u6570\u7ec4\uff09\nfn arrayTraversal(nums: []i32) i32 {\n var count: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u7ec4\u957f\u5ea6\u6210\u6b63\u6bd4\n for (nums) |_| {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
It's important to note that the input data size \\(n\\) should be determined based on the type of input data. For example, in the first example, \\(n\\) represents the input data size, while in the second example, the length of the array \\(n\\) is the data size.
"},{"location":"chapter_computational_complexity/time_complexity/#3-quadratic-order-on2","title":"3. \u00a0 Quadratic order \\(O(n^2)\\)","text":"Quadratic order means the number of operations grows quadratically with the input data size \\(n\\). Quadratic order typically appears in nested loops, where both the outer and inner loops have a time complexity of \\(O(n)\\), resulting in an overall complexity of \\(O(n^2)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef quadratic(n: int) -> int:\n \"\"\"\u5e73\u65b9\u9636\"\"\"\n count = 0\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i in range(n):\n for j in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636 */\nint Quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636 */\nfunc quadratic(n int) int {\n count := 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i := 0; i < n; i++ {\n for j := 0; j < n; j++ {\n count++\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636 */\nfunc quadratic(n: Int) -> Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0 ..< n {\n for _ in 0 ..< n {\n count += 1\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636 */\nfunction quadratic(n) {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636 */\nfunction quadratic(n: number): number {\n let count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636 */\nfn quadratic(n: i32) -> i32 {\n let mut count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for _ in 0..n {\n for _ in 0..n {\n count += 1;\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636 */\nint quadratic(int n) {\n int count = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n count++;\n }\n }\n return count;\n}\n
time_complexity.kt/* \u5e73\u65b9\u9636 */\nfun quadratic(n: Int): Int {\n var count = 0\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for (i in 0..<n) {\n for (j in 0..<n) {\n count++\n }\n }\n return count\n}\n
time_complexity.rb### \u5e73\u65b9\u9636 ###\ndef quadratic(n)\n count = 0\n\n # \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n for i in 0...n\n for j in 0...n\n count += 1\n end\n end\n\n count\nend\n
time_complexity.zig// \u5e73\u65b9\u9636\nfn quadratic(n: i32) i32 {\n var count: i32 = 0;\n var i: i32 = 0;\n // \u5faa\u73af\u6b21\u6570\u4e0e\u6570\u636e\u5927\u5c0f n \u6210\u5e73\u65b9\u5173\u7cfb\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < n) : (j += 1) {\n count += 1;\n }\n }\n return count;\n}\n
Code Visualization Full Screen >
The following image compares constant order, linear order, and quadratic order time complexities.
Figure 2-10 \u00a0 Constant, linear, and quadratic order time complexities
For instance, in bubble sort, the outer loop runs \\(n - 1\\) times, and the inner loop runs \\(n-1\\), \\(n-2\\), ..., \\(2\\), \\(1\\) times, averaging \\(n / 2\\) times, resulting in a time complexity of \\(O((n - 1) n / 2) = O(n^2)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef bubble_sort(nums: list[int]) -> int:\n \"\"\"\u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\"\"\"\n count = 0 # \u8ba1\u6570\u5668\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in range(len(nums) - 1, 0, -1):\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in range(i):\n if nums[j] > nums[j + 1]:\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp: int = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n return count\n
time_complexity.cpp/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(vector<int> &nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.size() - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.java/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.cs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint BubbleSort(int[] nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = nums.Length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.go/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums []int) int {\n count := 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i := len(nums) - 1; i > 0; i-- {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j := 0; j < i; j++ {\n if nums[j] > nums[j+1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp := nums[j]\n nums[j] = nums[j+1]\n nums[j+1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.swift/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n var count = 0 // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in nums.indices.dropFirst().reversed() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0 ..< i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.js/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums) {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.ts/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfunction bubbleSort(nums: number[]): number {\n let count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (let i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (let j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.dart/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(List<int> nums) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (var i = nums.length - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (var j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.rs/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n let mut count = 0; // \u8ba1\u6570\u5668\n\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (1..nums.len()).rev() {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0..i {\n if nums[j] > nums[j + 1] {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n let tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n count\n}\n
time_complexity.c/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nint bubbleSort(int *nums, int n) {\n int count = 0; // \u8ba1\u6570\u5668\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (int i = n - 1; i > 0; i--) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (int j = 0; j < i; j++) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n int tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
time_complexity.kt/* \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09 */\nfun bubbleSort(nums: IntArray): Int {\n var count = 0\n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for (i in nums.size - 1 downTo 1) {\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for (j in 0..<i) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n nums[j] = nums[j + 1].also { nums[j + 1] = nums[j] }\n count += 3 // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count\n}\n
time_complexity.rb### \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09###\ndef bubble_sort(nums)\n count = 0 # \u8ba1\u6570\u5668\n\n # \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n for i in (nums.length - 1).downto(0)\n # \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n for j in 0...i\n if nums[j] > nums[j + 1]\n # \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n tmp = nums[j]\n nums[j] = nums[j + 1]\n nums[j + 1] = tmp\n count += 3 # \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n end\n end\n end\n\n count\nend\n
time_complexity.zig// \u5e73\u65b9\u9636\uff08\u5192\u6ce1\u6392\u5e8f\uff09\nfn bubbleSort(nums: []i32) i32 {\n var count: i32 = 0; // \u8ba1\u6570\u5668 \n // \u5916\u5faa\u73af\uff1a\u672a\u6392\u5e8f\u533a\u95f4\u4e3a [0, i]\n var i: i32 = @as(i32, @intCast(nums.len)) - 1;\n while (i > 0) : (i -= 1) {\n var j: usize = 0;\n // \u5185\u5faa\u73af\uff1a\u5c06\u672a\u6392\u5e8f\u533a\u95f4 [0, i] \u4e2d\u7684\u6700\u5927\u5143\u7d20\u4ea4\u6362\u81f3\u8be5\u533a\u95f4\u7684\u6700\u53f3\u7aef\n while (j < i) : (j += 1) {\n if (nums[j] > nums[j + 1]) {\n // \u4ea4\u6362 nums[j] \u4e0e nums[j + 1]\n var tmp = nums[j];\n nums[j] = nums[j + 1];\n nums[j + 1] = tmp;\n count += 3; // \u5143\u7d20\u4ea4\u6362\u5305\u542b 3 \u4e2a\u5355\u5143\u64cd\u4f5c\n }\n }\n }\n return count;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_computational_complexity/time_complexity/#4-exponential-order-o2n","title":"4. \u00a0 Exponential order \\(O(2^n)\\)","text":"Biological \"cell division\" is a classic example of exponential order growth: starting with one cell, it becomes two after one division, four after two divisions, and so on, resulting in \\(2^n\\) cells after \\(n\\) divisions.
The following image and code simulate the cell division process, with a time complexity of \\(O(2^n)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef exponential(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n base = 1\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in range(n):\n for _ in range(base):\n count += 1\n base *= 2\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Exponential(int n) {\n int count = 0, bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc exponential(n int) int {\n count, base := 0, 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for i := 0; i < n; i++ {\n for j := 0; j < base; j++ {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc exponential(n: Int) -> Int {\n var count = 0\n var base = 1\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0 ..< n {\n for _ in 0 ..< base {\n count += 1\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n) {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction exponential(n: number): number {\n let count = 0,\n base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (let i = 0; i < n; i++) {\n for (let j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0, base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (var i = 0; i < n; i++) {\n for (var j = 0; j < base; j++) {\n count++;\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn exponential(n: i32) -> i32 {\n let mut count = 0;\n let mut base = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for _ in 0..n {\n for _ in 0..base {\n count += 1\n }\n base *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint exponential(int n) {\n int count = 0;\n int bas = 1;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < bas; j++) {\n count++;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
time_complexity.kt/* \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun exponential(n: Int): Int {\n var count = 0\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n var base = 1\n for (i in 0..<n) {\n for (j in 0..<base) {\n count++\n }\n base *= 2\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count\n}\n
time_complexity.rb### \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef exponential(n)\n count, base = 0, 1\n\n # \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n (0...n).each do\n (0...base).each { count += 1 }\n base *= 2\n end\n\n # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n count\nend\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn exponential(n: i32) i32 {\n var count: i32 = 0;\n var bas: i32 = 1;\n var i: i32 = 0;\n // \u7ec6\u80de\u6bcf\u8f6e\u4e00\u5206\u4e3a\u4e8c\uff0c\u5f62\u6210\u6570\u5217 1, 2, 4, 8, ..., 2^(n-1)\n while (i < n) : (i += 1) {\n var j: i32 = 0;\n while (j < bas) : (j += 1) {\n count += 1;\n }\n bas *= 2;\n }\n // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-11 \u00a0 Exponential order time complexity
In practice, exponential order often appears in recursive functions. For example, in the code below, it recursively splits into two halves, stopping after \\(n\\) divisions:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef exp_recur(n: int) -> int:\n \"\"\"\u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 1:\n return 1\n return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
time_complexity.cpp/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.java/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.cs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint ExpRecur(int n) {\n if (n == 1) return 1;\n return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
time_complexity.go/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc expRecur(n int) int {\n if n == 1 {\n return 1\n }\n return expRecur(n-1) + expRecur(n-1) + 1\n}\n
time_complexity.swift/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc expRecur(n: Int) -> Int {\n if n == 1 {\n return 1\n }\n return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
time_complexity.js/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n) {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.ts/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction expRecur(n: number): number {\n if (n === 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.dart/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.rs/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn exp_recur(n: i32) -> i32 {\n if n == 1 {\n return 1;\n }\n exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint expRecur(int n) {\n if (n == 1)\n return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.kt/* \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun expRecur(n: Int): Int {\n if (n == 1) {\n return 1\n }\n return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
time_complexity.rb### \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef exp_recur(n)\n return 1 if n == 1\n exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n
time_complexity.zig// \u6307\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn expRecur(n: i32) i32 {\n if (n == 1) return 1;\n return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
Code Visualization Full Screen >
Exponential order growth is extremely rapid and is commonly seen in exhaustive search methods (brute force, backtracking, etc.). For large-scale problems, exponential order is unacceptable, often requiring dynamic programming or greedy algorithms as solutions.
"},{"location":"chapter_computational_complexity/time_complexity/#5-logarithmic-order-olog-n","title":"5. \u00a0 Logarithmic order \\(O(\\log n)\\)","text":"In contrast to exponential order, logarithmic order reflects situations where \"the size is halved each round.\" Given an input data size \\(n\\), since the size is halved each round, the number of iterations is \\(\\log_2 n\\), the inverse function of \\(2^n\\).
The following image and code simulate the \"halving each round\" process, with a time complexity of \\(O(\\log_2 n)\\), commonly abbreviated as \\(O(\\log n)\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef logarithmic(n: int) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\"\"\"\n count = 0\n while n > 1:\n n = n / 2\n count += 1\n return count\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint Logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n /= 2;\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09*/\nfunc logarithmic(n int) int {\n count := 0\n for n > 1 {\n n = n / 2\n count++\n }\n return count\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunc logarithmic(n: Int) -> Int {\n var count = 0\n var n = n\n while n > 1 {\n n = n / 2\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n) {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfunction logarithmic(n: number): number {\n let count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n ~/ 2;\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfn logarithmic(mut n: i32) -> i32 {\n let mut count = 0;\n while n > 1 {\n n = n / 2;\n count += 1;\n }\n count\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nint logarithmic(int n) {\n int count = 0;\n while (n > 1) {\n n = n / 2;\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09 */\nfun logarithmic(n: Int): Int {\n var n1 = n\n var count = 0\n while (n1 > 1) {\n n1 /= 2\n count++\n }\n return count\n}\n
time_complexity.rb### \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09###\ndef logarithmic(n)\n count = 0\n\n while n > 1\n n /= 2\n count += 1\n end\n\n count\nend\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u5faa\u73af\u5b9e\u73b0\uff09\nfn logarithmic(n: i32) i32 {\n var count: i32 = 0;\n var n_var = n;\n while (n_var > 1)\n {\n n_var = n_var / 2;\n count +=1;\n }\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-12 \u00a0 Logarithmic order time complexity
Like exponential order, logarithmic order also frequently appears in recursive functions. The code below forms a recursive tree of height \\(\\log_2 n\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef log_recur(n: int) -> int:\n \"\"\"\u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n <= 1:\n return 0\n return log_recur(n / 2) + 1\n
time_complexity.cpp/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.java/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.cs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint LogRecur(int n) {\n if (n <= 1) return 0;\n return LogRecur(n / 2) + 1;\n}\n
time_complexity.go/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09*/\nfunc logRecur(n int) int {\n if n <= 1 {\n return 0\n }\n return logRecur(n/2) + 1\n}\n
time_complexity.swift/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc logRecur(n: Int) -> Int {\n if n <= 1 {\n return 0\n }\n return logRecur(n: n / 2) + 1\n}\n
time_complexity.js/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n) {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.ts/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction logRecur(n: number): number {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.dart/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1) return 0;\n return logRecur(n ~/ 2) + 1;\n}\n
time_complexity.rs/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn log_recur(n: i32) -> i32 {\n if n <= 1 {\n return 0;\n }\n log_recur(n / 2) + 1\n}\n
time_complexity.c/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint logRecur(int n) {\n if (n <= 1)\n return 0;\n return logRecur(n / 2) + 1;\n}\n
time_complexity.kt/* \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun logRecur(n: Int): Int {\n if (n <= 1)\n return 0\n return logRecur(n / 2) + 1\n}\n
time_complexity.rb### \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef log_recur(n)\n return 0 unless n > 1\n log_recur(n / 2) + 1\nend\n
time_complexity.zig// \u5bf9\u6570\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn logRecur(n: i32) i32 {\n if (n <= 1) return 0;\n return logRecur(n / 2) + 1;\n}\n
Code Visualization Full Screen >
Logarithmic order is typical in algorithms based on the divide-and-conquer strategy, embodying the \"split into many\" and \"simplify complex problems\" approach. It's slow-growing and is the most ideal time complexity after constant order.
What is the base of \\(O(\\log n)\\)?
Technically, \"splitting into \\(m\\)\" corresponds to a time complexity of \\(O(\\log_m n)\\). Using the logarithm base change formula, we can equate different logarithmic complexities:
\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]This means the base \\(m\\) can be changed without affecting the complexity. Therefore, we often omit the base \\(m\\) and simply denote logarithmic order as \\(O(\\log n)\\).
"},{"location":"chapter_computational_complexity/time_complexity/#6-linear-logarithmic-order-on-log-n","title":"6. \u00a0 Linear-logarithmic order \\(O(n \\log n)\\)","text":"Linear-logarithmic order often appears in nested loops, with the complexities of the two loops being \\(O(\\log n)\\) and \\(O(n)\\) respectively. The related code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef linear_log_recur(n: int) -> int:\n \"\"\"\u7ebf\u6027\u5bf9\u6570\u9636\"\"\"\n if n <= 1:\n return 1\n count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n for _ in range(n):\n count += 1\n return count\n
time_complexity.cpp/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.java/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.cs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint LinearLogRecur(int n) {\n if (n <= 1) return 1;\n int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.go/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n int) int {\n if n <= 1 {\n return 1\n }\n count := linearLogRecur(n/2) + linearLogRecur(n/2)\n for i := 0; i < n; i++ {\n count++\n }\n return count\n}\n
time_complexity.swift/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunc linearLogRecur(n: Int) -> Int {\n if n <= 1 {\n return 1\n }\n var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n for _ in stride(from: 0, to: n, by: 1) {\n count += 1\n }\n return count\n}\n
time_complexity.js/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n) {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.ts/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfunction linearLogRecur(n: number): number {\n if (n <= 1) return 1;\n let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (let i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.dart/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1) return 1;\n int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n for (var i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.rs/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfn linear_log_recur(n: i32) -> i32 {\n if n <= 1 {\n return 1;\n }\n let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n for _ in 0..n as i32 {\n count += 1;\n }\n return count;\n}\n
time_complexity.c/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nint linearLogRecur(int n) {\n if (n <= 1)\n return 1;\n int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n for (int i = 0; i < n; i++) {\n count++;\n }\n return count;\n}\n
time_complexity.kt/* \u7ebf\u6027\u5bf9\u6570\u9636 */\nfun linearLogRecur(n: Int): Int {\n if (n <= 1)\n return 1\n var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n for (i in 0..<n.toInt()) {\n count++\n }\n return count\n}\n
time_complexity.rb### \u7ebf\u6027\u5bf9\u6570\u9636 ###\ndef linear_log_recur(n)\n return 1 unless n > 1\n\n count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n (0...n).each { count += 1 }\n\n count\nend\n
time_complexity.zig// \u7ebf\u6027\u5bf9\u6570\u9636\nfn linearLogRecur(n: i32) i32 {\n if (n <= 1) return 1;\n var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n var i: i32 = 0;\n while (i < n) : (i += 1) {\n count += 1;\n }\n return count;\n}\n
Code Visualization Full Screen >
The image below demonstrates how linear-logarithmic order is generated. Each level of a binary tree has \\(n\\) operations, and the tree has \\(\\log_2 n + 1\\) levels, resulting in a time complexity of \\(O(n \\log n)\\).
Figure 2-13 \u00a0 Linear-logarithmic order time complexity
Mainstream sorting algorithms typically have a time complexity of \\(O(n \\log n)\\), such as quicksort, mergesort, and heapsort.
"},{"location":"chapter_computational_complexity/time_complexity/#7-factorial-order-on","title":"7. \u00a0 Factorial order \\(O(n!)\\)","text":"Factorial order corresponds to the mathematical problem of \"full permutation.\" Given \\(n\\) distinct elements, the total number of possible permutations is:
\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]Factorials are typically implemented using recursion. As shown in the image and code below, the first level splits into \\(n\\) branches, the second level into \\(n - 1\\) branches, and so on, stopping after the \\(n\\)th level:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig time_complexity.pydef factorial_recur(n: int) -> int:\n \"\"\"\u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\"\"\"\n if n == 0:\n return 1\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in range(n):\n count += factorial_recur(n - 1)\n return count\n
time_complexity.cpp/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.java/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.cs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint FactorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (int i = 0; i < n; i++) {\n count += FactorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.go/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n int) int {\n if n == 0 {\n return 1\n }\n count := 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for i := 0; i < n; i++ {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.swift/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunc factorialRecur(n: Int) -> Int {\n if n == 0 {\n return 1\n }\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0 ..< n {\n count += factorialRecur(n: n - 1)\n }\n return count\n}\n
time_complexity.js/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n) {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.ts/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfunction factorialRecur(n: number): number {\n if (n === 0) return 1;\n let count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (let i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.dart/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0) return 1;\n int count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (var i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.rs/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfn factorial_recur(n: i32) -> i32 {\n if n == 0 {\n return 1;\n }\n let mut count = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for _ in 0..n {\n count += factorial_recur(n - 1);\n }\n count\n}\n
time_complexity.c/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nint factorialRecur(int n) {\n if (n == 0)\n return 1;\n int count = 0;\n for (int i = 0; i < n; i++) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
time_complexity.kt/* \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09 */\nfun factorialRecur(n: Int): Int {\n if (n == 0)\n return 1\n var count = 0\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n for (i in 0..<n) {\n count += factorialRecur(n - 1)\n }\n return count\n}\n
time_complexity.rb### \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09###\ndef factorial_recur(n)\n return 1 if n == 0\n\n count = 0\n # \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n (0...n).each { count += factorial_recur(n - 1) }\n\n count\nend\n
time_complexity.zig// \u9636\u4e58\u9636\uff08\u9012\u5f52\u5b9e\u73b0\uff09\nfn factorialRecur(n: i32) i32 {\n if (n == 0) return 1;\n var count: i32 = 0;\n var i: i32 = 0;\n // \u4ece 1 \u4e2a\u5206\u88c2\u51fa n \u4e2a\n while (i < n) : (i += 1) {\n count += factorialRecur(n - 1);\n }\n return count;\n}\n
Code Visualization Full Screen >
Figure 2-14 \u00a0 Factorial order time complexity
Note that factorial order grows even faster than exponential order; it's unacceptable for larger \\(n\\) values.
"},{"location":"chapter_computational_complexity/time_complexity/#235-worst-best-and-average-time-complexities","title":"2.3.5 \u00a0 Worst, best, and average time complexities","text":"The time efficiency of an algorithm is often not fixed but depends on the distribution of the input data. Assume we have an array nums
of length \\(n\\), consisting of numbers from \\(1\\) to \\(n\\), each appearing only once, but in a randomly shuffled order. The task is to return the index of the element \\(1\\). We can draw the following conclusions:
nums = [?, ?, ..., 1]
, that is, when the last element is \\(1\\), it requires a complete traversal of the array, achieving the worst-case time complexity of \\(O(n)\\).nums = [1, ?, ?, ...]
, that is, when the first element is \\(1\\), no matter the length of the array, no further traversal is needed, achieving the best-case time complexity of \\(\\Omega(1)\\).The \"worst-case time complexity\" corresponds to the asymptotic upper bound, denoted by the big \\(O\\) notation. Correspondingly, the \"best-case time complexity\" corresponds to the asymptotic lower bound, denoted by \\(\\Omega\\):
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig worst_best_time_complexity.pydef random_numbers(n: int) -> list[int]:\n \"\"\"\u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71\"\"\"\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = [i for i in range(1, n + 1)]\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n random.shuffle(nums)\n return nums\n\ndef find_one(nums: list[int]) -> int:\n \"\"\"\u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\"\"\"\n for i in range(len(nums)):\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1:\n return i\n return -1\n
worst_best_time_complexity.cpp/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nvector<int> randomNumbers(int n) {\n vector<int> nums(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u4f7f\u7528\u7cfb\u7edf\u65f6\u95f4\u751f\u6210\u968f\u673a\u79cd\u5b50\n unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(vector<int> &nums) {\n for (int i = 0; i < nums.size(); i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.java/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] randomNumbers(int n) {\n Integer[] nums = new Integer[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n Collections.shuffle(Arrays.asList(nums));\n // Integer[] -> int[]\n int[] res = new int[n];\n for (int i = 0; i < n; i++) {\n res[i] = nums[i];\n }\n return res;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int[] nums) {\n for (int i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.cs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint[] RandomNumbers(int n) {\n int[] nums = new int[n];\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = 0; i < nums.Length; i++) {\n int index = new Random().Next(i, nums.Length);\n (nums[i], nums[index]) = (nums[index], nums[i]);\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint FindOne(int[] nums) {\n for (int i = 0; i < nums.Length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.go/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n int) []int {\n nums := make([]int, n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for i := 0; i < n; i++ {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n rand.Shuffle(len(nums), func(i, j int) {\n nums[i], nums[j] = nums[j], nums[i]\n })\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums []int) int {\n for i := 0; i < len(nums); i++ {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.swift/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunc randomNumbers(n: Int) -> [Int] {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n var nums = Array(1 ... n)\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle()\n return nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunc findOne(nums: [Int]) -> Int {\n for i in nums.indices {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return i\n }\n }\n return -1\n}\n
worst_best_time_complexity.js/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n) {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums) {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.ts/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfunction randomNumbers(n: number): number[] {\n const nums = Array(n);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (let i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (let i = 0; i < n; i++) {\n const r = Math.floor(Math.random() * (i + 1));\n const temp = nums[i];\n nums[i] = nums[r];\n nums[r] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfunction findOne(nums: number[]): number {\n for (let i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] === 1) {\n return i;\n }\n }\n return -1;\n}\n
worst_best_time_complexity.dart/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nList<int> randomNumbers(int n) {\n final nums = List.filled(n, 0);\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (var i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle();\n\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(List<int> nums) {\n for (var i = 0; i < nums.length; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1) return i;\n }\n\n return -1;\n}\n
worst_best_time_complexity.rs/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfn random_numbers(n: i32) -> Vec<i32> {\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n let mut nums = (1..=n).collect::<Vec<i32>>();\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle(&mut thread_rng());\n nums\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfn find_one(nums: &[i32]) -> Option<usize> {\n for i in 0..nums.len() {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if nums[i] == 1 {\n return Some(i);\n }\n }\n None\n}\n
worst_best_time_complexity.c/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nint *randomNumbers(int n) {\n // \u5206\u914d\u5806\u533a\u5185\u5b58\uff08\u521b\u5efa\u4e00\u7ef4\u53ef\u53d8\u957f\u6570\u7ec4\uff1a\u6570\u7ec4\u4e2d\u5143\u7d20\u6570\u91cf\u4e3a n \uff0c\u5143\u7d20\u7c7b\u578b\u4e3a int \uff09\n int *nums = (int *)malloc(n * sizeof(int));\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (int i = 0; i < n; i++) {\n nums[i] = i + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n for (int i = n - 1; i > 0; i--) {\n int j = rand() % (i + 1);\n int temp = nums[i];\n nums[i] = nums[j];\n nums[j] = temp;\n }\n return nums;\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nint findOne(int *nums, int n) {\n for (int i = 0; i < n; i++) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i;\n }\n return -1;\n}\n
worst_best_time_complexity.kt/* \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71 */\nfun randomNumbers(n: Int): Array<Int?> {\n val nums = IntArray(n)\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (i in 0..<n) {\n nums[i] = i + 1\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n val mutableList = nums.toMutableList()\n mutableList.shuffle()\n // Integer[] -> int[]\n val res = arrayOfNulls<Int>(n)\n for (i in 0..<n) {\n res[i] = mutableList[i]\n }\n return res\n}\n\n/* \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 */\nfun findOne(nums: Array<Int?>): Int {\n for (i in nums.indices) {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (nums[i] == 1)\n return i\n }\n return -1\n}\n
worst_best_time_complexity.rb### \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a: 1, 2, ..., n \uff0c\u987a\u5e8f\u88ab\u6253\u4e71 ###\ndef random_numbers(n)\n # \u751f\u6210\u6570\u7ec4 nums =: 1, 2, 3, ..., n\n nums = Array.new(n) { |i| i + 1 }\n # \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n nums.shuffle!\nend\n\n### \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15 ###\ndef find_one(nums)\n for i in 0...nums.length\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n # \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n return i if nums[i] == 1\n end\n\n -1\nend\n
worst_best_time_complexity.zig// \u751f\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5143\u7d20\u4e3a { 1, 2, ..., n }\uff0c\u987a\u5e8f\u88ab\u6253\u4e71\nfn randomNumbers(comptime n: usize) [n]i32 {\n var nums: [n]i32 = undefined;\n // \u751f\u6210\u6570\u7ec4 nums = { 1, 2, 3, ..., n }\n for (&nums, 0..) |*num, i| {\n num.* = @as(i32, @intCast(i)) + 1;\n }\n // \u968f\u673a\u6253\u4e71\u6570\u7ec4\u5143\u7d20\n const rand = std.crypto.random;\n rand.shuffle(i32, &nums);\n return nums;\n}\n\n// \u67e5\u627e\u6570\u7ec4 nums \u4e2d\u6570\u5b57 1 \u6240\u5728\u7d22\u5f15\nfn findOne(nums: []i32) i32 {\n for (nums, 0..) |num, i| {\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5934\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u4f73\u65f6\u95f4\u590d\u6742\u5ea6 O(1)\n // \u5f53\u5143\u7d20 1 \u5728\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u8fbe\u5230\u6700\u5dee\u65f6\u95f4\u590d\u6742\u5ea6 O(n)\n if (num == 1) return @intCast(i);\n }\n return -1;\n}\n
Code Visualization Full Screen >
It's important to note that the best-case time complexity is rarely used in practice, as it is usually only achievable under very low probabilities and might be misleading. The worst-case time complexity is more practical as it provides a safety value for efficiency, allowing us to confidently use the algorithm.
From the above example, it's clear that both the worst-case and best-case time complexities only occur under \"special data distributions,\" which may have a small probability of occurrence and may not accurately reflect the algorithm's run efficiency. In contrast, the average time complexity can reflect the algorithm's efficiency under random input data, denoted by the \\(\\Theta\\) notation.
For some algorithms, we can simply estimate the average case under a random data distribution. For example, in the aforementioned example, since the input array is shuffled, the probability of element \\(1\\) appearing at any index is equal. Therefore, the average number of loops for the algorithm is half the length of the array \\(n / 2\\), giving an average time complexity of \\(\\Theta(n / 2) = \\Theta(n)\\).
However, calculating the average time complexity for more complex algorithms can be quite difficult, as it's challenging to analyze the overall mathematical expectation under the data distribution. In such cases, we usually use the worst-case time complexity as the standard for judging the efficiency of the algorithm.
Why is the \\(\\Theta\\) symbol rarely seen?
Possibly because the \\(O\\) notation is more commonly spoken, it is often used to represent the average time complexity. However, strictly speaking, this practice is not accurate. In this book and other materials, if you encounter statements like \"average time complexity \\(O(n)\\)\", please understand it directly as \\(\\Theta(n)\\).
"},{"location":"chapter_data_structure/","title":"Chapter 3. \u00a0 Data structures","text":"Abstract
Data structures serve as a robust and diverse framework.
They offer a blueprint for the orderly organization of data, upon which algorithms come to life.
"},{"location":"chapter_data_structure/#chapter-contents","title":"Chapter contents","text":"When discussing data in computers, various forms like text, images, videos, voice and 3D models comes to mind. Despite their different organizational forms, they are all composed of various basic data types.
Basic data types are those that the CPU can directly operate on and are directly used in algorithms, mainly including the following.
byte
, short
, int
, long
.float
, double
, used to represent decimals.char
, used to represent letters, punctuation, and even emojis in various languages.bool
, used to represent \"yes\" or \"no\" decisions.Basic data types are stored in computers in binary form. One binary digit is 1 bit. In most modern operating systems, 1 byte consists of 8 bits.
The range of values for basic data types depends on the size of the space they occupy. Below, we take Java as an example.
byte
occupies 1 byte = 8 bits and can represent \\(2^8\\) numbers.int
occupies 4 bytes = 32 bits and can represent \\(2^{32}\\) numbers.The following table lists the space occupied, value range, and default values of various basic data types in Java. While memorizing this table isn't necessary, having a general understanding of it and referencing it when required is recommended.
Table 3-1 \u00a0 Space occupied and value range of basic data types
Type Symbol Space Occupied Minimum Value Maximum Value Default Value Integerbyte
1 byte \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) 0 short
2 bytes \\(-2^{15}\\) \\(2^{15} - 1\\) 0 int
4 bytes \\(-2^{31}\\) \\(2^{31} - 1\\) 0 long
8 bytes \\(-2^{63}\\) \\(2^{63} - 1\\) 0 Float float
4 bytes \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double
8 bytes \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) 0.0 Char char
2 bytes 0 \\(2^{16} - 1\\) 0 Boolean bool
1 byte \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\) Please note that the above table is specific to Java's basic data types. Every programming language has its own data type definitions, which might differ in space occupied, value ranges, and default values.
int
can be of any size, limited only by available memory; the floating-point float
is double precision 64-bit; there is no char
type, as a single character is actually a string str
of length 1.char
in C and C++ is 1 byte, while in most programming languages, it depends on the specific character encoding method, as detailed in the \"Character Encoding\" chapter.So, what is the connection between basic data types and data structures? We know that data structures are ways to organize and store data in computers. The focus here is on \"structure\" rather than \"data\".
If we want to represent \"a row of numbers\", we naturally think of using an array. This is because the linear structure of an array can represent the adjacency and the ordering of the numbers, but whether the stored content is an integer int
, a decimal float
, or a character char
, is irrelevant to the \"data structure\".
In other words, basic data types provide the \"content type\" of data, while data structures provide the \"way of organizing\" data. For example, in the following code, we use the same data structure (array) to store and represent different basic data types, including int
, float
, char
, bool
, etc.
# Using various basic data types to initialize arrays\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# Python's characters are actually strings of length 1\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# Python's lists can freely store various basic data types and object references\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// Using various basic data types to initialize arrays\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// Using various basic data types to initialize arrays\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// Using various basic data types to initialize arrays\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// Using various basic data types to initialize arrays\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// Using various basic data types to initialize arrays\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
// JavaScript's arrays can freely store various basic data types and objects\nconst array = [0, 0.0, 'a', false];\n
// Using various basic data types to initialize arrays\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// Using various basic data types to initialize arrays\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// Using various basic data types to initialize arrays\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0, 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// Using various basic data types to initialize arrays\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
\n
// Using various basic data types to initialize arrays\nvar numbers: [5]i32 = undefined;\nvar decimals: [5]f32 = undefined;\nvar characters: [5]u8 = undefined;\nvar bools: [5]bool = undefined;\n
"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 Character encoding *","text":"In the computer system, all data is stored in binary form, and characters (represented by char) are no exception. To represent characters, we need to develop a \"character set\" that defines a one-to-one mapping between each character and binary numbers. With the character set, computers can convert binary numbers to characters by looking up the table.
"},{"location":"chapter_data_structure/character_encoding/#341-ascii-character-set","title":"3.4.1 \u00a0 ASCII character set","text":"The \"ASCII code\" is one of the earliest character sets, officially known as the American Standard Code for Information Interchange. It uses 7 binary digits (the lower 7 bits of a byte) to represent a character, allowing for a maximum of 128 different characters. As shown in the Figure 3-6 , ASCII includes uppercase and lowercase English letters, numbers 0 ~ 9, various punctuation marks, and certain control characters (such as newline and tab).
Figure 3-6 \u00a0 ASCII code
However, ASCII can only represent English characters. With the globalization of computers, a character set called \"EASCII\" was developed to represent more languages. It expands from the 7-bit structure of ASCII to 8 bits, enabling the representation of 256 characters.
Globally, various region-specific EASCII character sets have been introduced. The first 128 characters of these sets are consistent with the ASCII, while the remaining 128 characters are defined differently to accommodate the requirements of different languages.
"},{"location":"chapter_data_structure/character_encoding/#342-gbk-character-set","title":"3.4.2 \u00a0 GBK character set","text":"Later, it was found that EASCII still could not meet the character requirements of many languages. For instance, there are nearly a hundred thousand Chinese characters, with several thousand used regularly. In 1980, the Standardization Administration of China released the \"GB2312\" character set, which included 6763 Chinese characters, essentially fulfilling the computer processing needs for the Chinese language.
However, GB2312 could not handle some rare and traditional characters. The \"GBK\" character set expands GB2312 and includes 21886 Chinese characters. In the GBK encoding scheme, ASCII characters are represented with one byte, while Chinese characters use two bytes.
"},{"location":"chapter_data_structure/character_encoding/#343-unicode-character-set","title":"3.4.3 \u00a0 Unicode character set","text":"With the rapid evolution of computer technology and a plethora of character sets and encoding standards, numerous problems arose. On the one hand, these character sets generally only defined characters for specific languages and could not function properly in multilingual environments. On the other hand, the existence of multiple character set standards for the same language caused garbled text when information was exchanged between computers using different encoding standards.
Researchers of that era thought: What if a comprehensive character set encompassing all global languages and symbols was developed? Wouldn't this resolve the issues associated with cross-linguistic environments and garbled text? Inspired by this idea, the extensive character set, Unicode, was born.
\"Unicode\" is referred to as \"\u7edf\u4e00\u7801\" (Unified Code) in Chinese, theoretically capable of accommodating over a million characters. It aims to incorporate characters from all over the world into a single set, providing a universal character set for processing and displaying various languages and reducing the issues of garbled text due to different encoding standards.
Since its release in 1991, Unicode has continually expanded to include new languages and characters. As of September 2022, Unicode contains 149,186 characters, including characters, symbols, and even emojis from various languages. In the vast Unicode character set, commonly used characters occupy 2 bytes, while some rare characters may occupy 3 or even 4 bytes.
Unicode is a universal character set that assigns a number (called a \"code point\") to each character, but it does not specify how these character code points should be stored in a computer system. One might ask: How does a system interpret Unicode code points of varying lengths within a text? For example, given a 2-byte code, how does the system determine if it represents a single 2-byte character or two 1-byte characters?
A straightforward solution to this problem is to store all characters as equal-length encodings. As shown in the Figure 3-7 , each character in \"Hello\" occupies 1 byte, while each character in \"\u7b97\u6cd5\" (algorithm) occupies 2 bytes. We could encode all characters in \"Hello \u7b97\u6cd5\" as 2 bytes by padding the higher bits with zeros. This method would enable the system to interpret a character every 2 bytes, recovering the content of the phrase.
Figure 3-7 \u00a0 Unicode encoding example
However, as ASCII has shown us, encoding English only requires 1 byte. Using the above approach would double the space occupied by English text compared to ASCII encoding, which is a waste of memory space. Therefore, a more efficient Unicode encoding method is needed.
"},{"location":"chapter_data_structure/character_encoding/#344-utf-8-encoding","title":"3.4.4 \u00a0 UTF-8 encoding","text":"Currently, UTF-8 has become the most widely used Unicode encoding method internationally. It is a variable-length encoding, using 1 to 4 bytes to represent a character, depending on the complexity of the character. ASCII characters need only 1 byte, Latin and Greek letters require 2 bytes, commonly used Chinese characters need 3 bytes, and some other rare characters need 4 bytes.
The encoding rules for UTF-8 are not complex and can be divided into two cases:
The Figure 3-8 shows the UTF-8 encoding for \"Hello\u7b97\u6cd5\". It can be observed that since the highest \\(n\\) bits are set to \\(1\\), the system can determine the length of the character as \\(n\\) by counting the number of highest bits set to \\(1\\).
But why set the highest 2 bits of the remaining bytes to \\(10\\)? Actually, this \\(10\\) serves as a kind of checksum. If the system starts parsing text from an incorrect byte, the \\(10\\) at the beginning of the byte can help the system quickly detect anomalies.
The reason for using \\(10\\) as a checksum is that, under UTF-8 encoding rules, it's impossible for the highest two bits of a character to be \\(10\\). This can be proven by contradiction: If the highest two bits of a character are \\(10\\), it indicates that the character's length is \\(1\\), corresponding to ASCII. However, the highest bit of an ASCII character should be \\(0\\), which contradicts the assumption.
Figure 3-8 \u00a0 UTF-8 encoding example
Apart from UTF-8, other common encoding methods include:
From the perspective of storage space, using UTF-8 to represent English characters is very efficient because it only requires 1 byte; using UTF-16 to encode some non-English characters (such as Chinese) can be more efficient because it only requires 2 bytes, while UTF-8 might need 3 bytes.
From a compatibility perspective, UTF-8 is the most versatile, with many tools and libraries supporting UTF-8 as a priority.
"},{"location":"chapter_data_structure/character_encoding/#345-character-encoding-in-programming-languages","title":"3.4.5 \u00a0 Character encoding in programming languages","text":"Historically, many programming languages utilized fixed-length encodings such as UTF-16 or UTF-32 for processing strings during program execution. This allows strings to be handled as arrays, offering several advantages:
The design of character encoding schemes in programming languages is an interesting topic involving various factors:
String
type uses UTF-16 encoding, with each character occupying 2 bytes. This was based on the initial belief that 16 bits were sufficient to represent all possible characters and proven incorrect later. As the Unicode standard expanded beyond 16 bits, characters in Java may now be represented by a pair of 16-bit values, known as \u201csurrogate pairs.\u201dDue to the underestimation of character counts, these languages had to use \"surrogate pairs\" to represent Unicode characters exceeding 16 bits. This approach has its drawbacks: strings containing surrogate pairs may have characters occupying 2 or 4 bytes, losing the advantage of fixed-length encoding. Additionally, handling surrogate pairs adds complexity and debugging difficulty to programming.
Addressing these challenges, some languages have adopted alternative encoding strategies:
str
type uses Unicode encoding with a flexible representation where the storage length of characters depends on the largest Unicode code point in the string. If all characters are ASCII, each character occupies 1 byte, 2 bytes for characters within the Basic Multilingual Plane (BMP), and 4 bytes for characters beyond the BMP.string
type internally uses UTF-8 encoding. Go also provides the rune
type for representing individual Unicode code points.str
and String
types use UTF-8 encoding internally. Rust also offers the char
type for individual Unicode code points.It\u2019s important to note that the above discussion pertains to how strings are stored in programming languages, which is different from how strings are stored in files or transmitted over networks. For file storage or network transmission, strings are usually encoded in UTF-8 format for optimal compatibility and space efficiency.
"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 Classification of data structures","text":"Common data structures include arrays, linked lists, stacks, queues, hash tables, trees, heaps, and graphs. They can be classified into \"logical structure\" and \"physical structure\".
"},{"location":"chapter_data_structure/classification_of_data_structure/#311-logical-structure-linear-and-non-linear","title":"3.1.1 \u00a0 Logical structure: linear and non-linear","text":"The logical structures reveal the logical relationships between data elements. In arrays and linked lists, data are arranged in a specific sequence, demonstrating the linear relationship between data; while in trees, data are arranged hierarchically from the top down, showing the derived relationship between \"ancestors\" and \"descendants\"; and graphs are composed of nodes and edges, reflecting the intricate network relationship.
As shown in the Figure 3-1 , logical structures can be divided into two major categories: \"linear\" and \"non-linear\". Linear structures are more intuitive, indicating data is arranged linearly in logical relationships; non-linear structures, conversely, are arranged non-linearly.
Figure 3-1 \u00a0 Linear and non-linear data structures
Non-linear data structures can be further divided into tree structures and network structures.
During the execution of an algorithm, the data being processed is stored in memory. The Figure 3-2 shows a computer memory stick where each black square is a physical memory space. We can think of memory as a vast Excel spreadsheet, with each cell capable of storing a certain amount of data.
The system accesses the data at the target location by means of a memory address. As shown in the Figure 3-2 , the computer assigns a unique identifier to each cell in the table according to specific rules, ensuring that each memory space has a unique memory address. With these addresses, the program can access the data stored in memory.
Figure 3-2 \u00a0 Memory stick, memory spaces, memory addresses
Tip
It's worth noting that comparing memory to an Excel spreadsheet is a simplified analogy. The actual working mechanism of memory is more complex, involving concepts like address space, memory management, cache mechanisms, virtual memory, and physical memory.
Memory is a shared resource for all programs. When a block of memory is occupied by one program, it cannot be simultaneously used by other programs. Therefore, considering memory resources is crucial in designing data structures and algorithms. For instance, the algorithm's peak memory usage should not exceed the remaining free memory of the system; if there is a lack of contiguous memory blocks, then the data structure chosen must be able to be stored in non-contiguous memory blocks.
As illustrated in the Figure 3-3 , the physical structure reflects the way data is stored in computer memory and it can be divided into contiguous space storage (arrays) and non-contiguous space storage (linked lists). The two types of physical structures exhibit complementary characteristics in terms of time efficiency and space efficiency.
Figure 3-3 \u00a0 Contiguous space storage and dispersed space storage
It is worth noting that all data structures are implemented based on arrays, linked lists, or a combination of both. For example, stacks and queues can be implemented using either arrays or linked lists; while implementations of hash tables may involve both arrays and linked lists. - Array-based implementations: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, Matrices, Tensors (arrays with dimensions \\(\\geq 3\\)). - Linked-list-based implementations: Stacks, Queues, Hash Tables, Trees, Heaps, Graphs, etc.
Data structures implemented based on arrays are also called \u201cStatic Data Structures,\u201d meaning their length cannot be changed after initialization. Conversely, those based on linked lists are called \u201cDynamic Data Structures,\u201d which can still adjust their size during program execution.
Tip
If you find it challenging to comprehend the physical structure, it is recommended that you read the next chapter, \"Arrays and Linked Lists,\" and revisit this section later.
"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 Number encoding *","text":"Note
In this book, chapters marked with an asterisk '*' are optional readings. If you are short on time or find them challenging, you may skip these initially and return to them after completing the essential chapters.
"},{"location":"chapter_data_structure/number_encoding/#331-integer-encoding","title":"3.3.1 \u00a0 Integer encoding","text":"In the table from the previous section, we observed that all integer types can represent one more negative number than positive numbers, such as the byte
range of \\([-128, 127]\\). This phenomenon seems counterintuitive, and its underlying reason involves knowledge of sign-magnitude, one's complement, and two's complement encoding.
Firstly, it's important to note that numbers are stored in computers using the two's complement form. Before analyzing why this is the case, let's define these three encoding methods:
The following diagram illustrates the conversions among sign-magnitude, one's complement, and two's complement:
Figure 3-4 \u00a0 Conversions between sign-magnitude, one's complement, and two's complement
Although sign-magnitude is the most intuitive, it has limitations. For one, negative numbers in sign-magnitude cannot be directly used in calculations. For example, in sign-magnitude, calculating \\(1 + (-2)\\) results in \\(-3\\), which is incorrect.
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]To address this, computers introduced the one's complement. If we convert to one's complement and calculate \\(1 + (-2)\\), then convert the result back to sign-magnitude, we get the correct result of \\(-1\\).
\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(Sign-magnitude)} + 1000 \\; 0010 \\; \\text{(Sign-magnitude)} \\newline & = 0000 \\; 0001 \\; \\text{(One's complement)} + 1111 \\; 1101 \\; \\text{(One's complement)} \\newline & = 1111 \\; 1110 \\; \\text{(One's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & \\rightarrow -1 \\end{aligned} \\]Additionally, there are two representations of zero in sign-magnitude: \\(+0\\) and \\(-0\\). This means two different binary encodings for zero, which could lead to ambiguity. For example, in conditional checks, not differentiating between positive and negative zero might result in incorrect outcomes. Addressing this ambiguity would require additional checks, potentially reducing computational efficiency.
\\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]Like sign-magnitude, one's complement also suffers from the positive and negative zero ambiguity. Therefore, computers further introduced the two's complement. Let's observe the conversion process for negative zero in sign-magnitude, one's complement, and two's complement:
\\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(Sign-magnitude)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(One's complement)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(Two's complement)} \\newline \\end{aligned} \\]Adding \\(1\\) to the one's complement of negative zero produces a carry, but with byte
length being only 8 bits, the carried-over \\(1\\) to the 9th bit is discarded. Therefore, the two's complement of negative zero is \\(0000 \\; 0000\\), the same as positive zero, thus resolving the ambiguity.
One last puzzle is the \\([-128, 127]\\) range for byte
, with an additional negative number, \\(-128\\). We observe that for the interval \\([-127, +127]\\), all integers have corresponding sign-magnitude, one's complement, and two's complement, allowing for mutual conversion between them.
However, the two's complement \\(1000 \\; 0000\\) is an exception without a corresponding sign-magnitude. According to the conversion method, its sign-magnitude would be \\(0000 \\; 0000\\), indicating zero. This presents a contradiction because its two's complement should represent itself. Computers designate this special two's complement \\(1000 \\; 0000\\) as representing \\(-128\\). In fact, the calculation of \\((-1) + (-127)\\) in two's complement results in \\(-128\\).
\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(Sign-magnitude)} + 1000 \\; 0001 \\; \\text{(Sign-magnitude)} \\newline & = 1000 \\; 0000 \\; \\text{(One's complement)} + 1111 \\; 1110 \\; \\text{(One's complement)} \\newline & = 1000 \\; 0001 \\; \\text{(Two's complement)} + 1111 \\; 1111 \\; \\text{(Two's complement)} \\newline & = 1000 \\; 0000 \\; \\text{(Two's complement)} \\newline & \\rightarrow -128 \\end{aligned} \\]As you might have noticed, all these calculations are additions, hinting at an important fact: computers' internal hardware circuits are primarily designed around addition operations. This is because addition is simpler to implement in hardware compared to other operations like multiplication, division, and subtraction, allowing for easier parallelization and faster computation.
It's important to note that this doesn't mean computers can only perform addition. By combining addition with basic logical operations, computers can execute a variety of other mathematical operations. For example, the subtraction \\(a - b\\) can be translated into \\(a + (-b)\\); multiplication and division can be translated into multiple additions or subtractions.
We can now summarize the reason for using two's complement in computers: with two's complement representation, computers can use the same circuits and operations to handle both positive and negative number addition, eliminating the need for special hardware circuits for subtraction and avoiding the ambiguity of positive and negative zero. This greatly simplifies hardware design and enhances computational efficiency.
The design of two's complement is quite ingenious, and due to space constraints, we'll stop here. Interested readers are encouraged to explore further.
"},{"location":"chapter_data_structure/number_encoding/#332-floating-point-number-encoding","title":"3.3.2 \u00a0 Floating-point number encoding","text":"You might have noticed something intriguing: despite having the same length of 4 bytes, why does a float
have a much larger range of values compared to an int
? This seems counterintuitive, as one would expect the range to shrink for float
since it needs to represent fractions.
In fact, this is due to the different representation method used by floating-point numbers (float
). Let's consider a 32-bit binary number as:
According to the IEEE 754 standard, a 32-bit float
consists of the following three parts:
The value of a binary float
number is calculated as:
Converted to a decimal formula, this becomes:
\\[ \\text{val} = (-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} - 127} \\times (1 + \\mathrm{N}) \\]The range of each component is:
\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} \\times 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]Figure 3-5 \u00a0 Example calculation of a float in IEEE 754 standard
Observing the diagram, given an example data \\(\\mathrm{S} = 0\\), \\(\\mathrm{E} = 124\\), \\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\), we have:
\\[ \\text{val} = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]Now we can answer the initial question: The representation of float
includes an exponent bit, leading to a much larger range than int
. Based on the above calculation, the maximum positive number representable by float
is approximately \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\), and the minimum negative number is obtained by switching the sign bit.
However, the trade-off for float
's expanded range is a sacrifice in precision. The integer type int
uses all 32 bits to represent the number, with values evenly distributed; but due to the exponent bit, the larger the value of a float
, the greater the difference between adjacent numbers.
As shown in the Table 3-2 , exponent bits \\(E = 0\\) and \\(E = 255\\) have special meanings, used to represent zero, infinity, \\(\\mathrm{NaN}\\), etc.
Table 3-2 \u00a0 Meaning of exponent bits
Exponent Bit E Fraction Bit \\(\\mathrm{N} = 0\\) Fraction Bit \\(\\mathrm{N} \\ne 0\\) Calculation Formula \\(0\\) \\(\\pm 0\\) Subnormal Numbers \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) Normal Numbers Normal Numbers \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)It's worth noting that subnormal numbers significantly improve the precision of floating-point numbers. The smallest positive normal number is \\(2^{-126}\\), and the smallest positive subnormal number is \\(2^{-126} \\times 2^{-23}\\).
Double-precision double
also uses a similar representation method to float
, which is not elaborated here for brevity.
byte
, short
, int
, long
), floating-point numbers (float
, double
), characters (char
), and booleans (boolean
). Their range depends on the size of the space occupied and the representation method.Q: Why does a hash table contain both linear and non-linear data structures?
The underlying structure of a hash table is an array. To resolve hash collisions, we may use \"chaining\": each bucket in the array points to a linked list, which, when exceeding a certain threshold, might be transformed into a tree (usually a red-black tree). From a storage perspective, the foundation of a hash table is an array, where each bucket slot might contain a value, a linked list, or a tree. Therefore, hash tables may contain both linear data structures (arrays, linked lists) and non-linear data structures (trees).
Q: Is the length of the char
type 1 byte?
The length of the char
type is determined by the encoding method used by the programming language. For example, Java, JavaScript, TypeScript, and C# all use UTF-16 encoding (to save Unicode code points), so the length of the char type is 2 bytes.
Q: Is there ambiguity in calling data structures based on arrays \"static data structures\"? Because operations like push and pop on stacks are \"dynamic\".
While stacks indeed allow for dynamic data operations, the data structure itself remains \"static\" (with unchangeable length). Even though data structures based on arrays can dynamically add or remove elements, their capacity is fixed. If the data volume exceeds the pre-allocated size, a new, larger array needs to be created, and the contents of the old array copied into it.
Q: When building stacks (queues) without specifying their size, why are they considered \"static data structures\"?
In high-level programming languages, we don't need to manually specify the initial capacity of stacks (queues); this task is automatically handled internally by the class. For example, the initial capacity of Java's ArrayList is usually 10. Furthermore, the expansion operation is also implemented automatically. See the subsequent \"List\" chapter for details.
"},{"location":"chapter_graph/","title":"Chapter 9. \u00a0 Graph","text":"Abstract
In the journey of life, we are like individual nodes, connected by countless invisible edges.
Every encountering and parting leaves a unique mark on this vast network graph.
"},{"location":"chapter_graph/#chapter-contents","title":"Chapter contents","text":"A \"graph\" is a type of nonlinear data structure, consisting of \"vertices\" and \"edges\". A graph \\(G\\) can be abstractly represented as a collection of a set of vertices \\(V\\) and a set of edges \\(E\\). The following example shows a graph containing 5 vertices and 7 edges.
\\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]If vertices are viewed as nodes and edges as references (pointers) connecting the nodes, graphs can be seen as a data structure that extends from linked lists. As shown below, compared to linear relationships (linked lists) and divide-and-conquer relationships (trees), network relationships (graphs) are more complex due to their higher degree of freedom.
Figure 9-1 \u00a0 Relationship between linked lists, trees, and graphs
"},{"location":"chapter_graph/graph/#911-common-types-of-graphs","title":"9.1.1 \u00a0 Common types of graphs","text":"Based on whether edges have direction, graphs can be divided into \"undirected graphs\" and \"directed graphs\", as shown below.
Figure 9-2 \u00a0 Directed and undirected graphs
Based on whether all vertices are connected, graphs can be divided into \"connected graphs\" and \"disconnected graphs\", as shown below.
Figure 9-3 \u00a0 Connected and disconnected graphs
We can also add a \"weight\" variable to edges, resulting in \"weighted graphs\" as shown below. For example, in mobile games like \"Honor of Kings\", the system calculates the \"closeness\" between players based on shared gaming time, and this closeness network can be represented with a weighted graph.
Figure 9-4 \u00a0 Weighted and unweighted graphs
Graph data structures include the following commonly used terms.
Common representations of graphs include \"adjacency matrices\" and \"adjacency lists\". The following examples use undirected graphs.
"},{"location":"chapter_graph/graph/#1-adjacency-matrix","title":"1. \u00a0 Adjacency matrix","text":"Let the number of vertices in the graph be \\(n\\), the \"adjacency matrix\" uses an \\(n \\times n\\) matrix to represent the graph, where each row (column) represents a vertex, and the matrix elements represent edges, with \\(1\\) or \\(0\\) indicating whether there is an edge between two vertices.
As shown below, let the adjacency matrix be \\(M\\), and the list of vertices be \\(V\\), then the matrix element \\(M[i, j] = 1\\) indicates there is an edge between vertex \\(V[i]\\) and vertex \\(V[j]\\), conversely \\(M[i, j] = 0\\) indicates there is no edge between the two vertices.
Figure 9-5 \u00a0 Representation of a graph with an adjacency matrix
Adjacency matrices have the following characteristics.
When representing graphs with adjacency matrices, it is possible to directly access matrix elements to obtain edges, thus operations of addition, deletion, lookup, and modification are very efficient, all with a time complexity of \\(O(1)\\). However, the space complexity of the matrix is \\(O(n^2)\\), which consumes more memory.
"},{"location":"chapter_graph/graph/#2-adjacency-list","title":"2. \u00a0 Adjacency list","text":"The \"adjacency list\" uses \\(n\\) linked lists to represent the graph, with each linked list node representing a vertex. The \\(i\\)-th linked list corresponds to vertex \\(i\\) and contains all adjacent vertices (vertices connected to that vertex). The Figure 9-6 shows an example of a graph stored using an adjacency list.
Figure 9-6 \u00a0 Representation of a graph with an adjacency list
The adjacency list only stores actual edges, and the total number of edges is often much less than \\(n^2\\), making it more space-efficient. However, finding edges in the adjacency list requires traversing the linked list, so its time efficiency is not as good as that of the adjacency matrix.
Observing the above figure, the structure of the adjacency list is very similar to the \"chaining\" in hash tables, hence we can use similar methods to optimize efficiency. For example, when the linked list is long, it can be transformed into an AVL tree or red-black tree, thus optimizing the time efficiency from \\(O(n)\\) to \\(O(\\log n)\\); the linked list can also be transformed into a hash table, thus reducing the time complexity to \\(O(1)\\).
"},{"location":"chapter_graph/graph/#913-common-applications-of-graphs","title":"9.1.3 \u00a0 Common applications of graphs","text":"As shown in the Table 9-1 , many real-world systems can be modeled with graphs, and corresponding problems can be reduced to graph computing problems.
Table 9-1 \u00a0 Common graphs in real life
Vertices Edges Graph Computing Problem Social Networks Users Friendships Potential Friend Recommendations Subway Lines Stations Connectivity Between Stations Shortest Route Recommendations Solar System Celestial Bodies Gravitational Forces Between Celestial Bodies Planetary Orbit Calculations"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 Basic operations on graphs","text":"The basic operations on graphs can be divided into operations on \"edges\" and operations on \"vertices\". Under the two representation methods of \"adjacency matrix\" and \"adjacency list\", the implementation methods are different.
"},{"location":"chapter_graph/graph_operations/#921-implementation-based-on-adjacency-matrix","title":"9.2.1 \u00a0 Implementation based on adjacency matrix","text":"Given an undirected graph with \\(n\\) vertices, the various operations are implemented as shown in the Figure 9-7 .
vertices
of length \\(n\\), using \\(O(n)\\) time; initialize an \\(n \\times n\\) size adjacency matrix adjMat
, using \\(O(n^2)\\) time.Figure 9-7 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency matrix
Below is the implementation code for graphs represented using an adjacency matrix:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig graph_adjacency_matrix.pyclass GraphAdjMat:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, vertices: list[int], edges: list[list[int]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.vertices: list[int] = []\n # \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n self.adj_mat: list[list[int]] = []\n # \u6dfb\u52a0\u9876\u70b9\n for val in vertices:\n self.add_vertex(val)\n # \u6dfb\u52a0\u8fb9\n # \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges:\n self.add_edge(e[0], e[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.vertices)\n\n def add_vertex(self, val: int):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n n = self.size()\n # \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.append(val)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n new_row = [0] * n\n self.adj_mat.append(new_row)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in self.adj_mat:\n row.append(0)\n\n def remove_vertex(self, index: int):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if index >= self.size():\n raise IndexError()\n # \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.pop(index)\n # \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in self.adj_mat:\n row.pop(index)\n\n def add_edge(self, i: int, j: int):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n # \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1\n self.adj_mat[j][i] = 1\n\n def remove_edge(self, i: int, j: int):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n # \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n # \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n raise IndexError()\n self.adj_mat[i][j] = 0\n self.adj_mat[j][i] = 0\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u77e9\u9635\"\"\"\n print(\"\u9876\u70b9\u5217\u8868 =\", self.vertices)\n print(\"\u90bb\u63a5\u77e9\u9635 =\")\n print_matrix(self.adj_mat)\n
graph_adjacency_matrix.cpp/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vector<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vector<vector<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const vector<int> &edge : edges) {\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() const {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.push_back(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n adjMat.emplace_back(vector<int>(n, 0));\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (vector<int> &row : adjMat) {\n row.push_back(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.erase(vertices.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.erase(adjMat.begin() + index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (vector<int> &row : adjMat) {\n row.erase(row.begin() + index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw out_of_range(\"\u9876\u70b9\u4e0d\u5b58\u5728\");\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void print() {\n cout << \"\u9876\u70b9\u5217\u8868 = \";\n printVector(vertices);\n cout << \"\u90bb\u63a5\u77e9\u9635 =\" << endl;\n printVectorMatrix(adjMat);\n }\n};\n
graph_adjacency_matrix.java/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<Integer> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<Integer>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = new ArrayList<>();\n this.adjMat = new ArrayList<>();\n // \u6dfb\u52a0\u9876\u70b9\n for (int val : vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (int[] e : edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return vertices.size();\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<Integer> newRow = new ArrayList<>(n);\n for (int j = 0; j < n; j++) {\n newRow.add(0);\n }\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<Integer> row : adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(int index) {\n if (index >= size())\n throw new IndexOutOfBoundsException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<Integer> row : adjMat) {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat.get(i).set(j, 1);\n adjMat.get(j).set(i, 1);\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n throw new IndexOutOfBoundsException();\n adjMat.get(i).set(j, 0);\n adjMat.get(j).set(i, 0);\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void print() {\n System.out.print(\"\u9876\u70b9\u5217\u8868 = \");\n System.out.println(vertices);\n System.out.println(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.cs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjMat(int[] vertices, int[][] edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n foreach (int val in vertices) {\n AddVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n foreach (int[] e in edges) {\n AddEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return vertices.Count;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(int val) {\n int n = Size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.Add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = new(n);\n for (int j = 0; j < n; j++) {\n newRow.Add(0);\n }\n adjMat.Add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n foreach (List<int> row in adjMat) {\n row.Add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(int index) {\n if (index >= Size())\n throw new IndexOutOfRangeException();\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.RemoveAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n foreach (List<int> row in adjMat) {\n row.RemoveAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void AddEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n public void RemoveEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n throw new IndexOutOfRangeException();\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n public void Print() {\n Console.Write(\"\u9876\u70b9\u5217\u8868 = \");\n PrintUtil.PrintList(vertices);\n Console.WriteLine(\"\u90bb\u63a5\u77e9\u9635 =\");\n PrintUtil.PrintMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.go/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjMat struct {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n vertices []int\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat [][]int\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n // \u6dfb\u52a0\u9876\u70b9\n n := len(vertices)\n adjMat := make([][]int, n)\n for i := range adjMat {\n adjMat[i] = make([]int, n)\n }\n // \u521d\u59cb\u5316\u56fe\n g := &graphAdjMat{\n vertices: vertices,\n adjMat: adjMat,\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for i := range edges {\n g.addEdge(edges[i][0], edges[i][1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjMat) size() int {\n return len(g.vertices)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjMat) addVertex(val int) {\n n := g.size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n g.vertices = append(g.vertices, val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n newRow := make([]int, n)\n g.adjMat = append(g.adjMat, newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i], 0)\n }\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjMat) removeVertex(index int) {\n if index >= g.size() {\n return\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i := range g.adjMat {\n g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n }\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) addEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n g.adjMat[i][j] = 1\n g.adjMat[j][i] = 1\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n }\n g.adjMat[i][j] = 0\n g.adjMat[j][i] = 0\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nfunc (g *graphAdjMat) print() {\n fmt.Printf(\"\\t\u9876\u70b9\u5217\u8868 = %v\\n\", g.vertices)\n fmt.Printf(\"\\t\u90bb\u63a5\u77e9\u9635 = \\n\")\n for i := range g.adjMat {\n fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n }\n}\n
graph_adjacency_matrix.swift/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n private var vertices: [Int] // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n private var adjMat: [[Int]] // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(vertices: [Int], edges: [[Int]]) {\n self.vertices = []\n adjMat = []\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n addVertex(val: val)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for e in edges {\n addEdge(i: e[0], j: e[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n func size() -> Int {\n vertices.count\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n func addVertex(val: Int) {\n let n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.append(val)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n let newRow = Array(repeating: 0, count: n)\n adjMat.append(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for i in adjMat.indices {\n adjMat[i].append(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n func removeVertex(index: Int) {\n if index >= size() {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.remove(at: index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for i in adjMat.indices {\n adjMat[i].remove(at: index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1\n adjMat[j][i] = 1\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n func removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n fatalError(\"\u8d8a\u754c\")\n }\n adjMat[i][j] = 0\n adjMat[j][i] = 0\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n func print() {\n Swift.print(\"\u9876\u70b9\u5217\u8868 = \", terminator: \"\")\n Swift.print(vertices)\n Swift.print(\"\u90bb\u63a5\u77e9\u9635 =\")\n PrintUtil.printMatrix(matrix: adjMat)\n }\n}\n
graph_adjacency_matrix.js/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices, edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val) {\n const n = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow = [];\n for (let j = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index) {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i, j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print() {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.ts/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n vertices: number[]; // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n adjMat: number[][]; // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n constructor(vertices: number[], edges: number[][]) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (const val of vertices) {\n this.addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (const e of edges) {\n this.addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(val: number): void {\n const n: number = this.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n this.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n const newRow: number[] = [];\n for (let j: number = 0; j < n; j++) {\n newRow.push(0);\n }\n this.adjMat.push(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (const row of this.adjMat) {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(index: number): void {\n if (index >= this.size()) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n this.vertices.splice(index, 1);\n\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n this.adjMat.splice(index, 1);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (const row of this.adjMat) {\n row.splice(index, 1);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n addEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) === (j, i)\n this.adjMat[i][j] = 1;\n this.adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n removeEdge(i: number, j: number): void {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n throw new RangeError('Index Out Of Bounds Exception');\n }\n this.adjMat[i][j] = 0;\n this.adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n print(): void {\n console.log('\u9876\u70b9\u5217\u8868 = ', this.vertices);\n console.log('\u90bb\u63a5\u77e9\u9635 =', this.adjMat);\n }\n}\n
graph_adjacency_matrix.dart/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat {\n List<int> vertices = []; // \u9876\u70b9\u5143\u7d20\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n List<List<int>> adjMat = []; //\u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n this.vertices = [];\n this.adjMat = [];\n // \u6dfb\u52a0\u9876\u70b9\n for (int val in vertices) {\n addVertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (List<int> e in edges) {\n addEdge(e[0], e[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return vertices.length;\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(int val) {\n int n = size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n List<int> newRow = List.filled(n, 0, growable: true);\n adjMat.add(newRow);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (List<int> row in adjMat) {\n row.add(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(int index) {\n if (index >= size()) {\n throw IndexError;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (List<int> row in adjMat) {\n row.removeAt(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void addEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n void removeEdge(int i, int j) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n throw IndexError;\n }\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n void printAdjMat() {\n print(\"\u9876\u70b9\u5217\u8868 = $vertices\");\n print(\"\u90bb\u63a5\u77e9\u9635 = \");\n printMatrix(adjMat);\n }\n}\n
graph_adjacency_matrix.rs/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjMat {\n // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub vertices: Vec<i32>,\n // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n let mut graph = GraphAdjMat {\n vertices: vec![],\n adj_mat: vec![],\n };\n // \u6dfb\u52a0\u9876\u70b9\n for val in vertices {\n graph.add_vertex(val);\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for edge in edges {\n graph.add_edge(edge[0], edge[1])\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n pub fn size(&self) -> usize {\n self.vertices.len()\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, val: i32) {\n let n = self.size();\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n self.vertices.push(val);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n self.adj_mat.push(vec![0; n]);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for row in &mut self.adj_mat {\n row.push(0);\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n pub fn remove_vertex(&mut self, index: usize) {\n if index >= self.size() {\n panic!(\"index error\")\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n self.vertices.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n self.adj_mat.remove(index);\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for row in &mut self.adj_mat {\n row.remove(index);\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n self.adj_mat[i][j] = 1;\n self.adj_mat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n pub fn remove_edge(&mut self, i: usize, j: usize) {\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if i >= self.size() || j >= self.size() || i == j {\n panic!(\"index error\")\n }\n self.adj_mat[i][j] = 0;\n self.adj_mat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n pub fn print(&self) {\n println!(\"\u9876\u70b9\u5217\u8868 = {:?}\", self.vertices);\n println!(\"\u90bb\u63a5\u77e9\u9635 =\");\n println!(\"[\");\n for row in &self.adj_mat {\n println!(\" {:?},\", row);\n }\n println!(\"]\")\n }\n}\n
graph_adjacency_matrix.c/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7ed3\u6784\u4f53 */\ntypedef struct {\n int vertices[MAX_SIZE];\n int adjMat[MAX_SIZE][MAX_SIZE];\n int size;\n} GraphAdjMat;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjMat *newGraphAdjMat() {\n GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n for (int j = 0; j < MAX_SIZE; j++) {\n graph->adjMat[i][j] = 0;\n }\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n free(graph);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjMat *graph, int val) {\n if (graph->size == MAX_SIZE) {\n fprintf(stderr, \"\u56fe\u7684\u9876\u70b9\u6570\u91cf\u5df2\u8fbe\u6700\u5927\u503c\\n\");\n return;\n }\n // \u6dfb\u52a0\u7b2c n \u4e2a\u9876\u70b9\uff0c\u5e76\u5c06\u7b2c n \u884c\u548c\u5217\u7f6e\u96f6\n int n = graph->size;\n graph->vertices[n] = val;\n for (int i = 0; i <= n; i++) {\n graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n }\n graph->size++;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n if (index < 0 || index >= graph->size) {\n fprintf(stderr, \"\u9876\u70b9\u7d22\u5f15\u8d8a\u754c\\n\");\n return;\n }\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n for (int i = index; i < graph->size - 1; i++) {\n graph->vertices[i] = graph->vertices[i + 1];\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n for (int i = index; i < graph->size - 1; i++) {\n for (int j = 0; j < graph->size; j++) {\n graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n }\n }\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (int i = 0; i < graph->size; i++) {\n for (int j = index; j < graph->size - 1; j++) {\n graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n }\n }\n graph->size--;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 1;\n graph->adjMat[j][i] = 1;\n}\n\n/* \u5220\u9664\u8fb9 */\n// \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n fprintf(stderr, \"\u8fb9\u7d22\u5f15\u8d8a\u754c\u6216\u76f8\u7b49\\n\");\n return;\n }\n graph->adjMat[i][j] = 0;\n graph->adjMat[j][i] = 0;\n}\n\n/* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n printf(\"\u9876\u70b9\u5217\u8868 = \");\n printArray(graph->vertices, graph->size);\n printf(\"\u90bb\u63a5\u77e9\u9635 =\\n\");\n for (int i = 0; i < graph->size; i++) {\n printArray(graph->adjMat[i], graph->size);\n }\n}\n
graph_adjacency_matrix.kt/* \u57fa\u4e8e\u90bb\u63a5\u77e9\u9635\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n val vertices: MutableList<Int> = ArrayList() // \u9876\u70b9\u5217\u8868\uff0c\u5143\u7d20\u4ee3\u8868\u201c\u9876\u70b9\u503c\u201d\uff0c\u7d22\u5f15\u4ee3\u8868\u201c\u9876\u70b9\u7d22\u5f15\u201d\n val adjMat: MutableList<MutableList<Int>> = ArrayList() // \u90bb\u63a5\u77e9\u9635\uff0c\u884c\u5217\u7d22\u5f15\u5bf9\u5e94\u201c\u9876\u70b9\u7d22\u5f15\u201d\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n // \u6dfb\u52a0\u9876\u70b9\n for (vertex in vertices) {\n addVertex(vertex)\n }\n // \u6dfb\u52a0\u8fb9\n // \u8bf7\u6ce8\u610f\uff0cedges \u5143\u7d20\u4ee3\u8868\u9876\u70b9\u7d22\u5f15\uff0c\u5373\u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n for (edge in edges) {\n addEdge(edge[0], edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n fun size(): Int {\n return vertices.size\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n fun addVertex(value: Int) {\n val n = size()\n // \u5411\u9876\u70b9\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u9876\u70b9\u7684\u503c\n vertices.add(value)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u884c\n val newRow: MutableList<Int> = mutableListOf()\n for (j in 0..<n) {\n newRow.add(0)\n }\n adjMat.add(newRow)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u6dfb\u52a0\u4e00\u5217\n for (row in adjMat) {\n row.add(0)\n }\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n fun removeVertex(index: Int) {\n if (index >= size()) throw IndexOutOfBoundsException()\n // \u5728\u9876\u70b9\u5217\u8868\u4e2d\u79fb\u9664\u7d22\u5f15 index \u7684\u9876\u70b9\n vertices.removeAt(index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u884c\n adjMat.removeAt(index)\n // \u5728\u90bb\u63a5\u77e9\u9635\u4e2d\u5220\u9664\u7d22\u5f15 index \u7684\u5217\n for (row in adjMat) {\n row.removeAt(index)\n }\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n fun addEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()\n // \u5728\u65e0\u5411\u56fe\u4e2d\uff0c\u90bb\u63a5\u77e9\u9635\u5173\u4e8e\u4e3b\u5bf9\u89d2\u7ebf\u5bf9\u79f0\uff0c\u5373\u6ee1\u8db3 (i, j) == (j, i)\n adjMat[i][j] = 1;\n adjMat[j][i] = 1;\n }\n\n /* \u5220\u9664\u8fb9 */\n // \u53c2\u6570 i, j \u5bf9\u5e94 vertices \u5143\u7d20\u7d22\u5f15\n fun removeEdge(i: Int, j: Int) {\n // \u7d22\u5f15\u8d8a\u754c\u4e0e\u76f8\u7b49\u5904\u7406\n if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) throw java.lang.IndexOutOfBoundsException()\n adjMat[i][j] = 0;\n adjMat[j][i] = 0;\n }\n\n /* \u6253\u5370\u90bb\u63a5\u77e9\u9635 */\n fun print() {\n print(\"\u9876\u70b9\u5217\u8868 = \")\n println(vertices);\n println(\"\u90bb\u63a5\u77e9\u9635 =\");\n printMatrix(adjMat)\n }\n}\n
graph_adjacency_matrix.rb[class]{GraphAdjMat}-[func]{}\n
graph_adjacency_matrix.zig[class]{GraphAdjMat}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_graph/graph_operations/#922-implementation-based-on-adjacency-list","title":"9.2.2 \u00a0 Implementation based on adjacency list","text":"Given an undirected graph with a total of \\(n\\) vertices and \\(m\\) edges, the various operations can be implemented as shown in the Figure 9-8 .
Figure 9-8 \u00a0 Initialization, adding and removing edges, adding and removing vertices in adjacency list
Below is the adjacency list code implementation. Compared to the above diagram, the actual code has the following differences.
key
being the vertex instance, value
being the list (linked list) of adjacent vertices of that vertex.Additionally, we use the Vertex
class to represent vertices in the adjacency list. The reason for this is: if, like with the adjacency matrix, list indexes were used to distinguish different vertices, then suppose you want to delete the vertex at index \\(i\\), you would need to traverse the entire adjacency list and decrement all indexes greater than \\(i\\) by \\(1\\), which is very inefficient. However, if each vertex is a unique Vertex
instance, then deleting a vertex does not require any changes to other vertices.
class GraphAdjList:\n \"\"\"\u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\"\"\"\n\n def __init__(self, edges: list[list[Vertex]]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n self.adj_list = dict[Vertex, list[Vertex]]()\n # \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges:\n self.add_vertex(edge[0])\n self.add_vertex(edge[1])\n self.add_edge(edge[0], edge[1])\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u9876\u70b9\u6570\u91cf\"\"\"\n return len(self.adj_list)\n\n def add_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u6dfb\u52a0\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list[vet1].append(vet2)\n self.adj_list[vet2].append(vet1)\n\n def remove_edge(self, vet1: Vertex, vet2: Vertex):\n \"\"\"\u5220\u9664\u8fb9\"\"\"\n if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n raise ValueError()\n # \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list[vet1].remove(vet2)\n self.adj_list[vet2].remove(vet1)\n\n def add_vertex(self, vet: Vertex):\n \"\"\"\u6dfb\u52a0\u9876\u70b9\"\"\"\n if vet in self.adj_list:\n return\n # \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list[vet] = []\n\n def remove_vertex(self, vet: Vertex):\n \"\"\"\u5220\u9664\u9876\u70b9\"\"\"\n if vet not in self.adj_list:\n raise ValueError()\n # \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.pop(vet)\n # \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for vertex in self.adj_list:\n if vet in self.adj_list[vertex]:\n self.adj_list[vertex].remove(vet)\n\n def print(self):\n \"\"\"\u6253\u5370\u90bb\u63a5\u8868\"\"\"\n print(\"\u90bb\u63a5\u8868 =\")\n for vertex in self.adj_list:\n tmp = [v.val for v in self.adj_list[vertex]]\n print(f\"{vertex.val}: {tmp},\")\n
graph_adjacency_list.cpp/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n public:\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n /* \u5728 vector \u4e2d\u5220\u9664\u6307\u5b9a\u8282\u70b9 */\n void remove(vector<Vertex *> &vec, Vertex *vet) {\n for (int i = 0; i < vec.size(); i++) {\n if (vec[i] == vet) {\n vec.erase(vec.begin() + i);\n break;\n }\n }\n }\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(const vector<vector<Vertex *>> &edges) {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const vector<Vertex *> &edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].push_back(vet2);\n adjList[vet2].push_back(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex *vet1, Vertex *vet2) {\n if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5220\u9664\u8fb9 vet1 - vet2\n remove(adjList[vet1], vet2);\n remove(adjList[vet2], vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex *vet) {\n if (adjList.count(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = vector<Vertex *>();\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex *vet) {\n if (!adjList.count(vet))\n throw invalid_argument(\"\u4e0d\u5b58\u5728\u9876\u70b9\");\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.erase(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (auto &adj : adjList) {\n remove(adj.second, vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void print() {\n cout << \"\u90bb\u63a5\u8868 =\" << endl;\n for (auto &adj : adjList) {\n const auto &key = adj.first;\n const auto &vec = adj.second;\n cout << key->val << \": \";\n printVector(vetsToVals(vec));\n }\n }\n};\n
graph_adjacency_list.java/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public GraphAdjList(Vertex[][] edges) {\n this.adjList = new HashMap<>();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (Vertex[] edge : edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public int size() {\n return adjList.size();\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList.get(vet1).add(vet2);\n adjList.get(vet2).add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw new IllegalArgumentException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList.get(vet1).remove(vet2);\n adjList.get(vet2).remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void addVertex(Vertex vet) {\n if (adjList.containsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.put(vet, new ArrayList<>());\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet))\n throw new IllegalArgumentException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (List<Vertex> list : adjList.values()) {\n list.remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void print() {\n System.out.println(\"\u90bb\u63a5\u8868 =\");\n for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n List<Integer> tmp = new ArrayList<>();\n for (Vertex vertex : pair.getValue())\n tmp.add(vertex.val);\n System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n }\n }\n}\n
graph_adjacency_list.cs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public Dictionary<Vertex, List<Vertex>> adjList;\n\n /* \u6784\u9020\u51fd\u6570 */\n public GraphAdjList(Vertex[][] edges) {\n adjList = [];\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n foreach (Vertex[] edge in edges) {\n AddVertex(edge[0]);\n AddVertex(edge[1]);\n AddEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int Size() {\n return adjList.Count;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public void AddEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1].Add(vet2);\n adjList[vet2].Add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n public void RemoveEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n throw new InvalidOperationException();\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1].Remove(vet2);\n adjList[vet2].Remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public void AddVertex(Vertex vet) {\n if (adjList.ContainsKey(vet))\n return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList.Add(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public void RemoveVertex(Vertex vet) {\n if (!adjList.ContainsKey(vet))\n throw new InvalidOperationException();\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.Remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n foreach (List<Vertex> list in adjList.Values) {\n list.Remove(vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public void Print() {\n Console.WriteLine(\"\u90bb\u63a5\u8868 =\");\n foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n List<int> tmp = [];\n foreach (Vertex vertex in pair.Value)\n tmp.Add(vertex.val);\n Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n }\n }\n}\n
graph_adjacency_list.go/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntype graphAdjList struct {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList map[Vertex][]Vertex\n}\n\n/* \u6784\u9020\u51fd\u6570 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n g := &graphAdjList{\n adjList: make(map[Vertex][]Vertex),\n }\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for _, edge := range edges {\n g.addVertex(edge[0])\n g.addVertex(edge[1])\n g.addEdge(edge[0], edge[1])\n }\n return g\n}\n\n/* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\nfunc (g *graphAdjList) size() int {\n return len(g.adjList)\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2, \u6dfb\u52a0\u533f\u540d struct{},\n g.adjList[vet1] = append(g.adjList[vet1], vet2)\n g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u5220\u9664\u8fb9 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n _, ok1 := g.adjList[vet1]\n _, ok2 := g.adjList[vet2]\n if !ok1 || !ok2 || vet1 == vet2 {\n panic(\"error\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if ok {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n _, ok := g.adjList[vet]\n if !ok {\n panic(\"error\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n delete(g.adjList, vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for v, list := range g.adjList {\n g.adjList[v] = DeleteSliceElms(list, vet)\n }\n}\n\n/* \u6253\u5370\u90bb\u63a5\u8868 */\nfunc (g *graphAdjList) print() {\n var builder strings.Builder\n fmt.Printf(\"\u90bb\u63a5\u8868 = \\n\")\n for k, v := range g.adjList {\n builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n for _, vet := range v {\n builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n }\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
graph_adjacency_list.swift/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n public private(set) var adjList: [Vertex: [Vertex]]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public init(edges: [[Vertex]]) {\n adjList = [:]\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n addVertex(vet: edge[0])\n addVertex(vet: edge[1])\n addEdge(vet1: edge[0], vet2: edge[1])\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n public func size() -> Int {\n adjList.count\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n public func addEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.append(vet2)\n adjList[vet2]?.append(vet1)\n }\n\n /* \u5220\u9664\u8fb9 */\n public func removeEdge(vet1: Vertex, vet2: Vertex) {\n if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.removeAll { $0 == vet2 }\n adjList[vet2]?.removeAll { $0 == vet1 }\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n public func addVertex(vet: Vertex) {\n if adjList[vet] != nil {\n return\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = []\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n public func removeVertex(vet: Vertex) {\n if adjList[vet] == nil {\n fatalError(\"\u53c2\u6570\u9519\u8bef\")\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.removeValue(forKey: vet)\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for key in adjList.keys {\n adjList[key]?.removeAll { $0 == vet }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n public func print() {\n Swift.print(\"\u90bb\u63a5\u8868 =\")\n for (vertex, list) in adjList {\n let list = list.map { $0.val }\n Swift.print(\"\\(vertex.val): \\(list),\")\n }\n }\n}\n
graph_adjacency_list.js/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size() {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1, vet2) {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet) {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet) {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print() {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.ts/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n adjList: Map<Vertex, Vertex[]>;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(edges: Vertex[][]) {\n this.adjList = new Map();\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (const edge of edges) {\n this.addVertex(edge[0]);\n this.addVertex(edge[1]);\n this.addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n size(): number {\n return this.adjList.size;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n addEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n this.adjList.get(vet1).push(vet2);\n this.adjList.get(vet2).push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n removeEdge(vet1: Vertex, vet2: Vertex): void {\n if (\n !this.adjList.has(vet1) ||\n !this.adjList.has(vet2) ||\n vet1 === vet2\n ) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n addVertex(vet: Vertex): void {\n if (this.adjList.has(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n this.adjList.set(vet, []);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n removeVertex(vet: Vertex): void {\n if (!this.adjList.has(vet)) {\n throw new Error('Illegal Argument Exception');\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n this.adjList.delete(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (const set of this.adjList.values()) {\n const index: number = set.indexOf(vet);\n if (index > -1) {\n set.splice(index, 1);\n }\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n print(): void {\n console.log('\u90bb\u63a5\u8868 =');\n for (const [key, value] of this.adjList.entries()) {\n const tmp = [];\n for (const vertex of value) {\n tmp.push(vertex.val);\n }\n console.log(key.val + ': ' + tmp.join());\n }\n }\n}\n
graph_adjacency_list.dart/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n Map<Vertex, List<Vertex>> adjList = {};\n\n /* \u6784\u9020\u65b9\u6cd5 */\n GraphAdjList(List<List<Vertex>> edges) {\n for (List<Vertex> edge in edges) {\n addVertex(edge[0]);\n addVertex(edge[1]);\n addEdge(edge[0], edge[1]);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n int size() {\n return adjList.length;\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n void addEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]!.add(vet2);\n adjList[vet2]!.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n void removeEdge(Vertex vet1, Vertex vet2) {\n if (!adjList.containsKey(vet1) ||\n !adjList.containsKey(vet2) ||\n vet1 == vet2) {\n throw ArgumentError;\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]!.remove(vet2);\n adjList[vet2]!.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n void addVertex(Vertex vet) {\n if (adjList.containsKey(vet)) return;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = [];\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n void removeVertex(Vertex vet) {\n if (!adjList.containsKey(vet)) {\n throw ArgumentError;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n adjList.forEach((key, value) {\n value.remove(vet);\n });\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n void printAdjList() {\n print(\"\u90bb\u63a5\u8868 =\");\n adjList.forEach((key, value) {\n List<int> tmp = [];\n for (Vertex vertex in value) {\n tmp.add(vertex.val);\n }\n print(\"${key.val}: $tmp,\");\n });\n }\n}\n
graph_adjacency_list.rs/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b\u578b */\npub struct GraphAdjList {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n pub adj_list: HashMap<Vertex, Vec<Vertex>>,\n}\n\nimpl GraphAdjList {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n let mut graph = GraphAdjList {\n adj_list: HashMap::new(),\n };\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for edge in edges {\n graph.add_vertex(edge[0]);\n graph.add_vertex(edge[1]);\n graph.add_edge(edge[0], edge[1]);\n }\n\n graph\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n #[allow(unused)]\n pub fn size(&self) -> usize {\n self.adj_list.len()\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n self.adj_list.get_mut(&vet1).unwrap().push(vet2);\n self.adj_list.get_mut(&vet2).unwrap().push(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n #[allow(unused)]\n pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n if !self.adj_list.contains_key(&vet1) || !self.adj_list.contains_key(&vet2) || vet1 == vet2\n {\n panic!(\"value error\");\n }\n // \u5220\u9664\u8fb9 vet1 - vet2\n self.adj_list\n .get_mut(&vet1)\n .unwrap()\n .retain(|&vet| vet != vet2);\n self.adj_list\n .get_mut(&vet2)\n .unwrap()\n .retain(|&vet| vet != vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n pub fn add_vertex(&mut self, vet: Vertex) {\n if self.adj_list.contains_key(&vet) {\n return;\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n self.adj_list.insert(vet, vec![]);\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n #[allow(unused)]\n pub fn remove_vertex(&mut self, vet: Vertex) {\n if !self.adj_list.contains_key(&vet) {\n panic!(\"value error\");\n }\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n self.adj_list.remove(&vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for list in self.adj_list.values_mut() {\n list.retain(|&v| v != vet);\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n pub fn print(&self) {\n println!(\"\u90bb\u63a5\u8868 =\");\n for (vertex, list) in &self.adj_list {\n let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n println!(\"{}: {:?},\", vertex.val, list);\n }\n }\n}\n
graph_adjacency_list.c/* \u8282\u70b9\u7ed3\u6784\u4f53 */\ntypedef struct AdjListNode {\n Vertex *vertex; // \u9876\u70b9\n struct AdjListNode *next; // \u540e\u7ee7\u8282\u70b9\n} AdjListNode;\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n node->vertex = vet;\n // \u5934\u63d2\u6cd5\n node->next = head->next;\n head->next = node;\n}\n\n/* \u5220\u9664\u8fb9\u8f85\u52a9\u51fd\u6570 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n AdjListNode *pre = head;\n AdjListNode *cur = head->next;\n // \u5728\u94fe\u8868\u4e2d\u641c\u7d22 vet \u5bf9\u5e94\u8282\u70b9\n while (cur != NULL && cur->vertex != vet) {\n pre = cur;\n cur = cur->next;\n }\n if (cur == NULL)\n return;\n // \u5c06 vet \u5bf9\u5e94\u8282\u70b9\u4ece\u94fe\u8868\u4e2d\u5220\u9664\n pre->next = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n}\n\n/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\ntypedef struct {\n AdjListNode *heads[MAX_SIZE]; // \u8282\u70b9\u6570\u7ec4\n int size; // \u8282\u70b9\u6570\u91cf\n} GraphAdjList;\n\n/* \u6784\u9020\u51fd\u6570 */\nGraphAdjList *newGraphAdjList() {\n GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n if (!graph) {\n return NULL;\n }\n graph->size = 0;\n for (int i = 0; i < MAX_SIZE; i++) {\n graph->heads[i] = NULL;\n }\n return graph;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n for (int i = 0; i < graph->size; i++) {\n AdjListNode *cur = graph->heads[i];\n while (cur != NULL) {\n AdjListNode *next = cur->next;\n if (cur != graph->heads[i]) {\n free(cur);\n }\n cur = next;\n }\n free(graph->heads[i]->vertex);\n free(graph->heads[i]);\n }\n free(graph);\n}\n\n/* \u67e5\u627e\u9876\u70b9\u5bf9\u5e94\u7684\u8282\u70b9 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n for (int i = 0; i < graph->size; i++) {\n if (graph->heads[i]->vertex == vet) {\n return graph->heads[i];\n }\n }\n return NULL;\n}\n\n/* \u6dfb\u52a0\u8fb9 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL && head1 != head2);\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n addEdgeHelper(head1, vet2);\n addEdgeHelper(head2, vet1);\n}\n\n/* \u5220\u9664\u8fb9 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n AdjListNode *head1 = findNode(graph, vet1);\n AdjListNode *head2 = findNode(graph, vet2);\n assert(head1 != NULL && head2 != NULL);\n // \u5220\u9664\u8fb9 vet1 - vet2\n removeEdgeHelper(head1, head2->vertex);\n removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u6dfb\u52a0\u9876\u70b9 */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n assert(graph != NULL && graph->size < MAX_SIZE);\n AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n head->vertex = vet;\n head->next = NULL;\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n graph->heads[graph->size++] = head;\n}\n\n/* \u5220\u9664\u9876\u70b9 */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n AdjListNode *node = findNode(graph, vet);\n assert(node != NULL);\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n AdjListNode *cur = node, *pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n free(pre);\n }\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (int i = 0; i < graph->size; i++) {\n cur = graph->heads[i];\n pre = NULL;\n while (cur) {\n pre = cur;\n cur = cur->next;\n if (cur && cur->vertex == vet) {\n pre->next = cur->next;\n free(cur);\n break;\n }\n }\n }\n // \u5c06\u8be5\u9876\u70b9\u4e4b\u540e\u7684\u9876\u70b9\u5411\u524d\u79fb\u52a8\uff0c\u4ee5\u586b\u8865\u7a7a\u7f3a\n int i;\n for (i = 0; i < graph->size; i++) {\n if (graph->heads[i] == node)\n break;\n }\n for (int j = i; j < graph->size - 1; j++) {\n graph->heads[j] = graph->heads[j + 1];\n }\n graph->size--;\n free(vet);\n}\n
graph_adjacency_list.kt/* \u57fa\u4e8e\u90bb\u63a5\u8868\u5b9e\u73b0\u7684\u65e0\u5411\u56fe\u7c7b */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n // \u90bb\u63a5\u8868\uff0ckey\uff1a\u9876\u70b9\uff0cvalue\uff1a\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n val adjList: MutableMap<Vertex, MutableList<Vertex>> = HashMap()\n\n /* \u6784\u9020\u51fd\u6570 */\n init {\n // \u6dfb\u52a0\u6240\u6709\u9876\u70b9\u548c\u8fb9\n for (edge in edges) {\n addVertex(edge[0]!!);\n addVertex(edge[1]!!);\n addEdge(edge[0]!!, edge[1]!!);\n }\n }\n\n /* \u83b7\u53d6\u9876\u70b9\u6570\u91cf */\n fun size(): Int {\n return adjList.size\n }\n\n /* \u6dfb\u52a0\u8fb9 */\n fun addEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u6dfb\u52a0\u8fb9 vet1 - vet2\n adjList[vet1]?.add(vet2)\n adjList[vet2]?.add(vet1);\n }\n\n /* \u5220\u9664\u8fb9 */\n fun removeEdge(vet1: Vertex, vet2: Vertex) {\n if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n throw IllegalArgumentException()\n // \u5220\u9664\u8fb9 vet1 - vet2\n adjList[vet1]?.remove(vet2);\n adjList[vet2]?.remove(vet1);\n }\n\n /* \u6dfb\u52a0\u9876\u70b9 */\n fun addVertex(vet: Vertex) {\n if (adjList.containsKey(vet))\n return\n // \u5728\u90bb\u63a5\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u94fe\u8868\n adjList[vet] = mutableListOf()\n }\n\n /* \u5220\u9664\u9876\u70b9 */\n fun removeVertex(vet: Vertex) {\n if (!adjList.containsKey(vet))\n throw IllegalArgumentException()\n // \u5728\u90bb\u63a5\u8868\u4e2d\u5220\u9664\u9876\u70b9 vet \u5bf9\u5e94\u7684\u94fe\u8868\n adjList.remove(vet);\n // \u904d\u5386\u5176\u4ed6\u9876\u70b9\u7684\u94fe\u8868\uff0c\u5220\u9664\u6240\u6709\u5305\u542b vet \u7684\u8fb9\n for (list in adjList.values) {\n list.remove(vet)\n }\n }\n\n /* \u6253\u5370\u90bb\u63a5\u8868 */\n fun print() {\n println(\"\u90bb\u63a5\u8868 =\")\n for (pair in adjList.entries) {\n val tmp = ArrayList<Int>()\n for (vertex in pair.value) {\n tmp.add(vertex.value)\n }\n println(\"${pair.key.value}: $tmp,\")\n }\n }\n}\n
graph_adjacency_list.rb[class]{GraphAdjList}-[func]{}\n
graph_adjacency_list.zig[class]{GraphAdjList}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_graph/graph_operations/#923-efficiency-comparison","title":"9.2.3 \u00a0 Efficiency comparison","text":"Assuming there are \\(n\\) vertices and \\(m\\) edges in the graph, the Table 9-2 compares the time efficiency and space efficiency of the adjacency matrix and adjacency list.
Table 9-2 \u00a0 Comparison of adjacency matrix and adjacency list
Adjacency matrix Adjacency list (Linked list) Adjacency list (Hash table) Determine adjacency \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) Add an edge \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Remove an edge \\(O(1)\\) \\(O(m)\\) \\(O(1)\\) Add a vertex \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) Remove a vertex \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) Memory space usage \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)Observing the Table 9-2 , it seems that the adjacency list (hash table) has the best time efficiency and space efficiency. However, in practice, operating on edges in the adjacency matrix is more efficient, requiring only a single array access or assignment operation. Overall, the adjacency matrix exemplifies the principle of \"space for time\", while the adjacency list exemplifies \"time for space\".
"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 Graph traversal","text":"Trees represent a \"one-to-many\" relationship, while graphs have a higher degree of freedom and can represent any \"many-to-many\" relationship. Therefore, we can consider trees as a special case of graphs. Clearly, tree traversal operations are also a special case of graph traversal operations.
Both graphs and trees require the application of search algorithms to implement traversal operations. Graph traversal can be divided into two types: \"Breadth-First Search (BFS)\" and \"Depth-First Search (DFS)\".
"},{"location":"chapter_graph/graph_traversal/#931-breadth-first-search","title":"9.3.1 \u00a0 Breadth-first search","text":"Breadth-first search is a near-to-far traversal method, starting from a certain node, always prioritizing the visit to the nearest vertices and expanding outwards layer by layer. As shown in the Figure 9-9 , starting from the top left vertex, first traverse all adjacent vertices of that vertex, then traverse all adjacent vertices of the next vertex, and so on, until all vertices have been visited.
Figure 9-9 \u00a0 Breadth-first traversal of a graph
"},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation","title":"1. \u00a0 Algorithm implementation","text":"BFS is usually implemented with the help of a queue, as shown in the code below. The queue has a \"first in, first out\" property, which aligns with the BFS idea of traversing \"from near to far\".
startVet
to the queue and start the loop.2.
until all vertices have been visited.To prevent revisiting vertices, we use a hash table visited
to record which nodes have been visited.
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]([start_vet])\n # \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n que = deque[Vertex]([start_vet])\n # \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while len(que) > 0:\n vet = que.popleft() # \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adj_vet in graph.adj_list[vet]:\n if adj_vet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.append(adj_vet) # \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adj_vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n
graph_bfs.cpp/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited = {startVet};\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n queue<Vertex *> que;\n que.push(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.empty()) {\n Vertex *vet = que.front();\n que.pop(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (auto adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.emplace(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.java/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new LinkedList<>();\n que.offer(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n Vertex vet = que.poll(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n que.offer(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.cs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [startVet];\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = new();\n que.Enqueue(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.Count > 0) {\n Vertex vet = que.Dequeue(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.Enqueue(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.Add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.go/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n visited[startVet] = struct{}{}\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS, \u4f7f\u7528\u5207\u7247\u6a21\u62df\u961f\u5217\n queue := make([]Vertex, 0)\n queue = append(queue, startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n for len(queue) > 0 {\n // \u961f\u9996\u9876\u70b9\u51fa\u961f\n vet := queue[0]\n queue = queue[1:]\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res = append(res, vet)\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n if !isExist {\n queue = append(queue, adjVet)\n visited[adjVet] = struct{}{}\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.swift/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = [startVet]\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n var que: [Vertex] = [startVet]\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.isEmpty {\n let vet = que.removeFirst() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.append(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.js/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.ts/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n const que = [startVet];\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.length) {\n const vet = que.shift(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet) ?? []) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.dart/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n // \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n visited.add(startVet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue<Vertex> que = Queue();\n que.add(startVet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (que.isNotEmpty) {\n Vertex vet = que.removeFirst(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.add(adjVet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res;\n}\n
graph_bfs.rs/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n visited.insert(start_vet);\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n let mut que = VecDeque::new();\n que.push_back(start_vet);\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while !que.is_empty() {\n let vet = que.pop_front().unwrap(); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n que.push_back(adj_vet); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.insert(adj_vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n res\n}\n
graph_bfs.c/* \u8282\u70b9\u961f\u5217\u7ed3\u6784\u4f53 */\ntypedef struct {\n Vertex *vertices[MAX_SIZE];\n int front, rear, size;\n} Queue;\n\n/* \u6784\u9020\u51fd\u6570 */\nQueue *newQueue() {\n Queue *q = (Queue *)malloc(sizeof(Queue));\n q->front = q->rear = q->size = 0;\n return q;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nint isEmpty(Queue *q) {\n return q->size == 0;\n}\n\n/* \u5165\u961f\u64cd\u4f5c */\nvoid enqueue(Queue *q, Vertex *vet) {\n q->vertices[q->rear] = vet;\n q->rear = (q->rear + 1) % MAX_SIZE;\n q->size++;\n}\n\n/* \u51fa\u961f\u64cd\u4f5c */\nVertex *dequeue(Queue *q) {\n Vertex *vet = q->vertices[q->front];\n q->front = (q->front + 1) % MAX_SIZE;\n q->size--;\n return vet;\n}\n\n/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (visited[i] == vet)\n return 1;\n }\n return 0;\n}\n\n/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n Queue *queue = newQueue();\n enqueue(queue, startVet);\n visited[(*visitedSize)++] = startVet;\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!isEmpty(queue)) {\n Vertex *vet = dequeue(queue); // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res[(*resSize)++] = vet; // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(visited, *visitedSize, node->vertex)) {\n enqueue(queue, node->vertex); // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited[(*visitedSize)++] = node->vertex; // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n node = node->next;\n }\n }\n // \u91ca\u653e\u5185\u5b58\n free(queue);\n}\n
graph_bfs.kt/* \u5e7f\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): List<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n val res: MutableList<Vertex> = ArrayList()\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n val visited: MutableSet<Vertex> = HashSet()\n visited.add(startVet)\n // \u961f\u5217\u7528\u4e8e\u5b9e\u73b0 BFS\n val que: Queue<Vertex> = LinkedList()\n que.offer(startVet)\n // \u4ee5\u9876\u70b9 vet \u4e3a\u8d77\u70b9\uff0c\u5faa\u73af\u76f4\u81f3\u8bbf\u95ee\u5b8c\u6240\u6709\u9876\u70b9\n while (!que.isEmpty()) {\n val vet = que.poll() // \u961f\u9996\u9876\u70b9\u51fa\u961f\n res.add(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet)) continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n\n que.offer(adjVet) // \u53ea\u5165\u961f\u672a\u8bbf\u95ee\u7684\u9876\u70b9\n visited.add(adjVet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n }\n }\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_bfs.rb[class]{}-[func]{graph_bfs}\n
graph_bfs.zig[class]{}-[func]{graphBFS}\n
Code Visualization Full Screen >
The code is relatively abstract, it is suggested to compare with the following figure to deepen the understanding.
<1><2><3><4><5><6><7><8><9><10><11>Figure 9-10 \u00a0 Steps of breadth-first search of a graph
Is the sequence of breadth-first traversal unique?
Not unique. Breadth-first traversal only requires traversing in a \"from near to far\" order, and the traversal order of multiple vertices at the same distance can be arbitrarily shuffled. For example, in the above figure, the visitation order of vertices \\(1\\) and \\(3\\) can be switched, as can the order of vertices \\(2\\), \\(4\\), and \\(6\\).
"},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity analysis","text":"Time complexity: All vertices will be enqueued and dequeued once, using \\(O(|V|)\\) time; in the process of traversing adjacent vertices, since it is an undirected graph, all edges will be visited \\(2\\) times, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.
Space complexity: The maximum number of vertices in list res
, hash table visited
, and queue que
is \\(|V|\\), using \\(O(|V|)\\) space.
Depth-first search is a traversal method that prioritizes going as far as possible and then backtracks when no further paths are available. As shown in the Figure 9-11 , starting from the top left vertex, visit some adjacent vertex of the current vertex until no further path is available, then return and continue until all vertices are traversed.
Figure 9-11 \u00a0 Depth-first traversal of a graph
"},{"location":"chapter_graph/graph_traversal/#1-algorithm-implementation_1","title":"1. \u00a0 Algorithm implementation","text":"This \"go as far as possible and then return\" algorithm paradigm is usually implemented based on recursion. Similar to breadth-first search, in depth-first search, we also need the help of a hash table visited
to record the visited vertices to avoid revisiting.
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570\"\"\"\n res.append(vet) # \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) # \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n # \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adj_list[vet]:\n if adjVet in visited:\n continue # \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n # \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n # \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n # \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res = []\n # \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited = set[Vertex]()\n dfs(graph, visited, res, start_vet)\n return res\n
graph_dfs.cpp/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n res.push_back(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.emplace(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex *adjVet : graph.adjList[vet]) {\n if (visited.count(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n vector<Vertex *> res;\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n unordered_set<Vertex *> visited;\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.java/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet : graph.adjList.get(vet)) {\n if (visited.contains(adjVet))\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = new ArrayList<>();\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = new HashSet<>();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.cs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n res.Add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.Add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n foreach (Vertex adjVet in graph.adjList[vet]) {\n if (visited.Contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9 \n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n DFS(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n HashSet<Vertex> visited = [];\n DFS(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.go/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n // append \u64cd\u4f5c\u4f1a\u8fd4\u56de\u65b0\u7684\u7684\u5f15\u7528\uff0c\u5fc5\u987b\u8ba9\u539f\u5f15\u7528\u91cd\u65b0\u8d4b\u503c\u4e3a\u65b0slice\u7684\u5f15\u7528\n *res = append(*res, vet)\n visited[vet] = struct{}{}\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for _, adjVet := range g.adjList[vet] {\n _, isExist := visited[adjVet]\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n if !isExist {\n dfs(g, visited, res, adjVet)\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n res := make([]Vertex, 0)\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n visited := make(map[Vertex]struct{})\n dfs(g, visited, &res, startVet)\n // \u8fd4\u56de\u9876\u70b9\u904d\u5386\u5e8f\u5217\n return res\n}\n
graph_dfs.swift/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n res.append(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for adjVet in graph.adjList[vet] ?? [] {\n if visited.contains(adjVet) {\n continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n var res: [Vertex] = []\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n var visited: Set<Vertex> = []\n dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n return res\n}\n
graph_dfs.js/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction dfs(graph, visited, res, vet) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph, startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.ts/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfunction dfs(\n graph: GraphAdjList,\n visited: Set<Vertex>,\n res: Vertex[],\n vet: Vertex\n): void {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (const adjVet of graph.adjList.get(vet)) {\n if (visited.has(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n const res: Vertex[] = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n const visited: Set<Vertex> = new Set();\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.dart/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(\n GraphAdjList graph,\n Set<Vertex> visited,\n List<Vertex> res,\n Vertex vet,\n) {\n res.add(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (Vertex adjVet in graph.adjList[vet]!) {\n if (visited.contains(adjVet)) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet);\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n List<Vertex> res = [];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n Set<Vertex> visited = {};\n dfs(graph, visited, res, startVet);\n return res;\n}\n
graph_dfs.rs/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n res.push(vet); // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.insert(vet); // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n if let Some(adj_vets) = graph.adj_list.get(&vet) {\n for &adj_vet in adj_vets {\n if visited.contains(&adj_vet) {\n continue; // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n }\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adj_vet);\n }\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n let mut res = vec![];\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n let mut visited = HashSet::new();\n dfs(&graph, &mut visited, &mut res, start_vet);\n\n res\n}\n
graph_dfs.c/* \u68c0\u67e5\u9876\u70b9\u662f\u5426\u5df2\u88ab\u8bbf\u95ee */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n // \u904d\u5386\u67e5\u627e\u8282\u70b9\uff0c\u4f7f\u7528 O(n) \u65f6\u95f4\n for (int i = 0; i < size; i++) {\n if (res[i] == vet) {\n return 1;\n }\n }\n return 0;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n res[(*resSize)++] = vet;\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n AdjListNode *node = findNode(graph, vet);\n while (node != NULL) {\n // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n if (!isVisited(res, *resSize, node->vertex)) {\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, res, resSize, node->vertex);\n }\n node = node->next;\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n dfs(graph, res, resSize, startVet);\n}\n
graph_dfs.kt/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386\u8f85\u52a9\u51fd\u6570 */\nfun dfs(\n graph: GraphAdjList,\n visited: MutableSet<Vertex?>,\n res: MutableList<Vertex?>,\n vet: Vertex?\n) {\n res.add(vet) // \u8bb0\u5f55\u8bbf\u95ee\u9876\u70b9\n visited.add(vet) // \u6807\u8bb0\u8be5\u9876\u70b9\u5df2\u88ab\u8bbf\u95ee\n // \u904d\u5386\u8be5\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\n for (adjVet in graph.adjList[vet]!!) {\n if (visited.contains(adjVet)) continue // \u8df3\u8fc7\u5df2\u88ab\u8bbf\u95ee\u7684\u9876\u70b9\n // \u9012\u5f52\u8bbf\u95ee\u90bb\u63a5\u9876\u70b9\n dfs(graph, visited, res, adjVet)\n }\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n// \u4f7f\u7528\u90bb\u63a5\u8868\u6765\u8868\u793a\u56fe\uff0c\u4ee5\u4fbf\u83b7\u53d6\u6307\u5b9a\u9876\u70b9\u7684\u6240\u6709\u90bb\u63a5\u9876\u70b9\nfun graphDFS(\n graph: GraphAdjList,\n startVet: Vertex?\n): List<Vertex?> {\n // \u9876\u70b9\u904d\u5386\u5e8f\u5217\n val res: MutableList<Vertex?> = ArrayList()\n // \u54c8\u5e0c\u8868\uff0c\u7528\u4e8e\u8bb0\u5f55\u5df2\u88ab\u8bbf\u95ee\u8fc7\u7684\u9876\u70b9\n val visited: MutableSet<Vertex?> = HashSet()\n dfs(graph, visited, res, startVet)\n return res\n}\n
graph_dfs.rb[class]{}-[func]{dfs}\n\n[class]{}-[func]{graph_dfs}\n
graph_dfs.zig[class]{}-[func]{dfs}\n\n[class]{}-[func]{graphDFS}\n
Code Visualization Full Screen >
The algorithm process of depth-first search is shown in the following figure.
To deepen the understanding, it is suggested to combine the following figure with the code to simulate (or draw) the entire DFS process in your mind, including when each recursive method is initiated and when it returns.
<1><2><3><4><5><6><7><8><9><10><11>Figure 9-12 \u00a0 Steps of depth-first search of a graph
Is the sequence of depth-first traversal unique?
Similar to breadth-first traversal, the order of the depth-first traversal sequence is also not unique. Given a certain vertex, exploring in any direction first is possible, that is, the order of adjacent vertices can be arbitrarily shuffled, all being part of depth-first traversal.
Taking tree traversal as an example, \"root \\(\\rightarrow\\) left \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\", \"left \\(\\rightarrow\\) right \\(\\rightarrow\\) root\" correspond to preorder, inorder, and postorder traversals, respectively. They showcase three types of traversal priorities, yet all three are considered depth-first traversal.
"},{"location":"chapter_graph/graph_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity analysis","text":"Time complexity: All vertices will be visited once, using \\(O(|V|)\\) time; all edges will be visited twice, using \\(O(2|E|)\\) time; overall using \\(O(|V| + |E|)\\) time.
Space complexity: The maximum number of vertices in list res
, hash table visited
is \\(|V|\\), and the maximum recursion depth is \\(|V|\\), therefore using \\(O(|V|)\\) space.
Q: Is a path defined as a sequence of vertices or a sequence of edges?
Definitions vary between different language versions on Wikipedia: the English version defines a path as \"a sequence of edges,\" while the Chinese version defines it as \"a sequence of vertices.\" Here is the original text from the English version: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.
In this document, a path is considered a sequence of edges, rather than a sequence of vertices. This is because there might be multiple edges connecting two vertices, in which case each edge corresponds to a path.
Q: In a disconnected graph, are there points that cannot be traversed to?
In a disconnected graph, starting from a certain vertex, there is at least one vertex that cannot be reached. Traversing a disconnected graph requires setting multiple starting points to traverse all connected components of the graph.
Q: In an adjacency list, does the order of \"all vertices connected to that vertex\" matter?
It can be in any order. However, in practical applications, it might be necessary to sort according to certain rules, such as the order in which vertices are added, or the order of vertex values, etc., to facilitate the quick search for vertices with certain extremal values.
"},{"location":"chapter_hashing/","title":"Chapter 6. \u00a0 Hash table","text":"Abstract
In the world of computing, a hash table is akin to an intelligent librarian.
It understands how to compute index numbers, enabling swift retrieval of the desired book.
"},{"location":"chapter_hashing/#chapter-contents","title":"Chapter contents","text":"The previous two sections introduced the working principle of hash tables and the methods to handle hash collisions. However, both open addressing and chaining can only ensure that the hash table functions normally when collisions occur, but cannot reduce the frequency of hash collisions.
If hash collisions occur too frequently, the performance of the hash table will deteriorate drastically. As shown in the Figure 6-8 , for a chaining hash table, in the ideal case, the key-value pairs are evenly distributed across the buckets, achieving optimal query efficiency; in the worst case, all key-value pairs are stored in the same bucket, degrading the time complexity to \\(O(n)\\).
Figure 6-8 \u00a0 Ideal and worst cases of hash collisions
The distribution of key-value pairs is determined by the hash function. Recalling the steps of calculating a hash function, first compute the hash value, then modulo it by the array length:
index = hash(key) % capacity\n
Observing the above formula, when the hash table capacity capacity
is fixed, the hash algorithm hash()
determines the output value, thereby determining the distribution of key-value pairs in the hash table.
This means that, to reduce the probability of hash collisions, we should focus on the design of the hash algorithm hash()
.
To achieve a \"fast and stable\" hash table data structure, hash algorithms should have the following characteristics:
In fact, hash algorithms are not only used to implement hash tables but are also widely applied in other fields.
For cryptographic applications, to prevent reverse engineering such as deducing the original password from the hash value, hash algorithms need higher-level security features.
Note that \"Uniform Distribution\" and \"Collision Resistance\" are two separate concepts. Satisfying uniform distribution does not necessarily mean collision resistance. For example, under random input key
, the hash function key % 100
can produce a uniformly distributed output. However, this hash algorithm is too simple, and all key
with the same last two digits will have the same output, making it easy to deduce a usable key
from the hash value, thereby cracking the password.
The design of hash algorithms is a complex issue that requires consideration of many factors. However, for some less demanding scenarios, we can also design some simple hash algorithms.
def add_hash(key: str) -> int:\n \"\"\"\u52a0\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash += ord(c)\n return hash % modulus\n\ndef mul_hash(key: str) -> int:\n \"\"\"\u4e58\u6cd5\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = 31 * hash + ord(c)\n return hash % modulus\n\ndef xor_hash(key: str) -> int:\n \"\"\"\u5f02\u6216\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash ^= ord(c)\n return hash % modulus\n\ndef rot_hash(key: str) -> int:\n \"\"\"\u65cb\u8f6c\u54c8\u5e0c\"\"\"\n hash = 0\n modulus = 1000000007\n for c in key:\n hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n return hash % modulus\n
simple_hash.cpp/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = (31 * hash + (int)c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash ^= (int)c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(string key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (unsigned char c : key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.java/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = (31 * hash + (int) c) % MODULUS;\n }\n return (int) hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash ^= (int) c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n long hash = 0;\n final int MODULUS = 1000000007;\n for (char c : key.toCharArray()) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n }\n return (int) hash;\n}\n
simple_hash.cs/* \u52a0\u6cd5\u54c8\u5e0c */\nint AddHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint MulHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = (31 * hash + c) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint XorHash(string key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash ^= c;\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint RotHash(string key) {\n long hash = 0;\n const int MODULUS = 1000000007;\n foreach (char c in key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n }\n return (int)hash;\n}\n
simple_hash.go/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = (31*hash + int64(b)) % modulus\n }\n return int(hash)\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key string) int {\n hash := 0\n modulus := 1000000007\n for _, b := range []byte(key) {\n fmt.Println(int(b))\n hash ^= int(b)\n hash = (31*hash + int(b)) % modulus\n }\n return hash & modulus\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key string) int {\n var hash int64\n var modulus int64\n\n modulus = 1000000007\n for _, b := range []byte(key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n }\n return int(hash)\n}\n
simple_hash.swift/* \u52a0\u6cd5\u54c8\u5e0c */\nfunc addHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunc mulHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = (31 * hash + Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunc xorHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash ^= Int(scalar.value)\n }\n }\n return hash & MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunc rotHash(key: String) -> Int {\n var hash = 0\n let MODULUS = 1_000_000_007\n for c in key {\n for scalar in c.unicodeScalars {\n hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n }\n }\n return hash\n}\n
simple_hash.js/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key) {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.ts/* \u52a0\u6cd5\u54c8\u5e0c */\nfunction addHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfunction mulHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfunction xorHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash ^= c.charCodeAt(0);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfunction rotHash(key: string): number {\n let hash = 0;\n const MODULUS = 1000000007;\n for (const c of key) {\n hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.dart/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash ^= key.codeUnitAt(i);\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(String key) {\n int hash = 0;\n final int MODULUS = 1000000007;\n for (int i = 0; i < key.length; i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n }\n return hash;\n}\n
simple_hash.rs/* \u52a0\u6cd5\u54c8\u5e0c */\nfn add_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfn mul_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = (31 * hash + c as i64) % MODULUS;\n }\n\n hash as i32\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfn xor_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash ^= c as i64;\n }\n\n (hash & MODULUS) as i32\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfn rot_hash(key: &str) -> i32 {\n let mut hash = 0_i64;\n const MODULUS: i64 = 1000000007;\n\n for c in key.chars() {\n hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n }\n\n hash as i32\n}\n
simple_hash.c/* \u52a0\u6cd5\u54c8\u5e0c */\nint addHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nint mulHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n }\n return (int)hash;\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nint xorHash(char *key) {\n int hash = 0;\n const int MODULUS = 1000000007;\n\n for (int i = 0; i < strlen(key); i++) {\n hash ^= (unsigned char)key[i];\n }\n return hash & MODULUS;\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nint rotHash(char *key) {\n long long hash = 0;\n const int MODULUS = 1000000007;\n for (int i = 0; i < strlen(key); i++) {\n hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n }\n\n return (int)hash;\n}\n
simple_hash.kt/* \u52a0\u6cd5\u54c8\u5e0c */\nfun addHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = (hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u4e58\u6cd5\u54c8\u5e0c */\nfun mulHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = (31 * hash + c.code) % MODULUS\n }\n return hash.toInt()\n}\n\n/* \u5f02\u6216\u54c8\u5e0c */\nfun xorHash(key: String): Int {\n var hash = 0\n for (c in key.toCharArray()) {\n hash = hash xor c.code\n }\n return hash and MODULUS\n}\n\n/* \u65cb\u8f6c\u54c8\u5e0c */\nfun rotHash(key: String): Int {\n var hash = 0L\n for (c in key.toCharArray()) {\n hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n }\n return hash.toInt()\n}\n
simple_hash.rb[class]{}-[func]{add_hash}\n\n[class]{}-[func]{mul_hash}\n\n[class]{}-[func]{xor_hash}\n\n[class]{}-[func]{rot_hash}\n
simple_hash.zig[class]{}-[func]{addHash}\n\n[class]{}-[func]{mulHash}\n\n[class]{}-[func]{xorHash}\n\n[class]{}-[func]{rotHash}\n
Code Visualization Full Screen >
It is observed that the last step of each hash algorithm is to take the modulus of the large prime number \\(1000000007\\) to ensure that the hash value is within an appropriate range. It is worth pondering why emphasis is placed on modulo a prime number, or what are the disadvantages of modulo a composite number? This is an interesting question.
To conclude: Using a large prime number as the modulus can maximize the uniform distribution of hash values. Since a prime number does not share common factors with other numbers, it can reduce the periodic patterns caused by the modulo operation, thus avoiding hash collisions.
For example, suppose we choose the composite number \\(9\\) as the modulus, which can be divided by \\(3\\), then all key
divisible by \\(3\\) will be mapped to hash values \\(0\\), \\(3\\), \\(6\\).
If the input key
happens to have this kind of arithmetic sequence distribution, then the hash values will cluster, thereby exacerbating hash collisions. Now, suppose we replace modulus
with the prime number \\(13\\), since there are no common factors between key
and modulus
, the uniformity of the output hash values will be significantly improved.
It is worth noting that if the key
is guaranteed to be randomly and uniformly distributed, then choosing a prime number or a composite number as the modulus can both produce uniformly distributed hash values. However, when the distribution of key
has some periodicity, modulo a composite number is more likely to result in clustering.
In summary, we usually choose a prime number as the modulus, and this prime number should be large enough to eliminate periodic patterns as much as possible, enhancing the robustness of the hash algorithm.
"},{"location":"chapter_hashing/hash_algorithm/#633-common-hash-algorithms","title":"6.3.3 \u00a0 Common hash algorithms","text":"It is not hard to see that the simple hash algorithms mentioned above are quite \"fragile\" and far from reaching the design goals of hash algorithms. For example, since addition and XOR obey the commutative law, additive hash and XOR hash cannot distinguish strings with the same content but in different order, which may exacerbate hash collisions and cause security issues.
In practice, we usually use some standard hash algorithms, such as MD5, SHA-1, SHA-2, and SHA-3. They can map input data of any length to a fixed-length hash value.
Over the past century, hash algorithms have been in a continuous process of upgrading and optimization. Some researchers strive to improve the performance of hash algorithms, while others, including hackers, are dedicated to finding security issues in hash algorithms. The Table 6-2 shows hash algorithms commonly used in practical applications.
Table 6-2 \u00a0 Common hash algorithms
MD5 SHA-1 SHA-2 SHA-3 Release Year 1992 1995 2002 2008 Output Length 128 bit 160 bit 256/512 bit 224/256/384/512 bit Hash Collisions Frequent Frequent Rare Rare Security Level Low, has been successfully attacked Low, has been successfully attacked High High Applications Abandoned, still used for data integrity checks Abandoned Cryptocurrency transaction verification, digital signatures, etc. Can be used to replace SHA-2"},{"location":"chapter_hashing/hash_algorithm/#hash-values-in-data-structures","title":"Hash values in data structures","text":"We know that the keys in a hash table can be of various data types such as integers, decimals, or strings. Programming languages usually provide built-in hash algorithms for these data types to calculate the bucket indices in the hash table. Taking Python as an example, we can use the hash()
function to compute the hash values for various data types.
Tip
Be aware that the definition and methods of the built-in hash value calculation functions in different programming languages vary.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig built_in_hash.pynum = 3\nhash_num = hash(num)\n# Hash value of integer 3 is 3\n\nbol = True\nhash_bol = hash(bol)\n# Hash value of boolean True is 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# Hash value of decimal 3.14159 is 326484311674566659\n\nstr = \"Hello \u7b97\u6cd5\"\nhash_str = hash(str)\n# Hash value of string \"Hello \u7b97\u6cd5\" is 4617003410720528961\n\ntup = (12836, \"\u5c0f\u54c8\")\nhash_tup = hash(tup)\n# Hash value of tuple (12836, '\u5c0f\u54c8') is 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# Hash value of ListNode object at 0x1058fd810 is 274267521\n
built_in_hash.cppint num = 3;\nsize_t hashNum = hash<int>()(num);\n// Hash value of integer 3 is 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// Hash value of boolean 1 is 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// Hash value of decimal 3.14159 is 4614256650576692846\n\nstring str = \"Hello \u7b97\u6cd5\";\nsize_t hashStr = hash<string>()(str);\n// Hash value of string \"Hello \u7b97\u6cd5\" is 15466937326284535026\n\n// In C++, built-in std::hash() only provides hash values for basic data types\n// Hash values for arrays and objects need to be implemented separately\n
built_in_hash.javaint num = 3;\nint hashNum = Integer.hashCode(num);\n// Hash value of integer 3 is 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// Hash value of decimal 3.14159 is -1340954729\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -727081396\n\nObject[] arr = { 12836, \"\u5c0f\u54c8\" };\nint hashTup = Arrays.hashCode(arr);\n// Hash value of array [12836, \u5c0f\u54c8] is 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// Hash value of ListNode object utils.ListNode@7dc5e7b4 is 2110121908\n
built_in_hash.csint num = 3;\nint hashNum = num.GetHashCode();\n// Hash value of integer 3 is 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// Hash value of boolean true is 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// Hash value of decimal 3.14159 is -1340954729;\n\nstring str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.GetHashCode();\n// Hash value of string \"Hello \u7b97\u6cd5\" is -586107568;\n\nobject[] arr = [12836, \"\u5c0f\u54c8\"];\nint hashTup = arr.GetHashCode();\n// Hash value of array [12836, \u5c0f\u54c8] is 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// Hash value of ListNode object 0 is 39053774;\n
built_in_hash.go// Go does not provide built-in hash code functions\n
built_in_hash.swiftlet num = 3\nlet hashNum = num.hashValue\n// Hash value of integer 3 is 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// Hash value of boolean true is -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// Hash value of decimal 3.14159 is -2465384235396674631\n\nlet str = \"Hello \u7b97\u6cd5\"\nlet hashStr = str.hashValue\n// Hash value of string \"Hello \u7b97\u6cd5\" is -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")]\nlet hashTup = arr.hashValue\n// Hash value of array [AnyHashable(12836), AnyHashable(\"\u5c0f\u54c8\")] is -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// Hash value of ListNode object utils.ListNode is -2434780518035996159\n
built_in_hash.js// JavaScript does not provide built-in hash code functions\n
built_in_hash.ts// TypeScript does not provide built-in hash code functions\n
built_in_hash.dartint num = 3;\nint hashNum = num.hashCode;\n// Hash value of integer 3 is 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// Hash value of boolean true is 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// Hash value of decimal 3.14159 is 2570631074981783\n\nString str = \"Hello \u7b97\u6cd5\";\nint hashStr = str.hashCode;\n// Hash value of string \"Hello \u7b97\u6cd5\" is 468167534\n\nList arr = [12836, \"\u5c0f\u54c8\"];\nint hashArr = arr.hashCode;\n// Hash value of array [12836, \u5c0f\u54c8] is 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// Hash value of ListNode object Instance of 'ListNode' is 1033450432\n
built_in_hash.rsuse std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// Hash value of integer 3 is 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// Hash value of boolean true is 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// Hash value of decimal 3.14159 is 2566941990314602357\n\nlet str = \"Hello \u7b97\u6cd5\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// Hash value of string \"Hello \u7b97\u6cd5\" is 16092673739211250988\n\nlet arr = (&12836, &\"\u5c0f\u54c8\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// Hash value of tuple (12836, \"\u5c0f\u54c8\") is 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// Hash value of ListNode object RefCell { value: ListNode { val: 42, next: None } } is 15387811073369036852\n
built_in_hash.c// C does not provide built-in hash code functions\n
built_in_hash.kt\n
built_in_hash.zig\n
Code Visualization Full Screen >
In many programming languages, only immutable objects can serve as the key
in a hash table. If we use a list (dynamic array) as a key
, when the contents of the list change, its hash value also changes, and we would no longer be able to find the original value
in the hash table.
Although the member variables of a custom object (such as a linked list node) are mutable, it is hashable. This is because the hash value of an object is usually generated based on its memory address, and even if the contents of the object change, the memory address remains the same, so the hash value remains unchanged.
You might have noticed that the hash values output in different consoles are different. This is because the Python interpreter adds a random salt to the string hash function each time it starts up. This approach effectively prevents HashDoS attacks and enhances the security of the hash algorithm.
"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 Hash collision","text":"As mentioned in the previous section, usually the input space of a hash function is much larger than its output space, making hash collisions theoretically inevitable. For example, if the input space consists of all integers and the output space is the size of the array capacity, multiple integers will inevitably map to the same bucket index.
Hash collisions can lead to incorrect query results, severely affecting the usability of hash tables. To solve this problem, we expand the hash table whenever a hash collision occurs, until the collision is resolved. This method is simple and effective but inefficient due to the extensive data transfer and hash value computation involved in resizing the hash table. To improve efficiency, we can adopt the following strategies:
There are mainly two methods for improving the structure of hash tables: \"Separate Chaining\" and \"Open Addressing\".
"},{"location":"chapter_hashing/hash_collision/#621-separate-chaining","title":"6.2.1 \u00a0 Separate chaining","text":"In the original hash table, each bucket can store only one key-value pair. \"Separate chaining\" transforms individual elements into a linked list, with key-value pairs as list nodes, storing all colliding key-value pairs in the same list. The Figure 6-5 shows an example of a hash table with separate chaining.
Figure 6-5 \u00a0 Separate chaining hash table
The operations of a hash table implemented with separate chaining have changed as follows:
key
, pass through the hash function to obtain the bucket index, access the head node of the list, then traverse the list and compare key
to find the target key-value pair.Separate chaining has the following limitations:
The code below provides a simple implementation of a separate chaining hash table, with two things to note:
class HashMapChaining:\n \"\"\"\u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets = [[] for _ in range(self.capacity)] # \u6876\u6570\u7ec4\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def get(self, key: int) -> str | None:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket:\n if pair.key == key:\n return pair.val\n # \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket:\n if pair.key == key:\n pair.val = val\n return\n # \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n pair = Pair(key, val)\n bucket.append(pair)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index = self.hash_func(key)\n bucket = self.buckets[index]\n # \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for pair in bucket:\n if pair.key == key:\n bucket.remove(pair)\n self.size -= 1\n break\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [[] for _ in range(self.capacity)]\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets:\n for pair in bucket:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for bucket in self.buckets:\n res = []\n for pair in bucket:\n res.append(str(pair.key) + \" -> \" + pair.val)\n print(res)\n
hash_map_chaining.cpp/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n vector<vector<Pair *>> buckets; // \u6876\u6570\u7ec4\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n buckets.resize(capacity);\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapChaining() {\n for (auto &bucket : buckets) {\n for (Pair *pair : bucket) {\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / (double)capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n return pair->val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair *pair : buckets[index]) {\n if (pair->key == key) {\n pair->val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].push_back(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n auto &bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (int i = 0; i < bucket.size(); i++) {\n if (bucket[i]->key == key) {\n Pair *tmp = bucket[i];\n bucket.erase(bucket.begin() + i); // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n delete tmp; // \u91ca\u653e\u5185\u5b58\n size--;\n return;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<vector<Pair *>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets.clear();\n buckets.resize(capacity);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (auto &bucket : bucketsTmp) {\n for (Pair *pair : bucket) {\n put(pair->key, pair->val);\n // \u91ca\u653e\u5185\u5b58\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (auto &bucket : buckets) {\n cout << \"[\";\n for (Pair *pair : bucket) {\n cout << pair->key << \" -> \" << pair->val << \", \";\n }\n cout << \"]\\n\";\n }\n }\n};\n
hash_map_chaining.java/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair : bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair : bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = new Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets.get(index);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair : bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new ArrayList<>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.add(new ArrayList<>());\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket : bucketsTmp) {\n for (Pair pair : bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (List<Pair> bucket : buckets) {\n List<String> res = new ArrayList<>();\n for (Pair pair : bucket) {\n res.add(pair.key + \" -> \" + pair.val);\n }\n System.out.println(res);\n }\n }\n}\n
hash_map_chaining.cs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n foreach (Pair pair in buckets[index]) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n buckets[index].Add(new Pair(key, val));\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n foreach (Pair pair in buckets[index].ToList()) {\n if (pair.key == key) {\n buckets[index].Remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new List<List<Pair>>(capacity);\n for (int i = 0; i < capacity; i++) {\n buckets.Add([]);\n }\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (List<Pair> bucket in bucketsTmp) {\n foreach (Pair pair in bucket) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (List<Pair> bucket in buckets) {\n List<string> res = [];\n foreach (Pair pair in bucket) {\n res.Add(pair.key + \" -> \" + pair.val);\n }\n foreach (string kv in res) {\n Console.WriteLine(kv);\n }\n }\n }\n}\n
hash_map_chaining.go/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntype hashMapChaining struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets [][]pair // \u6876\u6570\u7ec4\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapChaining() *hashMapChaining {\n buckets := make([][]pair, 4)\n for i := 0; i < 4; i++ {\n buckets[i] = make([]pair, 0)\n }\n return &hashMapChaining{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: buckets,\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n return key % m.capacity\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n return float64(m.size) / float64(m.capacity)\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (m *hashMapChaining) get(key int) string {\n idx := m.hashFunc(key)\n bucket := m.buckets[idx]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for _, p := range bucket {\n if p.key == key {\n return p.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (m *hashMapChaining) put(key int, val string) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if m.loadFactor() > m.loadThres {\n m.extend()\n }\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for i := range m.buckets[idx] {\n if m.buckets[idx][i].key == key {\n m.buckets[idx][i].val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n p := pair{\n key: key,\n val: val,\n }\n m.buckets[idx] = append(m.buckets[idx], p)\n m.size += 1\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (m *hashMapChaining) remove(key int) {\n idx := m.hashFunc(key)\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i, p := range m.buckets[idx] {\n if p.key == key {\n // \u5207\u7247\u5220\u9664\n m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n m.size -= 1\n break\n }\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n tmpBuckets := make([][]pair, len(m.buckets))\n for i := 0; i < len(m.buckets); i++ {\n tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n copy(tmpBuckets[i], m.buckets[i])\n }\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n m.capacity *= m.extendRatio\n m.buckets = make([][]pair, m.capacity)\n for i := 0; i < m.capacity; i++ {\n m.buckets[i] = make([]pair, 0)\n }\n m.size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, bucket := range tmpBuckets {\n for _, p := range bucket {\n m.put(p.key, p.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (m *hashMapChaining) print() {\n var builder strings.Builder\n\n for _, bucket := range m.buckets {\n builder.WriteString(\"[\")\n for _, p := range bucket {\n builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n }\n builder.WriteString(\"]\")\n fmt.Println(builder.String())\n builder.Reset()\n }\n}\n
hash_map_chaining.swift/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [[Pair]] // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: [], count: capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size) / Double(capacity)\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return pair.val\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de nil\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair(key: key, val: val)\n buckets[index].append(pair)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n let bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pairIndex, pair) in bucket.enumerated() {\n if pair.key == key {\n buckets[index].remove(at: pairIndex)\n size -= 1\n break\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: [], count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in bucketsTmp {\n for pair in bucket {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for bucket in buckets {\n let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n Swift.print(res)\n }\n }\n}\n
hash_map_chaining.js/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.ts/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n #size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio: number; // \u6269\u5bb9\u500d\u6570\n #buckets: Pair[][]; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0;\n this.#capacity = 4;\n this.#loadThres = 2.0 / 3.0;\n this.#extendRatio = 2;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key: number): number {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor(): number {\n return this.#size / this.#capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (const pair of bucket) {\n if (pair.key === key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n const index = this.#hashFunc(key);\n const bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (const pair of bucket) {\n if (pair.key === key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n const pair = new Pair(key, val);\n bucket.push(pair);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n const index = this.#hashFunc(key);\n let bucket = this.#buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (let i = 0; i < bucket.length; i++) {\n if (bucket[i].key === key) {\n bucket.splice(i, 1);\n this.#size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const bucket of bucketsTmp) {\n for (const pair of bucket) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const bucket of this.#buckets) {\n let res = [];\n for (const pair of bucket) {\n res.push(pair.key + ' -> ' + pair.val);\n }\n console.log(res);\n }\n }\n}\n
hash_map_chaining.dart/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining {\n late int size; // \u952e\u503c\u5bf9\u6570\u91cf\n late int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n late double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n late int extendRatio; // \u6269\u5bb9\u500d\u6570\n late List<List<Pair>> buckets; // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapChaining() {\n size = 0;\n capacity = 4;\n loadThres = 2.0 / 3.0;\n extendRatio = 2;\n buckets = List.generate(capacity, (_) => []);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return size / capacity;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (Pair pair in bucket) {\n if (pair.key == key) {\n return pair.val;\n }\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (Pair pair in bucket) {\n if (pair.key == key) {\n pair.val = val;\n return;\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n Pair pair = Pair(key, val);\n bucket.add(pair);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n List<Pair> bucket = buckets[index];\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (Pair pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair);\n size--;\n break;\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<List<Pair>> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = List.generate(capacity, (_) => []);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (List<Pair> bucket in bucketsTmp) {\n for (Pair pair in bucket) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (List<Pair> bucket in buckets) {\n List<String> res = [];\n for (Pair pair in bucket) {\n res.add(\"${pair.key} -> ${pair.val}\");\n }\n print(res);\n }\n }\n}\n
hash_map_chaining.rs/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapChaining {\n size: i32,\n capacity: i32,\n load_thres: f32,\n extend_ratio: i32,\n buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![vec![]; 4],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % self.capacity as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f32 {\n self.size as f32 / self.capacity as f32\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) -> Option<String> {\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for i in 0..bucket.len() {\n if bucket[i].key == key {\n let pair = bucket.remove(i);\n self.size -= 1;\n return Some(pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n None\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);\n\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![Vec::new(); self.capacity as usize];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for bucket in buckets_tmp {\n for pair in bucket {\n self.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for bucket in &self.buckets {\n let mut res = Vec::new();\n for pair in bucket {\n res.push(format!(\"{} -> {}\", pair.key, pair.val));\n }\n println!(\"{:?}\", res);\n }\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n\n let index = self.hash_func(key);\n let bucket = &mut self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for pair in bucket {\n if pair.key == key {\n pair.val = val.clone();\n return;\n }\n }\n let bucket = &mut self.buckets[index];\n\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n let pair = Pair {\n key,\n val: val.clone(),\n };\n bucket.push(pair);\n self.size += 1;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&self, key: i32) -> Option<&str> {\n let index = self.hash_func(key);\n let bucket = &self.buckets[index];\n\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for pair in bucket {\n if pair.key == key {\n return Some(&pair.val);\n }\n }\n\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de None\n None\n }\n}\n
hash_map_chaining.c/* \u94fe\u8868\u8282\u70b9 */\ntypedef struct Node {\n Pair *pair;\n struct Node *next;\n} Node;\n\n/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Node **buckets; // \u6876\u6570\u7ec4\n} HashMapChaining;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapChaining *newHashMapChaining() {\n HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n while (cur) {\n Node *tmp = cur;\n cur = cur->next;\n free(tmp->pair);\n free(tmp);\n }\n }\n free(hashMap->buckets);\n free(hashMap);\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapChaining *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapChaining *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n return cur->pair->val;\n }\n cur = cur->next;\n }\n return \"\"; // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n int index = hashFunc(hashMap, key);\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n Node *cur = hashMap->buckets[index];\n while (cur) {\n if (cur->pair->key == key) {\n strcpy(cur->pair->val, val); // \u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n return;\n }\n cur = cur->next;\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Pair *newPair = (Pair *)malloc(sizeof(Pair));\n newPair->key = key;\n strcpy(newPair->val, val);\n Node *newNode = (Node *)malloc(sizeof(Node));\n newNode->pair = newPair;\n newNode->next = hashMap->buckets[index];\n hashMap->buckets[index] = newNode;\n hashMap->size++;\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapChaining *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n int oldCapacity = hashMap->capacity;\n Node **oldBuckets = hashMap->buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n for (int i = 0; i < hashMap->capacity; i++) {\n hashMap->buckets[i] = NULL;\n }\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Node *cur = oldBuckets[i];\n while (cur) {\n put(hashMap, cur->pair->key, cur->pair->val);\n Node *temp = cur;\n cur = cur->next;\n // \u91ca\u653e\u5185\u5b58\n free(temp->pair);\n free(temp);\n }\n }\n\n free(oldBuckets);\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n Node *cur = hashMap->buckets[index];\n Node *pre = NULL;\n while (cur) {\n if (cur->pair->key == key) {\n // \u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n if (pre) {\n pre->next = cur->next;\n } else {\n hashMap->buckets[index] = cur->next;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur->pair);\n free(cur);\n hashMap->size--;\n return;\n }\n pre = cur;\n cur = cur->next;\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapChaining *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Node *cur = hashMap->buckets[i];\n printf(\"[\");\n while (cur) {\n printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n cur = cur->next;\n }\n printf(\"]\\n\");\n }\n}\n
hash_map_chaining.kt/* \u94fe\u5f0f\u5730\u5740\u54c8\u5e0c\u8868 */\nclass HashMapChaining() {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n val loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n val extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: MutableList<MutableList<Pair>> // \u6876\u6570\u7ec4\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = ArrayList(capacity)\n for (i in 0..<capacity) {\n buckets.add(mutableListOf())\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u627e\u5230 key \uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n for (pair in bucket) {\n if (pair.key == key) return pair.value\n }\n // \u82e5\u672a\u627e\u5230 key \uff0c\u5219\u8fd4\u56de null\n return null\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend()\n }\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u82e5\u9047\u5230\u6307\u5b9a key \uff0c\u5219\u66f4\u65b0\u5bf9\u5e94 val \u5e76\u8fd4\u56de\n for (pair in bucket) {\n if (pair.key == key) {\n pair.value = value\n return\n }\n }\n // \u82e5\u65e0\u8be5 key \uff0c\u5219\u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u81f3\u5c3e\u90e8\n val pair = Pair(key, value)\n bucket.add(pair)\n size++\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n val bucket = buckets[index]\n // \u904d\u5386\u6876\uff0c\u4ece\u4e2d\u5220\u9664\u952e\u503c\u5bf9\n for (pair in bucket) {\n if (pair.key == key) {\n bucket.remove(pair)\n size--\n break\n }\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fun extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n // mutablelist \u65e0\u56fa\u5b9a\u5927\u5c0f\n buckets = mutableListOf()\n for (i in 0..<capacity) {\n buckets.add(mutableListOf())\n }\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (bucket in bucketsTmp) {\n for (pair in bucket) {\n put(pair.key, pair.value)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (bucket in buckets) {\n val res = mutableListOf<String>()\n for (pair in bucket) {\n val k = pair.key\n val v = pair.value\n res.add(\"$k -> $v\")\n }\n println(res)\n }\n }\n}\n
hash_map_chaining.rb[class]{HashMapChaining}-[func]{}\n
hash_map_chaining.zig[class]{HashMapChaining}-[func]{}\n
Code Visualization Full Screen >
It's worth noting that when the list is very long, the query efficiency \\(O(n)\\) is poor. At this point, the list can be converted to an \"AVL tree\" or \"Red-Black tree\" to optimize the time complexity of the query operation to \\(O(\\log n)\\).
"},{"location":"chapter_hashing/hash_collision/#622-open-addressing","title":"6.2.2 \u00a0 Open addressing","text":"\"Open addressing\" does not introduce additional data structures but uses \"multiple probes\" to handle hash collisions. The probing methods mainly include linear probing, quadratic probing, and double hashing.
Let's use linear probing as an example to introduce the mechanism of open addressing hash tables.
"},{"location":"chapter_hashing/hash_collision/#1-linear-probing","title":"1. \u00a0 Linear probing","text":"Linear probing uses a fixed-step linear search for probing, differing from ordinary hash tables.
value
; if an empty bucket is encountered, it means the target element is not in the hash table, so return None
.The Figure 6-6 shows the distribution of key-value pairs in an open addressing (linear probing) hash table. According to this hash function, keys with the same last two digits will be mapped to the same bucket. Through linear probing, they are stored consecutively in that bucket and the buckets below it.
Figure 6-6 \u00a0 Distribution of key-value pairs in open addressing (linear probing) hash table
However, linear probing tends to create \"clustering\". Specifically, the longer a continuous position in the array is occupied, the more likely these positions are to encounter hash collisions, further promoting the growth of these clusters and eventually leading to deterioration in the efficiency of operations.
It's important to note that we cannot directly delete elements in an open addressing hash table. Deleting an element creates an empty bucket None
in the array. When searching for elements, if linear probing encounters this empty bucket, it will return, making the elements below this bucket inaccessible. The program may incorrectly assume these elements do not exist, as shown in the Figure 6-7 .
Figure 6-7 \u00a0 Query issues caused by deletion in open addressing
To solve this problem, we can use a \"lazy deletion\" mechanism: instead of directly removing elements from the hash table, use a constant TOMBSTONE
to mark the bucket. In this mechanism, both None
and TOMBSTONE
represent empty buckets and can hold key-value pairs. However, when linear probing encounters TOMBSTONE
, it should continue traversing since there may still be key-value pairs below it.
However, lazy deletion may accelerate the degradation of hash table performance. Every deletion operation produces a delete mark, and as TOMBSTONE
increases, so does the search time, as linear probing may have to skip multiple TOMBSTONE
to find the target element.
Therefore, consider recording the index of the first TOMBSTONE
encountered during linear probing and swapping the target element found with this TOMBSTONE
. The advantage of this is that each time a query or addition is performed, the element is moved to a bucket closer to the ideal position (starting point of probing), thereby optimizing the query efficiency.
The code below implements an open addressing (linear probing) hash table with lazy deletion. To make fuller use of the hash table space, we treat the hash table as a \"circular array,\" continuing to traverse from the beginning when the end of the array is passed.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig hash_map_open_addressing.pyclass HashMapOpenAddressing:\n \"\"\"\u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.size = 0 # \u952e\u503c\u5bf9\u6570\u91cf\n self.capacity = 4 # \u54c8\u5e0c\u8868\u5bb9\u91cf\n self.load_thres = 2.0 / 3.0 # \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n self.extend_ratio = 2 # \u6269\u5bb9\u500d\u6570\n self.buckets: list[Pair | None] = [None] * self.capacity # \u6876\u6570\u7ec4\n self.TOMBSTONE = Pair(-1, \"-1\") # \u5220\u9664\u6807\u8bb0\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n return key % self.capacity\n\n def load_factor(self) -> float:\n \"\"\"\u8d1f\u8f7d\u56e0\u5b50\"\"\"\n return self.size / self.capacity\n\n def find_bucket(self, key: int) -> int:\n \"\"\"\u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\"\"\"\n index = self.hash_func(key)\n first_tombstone = -1\n # \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index] is not None:\n # \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].key == key:\n # \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if first_tombstone != -1:\n self.buckets[first_tombstone] = self.buckets[index]\n self.buckets[index] = self.TOMBSTONE\n return first_tombstone # \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n return index # \u8fd4\u56de\u6876\u7d22\u5f15\n # \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n first_tombstone = index\n # \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity\n # \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return index if first_tombstone == -1 else first_tombstone\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n return self.buckets[index].val\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de None\n return None\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n # \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres:\n self.extend()\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index].val = val\n return\n # \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Pair(key, val)\n self.size += 1\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n # \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n index = self.find_bucket(key)\n # \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index] not in [None, self.TOMBSTONE]:\n self.buckets[index] = self.TOMBSTONE\n self.size -= 1\n\n def extend(self):\n \"\"\"\u6269\u5bb9\u54c8\u5e0c\u8868\"\"\"\n # \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n buckets_tmp = self.buckets\n # \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio\n self.buckets = [None] * self.capacity\n self.size = 0\n # \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp:\n if pair not in [None, self.TOMBSTONE]:\n self.put(pair.key, pair.val)\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is None:\n print(\"None\")\n elif pair is self.TOMBSTONE:\n print(\"TOMBSTONE\")\n else:\n print(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private:\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n const double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n const int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n vector<Pair *> buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~HashMapOpenAddressing() {\n for (Pair *pair : buckets) {\n if (pair != nullptr && pair != TOMBSTONE) {\n delete pair;\n }\n }\n delete TOMBSTONE;\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != nullptr) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n return buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n buckets[index]->val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n delete buckets[index];\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n vector<Pair *> bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = vector<Pair *>(capacity, nullptr);\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair *pair : bucketsTmp) {\n if (pair != nullptr && pair != TOMBSTONE) {\n put(pair->key, pair->val);\n delete pair;\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *pair : buckets) {\n if (pair == nullptr) {\n cout << \"nullptr\" << endl;\n } else if (pair == TOMBSTONE) {\n cout << \"TOMBSTONE\" << endl;\n } else {\n cout << pair->key << \" -> \" << pair->val << endl;\n }\n }\n }\n};\n
hash_map_open_addressing.java/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private int size; // \u952e\u503c\u5bf9\u6570\u91cf\n private int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private final double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private final int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n private Pair[] buckets; // \u6876\u6570\u7ec4\n private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private double loadFactor() {\n return (double) size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair pair : bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair pair : buckets) {\n if (pair == null) {\n System.out.println(\"null\");\n } else if (pair == TOMBSTONE) {\n System.out.println(\"TOMBSTONE\");\n } else {\n System.out.println(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.cs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n Pair[] buckets; // \u6876\u6570\u7ec4\n Pair TOMBSTONE = new(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public HashMapOpenAddressing() {\n size = 0;\n buckets = new Pair[capacity];\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n return key % capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double LoadFactor() {\n return (double)size / capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int FindBucket(int key) {\n int index = HashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index].key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index];\n buckets[index] = TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (LoadFactor() > loadThres) {\n Extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = new Pair(key, val);\n size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = FindBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE;\n size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void Extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair[] bucketsTmp = buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio;\n buckets = new Pair[capacity];\n size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n foreach (Pair pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n Put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair pair in buckets) {\n if (pair == null) {\n Console.WriteLine(\"null\");\n } else if (pair == TOMBSTONE) {\n Console.WriteLine(\"TOMBSTONE\");\n } else {\n Console.WriteLine(pair.key + \" -> \" + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.go/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntype hashMapOpenAddressing struct {\n size int // \u952e\u503c\u5bf9\u6570\u91cf\n capacity int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n loadThres float64 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extendRatio int // \u6269\u5bb9\u500d\u6570\n buckets []*pair // \u6876\u6570\u7ec4\n TOMBSTONE *pair // \u5220\u9664\u6807\u8bb0\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n return &hashMapOpenAddressing{\n size: 0,\n capacity: 4,\n loadThres: 2.0 / 3.0,\n extendRatio: 2,\n buckets: make([]*pair, 4),\n TOMBSTONE: &pair{-1, \"-1\"},\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n return key % h.capacity // \u6839\u636e\u952e\u8ba1\u7b97\u54c8\u5e0c\u503c\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n return float64(h.size) / float64(h.capacity) // \u8ba1\u7b97\u5f53\u524d\u8d1f\u8f7d\u56e0\u5b50\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n index := h.hashFunc(key) // \u83b7\u53d6\u521d\u59cb\u7d22\u5f15\n firstTombstone := -1 // \u8bb0\u5f55\u9047\u5230\u7684\u7b2c\u4e00\u4e2aTOMBSTONE\u7684\u4f4d\u7f6e\n for h.buckets[index] != nil {\n if h.buckets[index].key == key {\n if firstTombstone != -1 {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n h.buckets[firstTombstone] = h.buckets[index]\n h.buckets[index] = h.TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u627e\u5230\u7684\u7d22\u5f15\n }\n if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n firstTombstone = index // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\u7684\u4f4d\u7f6e\n }\n index = (index + 1) % h.capacity // \u7ebf\u6027\u63a2\u6d4b\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if firstTombstone != -1 {\n return firstTombstone\n }\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n return h.buckets[index].val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n }\n return \"\" // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de \"\"\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n if h.loadFactor() > h.loadThres {\n h.extend() // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n }\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n h.buckets[index] = &pair{key, val} // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n h.size++\n } else {\n h.buckets[index].val = val // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val\n }\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n index := h.findBucket(key) // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n h.buckets[index] = h.TOMBSTONE // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n h.size--\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) extend() {\n oldBuckets := h.buckets // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n h.capacity *= h.extendRatio // \u66f4\u65b0\u5bb9\u91cf\n h.buckets = make([]*pair, h.capacity) // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n h.size = 0 // \u91cd\u7f6e\u5927\u5c0f\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for _, pair := range oldBuckets {\n if pair != nil && pair != h.TOMBSTONE {\n h.put(pair.key, pair.val)\n }\n }\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (h *hashMapOpenAddressing) print() {\n for _, pair := range h.buckets {\n if pair == nil {\n fmt.Println(\"nil\")\n } else if pair == h.TOMBSTONE {\n fmt.Println(\"TOMBSTONE\")\n } else {\n fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n }\n }\n}\n
hash_map_open_addressing.swift/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n var size: Int // \u952e\u503c\u5bf9\u6570\u91cf\n var capacity: Int // \u54c8\u5e0c\u8868\u5bb9\u91cf\n var loadThres: Double // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n var extendRatio: Int // \u6269\u5bb9\u500d\u6570\n var buckets: [Pair?] // \u6876\u6570\u7ec4\n var TOMBSTONE: Pair // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init() {\n size = 0\n capacity = 4\n loadThres = 2.0 / 3.0\n extendRatio = 2\n buckets = Array(repeating: nil, count: capacity)\n TOMBSTONE = Pair(key: -1, val: \"-1\")\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n func hashFunc(key: Int) -> Int {\n key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n func loadFactor() -> Double {\n Double(size) / Double(capacity)\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n func findBucket(key: Int) -> Int {\n var index = hashFunc(key: key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while buckets[index] != nil {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if buckets[index]!.key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if firstTombstone != -1 {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n return buckets[index]!.val\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return nil\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if loadFactor() > loadThres {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index]!.val = val\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key: key, val: val)\n size += 1\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = findBucket(key: key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if buckets[index] != nil, buckets[index] != TOMBSTONE {\n buckets[index] = TOMBSTONE\n size -= 1\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n func extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = Array(repeating: nil, count: capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in bucketsTmp {\n if let pair, pair != TOMBSTONE {\n put(key: pair.key, val: pair.val)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in buckets {\n if pair == nil {\n Swift.print(\"null\")\n } else if pair == TOMBSTONE {\n Swift.print(\"TOMBSTONE\")\n } else {\n Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n }\n }\n }\n}\n
hash_map_open_addressing.js/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n #size; // \u952e\u503c\u5bf9\u6570\u91cf\n #capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n #loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n #extendRatio; // \u6269\u5bb9\u500d\u6570\n #buckets; // \u6876\u6570\u7ec4\n #TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.#size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.#capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.#loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.#extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.#buckets = Array(this.#capacity).fill(null); // \u6876\u6570\u7ec4\n this.#TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % this.#capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n #loadFactor() {\n return this.#size / this.#capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n #findBucket(key) {\n let index = this.#hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.#buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (this.#buckets[index].key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone !== -1) {\n this.#buckets[firstTombstone] = this.#buckets[index];\n this.#buckets[index] = this.#TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.#buckets[index] === this.#TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.#capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n return this.#buckets[index].val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key, val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.#loadFactor() > this.#loadThres) {\n this.#extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index].val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.#buckets[index] = new Pair(key, val);\n this.#size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.#findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.#buckets[index] !== null &&\n this.#buckets[index] !== this.#TOMBSTONE\n ) {\n this.#buckets[index] = this.#TOMBSTONE;\n this.#size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n #extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.#buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.#capacity *= this.#extendRatio;\n this.#buckets = Array(this.#capacity).fill(null);\n this.#size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.#TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n for (const pair of this.#buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.#TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.ts/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private size: number; // \u952e\u503c\u5bf9\u6570\u91cf\n private capacity: number; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private loadThres: number; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private extendRatio: number; // \u6269\u5bb9\u500d\u6570\n private buckets: Array<Pair | null>; // \u6876\u6570\u7ec4\n private TOMBSTONE: Pair; // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor() {\n this.size = 0; // \u952e\u503c\u5bf9\u6570\u91cf\n this.capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n this.loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n this.extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n this.buckets = Array(this.capacity).fill(null); // \u6876\u6570\u7ec4\n this.TOMBSTONE = new Pair(-1, '-1'); // \u5220\u9664\u6807\u8bb0\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % this.capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n private loadFactor(): number {\n return this.size / this.capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n private findBucket(key: number): number {\n let index = this.hashFunc(key);\n let firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (this.buckets[index] !== null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (this.buckets[index]!.key === key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone !== -1) {\n this.buckets[firstTombstone] = this.buckets[index];\n this.buckets[index] = this.TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (\n firstTombstone === -1 &&\n this.buckets[index] === this.TOMBSTONE\n ) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % this.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone === -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key: number): string | null {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n return this.buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n put(key: number, val: string): void {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (this.loadFactor() > this.loadThres) {\n this.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n this.buckets[index] = new Pair(key, val);\n this.size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n remove(key: number): void {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n const index = this.findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (\n this.buckets[index] !== null &&\n this.buckets[index] !== this.TOMBSTONE\n ) {\n this.buckets[index] = this.TOMBSTONE;\n this.size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n private extend(): void {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n const bucketsTmp = this.buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n this.capacity *= this.extendRatio;\n this.buckets = Array(this.capacity).fill(null);\n this.size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (const pair of bucketsTmp) {\n if (pair !== null && pair !== this.TOMBSTONE) {\n this.put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print(): void {\n for (const pair of this.buckets) {\n if (pair === null) {\n console.log('null');\n } else if (pair === this.TOMBSTONE) {\n console.log('TOMBSTONE');\n } else {\n console.log(pair.key + ' -> ' + pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.dart/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n late int _size; // \u952e\u503c\u5bf9\u6570\u91cf\n int _capacity = 4; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double _loadThres = 2.0 / 3.0; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int _extendRatio = 2; // \u6269\u5bb9\u500d\u6570\n late List<Pair?> _buckets; // \u6876\u6570\u7ec4\n Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n HashMapOpenAddressing() {\n _size = 0;\n _buckets = List.generate(_capacity, (index) => null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n return key % _capacity;\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n double loadFactor() {\n return _size / _capacity;\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n int findBucket(int key) {\n int index = hashFunc(key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (_buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (_buckets[index]!.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n _buckets[firstTombstone] = _buckets[index];\n _buckets[index] = _TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % _capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n return _buckets[index]!.val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > _loadThres) {\n extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index]!.val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n _buckets[index] = new Pair(key, val);\n _size++;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n _buckets[index] = _TOMBSTONE;\n _size--;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n void extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n List<Pair?> bucketsTmp = _buckets;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n _capacity *= _extendRatio;\n _buckets = List.generate(_capacity, (index) => null);\n _size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (Pair? pair in bucketsTmp) {\n if (pair != null && pair != _TOMBSTONE) {\n put(pair.key, pair.val);\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (Pair? pair in _buckets) {\n if (pair == null) {\n print(\"null\");\n } else if (pair == _TOMBSTONE) {\n print(\"TOMBSTONE\");\n } else {\n print(\"${pair.key} -> ${pair.val}\");\n }\n }\n }\n}\n
hash_map_open_addressing.rs/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nstruct HashMapOpenAddressing {\n size: usize, // \u952e\u503c\u5bf9\u6570\u91cf\n capacity: usize, // \u54c8\u5e0c\u8868\u5bb9\u91cf\n load_thres: f64, // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n extend_ratio: usize, // \u6269\u5bb9\u500d\u6570\n buckets: Vec<Option<Pair>>, // \u6876\u6570\u7ec4\n TOMBSTONE: Option<Pair>, // \u5220\u9664\u6807\u8bb0\n}\n\nimpl HashMapOpenAddressing {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new() -> Self {\n Self {\n size: 0,\n capacity: 4,\n load_thres: 2.0 / 3.0,\n extend_ratio: 2,\n buckets: vec![None; 4],\n TOMBSTONE: Some(Pair {\n key: -1,\n val: \"-1\".to_string(),\n }),\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n (key % self.capacity as i32) as usize\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fn load_factor(&self) -> f64 {\n self.size as f64 / self.capacity as f64\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fn find_bucket(&mut self, key: i32) -> usize {\n let mut index = self.hash_func(key);\n let mut first_tombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while self.buckets[index].is_some() {\n // \u82e5\u9047\u5230 key\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if self.buckets[index].as_ref().unwrap().key == key {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u5efa\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\n if first_tombstone != -1 {\n self.buckets[first_tombstone as usize] = self.buckets[index].take();\n self.buckets[index] = self.TOMBSTONE.clone();\n return first_tombstone as usize; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n first_tombstone = index as i32;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % self.capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n if first_tombstone == -1 {\n index\n } else {\n first_tombstone as usize\n }\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fn get(&mut self, key: i32) -> Option<&str> {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n None\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fn put(&mut self, key: i32, val: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if self.load_factor() > self.load_thres {\n self.extend();\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index].as_mut().unwrap().val = val;\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n self.buckets[index] = Some(Pair { key, val });\n self.size += 1;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fn remove(&mut self, key: i32) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n let index = self.find_bucket(key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n self.buckets[index] = self.TOMBSTONE.clone();\n self.size -= 1;\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fn extend(&mut self) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n let buckets_tmp = self.buckets.clone();\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n self.capacity *= self.extend_ratio;\n self.buckets = vec![None; self.capacity];\n self.size = 0;\n\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for pair in buckets_tmp {\n if pair.is_none() || pair == self.TOMBSTONE {\n continue;\n }\n let pair = pair.unwrap();\n\n self.put(pair.key, pair.val);\n }\n }\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fn print(&self) {\n for pair in &self.buckets {\n if pair.is_none() {\n println!(\"null\");\n } else if pair == &self.TOMBSTONE {\n println!(\"TOMBSTONE\");\n } else {\n let pair = pair.as_ref().unwrap();\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n }\n}\n
hash_map_open_addressing.c/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\ntypedef struct {\n int size; // \u952e\u503c\u5bf9\u6570\u91cf\n int capacity; // \u54c8\u5e0c\u8868\u5bb9\u91cf\n double loadThres; // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n int extendRatio; // \u6269\u5bb9\u500d\u6570\n Pair **buckets; // \u6876\u6570\u7ec4\n Pair *TOMBSTONE; // \u5220\u9664\u6807\u8bb0\n} HashMapOpenAddressing;\n\n/* \u6784\u9020\u51fd\u6570 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n hashMap->size = 0;\n hashMap->capacity = 4;\n hashMap->loadThres = 2.0 / 3.0;\n hashMap->extendRatio = 2;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n hashMap->TOMBSTONE->key = -1;\n hashMap->TOMBSTONE->val = \"-1\";\n\n return hashMap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n free(pair->val);\n free(pair);\n }\n }\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n return key % hashMap->capacity;\n}\n\n/* \u8d1f\u8f7d\u56e0\u5b50 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n int index = hashFunc(hashMap, key);\n int firstTombstone = -1;\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (hashMap->buckets[index] != NULL) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (hashMap->buckets[index]->key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n return firstTombstone; // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index; // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n firstTombstone = index;\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % hashMap->capacity;\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n return hashMap->buckets[index]->val;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\n return \"\";\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor(hashMap) > hashMap->loadThres) {\n extend(hashMap);\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n free(hashMap->buckets[index]->val);\n hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(hashMap->buckets[index]->val, val);\n hashMap->buckets[index]->val[strlen(val)] = '\\0';\n return;\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n Pair *pair = (Pair *)malloc(sizeof(Pair));\n pair->key = key;\n pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n strcpy(pair->val, val);\n pair->val[strlen(val)] = '\\0';\n\n hashMap->buckets[index] = pair;\n hashMap->size++;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n int index = findBucket(hashMap, key);\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n Pair *pair = hashMap->buckets[index];\n free(pair->val);\n free(pair);\n hashMap->buckets[index] = hashMap->TOMBSTONE;\n hashMap->size--;\n }\n}\n\n/* \u6269\u5bb9\u54c8\u5e0c\u8868 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n Pair **bucketsTmp = hashMap->buckets;\n int oldCapacity = hashMap->capacity;\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n hashMap->capacity *= hashMap->extendRatio;\n hashMap->buckets = (Pair **)malloc(sizeof(Pair *) * hashMap->capacity);\n hashMap->size = 0;\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (int i = 0; i < oldCapacity; i++) {\n Pair *pair = bucketsTmp[i];\n if (pair != NULL && pair != hashMap->TOMBSTONE) {\n put(hashMap, pair->key, pair->val);\n free(pair->val);\n free(pair);\n }\n }\n free(bucketsTmp);\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(HashMapOpenAddressing *hashMap) {\n for (int i = 0; i < hashMap->capacity; i++) {\n Pair *pair = hashMap->buckets[i];\n if (pair == NULL) {\n printf(\"NULL\\n\");\n } else if (pair == hashMap->TOMBSTONE) {\n printf(\"TOMBSTONE\\n\");\n } else {\n printf(\"%d -> %s\\n\", pair->key, pair->val);\n }\n }\n}\n
hash_map_open_addressing.kt/* \u5f00\u653e\u5bfb\u5740\u54c8\u5e0c\u8868 */\nclass HashMapOpenAddressing {\n private var size: Int = 0 // \u952e\u503c\u5bf9\u6570\u91cf\n private var capacity = 4 // \u54c8\u5e0c\u8868\u5bb9\u91cf\n private val loadThres: Double = 2.0 / 3.0 // \u89e6\u53d1\u6269\u5bb9\u7684\u8d1f\u8f7d\u56e0\u5b50\u9608\u503c\n private val extendRatio = 2 // \u6269\u5bb9\u500d\u6570\n private var buckets: Array<Pair?> // \u6876\u6570\u7ec4\n private val TOMBSTONE = Pair(-1, \"-1\") // \u5220\u9664\u6807\u8bb0\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init {\n buckets = arrayOfNulls(capacity)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n return key % capacity\n }\n\n /* \u8d1f\u8f7d\u56e0\u5b50 */\n fun loadFactor(): Double {\n return (size / capacity).toDouble()\n }\n\n /* \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15 */\n fun findBucket(key: Int): Int {\n var index = hashFunc(key)\n var firstTombstone = -1\n // \u7ebf\u6027\u63a2\u6d4b\uff0c\u5f53\u9047\u5230\u7a7a\u6876\u65f6\u8df3\u51fa\n while (buckets[index] != null) {\n // \u82e5\u9047\u5230 key \uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n if (buckets[index]?.key == key) {\n // \u82e5\u4e4b\u524d\u9047\u5230\u4e86\u5220\u9664\u6807\u8bb0\uff0c\u5219\u5c06\u952e\u503c\u5bf9\u79fb\u52a8\u81f3\u8be5\u7d22\u5f15\u5904\n if (firstTombstone != -1) {\n buckets[firstTombstone] = buckets[index]\n buckets[index] = TOMBSTONE\n return firstTombstone // \u8fd4\u56de\u79fb\u52a8\u540e\u7684\u6876\u7d22\u5f15\n }\n return index // \u8fd4\u56de\u6876\u7d22\u5f15\n }\n // \u8bb0\u5f55\u9047\u5230\u7684\u9996\u4e2a\u5220\u9664\u6807\u8bb0\n if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n firstTombstone = index\n }\n // \u8ba1\u7b97\u6876\u7d22\u5f15\uff0c\u8d8a\u8fc7\u5c3e\u90e8\u5219\u8fd4\u56de\u5934\u90e8\n index = (index + 1) % capacity\n }\n // \u82e5 key \u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u6dfb\u52a0\u70b9\u7684\u7d22\u5f15\n return if (firstTombstone == -1) index else firstTombstone\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8fd4\u56de\u5bf9\u5e94 val\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n return buckets[index]?.value\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de null\n return null\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n // \u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u9608\u503c\u65f6\uff0c\u6267\u884c\u6269\u5bb9\n if (loadFactor() > loadThres) {\n extend()\n }\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6 val \u5e76\u8fd4\u56de\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index]!!.value = value\n return\n }\n // \u82e5\u952e\u503c\u5bf9\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u8be5\u952e\u503c\u5bf9\n buckets[index] = Pair(key, value)\n size++\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n // \u641c\u7d22 key \u5bf9\u5e94\u7684\u6876\u7d22\u5f15\n val index = findBucket(key)\n // \u82e5\u627e\u5230\u952e\u503c\u5bf9\uff0c\u5219\u7528\u5220\u9664\u6807\u8bb0\u8986\u76d6\u5b83\n if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n buckets[index] = TOMBSTONE\n size--\n }\n }\n\n /* \u6269\u5bb9\u54c8\u5e0c\u8868 */\n fun extend() {\n // \u6682\u5b58\u539f\u54c8\u5e0c\u8868\n val bucketsTmp = buckets\n // \u521d\u59cb\u5316\u6269\u5bb9\u540e\u7684\u65b0\u54c8\u5e0c\u8868\n capacity *= extendRatio\n buckets = arrayOfNulls(capacity)\n size = 0\n // \u5c06\u952e\u503c\u5bf9\u4ece\u539f\u54c8\u5e0c\u8868\u642c\u8fd0\u81f3\u65b0\u54c8\u5e0c\u8868\n for (pair in bucketsTmp) {\n if (pair != null && pair != TOMBSTONE) {\n put(pair.key, pair.value)\n }\n }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (pair in buckets) {\n if (pair == null) {\n println(\"null\")\n } else if (pair == TOMBSTONE) {\n println(\"TOMESTOME\")\n } else {\n println(\"${pair.key} -> ${pair.value}\")\n }\n }\n }\n}\n
hash_map_open_addressing.rb[class]{HashMapOpenAddressing}-[func]{}\n
hash_map_open_addressing.zig[class]{HashMapOpenAddressing}-[func]{}\n
"},{"location":"chapter_hashing/hash_collision/#2-quadratic-probing","title":"2. \u00a0 Quadratic probing","text":"Quadratic probing is similar to linear probing and is one of the common strategies of open addressing. When a collision occurs, quadratic probing does not simply skip a fixed number of steps but skips \"the square of the number of probes,\" i.e., \\(1, 4, 9, \\dots\\) steps.
Quadratic probing has the following advantages:
However, quadratic probing is not perfect:
As the name suggests, the double hashing method uses multiple hash functions \\(f_1(x)\\), \\(f_2(x)\\), \\(f_3(x)\\), \\(\\dots\\) for probing.
None
.Compared to linear probing, double hashing is less prone to clustering but involves additional computation for multiple hash functions.
Tip
Please note that open addressing (linear probing, quadratic probing, and double hashing) hash tables all have the issue of \"not being able to directly delete elements.\"
"},{"location":"chapter_hashing/hash_collision/#623-choice-of-programming-languages","title":"6.2.3 \u00a0 Choice of programming languages","text":"Various programming languages have adopted different hash table implementation strategies, here are a few examples:
dict
dictionary uses pseudo-random numbers for probing.HashMap
reaches 64 and the length of a linked list reaches 8, the linked list is converted to a red-black tree to improve search performance.A \"hash table\", also known as a \"hash map\", achieves efficient element querying by establishing a mapping between keys and values. Specifically, when we input a key
into the hash table, we can retrieve the corresponding value
in \\(O(1)\\) time.
As shown in the Figure 6-1 , given \\(n\\) students, each with two pieces of data: \"name\" and \"student number\". If we want to implement a query feature that returns the corresponding name when given a student number, we can use the hash table shown in the Figure 6-1 .
Figure 6-1 \u00a0 Abstract representation of a hash table
Apart from hash tables, arrays and linked lists can also be used to implement querying functions. Their efficiency is compared in the Table 6-1 .
Table 6-1 \u00a0 Comparison of element query efficiency
Array Linked List Hash Table Find Element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) Add Element \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) Delete Element \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)Observations reveal that the time complexity for adding, deleting, and querying in a hash table is \\(O(1)\\), which is highly efficient.
"},{"location":"chapter_hashing/hash_map/#611-common-operations-of-hash-table","title":"6.1.1 \u00a0 Common operations of hash table","text":"Common operations of a hash table include initialization, querying, adding key-value pairs, and deleting key-value pairs, etc. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig hash_map.py# Initialize hash table\nhmap: dict = {}\n\n# Add operation\n# Add key-value pair (key, value) to the hash table\nhmap[12836] = \"Xiao Ha\"\nhmap[15937] = \"Xiao Luo\"\nhmap[16750] = \"Xiao Suan\"\nhmap[13276] = \"Xiao Fa\"\nhmap[10583] = \"Xiao Ya\"\n\n# Query operation\n# Input key into hash table, get value\nname: str = hmap[15937]\n\n# Delete operation\n# Delete key-value pair (key, value) from hash table\nhmap.pop(10583)\n
hash_map.cpp/* Initialize hash table */\nunordered_map<int, string> map;\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\";\nmap[15937] = \"Xiao Luo\";\nmap[16750] = \"Xiao Suan\";\nmap[13276] = \"Xiao Fa\";\nmap[10583] = \"Xiao Ya\";\n\n/* Query operation */\n// Input key into hash table, get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.erase(10583);\n
hash_map.java/* Initialize hash table */\nMap<Integer, String> map = new HashMap<>();\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.put(12836, \"Xiao Ha\"); \nmap.put(15937, \"Xiao Luo\"); \nmap.put(16750, \"Xiao Suan\"); \nmap.put(13276, \"Xiao Fa\");\nmap.put(10583, \"Xiao Ya\");\n\n/* Query operation */\n// Input key into hash table, get value\nString name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
hash_map.cs/* Initialize hash table */\nDictionary<int, string> map = new() {\n /* Add operation */\n // Add key-value pair (key, value) to the hash table\n { 12836, \"Xiao Ha\" },\n { 15937, \"Xiao Luo\" },\n { 16750, \"Xiao Suan\" },\n { 13276, \"Xiao Fa\" },\n { 10583, \"Xiao Ya\" }\n};\n\n/* Query operation */\n// Input key into hash table, get value\nstring name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.Remove(10583);\n
hash_map_test.go/* Initialize hash table */\nhmap := make(map[int]string)\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nhmap[12836] = \"Xiao Ha\"\nhmap[15937] = \"Xiao Luo\"\nhmap[16750] = \"Xiao Suan\"\nhmap[13276] = \"Xiao Fa\"\nhmap[10583] = \"Xiao Ya\"\n\n/* Query operation */\n// Input key into hash table, get value\nname := hmap[15937]\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\ndelete(hmap, 10583)\n
hash_map.swift/* Initialize hash table */\nvar map: [Int: String] = [:]\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\"\nmap[15937] = \"Xiao Luo\"\nmap[16750] = \"Xiao Suan\"\nmap[13276] = \"Xiao Fa\"\nmap[10583] = \"Xiao Ya\"\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map[15937]!\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.removeValue(forKey: 10583)\n
hash_map.js/* Initialize hash table */\nconst map = new Map();\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.set(12836, 'Xiao Ha');\nmap.set(15937, 'Xiao Luo');\nmap.set(16750, 'Xiao Suan');\nmap.set(13276, 'Xiao Fa');\nmap.set(10583, 'Xiao Ya');\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map.get(15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\n
hash_map.ts/* Initialize hash table */\nconst map = new Map<number, string>();\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.set(12836, 'Xiao Ha');\nmap.set(15937, 'Xiao Luo');\nmap.set(16750, 'Xiao Suan');\nmap.set(13276, 'Xiao Fa');\nmap.set(10583, 'Xiao Ya');\nconsole.info('\\nAfter adding, the hash table is\\nKey -> Value');\nconsole.info(map);\n\n/* Query operation */\n// Input key into hash table, get value\nlet name = map.get(15937);\nconsole.info('\\nInput student number 15937, query name ' + name);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.delete(10583);\nconsole.info('\\nAfter deleting 10583, the hash table is\\nKey -> Value');\nconsole.info(map);\n
hash_map.dart/* Initialize hash table */\nMap<int, String> map = {};\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap[12836] = \"Xiao Ha\";\nmap[15937] = \"Xiao Luo\";\nmap[16750] = \"Xiao Suan\";\nmap[13276] = \"Xiao Fa\";\nmap[10583] = \"Xiao Ya\";\n\n/* Query operation */\n// Input key into hash table, get value\nString name = map[15937];\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nmap.remove(10583);\n
hash_map.rsuse std::collections::HashMap;\n\n/* Initialize hash table */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* Add operation */\n// Add key-value pair (key, value) to the hash table\nmap.insert(12836, \"Xiao Ha\".to_string());\nmap.insert(15937, \"Xiao Luo\".to_string());\nmap.insert(16750, \"Xiao Suan\".to_string());\nmap.insert(13279, \"Xiao Fa\".to_string());\nmap.insert(10583, \"Xiao Ya\".to_string());\n\n/* Query operation */\n// Input key into hash table, get value\nlet _name: Option<&String> = map.get(&15937);\n\n/* Delete operation */\n// Delete key-value pair (key, value) from hash table\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c// C does not provide a built-in hash table\n
hash_map.kt\n
hash_map.zig\n
Code Visualization Full Screen >
There are three common ways to traverse a hash table: traversing key-value pairs, keys, and values. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig hash_map.py# Traverse hash table\n# Traverse key-value pairs key->value\nfor key, value in hmap.items():\n print(key, \"->\", value)\n# Traverse keys only\nfor key in hmap.keys():\n print(key)\n# Traverse values only\nfor value in hmap.values():\n print(value)\n
hash_map.cpp/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (auto kv: map) {\n cout << kv.first << \" -> \" << kv.second << endl;\n}\n// Traverse using iterator key->value\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n cout << iter->first << \"->\" << iter->second << endl;\n}\n
hash_map.java/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor (Map.Entry<Integer, String> kv: map.entrySet()) {\n System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// Traverse keys only\nfor (int key: map.keySet()) {\n System.out.println(key);\n}\n// Traverse values only\nfor (String val: map.values()) {\n System.out.println(val);\n}\n
hash_map.cs/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nforeach (var kv in map) {\n Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// Traverse keys only\nforeach (int key in map.Keys) {\n Console.WriteLine(key);\n}\n// Traverse values only\nforeach (string val in map.Values) {\n Console.WriteLine(val);\n}\n
hash_map_test.go/* Traverse hash table */\n// Traverse key-value pairs key->value\nfor key, value := range hmap {\n fmt.Println(key, \"->\", value)\n}\n// Traverse keys only\nfor key := range hmap {\n fmt.Println(key)\n}\n// Traverse values only\nfor _, value := range hmap {\n fmt.Println(value)\n}\n
hash_map.swift/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in map {\n print(\"\\(key) -> \\(value)\")\n}\n// Traverse keys only\nfor key in map.keys {\n print(key)\n}\n// Traverse values only\nfor value in map.values {\n print(value)\n}\n
hash_map.js/* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.ts/* Traverse hash table */\nconsole.info('\\nTraverse key-value pairs Key->Value');\nfor (const [k, v] of map.entries()) {\n console.info(k + ' -> ' + v);\n}\nconsole.info('\\nTraverse keys only Key');\nfor (const k of map.keys()) {\n console.info(k);\n}\nconsole.info('\\nTraverse values only Value');\nfor (const v of map.values()) {\n console.info(v);\n}\n
hash_map.dart/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nmap.forEach((key, value) {\nprint('$key -> $value');\n});\n\n// Traverse keys only Key\nmap.keys.forEach((key) {\nprint(key);\n});\n\n// Traverse values only Value\nmap.values.forEach((value) {\nprint(value);\n});\n
hash_map.rs/* Traverse hash table */\n// Traverse key-value pairs Key->Value\nfor (key, value) in &map {\n println!(\"{key} -> {value}\");\n}\n\n// Traverse keys only Key\nfor key in map.keys() {\n println!(\"{key}\"); \n}\n\n// Traverse values only Value\nfor value in map.values() {\n println!(\"{value}\");\n}\n
hash_map.c// C does not provide a built-in hash table\n
hash_map.kt\n
hash_map.zig// Zig example is not provided\n
Code Visualization Full Screen >
"},{"location":"chapter_hashing/hash_map/#612-simple-implementation-of-hash-table","title":"6.1.2 \u00a0 Simple implementation of hash table","text":"First, let's consider the simplest case: implementing a hash table using just an array. In the hash table, each empty slot in the array is called a \"bucket\", and each bucket can store one key-value pair. Therefore, the query operation involves finding the bucket corresponding to the key
and retrieving the value
from it.
So, how do we locate the appropriate bucket based on the key
? This is achieved through a \"hash function\". The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all possible keys, and the output space is all buckets (array indices). In other words, input a key
, and we can use the hash function to determine the storage location of the corresponding key-value pair in the array.
The calculation process of the hash function for a given key
is divided into the following two steps:
hash()
.capacity
to obtain the array index index
.index = hash(key) % capacity\n
Afterward, we can use index
to access the corresponding bucket in the hash table and thereby retrieve the value
.
Assuming array length capacity = 100
and hash algorithm hash(key) = key
, the hash function is key % 100
. The Figure 6-2 uses key
as the student number and value
as the name to demonstrate the working principle of the hash function.
Figure 6-2 \u00a0 Working principle of hash function
The following code implements a simple hash table. Here, we encapsulate key
and value
into a class Pair
to represent the key-value pair.
class Pair:\n \"\"\"\u952e\u503c\u5bf9\"\"\"\n\n def __init__(self, key: int, val: str):\n self.key = key\n self.val = val\n\nclass ArrayHashMap:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n # \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n self.buckets: list[Pair | None] = [None] * 100\n\n def hash_func(self, key: int) -> int:\n \"\"\"\u54c8\u5e0c\u51fd\u6570\"\"\"\n index = key % 100\n return index\n\n def get(self, key: int) -> str:\n \"\"\"\u67e5\u8be2\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n pair: Pair = self.buckets[index]\n if pair is None:\n return None\n return pair.val\n\n def put(self, key: int, val: str):\n \"\"\"\u6dfb\u52a0\u64cd\u4f5c\"\"\"\n pair = Pair(key, val)\n index: int = self.hash_func(key)\n self.buckets[index] = pair\n\n def remove(self, key: int):\n \"\"\"\u5220\u9664\u64cd\u4f5c\"\"\"\n index: int = self.hash_func(key)\n # \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None\n\n def entry_set(self) -> list[Pair]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\"\"\"\n result: list[Pair] = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair)\n return result\n\n def key_set(self) -> list[int]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u952e\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.key)\n return result\n\n def value_set(self) -> list[str]:\n \"\"\"\u83b7\u53d6\u6240\u6709\u503c\"\"\"\n result = []\n for pair in self.buckets:\n if pair is not None:\n result.append(pair.val)\n return result\n\n def print(self):\n \"\"\"\u6253\u5370\u54c8\u5e0c\u8868\"\"\"\n for pair in self.buckets:\n if pair is not None:\n print(pair.key, \"->\", pair.val)\n
array_hash_map.cpp/* \u952e\u503c\u5bf9 */\nstruct Pair {\n public:\n int key;\n string val;\n Pair(int key, string val) {\n this->key = key;\n this->val = val;\n }\n};\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private:\n vector<Pair *> buckets;\n\n public:\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = vector<Pair *>(100);\n }\n\n ~ArrayHashMap() {\n // \u91ca\u653e\u5185\u5b58\n for (const auto &bucket : buckets) {\n delete bucket;\n }\n buckets.clear();\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n string get(int key) {\n int index = hashFunc(key);\n Pair *pair = buckets[index];\n if (pair == nullptr)\n return \"\";\n return pair->val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, string val) {\n Pair *pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n int index = hashFunc(key);\n // \u91ca\u653e\u5185\u5b58\u5e76\u7f6e\u4e3a nullptr\n delete buckets[index];\n buckets[index] = nullptr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n vector<Pair *> pairSet() {\n vector<Pair *> pairSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n pairSet.push_back(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n vector<int> keySet() {\n vector<int> keySet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n keySet.push_back(pair->key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n vector<string> valueSet() {\n vector<string> valueSet;\n for (Pair *pair : buckets) {\n if (pair != nullptr) {\n valueSet.push_back(pair->val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void print() {\n for (Pair *kv : pairSet()) {\n cout << kv->key << \" -> \" << kv->val << endl;\n }\n }\n};\n
array_hash_map.java/* \u952e\u503c\u5bf9 */\nclass Pair {\n public int key;\n public String val;\n\n public Pair(int key, String val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private List<Pair> buckets;\n\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = new ArrayList<>();\n for (int i = 0; i < 100; i++) {\n buckets.add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private int hashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public String get(int key) {\n int index = hashFunc(key);\n Pair pair = buckets.get(index);\n if (pair == null)\n return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void put(int key, String val) {\n Pair pair = new Pair(key, val);\n int index = hashFunc(key);\n buckets.set(index, pair);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void remove(int key) {\n int index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets.set(index, null);\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> pairSet() {\n List<Pair> pairSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n pairSet.add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<Integer> keySet() {\n List<Integer> keySet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n keySet.add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<String> valueSet() {\n List<String> valueSet = new ArrayList<>();\n for (Pair pair : buckets) {\n if (pair != null)\n valueSet.add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void print() {\n for (Pair kv : pairSet()) {\n System.out.println(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.cs/* \u952e\u503c\u5bf9 int->string */\nclass Pair(int key, string val) {\n public int key = key;\n public string val = val;\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n List<Pair?> buckets;\n public ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = [];\n for (int i = 0; i < 100; i++) {\n buckets.Add(null);\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int HashFunc(int key) {\n int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public string? Get(int key) {\n int index = HashFunc(key);\n Pair? pair = buckets[index];\n if (pair == null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public void Put(int key, string val) {\n Pair pair = new(key, val);\n int index = HashFunc(key);\n buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public void Remove(int key) {\n int index = HashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public List<Pair> PairSet() {\n List<Pair> pairSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n pairSet.Add(pair);\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public List<int> KeySet() {\n List<int> keySet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n keySet.Add(pair.key);\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public List<string> ValueSet() {\n List<string> valueSet = [];\n foreach (Pair? pair in buckets) {\n if (pair != null)\n valueSet.Add(pair.val);\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public void Print() {\n foreach (Pair kv in PairSet()) {\n Console.WriteLine(kv.key + \" -> \" + kv.val);\n }\n }\n}\n
array_hash_map.go/* \u952e\u503c\u5bf9 */\ntype pair struct {\n key int\n val string\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntype arrayHashMap struct {\n buckets []*pair\n}\n\n/* \u521d\u59cb\u5316\u54c8\u5e0c\u8868 */\nfunc newArrayHashMap() *arrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets := make([]*pair, 100)\n return &arrayHashMap{buckets: buckets}\n}\n\n/* \u54c8\u5e0c\u51fd\u6570 */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n index := key % 100\n return index\n}\n\n/* \u67e5\u8be2\u64cd\u4f5c */\nfunc (a *arrayHashMap) get(key int) string {\n index := a.hashFunc(key)\n pair := a.buckets[index]\n if pair == nil {\n return \"Not Found\"\n }\n return pair.val\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nfunc (a *arrayHashMap) put(key int, val string) {\n pair := &pair{key: key, val: val}\n index := a.hashFunc(key)\n a.buckets[index] = pair\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nfunc (a *arrayHashMap) remove(key int) {\n index := a.hashFunc(key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n a.buckets[index] = nil\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u5bf9 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n var pairs []*pair\n for _, pair := range a.buckets {\n if pair != nil {\n pairs = append(pairs, pair)\n }\n }\n return pairs\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nfunc (a *arrayHashMap) keySet() []int {\n var keys []int\n for _, pair := range a.buckets {\n if pair != nil {\n keys = append(keys, pair.key)\n }\n }\n return keys\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nfunc (a *arrayHashMap) valueSet() []string {\n var values []string\n for _, pair := range a.buckets {\n if pair != nil {\n values = append(values, pair.val)\n }\n }\n return values\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nfunc (a *arrayHashMap) print() {\n for _, pair := range a.buckets {\n if pair != nil {\n fmt.Println(pair.key, \"->\", pair.val)\n }\n }\n}\n
array_hash_map.swift/* \u952e\u503c\u5bf9 */\nclass Pair: Equatable {\n public var key: Int\n public var val: String\n\n public init(key: Int, val: String) {\n self.key = key\n self.val = val\n }\n\n public static func == (lhs: Pair, rhs: Pair) -> Bool {\n lhs.key == rhs.key && lhs.val == rhs.val\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private var buckets: [Pair?]\n\n init() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n buckets = Array(repeating: nil, count: 100)\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private func hashFunc(key: Int) -> Int {\n let index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n func get(key: Int) -> String? {\n let index = hashFunc(key: key)\n let pair = buckets[index]\n return pair?.val\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n func put(key: Int, val: String) {\n let pair = Pair(key: key, val: val)\n let index = hashFunc(key: key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n func remove(key: Int) {\n let index = hashFunc(key: key)\n // \u7f6e\u4e3a nil \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = nil\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n func pairSet() -> [Pair] {\n buckets.compactMap { $0 }\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n func keySet() -> [Int] {\n buckets.compactMap { $0?.key }\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n func valueSet() -> [String] {\n buckets.compactMap { $0?.val }\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n func print() {\n for pair in pairSet() {\n Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n }\n }\n}\n
array_hash_map.js/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n constructor(key, val) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n #buckets;\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.#buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n #hashFunc(key) {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n get(key) {\n let index = this.#hashFunc(key);\n let pair = this.#buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n set(key, val) {\n let index = this.#hashFunc(key);\n this.#buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n delete(key) {\n let index = this.#hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.#buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n entries() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n keys() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n values() {\n let arr = [];\n for (let i = 0; i < this.#buckets.length; i++) {\n if (this.#buckets[i]) {\n arr.push(this.#buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.ts/* \u952e\u503c\u5bf9 Number -> String */\nclass Pair {\n public key: number;\n public val: string;\n\n constructor(key: number, val: string) {\n this.key = key;\n this.val = val;\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private readonly buckets: (Pair | null)[];\n\n constructor() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n this.buckets = new Array(100).fill(null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n private hashFunc(key: number): number {\n return key % 100;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n public get(key: number): string | null {\n let index = this.hashFunc(key);\n let pair = this.buckets[index];\n if (pair === null) return null;\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n public set(key: number, val: string) {\n let index = this.hashFunc(key);\n this.buckets[index] = new Pair(key, val);\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n public delete(key: number) {\n let index = this.hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n this.buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n public entries(): (Pair | null)[] {\n let arr: (Pair | null)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i]);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n public keys(): (number | undefined)[] {\n let arr: (number | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].key);\n }\n }\n return arr;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n public values(): (string | undefined)[] {\n let arr: (string | undefined)[] = [];\n for (let i = 0; i < this.buckets.length; i++) {\n if (this.buckets[i]) {\n arr.push(this.buckets[i].val);\n }\n }\n return arr;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n public print() {\n let pairSet = this.entries();\n for (const pair of pairSet) {\n console.info(`${pair.key} -> ${pair.val}`);\n }\n }\n}\n
array_hash_map.dart/* \u952e\u503c\u5bf9 */\nclass Pair {\n int key;\n String val;\n Pair(this.key, this.val);\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n late List<Pair?> _buckets;\n\n ArrayHashMap() {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n _buckets = List.filled(100, null);\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n int _hashFunc(int key) {\n final int index = key % 100;\n return index;\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n String? get(int key) {\n final int index = _hashFunc(key);\n final Pair? pair = _buckets[index];\n if (pair == null) {\n return null;\n }\n return pair.val;\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n void put(int key, String val) {\n final Pair pair = Pair(key, val);\n final int index = _hashFunc(key);\n _buckets[index] = pair;\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n void remove(int key) {\n final int index = _hashFunc(key);\n _buckets[index] = null;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n List<Pair> pairSet() {\n List<Pair> pairSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n pairSet.add(pair);\n }\n }\n return pairSet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n List<int> keySet() {\n List<int> keySet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n keySet.add(pair.key);\n }\n }\n return keySet;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n List<String> values() {\n List<String> valueSet = [];\n for (final Pair? pair in _buckets) {\n if (pair != null) {\n valueSet.add(pair.val);\n }\n }\n return valueSet;\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n void printHashMap() {\n for (final Pair kv in pairSet()) {\n print(\"${kv.key} -> ${kv.val}\");\n }\n }\n}\n
array_hash_map.rs/* \u952e\u503c\u5bf9 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n pub key: i32,\n pub val: String,\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\npub struct ArrayHashMap {\n buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n pub fn new() -> ArrayHashMap {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n Self {\n buckets: vec![None; 100],\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fn hash_func(&self, key: i32) -> usize {\n key as usize % 100\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n pub fn get(&self, key: i32) -> Option<&String> {\n let index = self.hash_func(key);\n self.buckets[index].as_ref().map(|pair| &pair.val)\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n pub fn put(&mut self, key: i32, val: &str) {\n let index = self.hash_func(key);\n self.buckets[index] = Some(Pair {\n key,\n val: val.to_string(),\n });\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n pub fn remove(&mut self, key: i32) {\n let index = self.hash_func(key);\n // \u7f6e\u4e3a None \uff0c\u4ee3\u8868\u5220\u9664\n self.buckets[index] = None;\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n pub fn entry_set(&self) -> Vec<&Pair> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref())\n .collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n pub fn key_set(&self) -> Vec<&i32> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n .collect()\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n pub fn value_set(&self) -> Vec<&String> {\n self.buckets\n .iter()\n .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n .collect()\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n pub fn print(&self) {\n for pair in self.entry_set() {\n println!(\"{} -> {}\", pair.key, pair.val);\n }\n }\n}\n
array_hash_map.c/* \u952e\u503c\u5bf9 int->string */\ntypedef struct {\n int key;\n char *val;\n} Pair;\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\ntypedef struct {\n Pair *buckets[HASHTABLE_CAPACITY];\n} ArrayHashMap;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayHashMap *newArrayHashMap() {\n ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n return hmap;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n for (int i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n free(hmap->buckets[i]->val);\n free(hmap->buckets[i]);\n }\n }\n free(hmap);\n}\n\n/* \u6dfb\u52a0\u64cd\u4f5c */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n Pair *Pair = malloc(sizeof(Pair));\n Pair->key = key;\n Pair->val = malloc(strlen(val) + 1);\n strcpy(Pair->val, val);\n\n int index = hashFunc(key);\n hmap->buckets[index] = Pair;\n}\n\n/* \u5220\u9664\u64cd\u4f5c */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n int index = hashFunc(key);\n free(hmap->buckets[index]->val);\n free(hmap->buckets[index]);\n hmap->buckets[index] = NULL;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n Pair *entries;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n entries = malloc(sizeof(Pair) * total);\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n entries[index].key = hmap->buckets[i]->key;\n entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n strcpy(entries[index].val, hmap->buckets[i]->val);\n index++;\n }\n }\n set->set = entries;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u952e */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n int *keys;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n keys = malloc(total * sizeof(int));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n keys[index] = hmap->buckets[i]->key;\n index++;\n }\n }\n set->set = keys;\n set->len = total;\n}\n\n/* \u83b7\u53d6\u6240\u6709\u503c */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n char **vals;\n int i = 0, index = 0;\n int total = 0;\n /* \u7edf\u8ba1\u6709\u6548\u952e\u503c\u5bf9\u6570\u91cf */\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n total++;\n }\n }\n vals = malloc(total * sizeof(char *));\n for (i = 0; i < HASHTABLE_CAPACITY; i++) {\n if (hmap->buckets[i] != NULL) {\n vals[index] = hmap->buckets[i]->val;\n index++;\n }\n }\n set->set = vals;\n set->len = total;\n}\n\n/* \u6253\u5370\u54c8\u5e0c\u8868 */\nvoid print(ArrayHashMap *hmap) {\n int i;\n MapSet set;\n pairSet(hmap, &set);\n Pair *entries = (Pair *)set.set;\n for (i = 0; i < set.len; i++) {\n printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n }\n free(set.set);\n}\n
array_hash_map.kt/* \u952e\u503c\u5bf9 */\nclass Pair(\n var key: Int,\n var value: String\n)\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private val buckets = arrayOfNulls<Pair>(100)\n\n init {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for (i in 0..<100) {\n buckets[i] = null\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n val index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val pair = buckets[index] ?: return null\n return pair.value\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n val pair = Pair(key, value)\n val index = hashFunc(key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n fun pairSet(): MutableList<Pair> {\n val pairSet = ArrayList<Pair>()\n for (pair in buckets) {\n if (pair != null) pairSet.add(pair)\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n fun keySet(): MutableList<Int> {\n val keySet = ArrayList<Int>()\n for (pair in buckets) {\n if (pair != null) keySet.add(pair.key)\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n fun valueSet(): MutableList<String> {\n val valueSet = ArrayList<String>()\n for (pair in buckets) {\n pair?.let { valueSet.add(it.value) }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (kv in pairSet()) {\n val key = kv.key\n val value = kv.value\n println(\"${key}->${value}\")\n }\n }\n}\n\n/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868 */\nclass ArrayHashMap {\n private val buckets = arrayOfNulls<Pair>(100)\n\n init {\n // \u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5305\u542b 100 \u4e2a\u6876\n for (i in 0..<100) {\n buckets[i] = null\n }\n }\n\n /* \u54c8\u5e0c\u51fd\u6570 */\n fun hashFunc(key: Int): Int {\n val index = key % 100\n return index\n }\n\n /* \u67e5\u8be2\u64cd\u4f5c */\n fun get(key: Int): String? {\n val index = hashFunc(key)\n val pair = buckets[index] ?: return null\n return pair.value\n }\n\n /* \u6dfb\u52a0\u64cd\u4f5c */\n fun put(key: Int, value: String) {\n val pair = Pair(key, value)\n val index = hashFunc(key)\n buckets[index] = pair\n }\n\n /* \u5220\u9664\u64cd\u4f5c */\n fun remove(key: Int) {\n val index = hashFunc(key)\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n buckets[index] = null\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9 */\n fun pairSet(): MutableList<Pair> {\n val pairSet = ArrayList<Pair>()\n for (pair in buckets) {\n if (pair != null) pairSet.add(pair)\n }\n return pairSet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u952e */\n fun keySet(): MutableList<Int> {\n val keySet = ArrayList<Int>()\n for (pair in buckets) {\n if (pair != null) keySet.add(pair.key)\n }\n return keySet\n }\n\n /* \u83b7\u53d6\u6240\u6709\u503c */\n fun valueSet(): MutableList<String> {\n val valueSet = ArrayList<String>()\n for (pair in buckets) {\n pair?.let { valueSet.add(it.value) }\n }\n return valueSet\n }\n\n /* \u6253\u5370\u54c8\u5e0c\u8868 */\n fun print() {\n for (kv in pairSet()) {\n val key = kv.key\n val value = kv.value\n println(\"${key}->${value}\")\n }\n }\n}\n
array_hash_map.rb[class]{Pair}-[func]{}\n\n[class]{ArrayHashMap}-[func]{}\n
array_hash_map.zig// \u952e\u503c\u5bf9\nconst Pair = struct {\n key: usize = undefined,\n val: []const u8 = undefined,\n\n pub fn init(key: usize, val: []const u8) Pair {\n return Pair {\n .key = key,\n .val = val,\n };\n }\n};\n\n// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u54c8\u5e0c\u8868\nfn ArrayHashMap(comptime T: type) type {\n return struct {\n bucket: ?std.ArrayList(?T) = null,\n mem_allocator: std.mem.Allocator = undefined,\n\n const Self = @This();\n\n // \u6784\u9020\u51fd\u6570\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n self.mem_allocator = allocator;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u957f\u5ea6\u4e3a 100 \u7684\u6876\uff08\u6570\u7ec4\uff09\n self.bucket = std.ArrayList(?T).init(self.mem_allocator);\n var i: i32 = 0;\n while (i < 100) : (i += 1) {\n try self.bucket.?.append(null);\n }\n }\n\n // \u6790\u6784\u51fd\u6570\n pub fn deinit(self: *Self) void {\n if (self.bucket != null) self.bucket.?.deinit();\n }\n\n // \u54c8\u5e0c\u51fd\u6570\n fn hashFunc(key: usize) usize {\n var index = key % 100;\n return index;\n }\n\n // \u67e5\u8be2\u64cd\u4f5c\n pub fn get(self: *Self, key: usize) []const u8 {\n var index = hashFunc(key);\n var pair = self.bucket.?.items[index];\n return pair.?.val;\n }\n\n // \u6dfb\u52a0\u64cd\u4f5c\n pub fn put(self: *Self, key: usize, val: []const u8) !void {\n var pair = Pair.init(key, val);\n var index = hashFunc(key);\n self.bucket.?.items[index] = pair;\n }\n\n // \u5220\u9664\u64cd\u4f5c\n pub fn remove(self: *Self, key: usize) !void {\n var index = hashFunc(key);\n // \u7f6e\u4e3a null \uff0c\u4ee3\u8868\u5220\u9664\n self.bucket.?.items[index] = null;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\u503c\u5bf9\n pub fn pairSet(self: *Self) !std.ArrayList(T) {\n var entry_set = std.ArrayList(T).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try entry_set.append(item.?);\n }\n return entry_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u952e\n pub fn keySet(self: *Self) !std.ArrayList(usize) {\n var key_set = std.ArrayList(usize).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try key_set.append(item.?.key);\n }\n return key_set;\n } \n\n // \u83b7\u53d6\u6240\u6709\u503c\n pub fn valueSet(self: *Self) !std.ArrayList([]const u8) {\n var value_set = std.ArrayList([]const u8).init(self.mem_allocator);\n for (self.bucket.?.items) |item| {\n if (item == null) continue;\n try value_set.append(item.?.val);\n }\n return value_set;\n }\n\n // \u6253\u5370\u54c8\u5e0c\u8868\n pub fn print(self: *Self) !void {\n var entry_set = try self.pairSet();\n defer entry_set.deinit();\n for (entry_set.items) |item| {\n std.debug.print(\"{} -> {s}\\n\", .{item.key, item.val});\n }\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_hashing/hash_map/#613-hash-collision-and-resizing","title":"6.1.3 \u00a0 Hash collision and resizing","text":"Fundamentally, the role of the hash function is to map the entire input space of all keys to the output space of all array indices. However, the input space is often much larger than the output space. Therefore, theoretically, there must be situations where \"multiple inputs correspond to the same output\".
For the hash function in the above example, if the last two digits of the input key
are the same, the output of the hash function will also be the same. For example, when querying for students with student numbers 12836 and 20336, we find:
12836 % 100 = 36\n20336 % 100 = 36\n
As shown in the Figure 6-3 , both student numbers point to the same name, which is obviously incorrect. This situation where multiple inputs correspond to the same output is known as \"hash collision\".
Figure 6-3 \u00a0 Example of hash collision
It is easy to understand that the larger the capacity \\(n\\) of the hash table, the lower the probability of multiple keys being allocated to the same bucket, and the fewer the collisions. Therefore, expanding the capacity of the hash table can reduce hash collisions.
As shown in the Figure 6-4 , before expansion, key-value pairs (136, A)
and (236, D)
collided; after expansion, the collision is resolved.
Figure 6-4 \u00a0 Hash table expansion
Similar to array expansion, resizing a hash table requires migrating all key-value pairs from the original hash table to the new one, which is time-consuming. Furthermore, since the capacity capacity
of the hash table changes, we need to recalculate the storage positions of all key-value pairs using the hash function, which adds to the computational overhead of the resizing process. Therefore, programming languages often reserve a sufficiently large capacity for the hash table to prevent frequent resizing.
The \"load factor\" is an important concept for hash tables. It is defined as the ratio of the number of elements in the hash table to the number of buckets. It is used to measure the severity of hash collisions and is often used as a trigger for resizing the hash table. For example, in Java, when the load factor exceeds \\(0.75\\), the system will resize the hash table to twice its original size.
"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 Summary","text":""},{"location":"chapter_hashing/summary/#1-key-review","title":"1. \u00a0 Key review","text":"key
, a hash table can retrieve the corresponding value
in \\(O(1)\\) time, which is highly efficient.key
to an array index, allowing access to the corresponding bucket to retrieve the value
.HashMap
uses chaining, while Python's dict
employs open addressing.Q: When does the time complexity of a hash table degrade to \\(O(n)\\)?
The time complexity of a hash table can degrade to \\(O(n)\\) when hash collisions are severe. When the hash function is well-designed, the capacity is set appropriately, and collisions are evenly distributed, the time complexity is \\(O(1)\\). We usually consider the time complexity to be \\(O(1)\\) when using built-in hash tables in programming languages.
Q: Why not use the hash function \\(f(x) = x\\)? This would eliminate collisions.
Under the hash function \\(f(x) = x\\), each element corresponds to a unique bucket index, which is equivalent to an array. However, the input space is usually much larger than the output space (array length), so the last step of a hash function is often to take the modulo of the array length. In other words, the goal of a hash table is to map a larger state space to a smaller one while providing \\(O(1)\\) query efficiency.
Q: Why can hash tables be more efficient than arrays, linked lists, or binary trees, even though they are implemented using these structures?
Firstly, hash tables have higher time efficiency but lower space efficiency. A significant portion of memory in hash tables remains unused.
Secondly, they are only more efficient in specific use cases. If a feature can be implemented with the same time complexity using an array or a linked list, it's usually faster than using a hash table. This is because the computation of the hash function incurs overhead, making the constant factor in the time complexity larger.
Lastly, the time complexity of hash tables can degrade. For example, in chaining, we perform search operations in a linked list or red-black tree, which still risks degrading to \\(O(n)\\) time.
Q: Does multiple hashing also have the flaw of not being able to delete elements directly? Can space marked as deleted be reused?
Multiple hashing is a form of open addressing, and all open addressing methods have the drawback of not being able to delete elements directly; they require marking elements as deleted. Marked spaces can be reused. When inserting new elements into the hash table, and the hash function points to a position marked as deleted, that position can be used by the new element. This maintains the probing sequence of the hash table while ensuring efficient use of space.
Q: Why do hash collisions occur during the search process in linear probing?
During the search process, the hash function points to the corresponding bucket and key-value pair. If the key
doesn't match, it indicates a hash collision. Therefore, linear probing will search downwards at a predetermined step size until the correct key-value pair is found or the search fails.
Q: Why can resizing a hash table alleviate hash collisions?
The last step of a hash function often involves taking the modulo of the array length \\(n\\), to keep the output within the array index range. When resizing, the array length \\(n\\) changes, and the indices corresponding to the keys may also change. Keys that were previously mapped to the same bucket might be distributed across multiple buckets after resizing, thereby mitigating hash collisions.
"},{"location":"chapter_heap/","title":"Chapter 8. \u00a0 Heap","text":"Abstract
The heap is like mountain peaks, stacked and undulating, each with its unique shape.
Among these peaks, the highest one always catches the eye first.
"},{"location":"chapter_heap/#chapter-contents","title":"Chapter contents","text":"In some cases, we want to build a heap using all elements of a list, and this process is known as \"heap construction operation.\"
"},{"location":"chapter_heap/build_heap/#821-implementing-with-heap-insertion-operation","title":"8.2.1 \u00a0 Implementing with heap insertion operation","text":"First, we create an empty heap and then iterate through the list, performing the \"heap insertion operation\" on each element in turn. This means adding the element to the end of the heap and then \"heapifying\" it from bottom to top.
Each time an element is added to the heap, the length of the heap increases by one. Since nodes are added to the binary tree from top to bottom, the heap is constructed \"from top to bottom.\"
Let the number of elements be \\(n\\), and each element's insertion operation takes \\(O(\\log{n})\\) time, thus the time complexity of this heap construction method is \\(O(n \\log n)\\).
"},{"location":"chapter_heap/build_heap/#822-implementing-by-heapifying-through-traversal","title":"8.2.2 \u00a0 Implementing by heapifying through traversal","text":"In fact, we can implement a more efficient method of heap construction in two steps.
After heapifying a node, the subtree with that node as the root becomes a valid sub-heap. Since the traversal is in reverse order, the heap is built \"from bottom to top.\"
The reason for choosing reverse traversal is that it ensures the subtree below the current node is already a valid sub-heap, making the heapification of the current node effective.
It's worth mentioning that since leaf nodes have no children, they naturally form valid sub-heaps and do not need to be heapified. As shown in the following code, the last non-leaf node is the parent of the last node; we start from it and traverse in reverse order to perform heapification:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef __init__(self, nums: list[int]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\"\"\"\n # \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n self.max_heap = nums\n # \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in range(self.parent(self.size() - 1), -1, -1):\n self.sift_down(i)\n
my_heap.cpp/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(vector<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.java/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<Integer> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new ArrayList<>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.cs/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(IEnumerable<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = new List<int>(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var size = Parent(this.Size() - 1);\n for (int i = size; i >= 0; i--) {\n SiftDown(i);\n }\n}\n
my_heap.go/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nfunc newMaxHeap(nums []any) *maxHeap {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n h := &maxHeap{data: nums}\n for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n h.siftDown(i)\n }\n return h\n}\n
my_heap.swift/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\ninit(nums: [Int]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n maxHeap = nums\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0 ... parent(i: size() - 1)).reversed() {\n siftDown(i: i)\n }\n}\n
my_heap.js/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.#maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n this.#siftDown(i);\n }\n}\n
my_heap.ts/* \u6784\u9020\u65b9\u6cd5\uff0c\u5efa\u7acb\u7a7a\u5806\u6216\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nconstructor(nums?: number[]) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n this.maxHeap = nums === undefined ? [] : [...nums];\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n this.siftDown(i);\n }\n}\n
my_heap.dart/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nMaxHeap(List<int> nums) {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n _maxHeap = nums;\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (int i = _parent(size() - 1); i >= 0; i--) {\n siftDown(i);\n }\n}\n
my_heap.rs/* \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\nfn new(nums: Vec<i32>) -> Self {\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n let mut heap = MaxHeap { max_heap: nums };\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for i in (0..=Self::parent(heap.size() - 1)).rev() {\n heap.sift_down(i);\n }\n heap\n}\n
my_heap.c/* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u5207\u7247\u5efa\u5806 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n // \u6240\u6709\u5143\u7d20\u5165\u5806\n MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n maxHeap->size = size;\n memcpy(maxHeap->data, nums, size * sizeof(int));\n for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n siftDown(maxHeap, i);\n }\n return maxHeap;\n}\n
my_heap.kt/* \u5927\u9876\u5806 */\nclass MaxHeap(nums: List<Int>?) {\n // \u4f7f\u7528\u5217\u8868\u800c\u975e\u6570\u7ec4\uff0c\u8fd9\u6837\u65e0\u987b\u8003\u8651\u6269\u5bb9\u95ee\u9898\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n private val maxHeap = ArrayList(nums!!)\n\n /* \u6784\u9020\u51fd\u6570\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806 */\n init {\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n for (i in parent(size() - 1) downTo 0) {\n siftDown(i)\n }\n }\n\n /* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n private fun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u4ea4\u6362\u5143\u7d20 */\n private fun swap(i: Int, j: Int) {\n maxHeap[i] = maxHeap[j].also { maxHeap[j] = maxHeap[i] }\n }\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n fun size(): Int {\n return maxHeap.size\n }\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n return size() == 0\n }\n\n /* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\n fun peek(): Int {\n return maxHeap[0]\n }\n\n /* \u5143\u7d20\u5165\u5806 */\n fun push(value: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(value)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1)\n }\n\n /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n private fun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n val p = parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n }\n\n /* \u5143\u7d20\u51fa\u5806 */\n fun pop(): Int {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u5220\u9664\u8282\u70b9\n val value = maxHeap.removeAt(size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return value\n }\n\n /* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n private fun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n }\n\n /* \u6253\u5370\u5806\uff08\u4e8c\u53c9\u6811\uff09 */\n fun print() {\n val queue = PriorityQueue { a: Int, b: Int -> b - a }\n queue.addAll(maxHeap)\n printHeap(queue)\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{__init__}\n
my_heap.zig// \u6784\u9020\u65b9\u6cd5\uff0c\u6839\u636e\u8f93\u5165\u5217\u8868\u5efa\u5806\nfn init(self: *Self, allocator: std.mem.Allocator, nums: []const T) !void {\n if (self.max_heap != null) return;\n self.max_heap = std.ArrayList(T).init(allocator);\n // \u5c06\u5217\u8868\u5143\u7d20\u539f\u5c01\u4e0d\u52a8\u6dfb\u52a0\u8fdb\u5806\n try self.max_heap.?.appendSlice(nums);\n // \u5806\u5316\u9664\u53f6\u8282\u70b9\u4ee5\u5916\u7684\u5176\u4ed6\u6240\u6709\u8282\u70b9\n var i: usize = parent(self.size() - 1) + 1;\n while (i > 0) : (i -= 1) {\n try self.siftDown(i - 1);\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/build_heap/#823-complexity-analysis","title":"8.2.3 \u00a0 Complexity analysis","text":"Next, let's attempt to calculate the time complexity of this second method of heap construction.
Multiplying the two, we get the time complexity of the heap construction process as \\(O(n \\log n)\\). But this estimate is not accurate, because it does not take into account the nature of the binary tree having far more nodes at the lower levels than at the top.
Let's perform a more accurate calculation. To simplify the calculation, assume a \"perfect binary tree\" with \\(n\\) nodes and height \\(h\\); this assumption does not affect the correctness of the result.
Figure 8-5 \u00a0 Node counts at each level of a perfect binary tree
As shown in the Figure 8-5 , the maximum number of iterations for a node \"to be heapified from top to bottom\" is equal to the distance from that node to the leaf nodes, which is precisely \"node height.\" Therefore, we can sum the \"number of nodes \\(\\times\\) node height\" at each level, to get the total number of heapification iterations for all nodes.
\\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]To simplify the above equation, we need to use knowledge of sequences from high school, first multiply \\(T(h)\\) by \\(2\\), to get:
\\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^h\\times1 \\newline \\end{aligned} \\]By subtracting \\(T(h)\\) from \\(2T(h)\\) using the method of displacement, we get:
\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]Observing the equation, \\(T(h)\\) is an geometric series, which can be directly calculated using the sum formula, resulting in a time complexity of:
\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]Further, a perfect binary tree with height \\(h\\) has \\(n = 2^{h+1} - 1\\) nodes, thus the complexity is \\(O(2^h) = O(n)\\). This calculation shows that the time complexity of inputting a list and constructing a heap is \\(O(n)\\), which is very efficient.
"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 Heap","text":"A \"heap\" is a complete binary tree that satisfies specific conditions and can be mainly divided into two types, as shown in the Figure 8-1 .
Figure 8-1 \u00a0 Min heap and max heap
As a special case of a complete binary tree, heaps have the following characteristics:
It should be noted that many programming languages provide a \"priority queue,\" which is an abstract data structure defined as a queue with priority sorting.
In fact, heaps are often used to implement priority queues, with max heaps equivalent to priority queues where elements are dequeued in descending order. From a usage perspective, we can consider \"priority queue\" and \"heap\" as equivalent data structures. Therefore, this book does not make a special distinction between the two, uniformly referring to them as \"heap.\"
Common operations on heaps are shown in the Table 8-1 , and the method names depend on the programming language.
Table 8-1 \u00a0 Efficiency of Heap Operations
Method name Description Time complexitypush()
Add an element to the heap \\(O(\\log n)\\) pop()
Remove the top element from the heap \\(O(\\log n)\\) peek()
Access the top element (for max/min heap, the max/min value) \\(O(1)\\) size()
Get the number of elements in the heap \\(O(1)\\) isEmpty()
Check if the heap is empty \\(O(1)\\) In practice, we can directly use the heap class (or priority queue class) provided by programming languages.
Similar to sorting algorithms where we have \"ascending order\" and \"descending order,\" we can switch between \"min heap\" and \"max heap\" by setting a flag
or modifying the Comparator
. The code is as follows:
# \u521d\u59cb\u5316\u5c0f\u9876\u5806\nmin_heap, flag = [], 1\n# \u521d\u59cb\u5316\u5927\u9876\u5806\nmax_heap, flag = [], -1\n\n# Python \u7684 heapq \u6a21\u5757\u9ed8\u8ba4\u5b9e\u73b0\u5c0f\u9876\u5806\n# \u8003\u8651\u5c06\u201c\u5143\u7d20\u53d6\u8d1f\u201d\u540e\u518d\u5165\u5806\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u5927\u5c0f\u5173\u7cfb\u98a0\u5012\uff0c\u4ece\u800c\u5b9e\u73b0\u5927\u9876\u5806\n# \u5728\u672c\u793a\u4f8b\u4e2d\uff0cflag = 1 \u65f6\u5bf9\u5e94\u5c0f\u9876\u5806\uff0cflag = -1 \u65f6\u5bf9\u5e94\u5927\u9876\u5806\n\n# \u5143\u7d20\u5165\u5806\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u83b7\u53d6\u5806\u9876\u5143\u7d20\npeek: int = flag * max_heap[0] # 5\n\n# \u5806\u9876\u5143\u7d20\u51fa\u5806\n# \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u83b7\u53d6\u5806\u5927\u5c0f\nsize: int = len(max_heap)\n\n# \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a\nis_empty: bool = not max_heap\n\n# \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
heap.cpp/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u521d\u59cb\u5316\u5927\u9876\u5806\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.top(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
heap.java/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.peek(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.size();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
heap.cs/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nPriorityQueue<int, int> minHeap = new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nint peek = maxHeap.Peek();//5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.Dequeue(); // 5\npeek = maxHeap.Dequeue(); // 4\npeek = maxHeap.Dequeue(); // 3\npeek = maxHeap.Dequeue(); // 2\npeek = maxHeap.Dequeue(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nint size = maxHeap.Count;\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
heap.go// Go \u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0 heap.Interface \u6765\u6784\u5efa\u6574\u6570\u5927\u9876\u5806\n// \u5b9e\u73b0 heap.Interface \u9700\u8981\u540c\u65f6\u5b9e\u73b0 sort.Interface\ntype intHeap []any\n\n// Push heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u63a8\u5165\u5143\u7d20\u5230\u5806\nfunc (h *intHeap) Push(x any) {\n // Push \u548c Pop \u4f7f\u7528 pointer receiver \u4f5c\u4e3a\u53c2\u6570\n // \u56e0\u4e3a\u5b83\u4eec\u4e0d\u4ec5\u4f1a\u5bf9\u5207\u7247\u7684\u5185\u5bb9\u8fdb\u884c\u8c03\u6574\uff0c\u8fd8\u4f1a\u4fee\u6539\u5207\u7247\u7684\u957f\u5ea6\u3002\n *h = append(*h, x.(int))\n}\n\n// Pop heap.Interface \u7684\u65b9\u6cd5\uff0c\u5b9e\u73b0\u5f39\u51fa\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Pop() any {\n // \u5f85\u51fa\u5806\u5143\u7d20\u5b58\u653e\u5728\u6700\u540e\n last := (*h)[len(*h)-1]\n *h = (*h)[:len(*h)-1]\n return last\n}\n\n// Len sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Len() int {\n return len(*h)\n}\n\n// Less sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Less(i, j int) bool {\n // \u5982\u679c\u5b9e\u73b0\u5c0f\u9876\u5806\uff0c\u5219\u9700\u8981\u8c03\u6574\u4e3a\u5c0f\u4e8e\u53f7\n return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// Swap sort.Interface \u7684\u65b9\u6cd5\nfunc (h *intHeap) Swap(i, j int) {\n (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u83b7\u53d6\u5806\u9876\u5143\u7d20\nfunc (h *intHeap) Top() any {\n return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n /* \u521d\u59cb\u5316\u5806 */\n // \u521d\u59cb\u5316\u5927\u9876\u5806\n maxHeap := &intHeap{}\n heap.Init(maxHeap)\n /* \u5143\u7d20\u5165\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u6dfb\u52a0\u5143\u7d20\n heap.Push(maxHeap, 1)\n heap.Push(maxHeap, 3)\n heap.Push(maxHeap, 2)\n heap.Push(maxHeap, 4)\n heap.Push(maxHeap, 5)\n\n /* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\n top := maxHeap.Top()\n fmt.Printf(\"\u5806\u9876\u5143\u7d20\u4e3a %d\\n\", top)\n\n /* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n // \u8c03\u7528 heap.Interface \u7684\u65b9\u6cd5\uff0c\u6765\u79fb\u9664\u5143\u7d20\n heap.Pop(maxHeap) // 5\n heap.Pop(maxHeap) // 4\n heap.Pop(maxHeap) // 3\n heap.Pop(maxHeap) // 2\n heap.Pop(maxHeap) // 1\n\n /* \u83b7\u53d6\u5806\u5927\u5c0f */\n size := len(*maxHeap)\n fmt.Printf(\"\u5806\u5143\u7d20\u6570\u91cf\u4e3a %d\\n\", size)\n\n /* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\n isEmpty := len(*maxHeap) == 0\n fmt.Printf(\"\u5806\u662f\u5426\u4e3a\u7a7a %t\\n\", isEmpty)\n}\n
heap.swift/* \u521d\u59cb\u5316\u5806 */\n// Swift \u7684 Heap \u7c7b\u578b\u540c\u65f6\u652f\u6301\u6700\u5927\u5806\u548c\u6700\u5c0f\u5806\uff0c\u4e14\u9700\u8981\u5f15\u5165 swift-collections\nvar heap = Heap<Int>()\n\n/* \u5143\u7d20\u5165\u5806 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = heap.max()!\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = heap.count\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet isEmpty = heap.isEmpty\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
heap.js// JavaScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.ts// TypeScript \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.dart// Dart \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.rsuse std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u521d\u59cb\u5316\u5927\u9876\u5806\nlet mut max_heap = BinaryHeap::new();\n\n/* \u5143\u7d20\u5165\u5806 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nlet peek = max_heap.peek().unwrap(); // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\nlet peek = max_heap.pop().unwrap(); // 5\nlet peek = max_heap.pop().unwrap(); // 4\nlet peek = max_heap.pop().unwrap(); // 3\nlet peek = max_heap.pop().unwrap(); // 2\nlet peek = max_heap.pop().unwrap(); // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nlet size = max_heap.len();\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nlet is_empty = max_heap.is_empty();\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c// C \u672a\u63d0\u4f9b\u5185\u7f6e Heap \u7c7b\n
heap.kt/* \u521d\u59cb\u5316\u5806 */\n// \u521d\u59cb\u5316\u5c0f\u9876\u5806\nvar minHeap = PriorityQueue<Int>()\n// \u521d\u59cb\u5316\u5927\u9876\u5806\uff08\u4f7f\u7528 lambda \u8868\u8fbe\u5f0f\u4fee\u6539 Comparator \u5373\u53ef\uff09\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u5143\u7d20\u5165\u5806 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u83b7\u53d6\u5806\u9876\u5143\u7d20 */\nvar peek = maxHeap.peek() // 5\n\n/* \u5806\u9876\u5143\u7d20\u51fa\u5806 */\n// \u51fa\u5806\u5143\u7d20\u4f1a\u5f62\u6210\u4e00\u4e2a\u4ece\u5927\u5230\u5c0f\u7684\u5e8f\u5217\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u83b7\u53d6\u5806\u5927\u5c0f */\nval size = maxHeap.size\n\n/* \u5224\u65ad\u5806\u662f\u5426\u4e3a\u7a7a */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u8f93\u5165\u5217\u8868\u5e76\u5efa\u5806 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
heap.rb\n
heap.zig\n
\u53ef\u89c6\u5316\u8fd0\u884c https://pythontutor.com/render.html#code=import%20heapq%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20min_heap,%20flag%20%3D%20%5B%5D,%201%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20max_heap,%20flag%20%3D%20%5B%5D,%20-1%0A%20%20%20%20%0A%20%20%20%20%23%20Python%20%E7%9A%84%20heapq%20%E6%A8%A1%E5%9D%97%E9%BB%98%E8%AE%A4%E5%AE%9E%E7%8E%B0%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E8%80%83%E8%99%91%E5%B0%86%E2%80%9C%E5%85%83%E7%B4%A0%E5%8F%96%E8%B4%9F%E2%80%9D%E5%90%8E%E5%86%8D%E5%85%A5%E5%A0%86%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%B0%86%E5%A4%A7%E5%B0%8F%E5%85%B3%E7%B3%BB%E9%A2%A0%E5%80%92%EF%BC%8C%E4%BB%8E%E8%80%8C%E5%AE%9E%E7%8E%B0%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E5%9C%A8%E6%9C%AC%E7%A4%BA%E4%BE%8B%E4%B8%AD%EF%BC%8Cflag%20%3D%201%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%B0%8F%E9%A1%B6%E5%A0%86%EF%BC%8Cflag%20%3D%20-1%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%201%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%203%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%202%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%205%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%204%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%0A%20%20%20%20peek%20%3D%20flag%20*%20max_heap%5B0%5D%20%23%205%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%0A%20%20%20%20%23%20%E5%87%BA%E5%A0%86%E5%85%83%E7%B4%A0%E4%BC%9A%E5%BD%A2%E6%88%90%E4%B8%80%E4%B8%AA%E4%BB%8E%E5%A4%A7%E5%88%B0%E5%B0%8F%E7%9A%84%E5%BA%8F%E5%88%97%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%205%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%204%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%203%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%202%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%201%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E5%A4%A7%E5%B0%8F%0A%20%20%20%20size%20%3D%20len%28max_heap%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%A0%86%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20not%20max_heap%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%BE%93%E5%85%A5%E5%88%97%E8%A1%A8%E5%B9%B6%E5%BB%BA%E5%A0%86%0A%20%20%20%20min_heap%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20heapq.heapify%28min_heap%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_heap/heap/#812-implementation-of-heaps","title":"8.1.2 \u00a0 Implementation of heaps","text":"The following implementation is of a max heap. To convert it into a min heap, simply invert all size logic comparisons (for example, replace \\(\\geq\\) with \\(\\leq\\)). Interested readers are encouraged to implement it on their own.
"},{"location":"chapter_heap/heap/#1-storage-and-representation-of-heaps","title":"1. \u00a0 Storage and representation of heaps","text":"As mentioned in the \"Binary Trees\" section, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, we will use arrays to store heaps.
When using an array to represent a binary tree, elements represent node values, and indexes represent node positions in the binary tree. Node pointers are implemented through an index mapping formula.
As shown in the Figure 8-2 , given an index \\(i\\), the index of its left child is \\(2i + 1\\), the index of its right child is \\(2i + 2\\), and the index of its parent is \\((i - 1) / 2\\) (floor division). When the index is out of bounds, it signifies a null node or the node does not exist.
Figure 8-2 \u00a0 Representation and storage of heaps
We can encapsulate the index mapping formula into functions for convenient later use:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef left(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\ndef right(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2 # \u5411\u4e0b\u6574\u9664\n
my_heap.cpp/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.java/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.cs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint Right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint Parent(int i) {\n return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.go/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (h *maxHeap) parent(i int) int {\n // \u5411\u4e0b\u6574\u9664\n return (i - 1) / 2\n}\n
my_heap.swift/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc left(i: Int) -> Int {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc right(i: Int) -> Int {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc parent(i: Int) -> Int {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.js/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#left(i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n#right(i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n#parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.ts/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nleft(i: number): number {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nright(i: number): number {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nparent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.dart/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _left(int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint _right(int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint _parent(int i) {\n return (i - 1) ~/ 2; // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rs/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn left(i: usize) -> usize {\n 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfn right(i: usize) -> usize {\n 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfn parent(i: usize) -> usize {\n (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.c/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint left(MaxHeap *maxHeap, int i) {\n return 2 * i + 1;\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nint right(MaxHeap *maxHeap, int i) {\n return 2 * i + 2;\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nint parent(MaxHeap *maxHeap, int i) {\n return (i - 1) / 2;\n}\n
my_heap.kt/* \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun left(i: Int): Int {\n return 2 * i + 1\n}\n\n/* \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfun right(i: Int): Int {\n return 2 * i + 2\n}\n\n/* \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfun parent(i: Int): Int {\n return (i - 1) / 2 // \u5411\u4e0b\u6574\u9664\n}\n
my_heap.rb[class]{MaxHeap}-[func]{left}\n\n[class]{MaxHeap}-[func]{right}\n\n[class]{MaxHeap}-[func]{parent}\n
my_heap.zig// \u83b7\u53d6\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\nfn left(i: usize) usize {\n return 2 * i + 1;\n}\n\n// \u83b7\u53d6\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\nfn right(i: usize) usize {\n return 2 * i + 2;\n}\n\n// \u83b7\u53d6\u7236\u8282\u70b9\u7684\u7d22\u5f15\nfn parent(i: usize) usize {\n // return (i - 1) / 2; // \u5411\u4e0b\u6574\u9664\n return @divFloor(i - 1, 2);\n}\n
"},{"location":"chapter_heap/heap/#2-accessing-the-top-element-of-the-heap","title":"2. \u00a0 Accessing the top element of the heap","text":"The top element of the heap is the root node of the binary tree, which is also the first element of the list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u5806\u9876\u5143\u7d20\"\"\"\n return self.max_heap[0]\n
my_heap.cpp/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap[0];\n}\n
my_heap.java/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return maxHeap.get(0);\n}\n
my_heap.cs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint Peek() {\n return maxHeap[0];\n}\n
my_heap.go/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc (h *maxHeap) peek() any {\n return h.data[0]\n}\n
my_heap.swift/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunc peek() -> Int {\n maxHeap[0]\n}\n
my_heap.js/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek() {\n return this.#maxHeap[0];\n}\n
my_heap.ts/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\npeek(): number {\n return this.maxHeap[0];\n}\n
my_heap.dart/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek() {\n return _maxHeap[0];\n}\n
my_heap.rs/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfn peek(&self) -> Option<i32> {\n self.max_heap.first().copied()\n}\n
my_heap.c/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peek(MaxHeap *maxHeap) {\n return maxHeap->data[0];\n}\n
my_heap.kt/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfun peek(): Int {\n return maxHeap[0]\n}\n
my_heap.rb[class]{MaxHeap}-[func]{peek}\n
my_heap.zig// \u8bbf\u95ee\u5806\u9876\u5143\u7d20\nfn peek(self: *Self) T {\n return self.max_heap.?.items[0];\n} \n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#3-inserting-an-element-into-the-heap","title":"3. \u00a0 Inserting an element into the heap","text":"Given an element val
, we first add it to the bottom of the heap. After addition, since val
may be larger than other elements in the heap, the heap's integrity might be compromised, thus it's necessary to repair the path from the inserted node to the root node. This operation is called \"heapifying\".
Considering starting from the node inserted, perform heapify from bottom to top. As shown in the Figure 8-3 , we compare the value of the inserted node with its parent node, and if the inserted node is larger, we swap them. Then continue this operation, repairing each node in the heap from bottom to top until passing the root node or encountering a node that does not need to be swapped.
<1><2><3><4><5><6><7><8><9>Figure 8-3 \u00a0 Steps of element insertion into the heap
Given a total of \\(n\\) nodes, the height of the tree is \\(O(\\log n)\\). Hence, the loop iterations for the heapify operation are at most \\(O(\\log n)\\), making the time complexity of the element insertion operation \\(O(\\log n)\\). The code is as shown:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef push(self, val: int):\n \"\"\"\u5143\u7d20\u5165\u5806\"\"\"\n # \u6dfb\u52a0\u8282\u70b9\n self.max_heap.append(val)\n # \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\"\"\"\n while True:\n # \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p = self.parent(i)\n # \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p)\n # \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n
my_heap.cpp/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.push_back(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap[i], maxHeap[p]);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.java/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u5165\u5806 */\nvoid Push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.Add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n SiftUp(Size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid SiftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = Parent(i);\n // \u82e5\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p])\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.go/* \u5143\u7d20\u5165\u5806 */\nfunc (h *maxHeap) push(val any) {\n // \u6dfb\u52a0\u8282\u70b9\n h.data = append(h.data, val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n h.siftUp(len(h.data) - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc (h *maxHeap) siftUp(i int) {\n for true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n p := h.parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.swift/* \u5143\u7d20\u5165\u5806 */\nfunc push(val: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.append(val)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(i: size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfunc siftUp(i: Int) {\n var i = i\n while true {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = parent(i: i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if p < 0 || maxHeap[i] <= maxHeap[p] {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.js/* \u5143\u7d20\u5165\u5806 */\npush(val) {\n // \u6dfb\u52a0\u8282\u70b9\n this.#maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.#siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\n#siftUp(i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.#parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u5165\u5806 */\npush(val: number): void {\n // \u6dfb\u52a0\u8282\u70b9\n this.maxHeap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n this.siftUp(this.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nsiftUp(i: number): void {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n const p = this.parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u5165\u5806 */\nvoid push(int val) {\n // \u6dfb\u52a0\u8282\u70b9\n _maxHeap.add(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = _parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u5165\u5806 */\nfn push(&mut self, val: i32) {\n // \u6dfb\u52a0\u8282\u70b9\n self.max_heap.push(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n self.sift_up(self.size() - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfn sift_up(&mut self, mut i: usize) {\n loop {\n // \u8282\u70b9 i \u5df2\u7ecf\u662f\u5806\u9876\u8282\u70b9\u4e86\uff0c\u7ed3\u675f\u5806\u5316\n if i == 0 {\n break;\n }\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n let p = Self::parent(i);\n // \u5f53\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if self.max_heap[i] <= self.max_heap[p] {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.c/* \u5143\u7d20\u5165\u5806 */\nvoid push(MaxHeap *maxHeap, int val) {\n // \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0d\u5e94\u8be5\u6dfb\u52a0\u8fd9\u4e48\u591a\u8282\u70b9\n if (maxHeap->size == MAX_SIZE) {\n printf(\"heap is full!\");\n return;\n }\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap->data[maxHeap->size] = val;\n maxHeap->size++;\n\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n int p = parent(maxHeap, i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
my_heap.kt/* \u5143\u7d20\u5165\u5806 */\nfun push(value: Int) {\n // \u6dfb\u52a0\u8282\u70b9\n maxHeap.add(value)\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n siftUp(size() - 1)\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316 */\nfun siftUp(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n val p = parent(i)\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, p)\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{push}\n\n[class]{MaxHeap}-[func]{sift_up}\n
my_heap.zig// \u5143\u7d20\u5165\u5806\nfn push(self: *Self, val: T) !void {\n // \u6dfb\u52a0\u8282\u70b9\n try self.max_heap.?.append(val);\n // \u4ece\u5e95\u81f3\u9876\u5806\u5316\n try self.siftUp(self.size() - 1);\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u5e95\u81f3\u9876\u5806\u5316\nfn siftUp(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u83b7\u53d6\u8282\u70b9 i \u7684\u7236\u8282\u70b9\n var p = parent(i);\n // \u5f53\u201c\u8d8a\u8fc7\u6839\u8282\u70b9\u201d\u6216\u201c\u8282\u70b9\u65e0\u987b\u4fee\u590d\u201d\u65f6\uff0c\u7ed3\u675f\u5806\u5316\n if (p < 0 or self.max_heap.?.items[i] <= self.max_heap.?.items[p]) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, p);\n // \u5faa\u73af\u5411\u4e0a\u5806\u5316\n i = p;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#4-removing-the-top-element-from-the-heap","title":"4. \u00a0 Removing the top element from the heap","text":"The top element of the heap is the root node of the binary tree, that is, the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree would change, making it difficult to use heapify for repairs subsequently. To minimize changes in element indexes, we use the following steps.
As shown in the Figure 8-4 , the direction of \"heapify from top to bottom\" is opposite to \"heapify from bottom to top\". We compare the value of the root node with its two children and swap it with the largest child. Then repeat this operation until passing the leaf node or encountering a node that does not need to be swapped.
<1><2><3><4><5><6><7><8><9><10>Figure 8-4 \u00a0 Steps of removing the top element from the heap
Similar to the element insertion operation, the time complexity of the top element removal operation is also \\(O(\\log n)\\). The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig my_heap.pydef pop(self) -> int:\n \"\"\"\u5143\u7d20\u51fa\u5806\"\"\"\n # \u5224\u7a7a\u5904\u7406\n if self.is_empty():\n raise IndexError(\"\u5806\u4e3a\u7a7a\")\n # \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1)\n # \u5220\u9664\u8282\u70b9\n val = self.max_heap.pop()\n # \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0)\n # \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n\ndef sift_down(self, i: int):\n \"\"\"\u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\"\"\"\n while True:\n # \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n l, r, ma = self.left(i), self.right(i), i\n if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n ma = l\n if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n ma = r\n # \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i:\n break\n # \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma)\n # \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n
my_heap.cpp/* \u5143\u7d20\u51fa\u5806 */\nvoid pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) {\n throw out_of_range(\"\u5806\u4e3a\u7a7a\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap[0], maxHeap[size() - 1]);\n // \u5220\u9664\u8282\u70b9\n maxHeap.pop_back();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n swap(maxHeap[i], maxHeap[ma]);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.java/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.remove(size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = left(i), r = right(i), ma = i;\n if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n ma = l;\n if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i)\n break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.cs/* \u5143\u7d20\u51fa\u5806 */\nint Pop() {\n // \u5224\u7a7a\u5904\u7406\n if (IsEmpty())\n throw new IndexOutOfRangeException();\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n Swap(0, Size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap.Last();\n maxHeap.RemoveAt(Size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n SiftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid SiftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = Left(i), r = Right(i), ma = i;\n if (l < Size() && maxHeap[l] > maxHeap[ma])\n ma = l;\n if (r < Size() && maxHeap[r] > maxHeap[ma])\n ma = r;\n // \u82e5\u201c\u8282\u70b9 i \u6700\u5927\u201d\u6216\u201c\u8d8a\u8fc7\u53f6\u8282\u70b9\u201d\uff0c\u5219\u7ed3\u675f\u5806\u5316\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n Swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.go/* \u5143\u7d20\u51fa\u5806 */\nfunc (h *maxHeap) pop() any {\n // \u5224\u7a7a\u5904\u7406\n if h.isEmpty() {\n fmt.Println(\"error\")\n return nil\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n h.swap(0, h.size()-1)\n // \u5220\u9664\u8282\u70b9\n val := h.data[len(h.data)-1]\n h.data = h.data[:len(h.data)-1]\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n h.siftDown(0)\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc (h *maxHeap) siftDown(i int) {\n for true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n l, r, max := h.left(i), h.right(i), i\n if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n max = l\n }\n if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n max = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if max == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n h.swap(i, max)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max\n }\n}\n
my_heap.swift/* \u5143\u7d20\u51fa\u5806 */\nfunc pop() -> Int {\n // \u5224\u7a7a\u5904\u7406\n if isEmpty() {\n fatalError(\"\u5806\u4e3a\u7a7a\")\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(i: 0, j: size() - 1)\n // \u5220\u9664\u8282\u70b9\n let val = maxHeap.remove(at: size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(i: 0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfunc siftDown(i: Int) {\n var i = i\n while true {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let l = left(i: i)\n let r = right(i: i)\n var ma = i\n if l < size(), maxHeap[l] > maxHeap[ma] {\n ma = l\n }\n if r < size(), maxHeap[r] > maxHeap[ma] {\n ma = r\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i: i, j: ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.js/* \u5143\u7d20\u51fa\u5806 */\npop() {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new Error('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.#swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.#maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.#siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\n#siftDown(i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.#left(i),\n r = this.#right(i);\n let ma = i;\n if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.#swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.ts/* \u5143\u7d20\u51fa\u5806 */\npop(): number {\n // \u5224\u7a7a\u5904\u7406\n if (this.isEmpty()) throw new RangeError('Heap is empty.');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n this.swap(0, this.size() - 1);\n // \u5220\u9664\u8282\u70b9\n const val = this.maxHeap.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n this.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nsiftDown(i: number): void {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n const l = this.left(i),\n r = this.right(i);\n let ma = i;\n if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma === i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n this.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.dart/* \u5143\u7d20\u51fa\u5806 */\nint pop() {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw Exception('\u5806\u4e3a\u7a7a');\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n _swap(0, size() - 1);\n // \u5220\u9664\u8282\u70b9\n int val = _maxHeap.removeLast();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n int l = _left(i);\n int r = _right(i);\n int ma = i;\n if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n _swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.rs/* \u5143\u7d20\u51fa\u5806 */\nfn pop(&mut self) -> i32 {\n // \u5224\u7a7a\u5904\u7406\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n let val = self.max_heap.remove(self.size() - 1);\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n self.sift_down(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n val\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfn sift_down(&mut self, mut i: usize) {\n loop {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n ma = l;\n }\n if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n ma = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if ma == i {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
my_heap.c/* \u5143\u7d20\u51fa\u5806 */\nint pop(MaxHeap *maxHeap) {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty(maxHeap)) {\n printf(\"heap is empty!\");\n return INT_MAX;\n }\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(maxHeap, 0, size(maxHeap) - 1);\n // \u5220\u9664\u8282\u70b9\n int val = maxHeap->data[maxHeap->size - 1];\n maxHeap->size--;\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(maxHeap, 0);\n\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a max\n int l = left(maxHeap, i);\n int r = right(maxHeap, i);\n int max = i;\n if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n max = l;\n }\n if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n max = r;\n }\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (max == i) {\n break;\n }\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(maxHeap, i, max);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = max;\n }\n}\n
my_heap.kt/* \u5143\u7d20\u51fa\u5806 */\nfun pop(): Int {\n // \u5224\u7a7a\u5904\u7406\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n swap(0, size() - 1)\n // \u5220\u9664\u8282\u70b9\n val value = maxHeap.removeAt(size() - 1)\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n siftDown(0)\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return value\n}\n\n/* \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316 */\nfun siftDown(it: Int) {\n // Kotlin\u7684\u51fd\u6570\u53c2\u6570\u4e0d\u53ef\u53d8\uff0c\u56e0\u6b64\u521b\u5efa\u4e34\u65f6\u53d8\u91cf\n var i = it\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n val l = left(i)\n val r = right(i)\n var ma = i\n if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break\n // \u4ea4\u6362\u4e24\u8282\u70b9\n swap(i, ma)\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma\n }\n}\n
my_heap.rb[class]{MaxHeap}-[func]{pop}\n\n[class]{MaxHeap}-[func]{sift_down}\n
my_heap.zig// \u5143\u7d20\u51fa\u5806\nfn pop(self: *Self) !T {\n // \u5224\u65ad\u5904\u7406\n if (self.isEmpty()) unreachable;\n // \u4ea4\u6362\u6839\u8282\u70b9\u4e0e\u6700\u53f3\u53f6\u8282\u70b9\uff08\u4ea4\u6362\u9996\u5143\u7d20\u4e0e\u5c3e\u5143\u7d20\uff09\n try self.swap(0, self.size() - 1);\n // \u5220\u9664\u8282\u70b9\n var val = self.max_heap.?.pop();\n // \u4ece\u9876\u81f3\u5e95\u5806\u5316\n try self.siftDown(0);\n // \u8fd4\u56de\u5806\u9876\u5143\u7d20\n return val;\n} \n\n// \u4ece\u8282\u70b9 i \u5f00\u59cb\uff0c\u4ece\u9876\u81f3\u5e95\u5806\u5316\nfn siftDown(self: *Self, i_: usize) !void {\n var i = i_;\n while (true) {\n // \u5224\u65ad\u8282\u70b9 i, l, r \u4e2d\u503c\u6700\u5927\u7684\u8282\u70b9\uff0c\u8bb0\u4e3a ma\n var l = left(i);\n var r = right(i);\n var ma = i;\n if (l < self.size() and self.max_heap.?.items[l] > self.max_heap.?.items[ma]) ma = l;\n if (r < self.size() and self.max_heap.?.items[r] > self.max_heap.?.items[ma]) ma = r;\n // \u82e5\u8282\u70b9 i \u6700\u5927\u6216\u7d22\u5f15 l, r \u8d8a\u754c\uff0c\u5219\u65e0\u987b\u7ee7\u7eed\u5806\u5316\uff0c\u8df3\u51fa\n if (ma == i) break;\n // \u4ea4\u6362\u4e24\u8282\u70b9\n try self.swap(i, ma);\n // \u5faa\u73af\u5411\u4e0b\u5806\u5316\n i = ma;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_heap/heap/#813-common-applications-of-heaps","title":"8.1.3 \u00a0 Common applications of heaps","text":"Q: Is the \"heap\" in data structures the same concept as the \"heap\" in memory management?
The two are not the same concept, even though they are both referred to as \"heap\". The heap in computer system memory is part of dynamic memory allocation, where the program can use it to store data during execution. The program can request a certain amount of heap memory to store complex structures like objects and arrays. When these data are no longer needed, the program needs to release this memory to prevent memory leaks. Compared to stack memory, the management and usage of heap memory need to be more cautious, as improper use may lead to memory leaks and dangling pointers.
"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 Top-k problem","text":"Question
Given an unordered array nums
of length \\(n\\), return the largest \\(k\\) elements in the array.
For this problem, we will first introduce two straightforward solutions, then explain a more efficient heap-based method.
"},{"location":"chapter_heap/top_k/#831-method-1-iterative-selection","title":"8.3.1 \u00a0 Method 1: Iterative selection","text":"We can perform \\(k\\) rounds of iterations as shown in the Figure 8-6 , extracting the \\(1^{st}\\), \\(2^{nd}\\), \\(\\dots\\), \\(k^{th}\\) largest elements in each round, with a time complexity of \\(O(nk)\\).
This method is only suitable when \\(k \\ll n\\), as the time complexity approaches \\(O(n^2)\\) when \\(k\\) is close to \\(n\\), which is very time-consuming.
Figure 8-6 \u00a0 Iteratively finding the largest k elements
Tip
When \\(k = n\\), we can obtain a complete ordered sequence, which is equivalent to the \"selection sort\" algorithm.
"},{"location":"chapter_heap/top_k/#832-method-2-sorting","title":"8.3.2 \u00a0 Method 2: Sorting","text":"As shown in the Figure 8-7 , we can first sort the array nums
and then return the last \\(k\\) elements, with a time complexity of \\(O(n \\log n)\\).
Clearly, this method \"overachieves\" the task, as we only need to find the largest \\(k\\) elements, without the need to sort the other elements.
Figure 8-7 \u00a0 Sorting to find the largest k elements
"},{"location":"chapter_heap/top_k/#833-method-3-heap","title":"8.3.3 \u00a0 Method 3: Heap","text":"We can solve the Top-k problem more efficiently based on heaps, as shown in the following process.
Figure 8-8 \u00a0 Find the largest k elements based on heap
Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig top_k.pydef top_k_heap(nums: list[int], k: int) -> list[int]:\n \"\"\"\u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\"\"\"\n # \u521d\u59cb\u5316\u5c0f\u9876\u5806\n heap = []\n # \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i in range(k):\n heapq.heappush(heap, nums[i])\n # \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in range(k, len(nums)):\n # \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap[0]:\n heapq.heappop(heap)\n heapq.heappush(heap, nums[i])\n return heap\n
top_k.cpp/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n priority_queue<int, vector<int>, greater<int>> heap;\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.push(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.size(); i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.top()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.java/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n Queue<Integer> heap = new PriorityQueue<Integer>();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.offer(nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll();\n heap.offer(nums[i]);\n }\n }\n return heap;\n}\n
top_k.cs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n PriorityQueue<int, int> heap = new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n heap.Enqueue(nums[i], nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.Length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.Peek()) {\n heap.Dequeue();\n heap.Enqueue(nums[i], nums[i]);\n }\n }\n return heap;\n}\n
top_k.go/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums []int, k int) *minHeap {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n h := &minHeap{}\n heap.Init(h)\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for i := 0; i < k; i++ {\n heap.Push(h, nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i := k; i < len(nums); i++ {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > h.Top().(int) {\n heap.Pop(h)\n heap.Push(h, nums[i])\n }\n }\n return h\n}\n
top_k.swift/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5c0f\u9876\u5806\uff0c\u5e76\u5c06\u524d k \u4e2a\u5143\u7d20\u5efa\u5806\n var heap = Heap(nums.prefix(k))\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for i in nums.indices.dropFirst(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if nums[i] > heap.min()! {\n _ = heap.removeMin()\n heap.insert(nums[i])\n }\n }\n return heap.unordered\n}\n
top_k.js/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap, val) {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums, k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.ts/* \u5143\u7d20\u5165\u5806 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n // \u5143\u7d20\u53d6\u53cd\n maxHeap.push(-val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.pop();\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n // \u5143\u7d20\u53d6\u53cd\n return -maxHeap.peek();\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n // \u5143\u7d20\u53d6\u53cd\n return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfunction topKHeap(nums: number[], k: number): number[] {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n const maxHeap = new MaxHeap([]);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (let i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (let i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n // \u8fd4\u56de\u5806\u4e2d\u5143\u7d20\n return getMinHeap(maxHeap);\n}\n
top_k.dart/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nMinHeap topKHeap(List<int> nums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\uff0c\u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n MinHeap heap = MinHeap(nums.sublist(0, k));\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < nums.length; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.pop();\n heap.push(nums[i]);\n }\n }\n return heap;\n}\n
top_k.rs/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n // BinaryHeap \u662f\u5927\u9876\u5806\uff0c\u4f7f\u7528 Reverse \u5c06\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u5b9e\u73b0\u5c0f\u9876\u5806\n let mut heap = BinaryHeap::<Reverse<i32>>::new();\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for &num in nums.iter().take(k) {\n heap.push(Reverse(num));\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for &num in nums.iter().skip(k) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if num > heap.peek().unwrap().0 {\n heap.pop();\n heap.push(Reverse(num));\n }\n }\n heap\n}\n
top_k.c/* \u5143\u7d20\u5165\u5806 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n // \u5143\u7d20\u53d6\u53cd\n push(maxHeap, -val);\n}\n\n/* \u5143\u7d20\u51fa\u5806 */\nint popMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -pop(maxHeap);\n}\n\n/* \u8bbf\u95ee\u5806\u9876\u5143\u7d20 */\nint peekMinHeap(MaxHeap *maxHeap) {\n // \u5143\u7d20\u53d6\u53cd\n return -peek(maxHeap);\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n/* \u53d6\u51fa\u5806\u4e2d\u5143\u7d20 */\nint *getMinHeap(MaxHeap *maxHeap) {\n // \u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\u5e76\u5b58\u5165 res \u6570\u7ec4\n int *res = (int *)malloc(maxHeap->size * sizeof(int));\n for (int i = 0; i < maxHeap->size; i++) {\n res[i] = -maxHeap->data[i];\n }\n return res;\n}\n\n// \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20\u7684\u51fd\u6570\nint *topKHeap(int *nums, int sizeNums, int k) {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n // \u8bf7\u6ce8\u610f\uff1a\u6211\u4eec\u5c06\u5806\u4e2d\u6240\u6709\u5143\u7d20\u53d6\u53cd\uff0c\u4ece\u800c\u7528\u5927\u9876\u5806\u6765\u6a21\u62df\u5c0f\u9876\u5806\n int *empty = (int *)malloc(0);\n MaxHeap *maxHeap = newMaxHeap(empty, 0);\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (int i = 0; i < k; i++) {\n pushMinHeap(maxHeap, nums[i]);\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (int i = k; i < sizeNums; i++) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > peekMinHeap(maxHeap)) {\n popMinHeap(maxHeap);\n pushMinHeap(maxHeap, nums[i]);\n }\n }\n int *res = getMinHeap(maxHeap);\n // \u91ca\u653e\u5185\u5b58\n delMaxHeap(maxHeap);\n return res;\n}\n
top_k.kt/* \u57fa\u4e8e\u5806\u67e5\u627e\u6570\u7ec4\u4e2d\u6700\u5927\u7684 k \u4e2a\u5143\u7d20 */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n // \u521d\u59cb\u5316\u5c0f\u9876\u5806\n val heap = PriorityQueue<Int>()\n // \u5c06\u6570\u7ec4\u7684\u524d k \u4e2a\u5143\u7d20\u5165\u5806\n for (i in 0..<k) {\n heap.offer(nums[i])\n }\n // \u4ece\u7b2c k+1 \u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u4fdd\u6301\u5806\u7684\u957f\u5ea6\u4e3a k\n for (i in k..<nums.size) {\n // \u82e5\u5f53\u524d\u5143\u7d20\u5927\u4e8e\u5806\u9876\u5143\u7d20\uff0c\u5219\u5c06\u5806\u9876\u5143\u7d20\u51fa\u5806\u3001\u5f53\u524d\u5143\u7d20\u5165\u5806\n if (nums[i] > heap.peek()) {\n heap.poll()\n heap.offer(nums[i])\n }\n }\n return heap\n}\n
top_k.rb[class]{}-[func]{top_k_heap}\n
top_k.zig[class]{}-[func]{topKHeap}\n
Code Visualization Full Screen >
A total of \\(n\\) rounds of heap insertions and deletions are performed, with the maximum heap size being \\(k\\), hence the time complexity is \\(O(n \\log k)\\). This method is very efficient; when \\(k\\) is small, the time complexity tends towards \\(O(n)\\); when \\(k\\) is large, the time complexity will not exceed \\(O(n \\log n)\\).
Additionally, this method is suitable for scenarios with dynamic data streams. By continuously adding data, we can maintain the elements within the heap, thereby achieving dynamic updates of the largest \\(k\\) elements.
"},{"location":"chapter_introduction/","title":"Chapter 1. \u00a0 Introduction to algorithms","text":"Abstract
A graceful maiden dances, intertwined with the data, her skirt swaying to the melody of algorithms.
She invites you to a dance, follow her steps, and enter the world of algorithms full of logic and beauty.
"},{"location":"chapter_introduction/#chapter-contents","title":"Chapter contents","text":"When we hear the word \"algorithm,\" we naturally think of mathematics. However, many algorithms do not involve complex mathematics but rely more on basic logic, which can be seen everywhere in our daily lives.
Before formally discussing algorithms, there's an interesting fact worth sharing: you have already unconsciously learned many algorithms and have become accustomed to applying them in your daily life. Here, I will give a few specific examples to prove this point.
Example 1: Looking Up a Dictionary. In an English dictionary, words are listed alphabetically. Suppose we're searching for a word that starts with the letter \\(r\\). This is typically done in the following way:
1.
and 2.
until you find the page where the word starts with \\(r\\).Figure 1-1 \u00a0 Process of Looking Up a Dictionary
This essential skill for elementary students, looking up a dictionary, is actually the famous \"Binary Search\" algorithm. From a data structure perspective, we can consider the dictionary as a sorted \"array\"; from an algorithmic perspective, the series of actions taken to look up a word in the dictionary can be viewed as \"Binary Search.\"
Example 2: Organizing Playing Cards. When playing cards, we need to arrange the cards in our hand in ascending order, as shown in the following process.
2.
until all cards are in order.Figure 1-2 \u00a0 Playing cards sorting process
The above method of organizing playing cards is essentially the \"Insertion Sort\" algorithm, which is very efficient for small datasets. Many programming languages' sorting functions include the insertion sort.
Example 3: Making Change. Suppose we buy goods worth \\(69\\) yuan at a supermarket and give the cashier \\(100\\) yuan, then the cashier needs to give us \\(31\\) yuan in change. They would naturally complete the thought process as shown below.
Figure 1-3 \u00a0 Change making process
In the above steps, we make the best choice at each step (using the largest denomination possible), ultimately resulting in a feasible change-making plan. From the perspective of data structures and algorithms, this method is essentially a \"Greedy\" algorithm.
From cooking a meal to interstellar travel, almost all problem-solving involves algorithms. The advent of computers allows us to store data structures in memory and write code to call the CPU and GPU to execute algorithms. In this way, we can transfer real-life problems to computers, solving various complex issues more efficiently.
Tip
If concepts such as data structures, algorithms, arrays, and binary search still seem somewhat obsecure, I encourage you to continue reading. This book will gently guide you into the realm of understanding data structures and algorithms.
"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 Summary","text":"An \"algorithm\" is a set of instructions or steps to solve a specific problem within a finite amount of time. It has the following characteristics:
A \"data structure\" is a way of organizing and storing data in a computer, with the following design goals:
Designing data structures is a balancing act, often requiring trade-offs. If you want to improve in one aspect, you often need to compromise in another. Here are two examples:
As shown in the Figure 1-4 , data structures and algorithms are highly related and closely integrated, specifically in the following three aspects:
Figure 1-4 \u00a0 Relationship between data structures and algorithms
Data structures and algorithms can be likened to a set of building blocks, as illustrated in the Figure 1-5 . A building block set includes numerous pieces, accompanied by detailed assembly instructions. Following these instructions step by step allows us to construct an intricate block model.
Figure 1-5 \u00a0 Assembling blocks
The detailed correspondence between the two is shown in the Table 1-1 .
Table 1-1 \u00a0 Comparing data structures and algorithms to building blocks
Data Structures and Algorithms Building Blocks Input data Unassembled blocks Data structure Organization of blocks, including shape, size, connections, etc Algorithm A series of steps to assemble the blocks into the desired shape Output data Completed Block modelIt's worth noting that data structures and algorithms are independent of programming languages. For this reason, this book is able to provide implementations in multiple programming languages.
Conventional Abbreviation
In real-life discussions, we often refer to \"Data Structures and Algorithms\" simply as \"Algorithms\". For example, the well-known LeetCode algorithm problems actually test both data structure and algorithm knowledge.
"},{"location":"chapter_preface/","title":"Chapter 0. \u00a0 Preface","text":"Abstract
Algorithms are like a beautiful symphony, with each line of code flowing like a rhythm.
May this book ring softly in your mind, leaving a unique and profound melody.
"},{"location":"chapter_preface/#chapter-contents","title":"Chapter contents","text":"This open-source project aims to create a free, and beginner-friendly crash course on data structures and algorithms.
If you are new to algorithms with limited exposure, or you have accumulated some experience in algorithms, but you only have a vague understanding of data structures and algorithms, and you are constantly jumping between \"yep\" and \"hmm\", then this book is for you!
If you have already accumulated a certain amount of problem-solving experience, and are familiar with most types of problems, then this book can help you review and organize your algorithm knowledge system. The repository's source code can be used as a \"problem-solving toolkit\" or an \"algorithm cheat sheet\".
If you are an algorithm expert, we look forward to receiving your valuable suggestions, or join us and collaborate.
Prerequisites
You should know how to write and read simple code in at least one programming language.
"},{"location":"chapter_preface/about_the_book/#012-content-structure","title":"0.1.2 \u00a0 Content structure","text":"The main content of the book is shown in the following figure.
Figure 0-1 \u00a0 Main content of the book
"},{"location":"chapter_preface/about_the_book/#013-acknowledgements","title":"0.1.3 \u00a0 Acknowledgements","text":"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.
The code review work for this book was completed by codingonion, Gonglja, gvenusleo, hpstory, justin\u2010tse, 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.
Throughout the creation of this book, numerous individuals provided invaluable assistance, including but not limited to:
Throughout the writing journey, I delved into numerous textbooks and articles on data structures and algorithms. These works served as exemplary models, ensuring the accuracy and quality of this book's content. I extend my gratitude to all who preceded me for their invaluable contributions!
This book advocates a combination of hands-on and minds-on learning, inspired in this regard by \"Dive into Deep Learning\". I highly recommend this excellent book to all readers.
Heartfelt thanks to my parents, whose ongoing support and encouragement have allowed me to do this interesting work.
"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 How to read","text":"Tip
For the best reading experience, it is recommended that you read through this section.
"},{"location":"chapter_preface/suggestions/#021-writing-conventions","title":"0.2.1 \u00a0 Writing conventions","text":"None
to mean null
.\"\"\"Header comments for labeling functions, classes, test samples, etc\"\"\"\n\n# Comments for explaining details\n\n\"\"\"\nMultiline\ncomments\n\"\"\"\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
/* Header comments for labeling functions, classes, test samples, etc */\n\n// Comments for explaining details.\n\n/**\n * Multiline\n * comments\n */\n
// Header comments for labeling functions, classes, test samples, etc\n\n// Comments for explaining details.\n\n// Multiline\n// comments\n
"},{"location":"chapter_preface/suggestions/#022-efficient-learning-via-animated-illustrations","title":"0.2.2 \u00a0 Efficient learning via animated illustrations","text":"Compared with text, videos and pictures have a higher density of information and are more structured, making them easier to understand. In this book, key and difficult concepts are mainly presented through animations and illustrations, with text serving as explanations and supplements.
When encountering content with animations or illustrations as shown in the Figure 0-2 , prioritize understanding the figure, with text as supplementary, integrating both for a comprehensive understanding.
Figure 0-2 \u00a0 Animated illustration example
"},{"location":"chapter_preface/suggestions/#023-deepen-understanding-through-coding-practice","title":"0.2.3 \u00a0 Deepen understanding through coding practice","text":"The source code of this book is hosted on the GitHub Repository. As shown in the Figure 0-3 , the source code comes with test examples and can be executed with just a single click.
If time permits, it's recommended to type out the code yourself. If pressed for time, at least read and run all the codes.
Compared to just reading code, writing code often yields more learning. Learning by doing is the real way to learn.
Figure 0-3 \u00a0 Running code example
Setting up to run the code involves three main steps.
Step 1: Install a local programming environment. Follow the tutorial in the appendix for installation, or skip this step if already installed.
Step 2: Clone or download the code repository. Visit the GitHub Repository.
If Git is installed, use the following command to clone the repository:
git clone https://github.com/krahets/hello-algo.git\n
Alternatively, you can also click the \"Download ZIP\" button at the location shown in the Figure 0-4 to directly download the code as a compressed ZIP file. Then, you can simply extract it locally.
Figure 0-4 \u00a0 Cloning repository and downloading code
Step 3: Run the source code. As shown in the Figure 0-5 , for the code block labeled with the file name at the top, we can find the corresponding source code file in the codes
folder of the repository. These files can be executed with a single click, which will help you save unnecessary debugging time and allow you to focus on learning.
Figure 0-5 \u00a0 Code block and corresponding source code file
"},{"location":"chapter_preface/suggestions/#024-learning-together-in-discussion","title":"0.2.4 \u00a0 Learning together in discussion","text":"While reading this book, please don't skip over the points that you didn't learn. Feel free to post your questions in the comment section. We will be happy to answer them and can usually respond within two days.
As illustrated in the Figure 0-6 , each chapter features a comment section at the bottom. I encourage you to pay attention to these comments. They not only expose you to others' encountered problems, aiding in identifying knowledge gaps and sparking deeper contemplation, but also invite you to generously contribute by answering fellow readers' inquiries, sharing insights, and fostering mutual improvement.
Figure 0-6 \u00a0 Comment section example
"},{"location":"chapter_preface/suggestions/#025-algorithm-learning-path","title":"0.2.5 \u00a0 Algorithm learning path","text":"Overall, the journey of mastering data structures and algorithms can be divided into three stages:
As shown in the Figure 0-7 , this book mainly covers \u201cStage 1,\u201d aiming to help you more efficiently embark on Stages 2 and 3.
Figure 0-7 \u00a0 Algorithm learning path
"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 Summary","text":"Abstract
A stack is like cats placed on top of each other, while a queue is like cats lined up one by one.
They represent the logical relationships of Last-In-First-Out (LIFO) and First-In-First-Out (FIFO), respectively.
"},{"location":"chapter_stack_and_queue/#chapter-contents","title":"Chapter contents","text":"In a queue, we can only delete elements from the head or add elements to the tail. As shown in the following diagram, a \"double-ended queue (deque)\" offers more flexibility, allowing the addition or removal of elements at both the head and the tail.
Figure 5-7 \u00a0 Operations in double-ended queue
"},{"location":"chapter_stack_and_queue/deque/#531-common-operations-in-double-ended-queue","title":"5.3.1 \u00a0 Common operations in double-ended queue","text":"The common operations in a double-ended queue are listed below, and the names of specific methods depend on the programming language used.
Table 5-3 \u00a0 Efficiency of double-ended queue operations
Method Name Description Time ComplexitypushFirst()
Add an element to the head \\(O(1)\\) pushLast()
Add an element to the tail \\(O(1)\\) popFirst()
Remove the first element \\(O(1)\\) popLast()
Remove the last element \\(O(1)\\) peekFirst()
Access the first element \\(O(1)\\) peekLast()
Access the last element \\(O(1)\\) Similarly, we can directly use the double-ended queue classes implemented in programming languages:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig deque.pyfrom collections import deque\n\n# Initialize the deque\ndeque: deque[int] = deque()\n\n# Enqueue elements\ndeque.append(2) # Add to the tail\ndeque.append(5)\ndeque.append(4)\ndeque.appendleft(3) # Add to the head\ndeque.appendleft(1)\n\n# Access elements\nfront: int = deque[0] # The first element\nrear: int = deque[-1] # The last element\n\n# Dequeue elements\npop_front: int = deque.popleft() # The first element dequeued\npop_rear: int = deque.pop() # The last element dequeued\n\n# Get the length of the deque\nsize: int = len(deque)\n\n# Check if the deque is empty\nis_empty: bool = len(deque) == 0\n
deque.cpp/* Initialize the deque */\ndeque<int> deque;\n\n/* Enqueue elements */\ndeque.push_back(2); // Add to the tail\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to the head\ndeque.push_front(1);\n\n/* Access elements */\nint front = deque.front(); // The first element\nint back = deque.back(); // The last element\n\n/* Dequeue elements */\ndeque.pop_front(); // The first element dequeued\ndeque.pop_back(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.size();\n\n/* Check if the deque is empty */\nbool empty = deque.empty();\n
deque.java/* Initialize the deque */\nDeque<Integer> deque = new LinkedList<>();\n\n/* Enqueue elements */\ndeque.offerLast(2); // Add to the tail\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3); // Add to the head\ndeque.offerFirst(1);\n\n/* Access elements */\nint peekFirst = deque.peekFirst(); // The first element\nint peekLast = deque.peekLast(); // The last element\n\n/* Dequeue elements */\nint popFirst = deque.pollFirst(); // The first element dequeued\nint popLast = deque.pollLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.size();\n\n/* Check if the deque is empty */\nboolean isEmpty = deque.isEmpty();\n
deque.cs/* Initialize the deque */\n// In C#, LinkedList is used as a deque\nLinkedList<int> deque = new();\n\n/* Enqueue elements */\ndeque.AddLast(2); // Add to the tail\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3); // Add to the head\ndeque.AddFirst(1);\n\n/* Access elements */\nint peekFirst = deque.First.Value; // The first element\nint peekLast = deque.Last.Value; // The last element\n\n/* Dequeue elements */\ndeque.RemoveFirst(); // The first element dequeued\ndeque.RemoveLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.Count;\n\n/* Check if the deque is empty */\nbool isEmpty = deque.Count == 0;\n
deque_test.go/* Initialize the deque */\n// In Go, use list as a deque\ndeque := list.New()\n\n/* Enqueue elements */\ndeque.PushBack(2) // Add to the tail\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3) // Add to the head\ndeque.PushFront(1)\n\n/* Access elements */\nfront := deque.Front() // The first element\nrear := deque.Back() // The last element\n\n/* Dequeue elements */\ndeque.Remove(front) // The first element dequeued\ndeque.Remove(rear) // The last element dequeued\n\n/* Get the length of the deque */\nsize := deque.Len()\n\n/* Check if the deque is empty */\nisEmpty := deque.Len() == 0\n
deque.swift/* Initialize the deque */\n// Swift does not have a built-in deque class, so Array can be used as a deque\nvar deque: [Int] = []\n\n/* Enqueue elements */\ndeque.append(2) // Add to the tail\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // Add to the head\ndeque.insert(1, at: 0)\n\n/* Access elements */\nlet peekFirst = deque.first! // The first element\nlet peekLast = deque.last! // The last element\n\n/* Dequeue elements */\n// Using Array, popFirst has a complexity of O(n)\nlet popFirst = deque.removeFirst() // The first element dequeued\nlet popLast = deque.removeLast() // The last element dequeued\n\n/* Get the length of the deque */\nlet size = deque.count\n\n/* Check if the deque is empty */\nlet isEmpty = deque.isEmpty\n
deque.js/* Initialize the deque */\n// JavaScript does not have a built-in deque, so Array is used as a deque\nconst deque = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Note that unshift() has a time complexity of O(n) as it's an array\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst = deque[0]; // The first element\nconst peekLast = deque[deque.length - 1]; // The last element\n\n/* Dequeue elements */\n// Note that shift() has a time complexity of O(n) as it's an array\nconst popFront = deque.shift(); // The first element dequeued\nconst popBack = deque.pop(); // The last element dequeued\n\n/* Get the length of the deque */\nconst size = deque.length;\n\n/* Check if the deque is empty */\nconst isEmpty = size === 0;\n
deque.ts/* Initialize the deque */\n// TypeScript does not have a built-in deque, so Array is used as a deque\nconst deque: number[] = [];\n\n/* Enqueue elements */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// Note that unshift() has a time complexity of O(n) as it's an array\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* Access elements */\nconst peekFirst: number = deque[0]; // The first element\nconst peekLast: number = deque[deque.length - 1]; // The last element\n\n/* Dequeue elements */\n// Note that shift() has a time complexity of O(n) as it's an array\nconst popFront: number = deque.shift() as number; // The first element dequeued\nconst popBack: number = deque.pop() as number; // The last element dequeued\n\n/* Get the length of the deque */\nconst size: number = deque.length;\n\n/* Check if the deque is empty */\nconst isEmpty: boolean = size === 0;\n
deque.dart/* Initialize the deque */\n// In Dart, Queue is defined as a deque\nQueue<int> deque = Queue<int>();\n\n/* Enqueue elements */\ndeque.addLast(2); // Add to the tail\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // Add to the head\ndeque.addFirst(1);\n\n/* Access elements */\nint peekFirst = deque.first; // The first element\nint peekLast = deque.last; // The last element\n\n/* Dequeue elements */\nint popFirst = deque.removeFirst(); // The first element dequeued\nint popLast = deque.removeLast(); // The last element dequeued\n\n/* Get the length of the deque */\nint size = deque.length;\n\n/* Check if the deque is empty */\nbool isEmpty = deque.isEmpty;\n
deque.rs/* Initialize the deque */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(2); // Add to the tail\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // Add to the head\ndeque.push_front(1);\n\n/* Access elements */\nif let Some(front) = deque.front() { // The first element\n}\nif let Some(rear) = deque.back() { // The last element\n}\n\n/* Dequeue elements */\nif let Some(pop_front) = deque.pop_front() { // The first element dequeued\n}\nif let Some(pop_rear) = deque.pop_back() { // The last element dequeued\n}\n\n/* Get the length of the deque */\nlet size = deque.len();\n\n/* Check if the deque is empty */\nlet is_empty = deque.is_empty();\n
deque.c// C does not provide a built-in deque\n
deque.kt\n
deque.zig\n
Visualizing Code https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%0A%20%20%20%20deq%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E9%98%9F%0A%20%20%20%20deq.append%282%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E5%B0%BE%0A%20%20%20%20deq.append%285%29%0A%20%20%20%20deq.append%284%29%0A%20%20%20%20deq.appendleft%283%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E9%A6%96%0A%20%20%20%20deq.appendleft%281%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20front%20%3D%20deq%5B0%5D%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%20front%20%3D%22,%20front%29%0A%20%20%20%20rear%20%3D%20deq%5B-1%5D%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%20rear%20%3D%22,%20rear%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20pop_front%20%3D%20deq.popleft%28%29%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_front%20%3D%22,%20pop_front%29%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%20%20%20%20pop_rear%20%3D%20deq.pop%28%29%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_rear%20%3D%22,%20pop_rear%29%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E7%9A%84%E9%95%BF%E5%BA%A6%0A%20%20%20%20size%20%3D%20len%28deq%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E9%95%BF%E5%BA%A6%20size%20%3D%22,%20size%29%0A%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20len%28deq%29%20%3D%3D%200%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%20%3D%22,%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_stack_and_queue/deque/#532-implementing-a-double-ended-queue","title":"5.3.2 \u00a0 Implementing a double-ended queue *","text":"The implementation of a double-ended queue is similar to that of a regular queue, it can be based on either a linked list or an array as the underlying data structure.
"},{"location":"chapter_stack_and_queue/deque/#1-implementation-based-on-doubly-linked-list","title":"1. \u00a0 Implementation based on doubly linked list","text":"Recall from the previous section that we used a regular singly linked list to implement a queue, as it conveniently allows for deleting from the head (corresponding to the dequeue operation) and adding new elements after the tail (corresponding to the enqueue operation).
For a double-ended queue, both the head and the tail can perform enqueue and dequeue operations. In other words, a double-ended queue needs to implement operations in the opposite direction as well. For this, we use a \"doubly linked list\" as the underlying data structure of the double-ended queue.
As shown in the Figure 5-8 , we treat the head and tail nodes of the doubly linked list as the front and rear of the double-ended queue, respectively, and implement the functionality to add and remove nodes at both ends.
LinkedListDequepushLast()pushFirst()popLast()popFirst()Figure 5-8 \u00a0 Implementing Double-Ended Queue with Doubly Linked List for Enqueue and Dequeue Operations
The implementation code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_deque.pyclass ListNode:\n \"\"\"\u53cc\u5411\u94fe\u8868\u8282\u70b9\"\"\"\n\n def __init__(self, val: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self.val: int = val\n self.next: ListNode | None = None # \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n self.prev: ListNode | None = None # \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\nclass LinkedListDeque:\n \"\"\"\u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0 # \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self.size() == 0\n\n def push(self, num: int, is_front: bool):\n \"\"\"\u5165\u961f\u64cd\u4f5c\"\"\"\n node = ListNode(num)\n # \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if self.is_empty():\n self._front = self._rear = node\n # \u961f\u9996\u5165\u961f\u64cd\u4f5c\n elif is_front:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self._front.prev = node\n node.next = self._front\n self._front = node # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else:\n # \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self._rear.next = node\n node.prev = self._rear\n self._rear = node # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size += 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n self.push(num, True)\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n self.push(num, False)\n\n def pop(self, is_front: bool) -> int:\n \"\"\"\u51fa\u961f\u64cd\u4f5c\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front:\n val: int = self._front.val # \u6682\u5b58\u5934\u8282\u70b9\u503c\n # \u5220\u9664\u5934\u8282\u70b9\n fnext: ListNode | None = self._front.next\n if fnext != None:\n fnext.prev = None\n self._front.next = None\n self._front = fnext # \u66f4\u65b0\u5934\u8282\u70b9\n # \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else:\n val: int = self._rear.val # \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n # \u5220\u9664\u5c3e\u8282\u70b9\n rprev: ListNode | None = self._rear.prev\n if rprev != None:\n rprev.next = None\n self._rear.prev = None\n self._rear = rprev # \u66f4\u65b0\u5c3e\u8282\u70b9\n self._size -= 1 # \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n return self.pop(True)\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n return self.pop(False)\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._rear.val\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n node = self._front\n res = [0] * self.size()\n for i in range(self.size()):\n res[i] = node.val\n node = node.next\n return res\n
linkedlist_deque.cpp/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nstruct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\u6307\u9488\n DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n }\n};\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private:\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n LinkedListDeque() : front(nullptr), rear(nullptr) {\n }\n\n /* \u6790\u6784\u65b9\u6cd5 */\n ~LinkedListDeque() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n DoublyListNode *pre, *cur = front;\n while (cur != nullptr) {\n pre = cur;\n cur = cur->next;\n delete pre;\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int num, bool isFront) {\n DoublyListNode *node = new DoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front->prev = node;\n node->next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear->next = node;\n node->prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int pop(bool isFront) {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front->val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n DoublyListNode *fNext = front->next;\n if (fNext != nullptr) {\n fNext->prev = nullptr;\n front->next = nullptr;\n }\n delete front;\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear->val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n DoublyListNode *rPrev = rear->prev;\n if (rPrev != nullptr) {\n rPrev->next = nullptr;\n rear->prev = nullptr;\n }\n delete rear;\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return rear->val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n DoublyListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_deque.java/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(int val) {\n this.val = val;\n prev = next = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private void push(int num, boolean isFront) {\n ListNode node = new ListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty())\n front = rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private int pop(boolean isFront) {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode fNext = front.next;\n if (fNext != null) {\n fNext.prev = null;\n front.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = rear.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode rPrev = rear.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return rear.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.cs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(int val) {\n public int val = val; // \u8282\u70b9\u503c\n public ListNode? next = null; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n public ListNode? prev = null; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n ListNode? front, rear; // \u5934\u8282\u70b9 front, \u5c3e\u8282\u70b9 rear\n int queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n public LinkedListDeque() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void Push(int num, bool isFront) {\n ListNode node = new(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (IsEmpty()) {\n front = node;\n rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front!.prev = node;\n node.next = front;\n front = node; // \u66f4\u65b0\u5934\u8282\u70b9 \n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear!.next = node;\n node.prev = rear;\n rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n Push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n Push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? Pop(bool isFront) {\n if (IsEmpty())\n throw new Exception();\n int? val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = front?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = front?.next;\n if (fNext != null) {\n fNext.prev = null;\n front!.next = null;\n }\n front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = rear?.prev;\n if (rPrev != null) {\n rPrev.next = null;\n rear!.prev = null;\n }\n rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n\n queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int? PopFirst() {\n return Pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int? PopLast() {\n return Pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int? PeekFirst() {\n if (IsEmpty())\n throw new Exception();\n return front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int? PeekLast() {\n if (IsEmpty())\n throw new Exception();\n return rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int?[] ToArray() {\n ListNode? node = front;\n int?[] res = new int?[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node?.val;\n node = node?.next;\n }\n\n return res;\n }\n}\n
linkedlist_deque.go/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype linkedListDeque struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u53cc\u7aef\u961f\u5217 */\nfunc newLinkedListDeque() *linkedListDeque {\n return &linkedListDeque{\n data: list.New(),\n }\n}\n\n/* \u961f\u9996\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushFirst(value any) {\n s.data.PushFront(value)\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u5165\u961f */\nfunc (s *linkedListDeque) pushLast(value any) {\n s.data.PushBack(value)\n}\n\n/* \u961f\u9996\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u961f\u5c3e\u5143\u7d20\u51fa\u961f */\nfunc (s *linkedListDeque) popLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListDeque) peekFirst() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (s *linkedListDeque) peekLast() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListDeque) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListDeque) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListDeque) toList() *list.List {\n return s.data\n}\n
linkedlist_deque.swift/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n var val: Int // \u8282\u70b9\u503c\n var next: ListNode? // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n weak var prev: ListNode? // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n init(val: Int) {\n self.val = val\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? // \u5934\u8282\u70b9 front\n private var rear: ListNode? // \u5c3e\u8282\u70b9 rear\n private var _size: Int // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n private func push(num: Int, isFront: Bool) {\n let node = ListNode(val: num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if isEmpty() {\n front = node\n rear = node\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if isFront {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _size += 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n push(num: num, isFront: true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n push(num: num, isFront: false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n private func pop(isFront: Bool) -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n let val: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if isFront {\n val = front!.val // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let fNext = front?.next\n if fNext != nil {\n fNext?.prev = nil\n front?.next = nil\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = rear!.val // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let rPrev = rear?.prev\n if rPrev != nil {\n rPrev?.next = nil\n rear?.prev = nil\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _size -= 1 // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n pop(isFront: true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n pop(isFront: false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return rear!.val\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_deque.js/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val; // \u8282\u70b9\u503c\n\n constructor(val) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n #front; // \u5934\u8282\u70b9 front\n #rear; // \u5c3e\u8282\u70b9 rear\n #queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n this.#queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.#rear.next = node;\n node.prev = this.#rear;\n this.#rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val) {\n const node = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.#queSize === 0) {\n this.#front = node;\n this.#rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.#front.prev = node;\n node.next = this.#front;\n this.#front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp = this.#rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.#rear.prev = null;\n }\n this.#rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst() {\n if (this.#queSize === 0) {\n return null;\n }\n const value = this.#front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp = this.#front.next;\n if (temp !== null) {\n temp.prev = null;\n this.#front.next = null;\n }\n this.#front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.#queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n return this.#queSize === 0 ? null : this.#rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n return this.#queSize === 0 ? null : this.#front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print() {\n const arr = [];\n let temp = this.#front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.ts/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n prev: ListNode; // \u524d\u9a71\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n next: ListNode; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528 (\u6307\u9488)\n val: number; // \u8282\u70b9\u503c\n\n constructor(val: number) {\n this.val = val;\n this.next = null;\n this.prev = null;\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private front: ListNode; // \u5934\u8282\u70b9 front\n private rear: ListNode; // \u5c3e\u8282\u70b9 rear\n private queSize: number; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n constructor() {\n this.front = null;\n this.rear = null;\n this.queSize = 0;\n }\n\n /* \u961f\u5c3e\u5165\u961f\u64cd\u4f5c */\n pushLast(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n this.rear.next = node;\n node.prev = this.rear;\n this.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u9996\u5165\u961f\u64cd\u4f5c */\n pushFirst(val: number): void {\n const node: ListNode = new ListNode(val);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (this.queSize === 0) {\n this.front = node;\n this.rear = node;\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n this.front.prev = node;\n node.next = this.front;\n this.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c */\n popLast(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.rear.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n let temp: ListNode = this.rear.prev;\n if (temp !== null) {\n temp.next = null;\n this.rear.prev = null;\n }\n this.rear = temp; // \u66f4\u65b0\u5c3e\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u961f\u9996\u51fa\u961f\u64cd\u4f5c */\n popFirst(): number {\n if (this.queSize === 0) {\n return null;\n }\n const value: number = this.front.val; // \u5b58\u50a8\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n let temp: ListNode = this.front.next;\n if (temp !== null) {\n temp.prev = null;\n this.front.next = null;\n }\n this.front = temp; // \u66f4\u65b0\u5934\u8282\u70b9\n this.queSize--;\n return value;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n return this.queSize === 0 ? null : this.rear.val;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n return this.queSize === 0 ? null : this.front.val;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u6253\u5370\u53cc\u5411\u961f\u5217 */\n print(): void {\n const arr: number[] = [];\n let temp: ListNode = this.front;\n while (temp !== null) {\n arr.push(temp.val);\n temp = temp.next;\n }\n console.log('[' + arr.join(', ') + ']');\n }\n}\n
linkedlist_deque.dart/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode {\n int val; // \u8282\u70b9\u503c\n ListNode? next; // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n ListNode? prev; // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n\n ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u5bf9\u5217 */\nclass LinkedListDeque {\n late ListNode? _front; // \u5934\u8282\u70b9 _front\n late ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n LinkedListDeque() {\n this._front = null;\n this._rear = null;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u957f\u5ea6 */\n int size() {\n return this._queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n void push(int _num, bool isFront) {\n final ListNode node = ListNode(_num);\n if (isEmpty()) {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 _front \u548c _rear \u90fd\u6307\u5411 node\n _front = _rear = node;\n } else if (isFront) {\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n _front!.prev = node;\n node.next = _front;\n _front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n _rear!.next = node;\n node.prev = _rear;\n _rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n push(_num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n push(_num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n int? pop(bool isFront) {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de null\n if (isEmpty()) {\n return null;\n }\n final int val;\n if (isFront) {\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n val = _front!.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode? fNext = _front!.next;\n if (fNext != null) {\n fNext.prev = null;\n _front!.next = null;\n }\n _front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n } else {\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n val = _rear!.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n ListNode? rPrev = _rear!.prev;\n if (rPrev != null) {\n rPrev.next = null;\n _rear!.prev = null;\n }\n _rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n _queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int? popFirst() {\n return pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int? popLast() {\n return pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int? peekFirst() {\n return _front?.val;\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int? peekLast() {\n return _rear?.val;\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> res = [];\n for (int i = 0; i < _queSize; i++) {\n res.add(node!.val);\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_deque.rs/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\npub struct ListNode<T> {\n pub val: T, // \u8282\u70b9\u503c\n pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n}\n\nimpl<T> ListNode<T> {\n pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n Rc::new(RefCell::new(ListNode {\n val,\n next: None,\n prev: None,\n }))\n }\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear\n que_size: usize, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n pub fn push(&mut self, num: T, is_front: bool) {\n let node = ListNode::new(num);\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n if is_front {\n match self.front.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.rear = Some(node.clone());\n self.front = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n Some(old_front) => {\n old_front.borrow_mut().prev = Some(node.clone());\n node.borrow_mut().next = Some(old_front);\n self.front = Some(node); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n }\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n match self.rear.take() {\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n None => {\n self.front = Some(node.clone());\n self.rear = Some(node);\n }\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(node.clone());\n node.borrow_mut().prev = Some(old_rear);\n self.rear = Some(node); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n }\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: T) {\n self.push(num, true);\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: T) {\n self.push(num, false);\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n pub fn pop(&mut self, is_front: bool) -> Option<T> {\n // \u82e5\u961f\u5217\u4e3a\u7a7a\uff0c\u76f4\u63a5\u8fd4\u56de None\n if self.is_empty() {\n return None;\n };\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if is_front {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n new_front.borrow_mut().prev.take();\n self.front = Some(new_front); // \u66f4\u65b0\u5934\u8282\u70b9\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n self.rear.take().map(|old_rear| {\n match old_rear.borrow_mut().prev.take() {\n Some(new_rear) => {\n new_rear.borrow_mut().next.take();\n self.rear = Some(new_rear); // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n None => {\n self.front.take();\n }\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val\n })\n }\n }\n\n /* \u961f\u9996\u51fa\u961f */\n pub fn pop_first(&mut self) -> Option<T> {\n return self.pop(true);\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n pub fn pop_last(&mut self) -> Option<T> {\n return self.pop(false);\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.rear.as_ref()\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_deque.c/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\ntypedef struct DoublyListNode {\n int val; // \u8282\u70b9\u503c\n struct DoublyListNode *next; // \u540e\u7ee7\u8282\u70b9\n struct DoublyListNode *prev; // \u524d\u9a71\u8282\u70b9\n} DoublyListNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nDoublyListNode *newDoublyListNode(int num) {\n DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n new->val = num;\n new->next = NULL;\n new->prev = NULL;\n return new;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delDoublyListNode(DoublyListNode *node) {\n free(node);\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n DoublyListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize; // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n} LinkedListDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListDeque *newLinkedListDeque() {\n LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n deque->front = NULL;\n deque->rear = NULL;\n deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n DoublyListNode *tmp = deque->front;\n deque->front = deque->front->next;\n free(tmp);\n }\n // \u91ca\u653e deque \u7ed3\u6784\u4f53\n free(deque);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListDeque *deque) {\n return (size(deque) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n DoublyListNode *node = newDoublyListNode(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411node\n if (empty(deque)) {\n deque->front = deque->rear = node;\n }\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n deque->front->prev = node;\n node->next = deque->front;\n deque->front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n deque->rear->next = node;\n node->prev = deque->rear;\n deque->rear = node;\n }\n deque->queSize++; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n push(deque, num, true);\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(LinkedListDeque *deque, int num) {\n push(deque, num, false);\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(LinkedListDeque *deque) {\n assert(size(deque) && deque->front);\n return deque->front->val;\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(LinkedListDeque *deque) {\n assert(size(deque) && deque->rear);\n return deque->rear->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListDeque *deque, bool isFront) {\n if (empty(deque))\n return -1;\n int val;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n val = peekFirst(deque); // \u6682\u5b58\u5934\u8282\u70b9\u503c\n DoublyListNode *fNext = deque->front->next;\n if (fNext) {\n fNext->prev = NULL;\n deque->front->next = NULL;\n delDoublyListNode(deque->front);\n }\n deque->front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n }\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n else {\n val = peekLast(deque); // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n DoublyListNode *rPrev = deque->rear->prev;\n if (rPrev) {\n rPrev->next = NULL;\n deque->rear->prev = NULL;\n delDoublyListNode(deque->rear);\n }\n deque->rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n deque->queSize--; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(LinkedListDeque *deque) {\n return pop(deque, true);\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(LinkedListDeque *deque) {\n return pop(deque, false);\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n int *arr = malloc(sizeof(int) * deque->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n DoublyListNode *node;\n for (i = 0, node = deque->front; i < deque->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, deque->queSize);\n free(arr);\n}\n
linkedlist_deque.kt/* \u53cc\u5411\u94fe\u8868\u8282\u70b9 */\nclass ListNode(var value: Int) {\n // \u8282\u70b9\u503c\n var next: ListNode? = null // \u540e\u7ee7\u8282\u70b9\u5f15\u7528\n var prev: ListNode? = null // \u524d\u9a71\u8282\u70b9\u5f15\u7528\n}\n\n/* \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass LinkedListDeque {\n private var front: ListNode? = null // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private var rear: ListNode? = null\n private var queSize = 0 // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u961f\u64cd\u4f5c */\n fun push(num: Int, isFront: Boolean) {\n val node = ListNode(num)\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (isEmpty()) {\n rear = node\n front = rear\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (isFront) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n front?.prev = node\n node.next = front\n front = node // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n rear?.next = node\n node.prev = rear\n rear = node // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize++ // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n }\n\n /* \u961f\u9996\u5165\u961f */\n fun pushFirst(num: Int) {\n push(num, true)\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n fun pushLast(num: Int) {\n push(num, false)\n }\n\n /* \u51fa\u961f\u64cd\u4f5c */\n fun pop(isFront: Boolean): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n\n val value: Int\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (isFront) {\n value = front!!.value // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n val fNext = front!!.next\n if (fNext != null) {\n fNext.prev = null\n front!!.next = null\n }\n front = fNext // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n value = rear!!.value // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n val rPrev = rear!!.prev\n if (rPrev != null) {\n rPrev.next = null\n rear!!.prev = null\n }\n rear = rPrev // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n queSize-- // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return value\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fun popFirst(): Int {\n return pop(true)\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fun popLast(): Int {\n return pop(false)\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) {\n throw IndexOutOfBoundsException()\n\n }\n return front!!.value\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return rear!!.value\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!.value\n node = node.next\n }\n return res\n }\n}\n
linkedlist_deque.rb[class]{ListNode}-[func]{}\n\n[class]{LinkedListDeque}-[func]{}\n
linkedlist_deque.zig// \u53cc\u5411\u94fe\u8868\u8282\u70b9\nfn ListNode(comptime T: type) type {\n return struct {\n const Self = @This();\n\n val: T = undefined, // \u8282\u70b9\u503c\n next: ?*Self = null, // \u540e\u7ee7\u8282\u70b9\u6307\u9488\n prev: ?*Self = null, // \u524d\u9a71\u8282\u70b9\u6307\u9488\n\n // Initialize a list node with specific value\n pub fn init(self: *Self, x: i32) void {\n self.val = x;\n self.next = null;\n self.prev = null;\n }\n };\n}\n\n// \u57fa\u4e8e\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\nfn LinkedListDeque(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u5165\u961f\u64cd\u4f5c\n pub fn push(self: *Self, num: T, is_front: bool) !void {\n var node = try self.mem_allocator.create(ListNode(T));\n node.init(num);\n // \u82e5\u94fe\u8868\u4e3a\u7a7a\uff0c\u5219\u4ee4 front \u548c rear \u90fd\u6307\u5411 node\n if (self.isEmpty()) {\n self.front = node;\n self.rear = node;\n // \u961f\u9996\u5165\u961f\u64cd\u4f5c\n } else if (is_front) {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5934\u90e8\n self.front.?.prev = node;\n node.next = self.front;\n self.front = node; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u5165\u961f\u64cd\u4f5c\n } else {\n // \u5c06 node \u6dfb\u52a0\u81f3\u94fe\u8868\u5c3e\u90e8\n self.rear.?.next = node;\n node.prev = self.rear;\n self.rear = node; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size += 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n } \n\n // \u961f\u9996\u5165\u961f\n pub fn pushFirst(self: *Self, num: T) !void {\n try self.push(num, true);\n } \n\n // \u961f\u5c3e\u5165\u961f\n pub fn pushLast(self: *Self, num: T) !void {\n try self.push(num, false);\n } \n\n // \u51fa\u961f\u64cd\u4f5c\n pub fn pop(self: *Self, is_front: bool) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n var val: T = undefined;\n // \u961f\u9996\u51fa\u961f\u64cd\u4f5c\n if (is_front) {\n val = self.front.?.val; // \u6682\u5b58\u5934\u8282\u70b9\u503c\n // \u5220\u9664\u5934\u8282\u70b9\n var fNext = self.front.?.next;\n if (fNext != null) {\n fNext.?.prev = null;\n self.front.?.next = null;\n }\n self.front = fNext; // \u66f4\u65b0\u5934\u8282\u70b9\n // \u961f\u5c3e\u51fa\u961f\u64cd\u4f5c\n } else {\n val = self.rear.?.val; // \u6682\u5b58\u5c3e\u8282\u70b9\u503c\n // \u5220\u9664\u5c3e\u8282\u70b9\n var rPrev = self.rear.?.prev;\n if (rPrev != null) {\n rPrev.?.next = null;\n self.rear.?.prev = null;\n }\n self.rear = rPrev; // \u66f4\u65b0\u5c3e\u8282\u70b9\n }\n self.que_size -= 1; // \u66f4\u65b0\u961f\u5217\u957f\u5ea6\n return val;\n } \n\n // \u961f\u9996\u51fa\u961f\n pub fn popFirst(self: *Self) T {\n return self.pop(true);\n } \n\n // \u961f\u5c3e\u51fa\u961f\n pub fn popLast(self: *Self) T {\n return self.pop(false);\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peekFirst(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\n pub fn peekLast(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return self.rear.?.val;\n }\n\n // \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
"},{"location":"chapter_stack_and_queue/deque/#2-implementation-based-on-array","title":"2. \u00a0 Implementation based on array","text":"As shown in the Figure 5-9 , similar to implementing a queue with an array, we can also use a circular array to implement a double-ended queue.
ArrayDequepushLast()pushFirst()popLast()popFirst()Figure 5-9 \u00a0 Implementing Double-Ended Queue with Array for Enqueue and Dequeue Operations
The implementation only needs to add methods for \"front enqueue\" and \"rear dequeue\":
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_deque.pyclass ArrayDeque:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217\"\"\"\n\n def __init__(self, capacity: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * capacity\n self._front: int = 0\n self._size: int = 0\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def index(self, i: int) -> int:\n \"\"\"\u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15\"\"\"\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n # \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + self.capacity()) % self.capacity()\n\n def push_first(self, num: int):\n \"\"\"\u961f\u9996\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self._front = self.index(self._front - 1)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self._nums[self._front] = num\n self._size += 1\n\n def push_last(self, num: int):\n \"\"\"\u961f\u5c3e\u5165\u961f\"\"\"\n if self._size == self.capacity():\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear = self.index(self._front + self._size)\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop_first(self) -> int:\n \"\"\"\u961f\u9996\u51fa\u961f\"\"\"\n num = self.peek_first()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self._front = self.index(self._front + 1)\n self._size -= 1\n return num\n\n def pop_last(self) -> int:\n \"\"\"\u961f\u5c3e\u51fa\u961f\"\"\"\n num = self.peek_last()\n self._size -= 1\n return num\n\n def peek_first(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def peek_last(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u5c3e\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n # \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last = self.index(self._front + self._size - 1)\n return self._nums[last]\n\n def to_array(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370\"\"\"\n # \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res = []\n for i in range(self._size):\n res.append(self._nums[self.index(self._front + i)])\n return res\n
array_deque.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private:\n vector<int> nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n nums.resize(capacity);\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return nums.size();\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int num) {\n if (queSize == capacity()) {\n cout << \"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty())\n throw out_of_range(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> res(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n};\n
array_deque.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n this.nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void pushFirst(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void pushLast(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int popFirst() {\n int num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int popLast() {\n int num = peekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peekFirst() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int peekLast() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayDeque(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int Index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + Capacity()) % Capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n public void PushFirst(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = Index(front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num;\n queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n public void PushLast(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = Index(front + queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n public int PopFirst() {\n int num = PeekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = Index(front + 1);\n queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n public int PopLast() {\n int num = PeekLast();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int PeekFirst() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n return nums[front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n public int PeekLast() {\n if (IsEmpty()) {\n throw new InvalidOperationException();\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = Index(front + queSize - 1);\n return nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[Index(j)];\n }\n return res;\n }\n}\n
array_deque.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntype arrayDeque struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n return &arrayDeque{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayDeque) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayDeque) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nfunc (q *arrayDeque) index(i int) int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u961f\u9996\u5165\u961f */\nfunc (q *arrayDeque) pushFirst(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n q.front = q.index(q.front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n q.nums[q.front] = num\n q.queSize++\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nfunc (q *arrayDeque) pushLast(num int) {\n if q.queSize == q.queCapacity {\n fmt.Println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n rear := q.index(q.front + q.queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u961f\u9996\u51fa\u961f */\nfunc (q *arrayDeque) popFirst() any {\n num := q.peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n q.front = q.index(q.front + 1)\n q.queSize--\n return num\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nfunc (q *arrayDeque) popLast() any {\n num := q.peekLast()\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayDeque) peekFirst() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nfunc (q *arrayDeque) peekLast() any {\n if q.isEmpty() {\n return nil\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n last := q.index(q.front + q.queSize - 1)\n return q.nums[last]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayDeque) toSlice() []int {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n res := make([]int, q.queSize)\n for i, j := 0, q.front; i < q.queSize; i++ {\n res[i] = q.nums[q.index(j)]\n j++\n }\n return res\n}\n
array_deque.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var _size: Int // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(capacity: Int) {\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private func index(i: Int) -> Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n func pushFirst(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(i: front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n _size += 1\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n func pushLast(num: Int) {\n if size() == capacity() {\n print(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = index(i: front + size())\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u961f\u9996\u51fa\u961f */\n func popFirst() -> Int {\n let num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(i: front + 1)\n _size -= 1\n return num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n func popLast() -> Int {\n let num = peekLast()\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peekFirst() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n func peekLast() -> Int {\n if isEmpty() {\n fatalError(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = index(i: front + size() - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n (front ..< front + size()).map { nums[index(i: $0)] }\n }\n}\n
array_deque.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity) {\n this.#nums = new Array(capacity);\n this.#front = 0;\n this.#queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.#front = this.index(this.#front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.#nums[this.#front] = num;\n this.#queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num) {\n if (this.#queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear = this.index(this.#front + this.#queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst() {\n const num = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.#front = this.index(this.#front + 1);\n this.#queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast() {\n const num = this.peekLast();\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.#nums[this.#front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast() {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.#front + this.#queSize - 1);\n return this.#nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res = [];\n for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n res[i] = this.#nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = 0;\n this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n index(i: number): number {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + this.capacity()) % this.capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n pushFirst(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n this.front = this.index(this.front - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n this.nums[this.front] = num;\n this.queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pushLast(num: number): void {\n if (this.queSize === this.capacity()) {\n console.log('\u53cc\u5411\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n const rear: number = this.index(this.front + this.queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n popFirst(): number {\n const num: number = this.peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n this.front = this.index(this.front + 1);\n this.queSize--;\n return num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n popLast(): number {\n const num: number = this.peekLast();\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peekFirst(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n return this.nums[this.front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n peekLast(): number {\n if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n const last = this.index(this.front + this.queSize - 1);\n return this.nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const res: number[] = [];\n for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n res[i] = this.nums[this.index(j)];\n }\n return res;\n }\n}\n
array_deque.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque {\n late List<int> _nums; // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayDeque(int capacity) {\n this._nums = List.filled(capacity, 0);\n this._front = this._queSize = 0;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n int index(int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity();\n }\n\n /* \u961f\u9996\u5165\u961f */\n void pushFirst(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 _front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n _front = index(_front - 1);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u9996\n _nums[_front] = _num;\n _queSize++;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n void pushLast(int _num) {\n if (_queSize == capacity()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = index(_front + _queSize);\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n int popFirst() {\n int _num = peekFirst();\n // \u961f\u9996\u6307\u9488\u5411\u53f3\u79fb\u52a8\u4e00\u4f4d\n _front = index(_front + 1);\n _queSize--;\n return _num;\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n int popLast() {\n int _num = peekLast();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peekFirst() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n int peekLast() {\n if (isEmpty()) {\n throw Exception(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\");\n }\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n int last = index(_front + _queSize - 1);\n return _nums[last];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[index(j)];\n }\n return res;\n }\n}\n
array_deque.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nstruct ArrayDeque {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: usize, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: usize, // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n}\n\nimpl ArrayDeque {\n /* \u6784\u9020\u65b9\u6cd5 */\n pub fn new(capacity: usize) -> Self {\n Self {\n nums: vec![0; capacity],\n front: 0,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n pub fn capacity(&self) -> usize {\n self.nums.len()\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n self.que_size\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n fn index(&self, i: i32) -> usize {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + self.capacity() as i32) % self.capacity() as i32) as usize;\n }\n\n /* \u961f\u9996\u5165\u961f */\n pub fn push_first(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n self.front = self.index(self.front as i32 - 1);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n self.nums[self.front] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n pub fn push_last(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n let rear = self.index(self.front as i32 + self.que_size as i32);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear] = num;\n self.que_size += 1;\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fn pop_first(&mut self) -> i32 {\n let num = self.peek_first();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n self.front = self.index(self.front as i32 + 1);\n self.que_size -= 1;\n num\n }\n\n /* \u961f\u5c3e\u51fa\u961f */\n fn pop_last(&mut self) -> i32 {\n let num = self.peek_last();\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek_first(&self) -> i32 {\n if self.is_empty() {\n panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n };\n self.nums[self.front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fn peek_last(&self) -> i32 {\n if self.is_empty() {\n panic!(\"\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\")\n };\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n self.nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fn to_array(&self) -> Vec<i32> {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n let mut res = vec![0; self.que_size];\n let mut j = self.front;\n for i in 0..self.que_size {\n res[i] = self.nums[self.index(j as i32)];\n j += 1;\n }\n res\n }\n}\n
array_deque.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayDeque;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayDeque *newArrayDeque(int capacity) {\n ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n // \u521d\u59cb\u5316\u6570\u7ec4\n deque->queCapacity = capacity;\n deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n deque->front = deque->queSize = 0;\n return deque;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayDeque(ArrayDeque *deque) {\n free(deque->nums);\n free(deque);\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayDeque *deque) {\n return deque->queCapacity;\n}\n\n/* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayDeque *deque) {\n return deque->queSize;\n}\n\n/* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayDeque *deque) {\n return deque->queSize == 0;\n}\n\n/* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\nint dequeIndex(ArrayDeque *deque, int i) {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u65f6\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u961f\u9996\u5165\u961f */\nvoid pushFirst(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u56de\u5230\u5c3e\u90e8\n deque->front = dequeIndex(deque, deque->front - 1);\n // \u5c06 num \u6dfb\u52a0\u5230\u961f\u9996\n deque->nums[deque->front] = num;\n deque->queSize++;\n}\n\n/* \u961f\u5c3e\u5165\u961f */\nvoid pushLast(ArrayDeque *deque, int num) {\n if (deque->queSize == capacity(deque)) {\n printf(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n int rear = dequeIndex(deque, deque->front + deque->queSize);\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n deque->nums[rear] = num;\n deque->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peekFirst(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n return deque->nums[deque->front];\n}\n\n/* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\nint peekLast(ArrayDeque *deque) {\n // \u8bbf\u95ee\u5f02\u5e38\uff1a\u53cc\u5411\u961f\u5217\u4e3a\u7a7a\n assert(empty(deque) == 0);\n int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n return deque->nums[last];\n}\n\n/* \u961f\u9996\u51fa\u961f */\nint popFirst(ArrayDeque *deque) {\n int num = peekFirst(deque);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n deque->front = dequeIndex(deque, deque->front + 1);\n deque->queSize--;\n return num;\n}\n\n/* \u961f\u5c3e\u51fa\u961f */\nint popLast(ArrayDeque *deque) {\n int num = peekLast(deque);\n deque->queSize--;\n return num;\n}\n
array_deque.kt/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u53cc\u5411\u961f\u5217 */\nclass ArrayDeque(capacity: Int) {\n private var nums = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u53cc\u5411\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u53cc\u5411\u961f\u5217\u957f\u5ea6\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u83b7\u53d6\u53cc\u5411\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u53cc\u5411\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u8ba1\u7b97\u73af\u5f62\u6570\u7ec4\u7d22\u5f15 */\n private fun index(i: Int): Int {\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0\u6570\u7ec4\u9996\u5c3e\u76f8\u8fde\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\uff0c\u56de\u5230\u5934\u90e8\n // \u5f53 i \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\uff0c\u56de\u5230\u5c3e\u90e8\n return (i + capacity()) % capacity()\n }\n\n /* \u961f\u9996\u5165\u961f */\n fun pushFirst(num: Int) {\n if (queSize == capacity()) {\n println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u961f\u9996\u6307\u9488\u5411\u5de6\u79fb\u52a8\u4e00\u4f4d\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 front \u8d8a\u8fc7\u6570\u7ec4\u5934\u90e8\u540e\u56de\u5230\u5c3e\u90e8\n front = index(front - 1)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u9996\n nums[front] = num\n queSize++\n }\n\n /* \u961f\u5c3e\u5165\u961f */\n fun pushLast(num: Int) {\n if (queSize == capacity()) {\n println(\"\u53cc\u5411\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n val rear = index(front + queSize)\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u961f\u9996\u51fa\u961f */\n fun popFirst(): Int {\n val num = peekFirst()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\n front = index(front + 1)\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun popLast(): Int {\n val num = peekLast()\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peekFirst(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8bbf\u95ee\u961f\u5c3e\u5143\u7d20 */\n fun peekLast(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n // \u8ba1\u7b97\u5c3e\u5143\u7d20\u7d22\u5f15\n val last = index(front + queSize - 1)\n return nums[last]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4\u7528\u4e8e\u6253\u5370 */\n fun toArray(): IntArray {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i < queSize) {\n res[i] = nums[index(j)]\n i++\n j++\n }\n return res\n }\n}\n
array_deque.rb[class]{ArrayDeque}-[func]{}\n
array_deque.zig[class]{ArrayDeque}-[func]{}\n
"},{"location":"chapter_stack_and_queue/deque/#533-applications-of-double-ended-queue","title":"5.3.3 \u00a0 Applications of double-ended queue","text":"The double-ended queue combines the logic of both stacks and queues, thus, it can implement all their respective use cases while offering greater flexibility.
We know that software's \"undo\" feature is typically implemented using a stack: the system pushes
each change operation onto the stack and then pops
to implement undoing. However, considering the limitations of system resources, software often restricts the number of undo steps (for example, only allowing the last 50 steps). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (the front of the queue). But a regular stack cannot perform this function, where a double-ended queue becomes necessary. Note that the core logic of \"undo\" still follows the Last-In-First-Out principle of a stack, but a double-ended queue can more flexibly implement some additional logic.
\"Queue\" is a linear data structure that follows the First-In-First-Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers join the queue at the rear, and the person at the front leaves the queue first.
As shown in the Figure 5-4 , we call the front of the queue the \"head\" and the back the \"tail.\" The operation of adding elements to the rear of the queue is termed \"enqueue,\" and the operation of removing elements from the front is termed \"dequeue.\"
Figure 5-4 \u00a0 Queue's first-in-first-out rule
"},{"location":"chapter_stack_and_queue/queue/#521-common-operations-on-queue","title":"5.2.1 \u00a0 Common operations on queue","text":"The common operations on a queue are shown in the Table 5-2 . Note that method names may vary across different programming languages. Here, we use the same naming convention as that used for stacks.
Table 5-2 \u00a0 Efficiency of queue operations
Method Name Description Time Complexitypush()
Enqueue an element, add it to the tail \\(O(1)\\) pop()
Dequeue the head element \\(O(1)\\) peek()
Access the head element \\(O(1)\\) We can directly use the ready-made queue classes in programming languages:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig queue.pyfrom collections import deque\n\n# Initialize the queue\n# In Python, we generally use the deque class as a queue\n# Although queue.Queue() is a pure queue class, it's not very user-friendly, so it's not recommended\nque: deque[int] = deque()\n\n# Enqueue elements\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# Access the first element\nfront: int = que[0]\n\n# Dequeue an element\npop: int = que.popleft()\n\n# Get the length of the queue\nsize: int = len(que)\n\n# Check if the queue is empty\nis_empty: bool = len(que) == 0\n
queue.cpp/* Initialize the queue */\nqueue<int> queue;\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element*/\nint front = queue.front();\n\n/* Dequeue an element */\nqueue.pop();\n\n/* Get the length of the queue */\nint size = queue.size();\n\n/* Check if the queue is empty */\nbool empty = queue.empty();\n
queue.java/* Initialize the queue */\nQueue<Integer> queue = new LinkedList<>();\n\n/* Enqueue elements */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* Access the first element */\nint peek = queue.peek();\n\n/* Dequeue an element */\nint pop = queue.poll();\n\n/* Get the length of the queue */\nint size = queue.size();\n\n/* Check if the queue is empty */\nboolean isEmpty = queue.isEmpty();\n
queue.cs/* Initialize the queue */\nQueue<int> queue = new();\n\n/* Enqueue elements */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* Access the first element */\nint peek = queue.Peek();\n\n/* Dequeue an element */\nint pop = queue.Dequeue();\n\n/* Get the length of the queue */\nint size = queue.Count;\n\n/* Check if the queue is empty */\nbool isEmpty = queue.Count == 0;\n
queue_test.go/* Initialize the queue */\n// In Go, use list as a queue\nqueue := list.New()\n\n/* Enqueue elements */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* Access the first element */\npeek := queue.Front()\n\n/* Dequeue an element */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* Get the length of the queue */\nsize := queue.Len()\n\n/* Check if the queue is empty */\nisEmpty := queue.Len() == 0\n
queue.swift/* Initialize the queue */\n// Swift does not have a built-in queue class, so Array can be used as a queue\nvar queue: [Int] = []\n\n/* Enqueue elements */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* Access the first element */\nlet peek = queue.first!\n\n/* Dequeue an element */\n// Since it's an array, removeFirst has a complexity of O(n)\nlet pool = queue.removeFirst()\n\n/* Get the length of the queue */\nlet size = queue.count\n\n/* Check if the queue is empty */\nlet isEmpty = queue.isEmpty\n
queue.js/* Initialize the queue */\n// JavaScript does not have a built-in queue, so Array can be used as a queue\nconst queue = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element */\nconst peek = queue[0];\n\n/* Dequeue an element */\n// Since the underlying structure is an array, shift() method has a time complexity of O(n)\nconst pop = queue.shift();\n\n/* Get the length of the queue */\nconst size = queue.length;\n\n/* Check if the queue is empty */\nconst empty = queue.length === 0;\n
queue.ts/* Initialize the queue */\n// TypeScript does not have a built-in queue, so Array can be used as a queue \nconst queue: number[] = [];\n\n/* Enqueue elements */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* Access the first element */\nconst peek = queue[0];\n\n/* Dequeue an element */\n// Since the underlying structure is an array, shift() method has a time complexity of O(n)\nconst pop = queue.shift();\n\n/* Get the length of the queue */\nconst size = queue.length;\n\n/* Check if the queue is empty */\nconst empty = queue.length === 0;\n
queue.dart/* Initialize the queue */\n// In Dart, the Queue class is a double-ended queue but can be used as a queue\nQueue<int> queue = Queue();\n\n/* Enqueue elements */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* Access the first element */\nint peek = queue.first;\n\n/* Dequeue an element */\nint pop = queue.removeFirst();\n\n/* Get the length of the queue */\nint size = queue.length;\n\n/* Check if the queue is empty */\nbool isEmpty = queue.isEmpty;\n
queue.rs/* Initialize the double-ended queue */\n// In Rust, use a double-ended queue as a regular queue\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* Enqueue elements */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* Access the first element */\nif let Some(front) = deque.front() {\n}\n\n/* Dequeue an element */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* Get the length of the queue */\nlet size = deque.len();\n\n/* Check if the queue is empty */\nlet is_empty = deque.is_empty();\n
queue.c// C does not provide a built-in queue\n
queue.kt\n
queue.zig\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/queue/#522-implementing-a-queue","title":"5.2.2 \u00a0 Implementing a queue","text":"To implement a queue, we need a data structure that allows adding elements at one end and removing them at the other. Both linked lists and arrays meet this requirement.
"},{"location":"chapter_stack_and_queue/queue/#1-implementation-based-on-a-linked-list","title":"1. \u00a0 Implementation based on a linked list","text":"As shown in the Figure 5-5 , we can consider the \"head node\" and \"tail node\" of a linked list as the \"front\" and \"rear\" of the queue, respectively. It is stipulated that nodes can only be added at the rear and removed at the front.
LinkedListQueuepush()pop()Figure 5-5 \u00a0 Implementing Queue with Linked List for Enqueue and Dequeue Operations
Below is the code for implementing a queue using a linked list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_queue.pyclass LinkedListQueue:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._front: ListNode | None = None # \u5934\u8282\u70b9 front\n self._rear: ListNode | None = None # \u5c3e\u8282\u70b9 rear\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._front\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n # \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n node = ListNode(num)\n # \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if self._front is None:\n self._front = node\n self._rear = node\n # \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else:\n self._rear.next = node\n self._rear = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num = self.peek()\n # \u5220\u9664\u5934\u8282\u70b9\n self._front = self._front.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._front.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n queue = []\n temp = self._front\n while temp:\n queue.append(temp.val)\n temp = temp.next\n return queue\n
linkedlist_queue.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private:\n ListNode *front, *rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n int queSize;\n\n public:\n LinkedListQueue() {\n front = nullptr;\n rear = nullptr;\n queSize = 0;\n }\n\n ~LinkedListQueue() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(front);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode *node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == nullptr) {\n front = node;\n rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear->next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n ListNode *tmp = front;\n front = front->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (size() == 0)\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return front->val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = front;\n vector<int> res(size());\n for (int i = 0; i < res.size(); i++) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_queue.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private ListNode front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = front;\n int[] res = new int[size()];\n for (int i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? front, rear; // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear \n int queSize = 0;\n\n public LinkedListQueue() {\n front = null;\n rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n ListNode node = new(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node;\n rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else if (rear != null) {\n rear.next = node;\n rear = node;\n }\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (front == null)\n return [];\n\n ListNode? node = front;\n int[] res = new int[Size()];\n for (int i = 0; i < res.Length; i++) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntype linkedListQueue struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u961f\u5217\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newLinkedListQueue() *linkedListQueue {\n return &linkedListQueue{\n data: list.New(),\n }\n}\n\n/* \u5165\u961f */\nfunc (s *linkedListQueue) push(value any) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u961f */\nfunc (s *linkedListQueue) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (s *linkedListQueue) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Front()\n return e.Value\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (s *linkedListQueue) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListQueue) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListQueue) toList() *list.List {\n return s.data\n}\n
linkedlist_queue.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private var front: ListNode? // \u5934\u8282\u70b9\n private var rear: ListNode? // \u5c3e\u8282\u70b9\n private var _size: Int\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let node = ListNode(x: num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if front == nil {\n front = node\n rear = node\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n rear?.next = node\n rear = node\n }\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return front!.val\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = front\n var res = Array(repeating: 0, count: size())\n for i in res.indices {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_queue.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n #front; // \u5934\u8282\u70b9 #front\n #rear; // \u5c3e\u8282\u70b9 #rear\n #queSize = 0;\n\n constructor() {\n this.#front = null;\n this.#rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.#front) {\n this.#front = node;\n this.#rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.#rear.next = node;\n this.#rear = node;\n }\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n this.#front = this.#front.next;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#front.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#front;\n const res = new Array(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_queue.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n private front: ListNode | null; // \u5934\u8282\u70b9 front\n private rear: ListNode | null; // \u5c3e\u8282\u70b9 rear\n private queSize: number = 0;\n\n constructor() {\n this.front = null;\n this.rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n const node = new ListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (!this.front) {\n this.front = node;\n this.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n this.rear!.next = node;\n this.rear = node;\n }\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n if (!this.front) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n // \u5220\u9664\u5934\u8282\u70b9\n this.front = this.front.next;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.size === 0) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.front;\n const res = new Array<number>(this.size);\n for (let i = 0; i < res.length; i++) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_queue.dart/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue {\n ListNode? _front; // \u5934\u8282\u70b9 _front\n ListNode? _rear; // \u5c3e\u8282\u70b9 _rear\n int _queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n LinkedListQueue() {\n _front = null;\n _rear = null;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 _num\n final node = ListNode(_num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (_front == null) {\n _front = node;\n _rear = node;\n } else {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n _rear!.next = node;\n _rear = node;\n }\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n final int _num = peek();\n // \u5220\u9664\u5934\u8282\u70b9\n _front = _front!.next;\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (_queSize == 0) {\n throw Exception('\u961f\u5217\u4e3a\u7a7a');\n }\n return _front!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() {\n ListNode? node = _front;\n final List<int> queue = [];\n while (node != null) {\n queue.add(node.val);\n node = node.next;\n }\n return queue;\n }\n}\n
linkedlist_queue.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n front: Option<Rc<RefCell<ListNode<T>>>>, // \u5934\u8282\u70b9 front\n rear: Option<Rc<RefCell<ListNode<T>>>>, // \u5c3e\u8282\u70b9 rear\n que_size: usize, // \u961f\u5217\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n pub fn new() -> Self {\n Self {\n front: None,\n rear: None,\n que_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.que_size;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u961f */\n pub fn push(&mut self, num: T) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n let new_rear = ListNode::new(num);\n match self.rear.take() {\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n Some(old_rear) => {\n old_rear.borrow_mut().next = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n None => {\n self.front = Some(new_rear.clone());\n self.rear = Some(new_rear);\n }\n }\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n pub fn pop(&mut self) -> Option<T> {\n self.front.take().map(|old_front| {\n match old_front.borrow_mut().next.take() {\n Some(new_front) => {\n self.front = Some(new_front);\n }\n None => {\n self.rear.take();\n }\n }\n self.que_size -= 1;\n Rc::try_unwrap(old_front).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.front.as_ref()\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.insert(0, node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_queue.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n ListNode *front, *rear;\n int queSize;\n} LinkedListQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListQueue *newLinkedListQueue() {\n LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n queue->front = NULL;\n queue->rear = NULL;\n queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n // \u91ca\u653e\u6240\u6709\u8282\u70b9\n while (queue->front != NULL) {\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n }\n // \u91ca\u653e queue \u7ed3\u6784\u4f53\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(LinkedListQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(LinkedListQueue *queue) {\n return (size(queue) == 0);\n}\n\n/* \u5165\u961f */\nvoid push(LinkedListQueue *queue, int num) {\n // \u5c3e\u8282\u70b9\u5904\u6dfb\u52a0 node\n ListNode *node = newListNode(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (queue->front == NULL) {\n queue->front = node;\n queue->rear = node;\n }\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n else {\n queue->rear->next = node;\n queue->rear = node;\n }\n queue->queSize++;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(LinkedListQueue *queue) {\n assert(size(queue) && queue->front);\n return queue->front->val;\n}\n\n/* \u51fa\u961f */\nint pop(LinkedListQueue *queue) {\n int num = peek(queue);\n ListNode *tmp = queue->front;\n queue->front = queue->front->next;\n free(tmp);\n queue->queSize--;\n return num;\n}\n\n/* \u6253\u5370\u961f\u5217 */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n int *arr = malloc(sizeof(int) * queue->queSize);\n // \u62f7\u8d1d\u94fe\u8868\u4e2d\u7684\u6570\u636e\u5230\u6570\u7ec4\n int i;\n ListNode *node;\n for (i = 0, node = queue->front; i < queue->queSize; i++) {\n arr[i] = node->val;\n node = node->next;\n }\n printArray(arr, queue->queSize);\n free(arr);\n}\n
linkedlist_queue.kt/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217 */\nclass LinkedListQueue(\n // \u5934\u8282\u70b9 front \uff0c\u5c3e\u8282\u70b9 rear\n private var front: ListNode? = null,\n private var rear: ListNode? = null,\n private var queSize: Int = 0\n) {\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u961f */\n fun push(num: Int) {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n val node = ListNode(num)\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (front == null) {\n front = node\n rear = node\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n rear?.next = node\n rear = node\n }\n queSize++\n }\n\n /* \u51fa\u961f */\n fun pop(): Int {\n val num = peek()\n // \u5220\u9664\u5934\u8282\u70b9\n front = front?.next\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return front!!.value\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = front\n val res = IntArray(size())\n for (i in res.indices) {\n res[i] = node!!.value\n node = node.next\n }\n return res\n }\n}\n
linkedlist_queue.rb[class]{LinkedListQueue}-[func]{}\n
linkedlist_queue.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u961f\u5217\nfn LinkedListQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n front: ?*inc.ListNode(T) = null, // \u5934\u8282\u70b9 front\n rear: ?*inc.ListNode(T) = null, // \u5c3e\u8282\u70b9 rear\n que_size: usize = 0, // \u961f\u5217\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u961f\u5217\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.front = null;\n self.rear = null;\n self.que_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.que_size;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.front.?.val;\n } \n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n // \u5982\u679c\u961f\u5217\u4e3a\u7a7a\uff0c\u5219\u4ee4\u5934\u3001\u5c3e\u8282\u70b9\u90fd\u6307\u5411\u8be5\u8282\u70b9\n if (self.front == null) {\n self.front = node;\n self.rear = node;\n // \u5982\u679c\u961f\u5217\u4e0d\u4e3a\u7a7a\uff0c\u5219\u5c06\u8be5\u8282\u70b9\u6dfb\u52a0\u5230\u5c3e\u8282\u70b9\u540e\n } else {\n self.rear.?.next = node;\n self.rear = node;\n }\n self.que_size += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u5220\u9664\u5934\u8282\u70b9\n self.front = self.front.?.next;\n self.que_size -= 1;\n return num;\n } \n\n // \u5c06\u94fe\u8868\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.front;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[i] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/queue/#2-implementation-based-on-an-array","title":"2. \u00a0 Implementation based on an array","text":"Deleting the first element in an array has a time complexity of \\(O(n)\\), which would make the dequeue operation inefficient. However, this problem can be cleverly avoided as follows.
We use a variable front
to indicate the index of the front element and maintain a variable size
to record the queue's length. Define rear = front + size
, which points to the position immediately following the tail element.
With this design, the effective interval of elements in the array is [front, rear - 1]
. The implementation methods for various operations are shown in the Figure 5-6 .
rear
index and increase size
by 1.front
by 1 and decrease size
by 1.Both enqueue and dequeue operations only require a single operation, each with a time complexity of \\(O(1)\\).
ArrayQueuepush()pop()Figure 5-6 \u00a0 Implementing Queue with Array for Enqueue and Dequeue Operations
You might notice a problem: as enqueue and dequeue operations are continuously performed, both front
and rear
move to the right and will eventually reach the end of the array and can't move further. To resolve this, we can treat the array as a \"circular array\" where connecting the end of the array back to its beginning.
In a circular array, front
or rear
needs to loop back to the start of the array upon reaching the end. This cyclical pattern can be achieved with a \"modulo operation\" as shown in the code below:
class ArrayQueue:\n \"\"\"\u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\"\"\"\n\n def __init__(self, size: int):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._nums: list[int] = [0] * size # \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n self._front: int = 0 # \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n self._size: int = 0 # \u961f\u5217\u957f\u5ea6\n\n def capacity(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\"\"\"\n return len(self._nums)\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._size == 0\n\n def push(self, num: int):\n \"\"\"\u5165\u961f\"\"\"\n if self._size == self.capacity():\n raise IndexError(\"\u961f\u5217\u5df2\u6ee1\")\n # \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n # \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear: int = (self._front + self._size) % self.capacity()\n # \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self._nums[rear] = num\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u961f\"\"\"\n num: int = self.peek()\n # \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self._front = (self._front + 1) % self.capacity()\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u961f\u9996\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u961f\u5217\u4e3a\u7a7a\")\n return self._nums[self._front]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n res = [0] * self.size()\n j: int = self._front\n for i in range(self.size()):\n res[i] = self._nums[(j % self.capacity())]\n j += 1\n return res\n
array_queue.cpp/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private:\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n\n public:\n ArrayQueue(int capacity) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = new int[capacity];\n queCapacity = capacity;\n front = queSize = 0;\n }\n\n ~ArrayQueue() {\n delete[] nums;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capacity() {\n return queCapacity;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u961f */\n void push(int num) {\n if (queSize == queCapacity) {\n cout << \"\u961f\u5217\u5df2\u6ee1\" << endl;\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % queCapacity;\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty())\n throw out_of_range(\"\u961f\u5217\u4e3a\u7a7a\");\n return nums[front];\n }\n\n /* \u5c06\u6570\u7ec4\u8f6c\u5316\u4e3a Vector \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n vector<int> arr(queSize);\n for (int i = 0, j = front; i < queSize; i++, j++) {\n arr[i] = nums[j % queCapacity];\n }\n return arr;\n }\n};\n
array_queue.java/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n public int capacity() {\n return nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void push(int num) {\n if (queSize == capacity()) {\n System.out.println(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int pop() {\n int num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % capacity()];\n }\n return res;\n }\n}\n
array_queue.cs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n int[] nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u961f\u5217\u957f\u5ea6\n\n public ArrayQueue(int capacity) {\n nums = new int[capacity];\n front = queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int Capacity() {\n return nums.Length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n public int Size() {\n return queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return queSize == 0;\n }\n\n /* \u5165\u961f */\n public void Push(int num) {\n if (queSize == Capacity()) {\n Console.WriteLine(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (front + queSize) % Capacity();\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num;\n queSize++;\n }\n\n /* \u51fa\u961f */\n public int Pop() {\n int num = Peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % Capacity();\n queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return nums[front];\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n public int[] ToArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n int[] res = new int[queSize];\n for (int i = 0, j = front; i < queSize; i++, j++) {\n res[i] = nums[j % this.Capacity()];\n }\n return res;\n }\n}\n
array_queue.go/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntype arrayQueue struct {\n nums []int // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize int // \u961f\u5217\u957f\u5ea6\n queCapacity int // \u961f\u5217\u5bb9\u91cf\uff08\u5373\u6700\u5927\u5bb9\u7eb3\u5143\u7d20\u6570\u91cf\uff09\n}\n\n/* \u521d\u59cb\u5316\u961f\u5217 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n return &arrayQueue{\n nums: make([]int, queCapacity),\n queCapacity: queCapacity,\n front: 0,\n queSize: 0,\n }\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nfunc (q *arrayQueue) size() int {\n return q.queSize\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nfunc (q *arrayQueue) isEmpty() bool {\n return q.queSize == 0\n}\n\n/* \u5165\u961f */\nfunc (q *arrayQueue) push(num int) {\n // \u5f53 rear == queCapacity \u8868\u793a\u961f\u5217\u5df2\u6ee1\n if q.queSize == q.queCapacity {\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n rear := (q.front + q.queSize) % q.queCapacity\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n q.nums[rear] = num\n q.queSize++\n}\n\n/* \u51fa\u961f */\nfunc (q *arrayQueue) pop() any {\n num := q.peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n q.front = (q.front + 1) % q.queCapacity\n q.queSize--\n return num\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nfunc (q *arrayQueue) peek() any {\n if q.isEmpty() {\n return nil\n }\n return q.nums[q.front]\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (q *arrayQueue) toSlice() []int {\n rear := (q.front + q.queSize)\n if rear >= q.queCapacity {\n rear %= q.queCapacity\n return append(q.nums[q.front:], q.nums[:rear]...)\n }\n return q.nums[q.front:rear]\n}\n
array_queue.swift/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private var nums: [Int] // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front: Int // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var _size: Int // \u961f\u5217\u957f\u5ea6\n\n init(capacity: Int) {\n // \u521d\u59cb\u5316\u6570\u7ec4\n nums = Array(repeating: 0, count: capacity)\n front = 0\n _size = 0\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n func capacity() -> Int {\n nums.count\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u961f */\n func push(num: Int) {\n if size() == capacity() {\n print(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (front + size()) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n _size += 1\n }\n\n /* \u51fa\u961f */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u961f\u5217\u4e3a\u7a7a\")\n }\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n func toArray() -> [Int] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n (front ..< front + size()).map { nums[$0 % capacity()] }\n }\n}\n
array_queue.js/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n #nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n #front = 0; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n #queSize = 0; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity) {\n this.#nums = new Array(capacity);\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity() {\n return this.#nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size() {\n return this.#queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num) {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.#front + this.size) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.#nums[rear] = num;\n this.#queSize++;\n }\n\n /* \u51fa\u961f */\n pop() {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.#front = (this.#front + 1) % this.capacity;\n this.#queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek() {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.#nums[this.#front];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n arr[i] = this.#nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.ts/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n private nums: number[]; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private front: number; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private queSize: number; // \u961f\u5217\u957f\u5ea6\n\n constructor(capacity: number) {\n this.nums = new Array(capacity);\n this.front = this.queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n get capacity(): number {\n return this.nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n get size(): number {\n return this.queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.queSize === 0;\n }\n\n /* \u5165\u961f */\n push(num: number): void {\n if (this.size === this.capacity) {\n console.log('\u961f\u5217\u5df2\u6ee1');\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n const rear = (this.front + this.queSize) % this.capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n this.nums[rear] = num;\n this.queSize++;\n }\n\n /* \u51fa\u961f */\n pop(): number {\n const num = this.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n this.front = (this.front + 1) % this.capacity;\n this.queSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n peek(): number {\n if (this.isEmpty()) throw new Error('\u961f\u5217\u4e3a\u7a7a');\n return this.nums[this.front];\n }\n\n /* \u8fd4\u56de Array */\n toArray(): number[] {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n const arr = new Array(this.size);\n for (let i = 0, j = this.front; i < this.size; i++, j++) {\n arr[i] = this.nums[j % this.capacity];\n }\n return arr;\n }\n}\n
array_queue.dart/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue {\n late List<int> _nums; // \u7528\u4e8e\u50a8\u5b58\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n late int _front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n late int _queSize; // \u961f\u5217\u957f\u5ea6\n\n ArrayQueue(int capacity) {\n _nums = List.filled(capacity, 0);\n _front = _queSize = 0;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n int capaCity() {\n return _nums.length;\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n int size() {\n return _queSize;\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _queSize == 0;\n }\n\n /* \u5165\u961f */\n void push(int _num) {\n if (_queSize == capaCity()) {\n throw Exception(\"\u961f\u5217\u5df2\u6ee1\");\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (_front + _queSize) % capaCity();\n // \u5c06 _num \u6dfb\u52a0\u81f3\u961f\u5c3e\n _nums[rear] = _num;\n _queSize++;\n }\n\n /* \u51fa\u961f */\n int pop() {\n int _num = peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n _front = (_front + 1) % capaCity();\n _queSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u961f\u5217\u4e3a\u7a7a\");\n }\n return _nums[_front];\n }\n\n /* \u8fd4\u56de Array */\n List<int> toArray() {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n final List<int> res = List.filled(_queSize, 0);\n for (int i = 0, j = _front; i < _queSize; i++, j++) {\n res[i] = _nums[j % capaCity()];\n }\n return res;\n }\n}\n
array_queue.rs/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nstruct ArrayQueue {\n nums: Vec<i32>, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n front: i32, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n que_size: i32, // \u961f\u5217\u957f\u5ea6\n que_capacity: i32, // \u961f\u5217\u5bb9\u91cf\n}\n\nimpl ArrayQueue {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(capacity: i32) -> ArrayQueue {\n ArrayQueue {\n nums: vec![0; capacity as usize],\n front: 0,\n que_size: 0,\n que_capacity: capacity,\n }\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fn capacity(&self) -> i32 {\n self.que_capacity\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fn size(&self) -> i32 {\n self.que_size\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.que_size == 0\n }\n\n /* \u5165\u961f */\n fn push(&mut self, num: i32) {\n if self.que_size == self.capacity() {\n println!(\"\u961f\u5217\u5df2\u6ee1\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n let rear = (self.front + self.que_size) % self.que_capacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n self.nums[rear as usize] = num;\n self.que_size += 1;\n }\n\n /* \u51fa\u961f */\n fn pop(&mut self) -> i32 {\n let num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.que_capacity;\n self.que_size -= 1;\n num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fn peek(&self) -> i32 {\n if self.is_empty() {\n panic!(\"index out of bounds\");\n }\n self.nums[self.front as usize]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fn to_vector(&self) -> Vec<i32> {\n let cap = self.que_capacity;\n let mut j = self.front;\n let mut arr = vec![0; self.que_size as usize];\n for i in 0..self.que_size {\n arr[i as usize] = self.nums[(j % cap) as usize];\n j += 1;\n }\n arr\n }\n}\n
array_queue.c/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\ntypedef struct {\n int *nums; // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n int front; // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n int queSize; // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n int queCapacity; // \u961f\u5217\u5bb9\u91cf\n} ArrayQueue;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayQueue *newArrayQueue(int capacity) {\n ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n // \u521d\u59cb\u5316\u6570\u7ec4\n queue->queCapacity = capacity;\n queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n queue->front = queue->queSize = 0;\n return queue;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayQueue(ArrayQueue *queue) {\n free(queue->nums);\n free(queue);\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\nint capacity(ArrayQueue *queue) {\n return queue->queCapacity;\n}\n\n/* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\nint size(ArrayQueue *queue) {\n return queue->queSize;\n}\n\n/* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\nbool empty(ArrayQueue *queue) {\n return queue->queSize == 0;\n}\n\n/* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\nint peek(ArrayQueue *queue) {\n assert(size(queue) != 0);\n return queue->nums[queue->front];\n}\n\n/* \u5165\u961f */\nvoid push(ArrayQueue *queue, int num) {\n if (size(queue) == capacity(queue)) {\n printf(\"\u961f\u5217\u5df2\u6ee1\\r\\n\");\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n int rear = (queue->front + queue->queSize) % queue->queCapacity;\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n queue->nums[rear] = num;\n queue->queSize++;\n}\n\n/* \u51fa\u961f */\nint pop(ArrayQueue *queue) {\n int num = peek(queue);\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n queue->front = (queue->front + 1) % queue->queCapacity;\n queue->queSize--;\n return num;\n}\n
array_queue.kt/* \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217 */\nclass ArrayQueue(capacity: Int) {\n private val nums = IntArray(capacity) // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4\n private var front = 0 // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n private var queSize = 0 // \u961f\u5217\u957f\u5ea6\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf */\n fun capacity(): Int {\n return nums.size\n }\n\n /* \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6 */\n fun size(): Int {\n return queSize\n }\n\n /* \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return queSize == 0\n }\n\n /* \u5165\u961f */\n fun push(num: Int) {\n if (queSize == capacity()) {\n println(\"\u961f\u5217\u5df2\u6ee1\")\n return\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n val rear = (front + queSize) % capacity()\n // \u5c06 num \u6dfb\u52a0\u81f3\u961f\u5c3e\n nums[rear] = num\n queSize++\n }\n\n /* \u51fa\u961f */\n fun pop(): Int {\n val num = peek()\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n front = (front + 1) % capacity()\n queSize--\n return num\n }\n\n /* \u8bbf\u95ee\u961f\u9996\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return nums[front]\n }\n\n /* \u8fd4\u56de\u6570\u7ec4 */\n fun toArray(): IntArray {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n val res = IntArray(queSize)\n var i = 0\n var j = front\n while (i < queSize) {\n res[i] = nums[j % capacity()]\n i++\n j++\n }\n return res\n }\n}\n
array_queue.rb[class]{ArrayQueue}-[func]{}\n
array_queue.zig// \u57fa\u4e8e\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\u961f\u5217\nfn ArrayQueue(comptime T: type) type {\n return struct {\n const Self = @This();\n\n nums: []T = undefined, // \u7528\u4e8e\u5b58\u50a8\u961f\u5217\u5143\u7d20\u7684\u6570\u7ec4 \n cap: usize = 0, // \u961f\u5217\u5bb9\u91cf\n front: usize = 0, // \u961f\u9996\u6307\u9488\uff0c\u6307\u5411\u961f\u9996\u5143\u7d20\n queSize: usize = 0, // \u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e + 1\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6570\u7ec4\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator, cap: usize) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.cap = cap;\n self.nums = try self.mem_allocator.alloc(T, self.cap);\n @memset(self.nums, @as(T, 0));\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u5bb9\u91cf\n pub fn capacity(self: *Self) usize {\n return self.cap;\n }\n\n // \u83b7\u53d6\u961f\u5217\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.queSize;\n }\n\n // \u5224\u65ad\u961f\u5217\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.queSize == 0;\n }\n\n // \u5165\u961f\n pub fn push(self: *Self, num: T) !void {\n if (self.size() == self.capacity()) {\n std.debug.print(\"\u961f\u5217\u5df2\u6ee1\\n\", .{});\n return;\n }\n // \u8ba1\u7b97\u961f\u5c3e\u6307\u9488\uff0c\u6307\u5411\u961f\u5c3e\u7d22\u5f15 + 1\n // \u901a\u8fc7\u53d6\u4f59\u64cd\u4f5c\u5b9e\u73b0 rear \u8d8a\u8fc7\u6570\u7ec4\u5c3e\u90e8\u540e\u56de\u5230\u5934\u90e8\n var rear = (self.front + self.queSize) % self.capacity();\n // \u5728\u5c3e\u8282\u70b9\u540e\u6dfb\u52a0 num\n self.nums[rear] = num;\n self.queSize += 1;\n } \n\n // \u51fa\u961f\n pub fn pop(self: *Self) T {\n var num = self.peek();\n // \u961f\u9996\u6307\u9488\u5411\u540e\u79fb\u52a8\u4e00\u4f4d\uff0c\u82e5\u8d8a\u8fc7\u5c3e\u90e8\uff0c\u5219\u8fd4\u56de\u5230\u6570\u7ec4\u5934\u90e8\n self.front = (self.front + 1) % self.capacity();\n self.queSize -= 1;\n return num;\n } \n\n // \u8bbf\u95ee\u961f\u9996\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u961f\u5217\u4e3a\u7a7a\");\n return self.nums[self.front];\n } \n\n // \u8fd4\u56de\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n // \u4ec5\u8f6c\u6362\u6709\u6548\u957f\u5ea6\u8303\u56f4\u5185\u7684\u5217\u8868\u5143\u7d20\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n var j: usize = self.front;\n while (i < self.size()) : ({ i += 1; j += 1; }) {\n res[i] = self.nums[j % self.capacity()];\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
The above implementation of the queue still has its limitations: its length is fixed. However, this issue is not difficult to resolve. We can replace the array with a dynamic array that can expand itself if needed. Interested readers can try to implement this themselves.
The comparison of the two implementations is consistent with that of the stack and is not repeated here.
"},{"location":"chapter_stack_and_queue/queue/#523-typical-applications-of-queue","title":"5.2.3 \u00a0 Typical applications of queue","text":"A \"Stack\" is a linear data structure that follows the principle of Last-In-First-Out (LIFO).
We can compare a stack to a pile of plates on a table. To access the bottom plate, one must first remove the plates on top. By replacing the plates with various types of elements (such as integers, characters, objects, etc.), we obtain the data structure known as a stack.
As shown in the Figure 5-1 , we refer to the top of the pile of elements as the \"top of the stack\" and the bottom as the \"bottom of the stack.\" The operation of adding elements to the top of the stack is called \"push,\" and the operation of removing the top element is called \"pop.\"
Figure 5-1 \u00a0 Stack's last-in-first-out rule
"},{"location":"chapter_stack_and_queue/stack/#511-common-operations-on-stack","title":"5.1.1 \u00a0 Common operations on stack","text":"The common operations on a stack are shown in the Table 5-1 . The specific method names depend on the programming language used. Here, we use push()
, pop()
, and peek()
as examples.
Table 5-1 \u00a0 Efficiency of stack operations
Method Description Time Complexitypush()
Push an element onto the stack (add to the top) \\(O(1)\\) pop()
Pop the top element from the stack \\(O(1)\\) peek()
Access the top element of the stack \\(O(1)\\) Typically, we can directly use the stack class built into the programming language. However, some languages may not specifically provide a stack class. In these cases, we can use the language's \"array\" or \"linked list\" as a stack and ignore operations that are not related to stack logic in the program.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinZig stack.py# Initialize the stack\n# Python does not have a built-in stack class, so a list can be used as a stack\nstack: list[int] = []\n\n# Push elements onto the stack\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# Access the top element of the stack\npeek: int = stack[-1]\n\n# Pop an element from the stack\npop: int = stack.pop()\n\n# Get the length of the stack\nsize: int = len(stack)\n\n# Check if the stack is empty\nis_empty: bool = len(stack) == 0\n
stack.cpp/* Initialize the stack */\nstack<int> stack;\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nint top = stack.top();\n\n/* Pop an element from the stack */\nstack.pop(); // No return value\n\n/* Get the length of the stack */\nint size = stack.size();\n\n/* Check if the stack is empty */\nbool empty = stack.empty();\n
stack.java/* Initialize the stack */\nStack<Integer> stack = new Stack<>();\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nint peek = stack.peek();\n\n/* Pop an element from the stack */\nint pop = stack.pop();\n\n/* Get the length of the stack */\nint size = stack.size();\n\n/* Check if the stack is empty */\nboolean isEmpty = stack.isEmpty();\n
stack.cs/* Initialize the stack */\nStack<int> stack = new();\n\n/* Push elements onto the stack */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* Access the top element of the stack */\nint peek = stack.Peek();\n\n/* Pop an element from the stack */\nint pop = stack.Pop();\n\n/* Get the length of the stack */\nint size = stack.Count;\n\n/* Check if the stack is empty */\nbool isEmpty = stack.Count == 0;\n
stack_test.go/* Initialize the stack */\n// In Go, it is recommended to use a Slice as a stack\nvar stack []int\n\n/* Push elements onto the stack */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* Access the top element of the stack */\npeek := stack[len(stack)-1]\n\n/* Pop an element from the stack */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* Get the length of the stack */\nsize := len(stack)\n\n/* Check if the stack is empty */\nisEmpty := len(stack) == 0\n
stack.swift/* Initialize the stack */\n// Swift does not have a built-in stack class, so Array can be used as a stack\nvar stack: [Int] = []\n\n/* Push elements onto the stack */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* Access the top element of the stack */\nlet peek = stack.last!\n\n/* Pop an element from the stack */\nlet pop = stack.removeLast()\n\n/* Get the length of the stack */\nlet size = stack.count\n\n/* Check if the stack is empty */\nlet isEmpty = stack.isEmpty\n
stack.js/* Initialize the stack */\n// JavaScript does not have a built-in stack class, so Array can be used as a stack\nconst stack = [];\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nconst peek = stack[stack.length-1];\n\n/* Pop an element from the stack */\nconst pop = stack.pop();\n\n/* Get the length of the stack */\nconst size = stack.length;\n\n/* Check if the stack is empty */\nconst is_empty = stack.length === 0;\n
stack.ts/* Initialize the stack */\n// TypeScript does not have a built-in stack class, so Array can be used as a stack\nconst stack: number[] = [];\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nconst peek = stack[stack.length - 1];\n\n/* Pop an element from the stack */\nconst pop = stack.pop();\n\n/* Get the length of the stack */\nconst size = stack.length;\n\n/* Check if the stack is empty */\nconst is_empty = stack.length === 0;\n
stack.dart/* Initialize the stack */\n// Dart does not have a built-in stack class, so List can be used as a stack\nList<int> stack = [];\n\n/* Push elements onto the stack */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* Access the top element of the stack */\nint peek = stack.last;\n\n/* Pop an element from the stack */\nint pop = stack.removeLast();\n\n/* Get the length of the stack */\nint size = stack.length;\n\n/* Check if the stack is empty */\nbool isEmpty = stack.isEmpty;\n
stack.rs/* Initialize the stack */\n// Use Vec as a stack\nlet mut stack: Vec<i32> = Vec::new();\n\n/* Push elements onto the stack */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* Access the top element of the stack */\nlet top = stack.last().unwrap();\n\n/* Pop an element from the stack */\nlet pop = stack.pop().unwrap();\n\n/* Get the length of the stack */\nlet size = stack.len();\n\n/* Check if the stack is empty */\nlet is_empty = stack.is_empty();\n
stack.c// C does not provide a built-in stack\n
stack.kt\n
stack.zig\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#512-implementing-a-stack","title":"5.1.2 \u00a0 Implementing a stack","text":"To gain a deeper understanding of how a stack operates, let's try implementing a stack class ourselves.
A stack follows the principle of Last-In-First-Out, which means we can only add or remove elements at the top of the stack. However, both arrays and linked lists allow adding and removing elements at any position, therefore a stack can be seen as a restricted array or linked list. In other words, we can \"shield\" certain irrelevant operations of an array or linked list, aligning their external behavior with the characteristics of a stack.
"},{"location":"chapter_stack_and_queue/stack/#1-implementation-based-on-a-linked-list","title":"1. \u00a0 Implementation based on a linked list","text":"When implementing a stack using a linked list, we can consider the head node of the list as the top of the stack and the tail node as the bottom of the stack.
As shown in the Figure 5-2 , for the push operation, we simply insert elements at the head of the linked list. This method of node insertion is known as \"head insertion.\" For the pop operation, we just need to remove the head node from the list.
LinkedListStackpush()pop()Figure 5-2 \u00a0 Implementing Stack with Linked List for Push and Pop Operations
Below is an example code for implementing a stack based on a linked list:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig linkedlist_stack.pyclass LinkedListStack:\n \"\"\"\u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._peek: ListNode | None = None\n self._size: int = 0\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return self._size\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return not self._peek\n\n def push(self, val: int):\n \"\"\"\u5165\u6808\"\"\"\n node = ListNode(val)\n node.next = self._peek\n self._peek = node\n self._size += 1\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n num = self.peek()\n self._peek = self._peek.next\n self._size -= 1\n return num\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._peek.val\n\n def to_list(self) -> list[int]:\n \"\"\"\u8f6c\u5316\u4e3a\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n arr = []\n node = self._peek\n while node:\n arr.append(node.val)\n node = node.next\n arr.reverse()\n return arr\n
linkedlist_stack.cpp/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private:\n ListNode *stackTop; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize; // \u6808\u7684\u957f\u5ea6\n\n public:\n LinkedListStack() {\n stackTop = nullptr;\n stkSize = 0;\n }\n\n ~LinkedListStack() {\n // \u904d\u5386\u94fe\u8868\u5220\u9664\u8282\u70b9\uff0c\u91ca\u653e\u5185\u5b58\n freeMemoryLinkedList(stackTop);\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n ListNode *node = new ListNode(num);\n node->next = stackTop;\n stackTop = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n int num = top();\n ListNode *tmp = stackTop;\n stackTop = stackTop->next;\n // \u91ca\u653e\u5185\u5b58\n delete tmp;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stackTop->val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n vector<int> toVector() {\n ListNode *node = stackTop;\n vector<int> res(size());\n for (int i = res.size() - 1; i >= 0; i--) {\n res[i] = node->val;\n node = node->next;\n }\n return res;\n }\n};\n
linkedlist_stack.java/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private ListNode stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n ListNode node = new ListNode(num);\n node.next = stackPeek;\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n int num = peek();\n stackPeek = stackPeek.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stackPeek.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] toArray() {\n ListNode node = stackPeek;\n int[] res = new int[size()];\n for (int i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.cs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n public LinkedListStack() {\n stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n ListNode node = new(num) {\n next = stackPeek\n };\n stackPeek = node;\n stkSize++;\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n int num = Peek();\n stackPeek = stackPeek!.next;\n stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stackPeek!.val;\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n if (stackPeek == null)\n return [];\n\n ListNode? node = stackPeek;\n int[] res = new int[Size()];\n for (int i = res.Length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.go/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntype linkedListStack struct {\n // \u4f7f\u7528\u5185\u7f6e\u5305 list \u6765\u5b9e\u73b0\u6808\n data *list.List\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newLinkedListStack() *linkedListStack {\n return &linkedListStack{\n data: list.New(),\n }\n}\n\n/* \u5165\u6808 */\nfunc (s *linkedListStack) push(value int) {\n s.data.PushBack(value)\n}\n\n/* \u51fa\u6808 */\nfunc (s *linkedListStack) pop() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n s.data.Remove(e)\n return e.Value\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nfunc (s *linkedListStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n e := s.data.Back()\n return e.Value\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nfunc (s *linkedListStack) size() int {\n return s.data.Len()\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *linkedListStack) isEmpty() bool {\n return s.data.Len() == 0\n}\n\n/* \u83b7\u53d6 List \u7528\u4e8e\u6253\u5370 */\nfunc (s *linkedListStack) toList() *list.List {\n return s.data\n}\n
linkedlist_stack.swift/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private var _peek: ListNode? // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var _size: Int // \u6808\u7684\u957f\u5ea6\n\n init() {\n _size = 0\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n _size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n size() == 0\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n let node = ListNode(x: num)\n node.next = _peek\n _peek = node\n _size += 1\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n let num = peek()\n _peek = _peek?.next\n _size -= 1\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return _peek!.val\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n var node = _peek\n var res = Array(repeating: 0, count: size())\n for i in res.indices.reversed() {\n res[i] = node!.val\n node = node?.next\n }\n return res\n }\n}\n
linkedlist_stack.js/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n #stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n #stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.#stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n const node = new ListNode(num);\n node.next = this.#stackPeek;\n this.#stackPeek = node;\n this.#stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop() {\n const num = this.peek();\n this.#stackPeek = this.#stackPeek.next;\n this.#stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek() {\n if (!this.#stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray() {\n let node = this.#stackPeek;\n const res = new Array(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node.val;\n node = node.next;\n }\n return res;\n }\n}\n
linkedlist_stack.ts/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n private stackPeek: ListNode | null; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private stkSize: number = 0; // \u6808\u7684\u957f\u5ea6\n\n constructor() {\n this.stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.size === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n const node = new ListNode(num);\n node.next = this.stackPeek;\n this.stackPeek = node;\n this.stkSize++;\n }\n\n /* \u51fa\u6808 */\n pop(): number {\n const num = this.peek();\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n this.stackPeek = this.stackPeek.next;\n this.stkSize--;\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n peek(): number {\n if (!this.stackPeek) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stackPeek.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n toArray(): number[] {\n let node = this.stackPeek;\n const res = new Array<number>(this.size);\n for (let i = res.length - 1; i >= 0; i--) {\n res[i] = node!.val;\n node = node!.next;\n }\n return res;\n }\n}\n
linkedlist_stack.dart/* \u57fa\u4e8e\u94fe\u8868\u7c7b\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack {\n ListNode? _stackPeek; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int _stkSize = 0; // \u6808\u7684\u957f\u5ea6\n\n LinkedListStack() {\n _stackPeek = null;\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stkSize;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stkSize == 0;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n final ListNode node = ListNode(_num);\n node.next = _stackPeek;\n _stackPeek = node;\n _stkSize++;\n }\n\n /* \u51fa\u6808 */\n int pop() {\n final int _num = peek();\n _stackPeek = _stackPeek!.next;\n _stkSize--;\n return _num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (_stackPeek == null) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stackPeek!.val;\n }\n\n /* \u5c06\u94fe\u8868\u8f6c\u5316\u4e3a List \u5e76\u8fd4\u56de */\n List<int> toList() {\n ListNode? node = _stackPeek;\n List<int> list = [];\n while (node != null) {\n list.add(node.val);\n node = node.next;\n }\n list = list.reversed.toList();\n return list;\n }\n}\n
linkedlist_stack.rs/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize, // \u6808\u7684\u957f\u5ea6\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n pub fn new() -> Self {\n Self {\n stack_peek: None,\n stk_size: 0,\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n pub fn size(&self) -> usize {\n return self.stk_size;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n pub fn is_empty(&self) -> bool {\n return self.size() == 0;\n }\n\n /* \u5165\u6808 */\n pub fn push(&mut self, num: T) {\n let node = ListNode::new(num);\n node.borrow_mut().next = self.stack_peek.take();\n self.stack_peek = Some(node);\n self.stk_size += 1;\n }\n\n /* \u51fa\u6808 */\n pub fn pop(&mut self) -> Option<T> {\n self.stack_peek.take().map(|old_head| {\n match old_head.borrow_mut().next.take() {\n Some(new_head) => {\n self.stack_peek = Some(new_head);\n }\n None => {\n self.stack_peek = None;\n }\n }\n self.stk_size -= 1;\n Rc::try_unwrap(old_head).ok().unwrap().into_inner().val\n })\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n self.stack_peek.as_ref()\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n if let Some(node) = head {\n let mut nums = self.to_array(node.borrow().next.as_ref());\n nums.push(node.borrow().val);\n return nums;\n }\n return Vec::new();\n }\n}\n
linkedlist_stack.c/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n ListNode *top; // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n int size; // \u6808\u7684\u957f\u5ea6\n} LinkedListStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nLinkedListStack *newLinkedListStack() {\n LinkedListStack *s = malloc(sizeof(LinkedListStack));\n s->top = NULL;\n s->size = 0;\n return s;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delLinkedListStack(LinkedListStack *s) {\n while (s->top) {\n ListNode *n = s->top->next;\n free(s->top);\n s->top = n;\n }\n free(s);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(LinkedListStack *s) {\n return s->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(LinkedListStack *s) {\n return size(s) == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(LinkedListStack *s, int num) {\n ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n node->next = s->top; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6307\u9488\u57df\n node->val = num; // \u66f4\u65b0\u65b0\u52a0\u8282\u70b9\u6570\u636e\u57df\n s->top = node; // \u66f4\u65b0\u6808\u9876\n s->size++; // \u66f4\u65b0\u6808\u5927\u5c0f\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(LinkedListStack *s) {\n if (s->size == 0) {\n printf(\"\u6808\u4e3a\u7a7a\\n\");\n return INT_MAX;\n }\n return s->top->val;\n}\n\n/* \u51fa\u6808 */\nint pop(LinkedListStack *s) {\n int val = peek(s);\n ListNode *tmp = s->top;\n s->top = s->top->next;\n // \u91ca\u653e\u5185\u5b58\n free(tmp);\n s->size--;\n return val;\n}\n
linkedlist_stack.kt/* \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808 */\nclass LinkedListStack(\n private var stackPeek: ListNode? = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n private var stkSize: Int = 0 // \u6808\u7684\u957f\u5ea6\n) {\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fun size(): Int {\n return stkSize\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u6808 */\n fun push(num: Int) {\n val node = ListNode(num)\n node.next = stackPeek\n stackPeek = node\n stkSize++\n }\n\n /* \u51fa\u6808 */\n fun pop(): Int? {\n val num = peek()\n stackPeek = stackPeek?.next\n stkSize--;\n return num\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fun peek(): Int? {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stackPeek?.value\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): IntArray {\n var node = stackPeek\n val res = IntArray(size())\n for (i in res.size - 1 downTo 0) {\n res[i] = node?.value!!\n node = node.next\n }\n return res\n }\n}\n
linkedlist_stack.rb[class]{LinkedListStack}-[func]{}\n
linkedlist_stack.zig// \u57fa\u4e8e\u94fe\u8868\u5b9e\u73b0\u7684\u6808\nfn LinkedListStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack_top: ?*inc.ListNode(T) = null, // \u5c06\u5934\u8282\u70b9\u4f5c\u4e3a\u6808\u9876\n stk_size: usize = 0, // \u6808\u7684\u957f\u5ea6\n mem_arena: ?std.heap.ArenaAllocator = null,\n mem_allocator: std.mem.Allocator = undefined, // \u5185\u5b58\u5206\u914d\u5668\n\n // \u6784\u9020\u51fd\u6570\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) !void {\n if (self.mem_arena == null) {\n self.mem_arena = std.heap.ArenaAllocator.init(allocator);\n self.mem_allocator = self.mem_arena.?.allocator();\n }\n self.stack_top = null;\n self.stk_size = 0;\n }\n\n // \u6790\u6784\u51fd\u6570\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.mem_arena == null) return;\n self.mem_arena.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stk_size;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.size() == 0) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack_top.?.val;\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n var node = try self.mem_allocator.create(inc.ListNode(T));\n node.init(num);\n node.next = self.stack_top;\n self.stack_top = node;\n self.stk_size += 1;\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.peek();\n self.stack_top = self.stack_top.?.next;\n self.stk_size -= 1;\n return num;\n } \n\n // \u5c06\u6808\u8f6c\u6362\u4e3a\u6570\u7ec4\n pub fn toArray(self: *Self) ![]T {\n var node = self.stack_top;\n var res = try self.mem_allocator.alloc(T, self.size());\n @memset(res, @as(T, 0));\n var i: usize = 0;\n while (i < res.len) : (i += 1) {\n res[res.len - i - 1] = node.?.val;\n node = node.?.next;\n }\n return res;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#2-implementation-based-on-an-array","title":"2. \u00a0 Implementation based on an array","text":"When implementing a stack using an array, we can consider the end of the array as the top of the stack. As shown in the Figure 5-3 , push and pop operations correspond to adding and removing elements at the end of the array, respectively, both with a time complexity of \\(O(1)\\).
ArrayStackpush()pop()Figure 5-3 \u00a0 Implementing Stack with Array for Push and Pop Operations
Since the elements to be pushed onto the stack may continuously increase, we can use a dynamic array, thus avoiding the need to handle array expansion ourselves. Here is an example code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig array_stack.pyclass ArrayStack:\n \"\"\"\u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\"\"\"\n\n def __init__(self):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._stack: list[int] = []\n\n def size(self) -> int:\n \"\"\"\u83b7\u53d6\u6808\u7684\u957f\u5ea6\"\"\"\n return len(self._stack)\n\n def is_empty(self) -> bool:\n \"\"\"\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\"\"\"\n return self._stack == []\n\n def push(self, item: int):\n \"\"\"\u5165\u6808\"\"\"\n self._stack.append(item)\n\n def pop(self) -> int:\n \"\"\"\u51fa\u6808\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack.pop()\n\n def peek(self) -> int:\n \"\"\"\u8bbf\u95ee\u6808\u9876\u5143\u7d20\"\"\"\n if self.is_empty():\n raise IndexError(\"\u6808\u4e3a\u7a7a\")\n return self._stack[-1]\n\n def to_list(self) -> list[int]:\n \"\"\"\u8fd4\u56de\u5217\u8868\u7528\u4e8e\u6253\u5370\"\"\"\n return self._stack\n
array_stack.cpp/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private:\n vector<int> stack;\n\n public:\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return stack.size() == 0;\n }\n\n /* \u5165\u6808 */\n void push(int num) {\n stack.push_back(num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n int num = top();\n stack.pop_back();\n return num;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int top() {\n if (isEmpty())\n throw out_of_range(\"\u6808\u4e3a\u7a7a\");\n return stack.back();\n }\n\n /* \u8fd4\u56de Vector */\n vector<int> toVector() {\n return stack;\n }\n};\n
array_stack.java/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private ArrayList<Integer> stack;\n\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = new ArrayList<>();\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int size() {\n return stack.size();\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public boolean isEmpty() {\n return size() == 0;\n }\n\n /* \u5165\u6808 */\n public void push(int num) {\n stack.add(num);\n }\n\n /* \u51fa\u6808 */\n public int pop() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.remove(size() - 1);\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int peek() {\n if (isEmpty())\n throw new IndexOutOfBoundsException();\n return stack.get(size() - 1);\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public Object[] toArray() {\n return stack.toArray();\n }\n}\n
array_stack.cs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n List<int> stack;\n public ArrayStack() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n public int Size() {\n return stack.Count;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n public bool IsEmpty() {\n return Size() == 0;\n }\n\n /* \u5165\u6808 */\n public void Push(int num) {\n stack.Add(num);\n }\n\n /* \u51fa\u6808 */\n public int Pop() {\n if (IsEmpty())\n throw new Exception();\n var val = Peek();\n stack.RemoveAt(Size() - 1);\n return val;\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n public int Peek() {\n if (IsEmpty())\n throw new Exception();\n return stack[Size() - 1];\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n public int[] ToArray() {\n return [.. stack];\n }\n}\n
array_stack.go/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntype arrayStack struct {\n data []int // \u6570\u636e\n}\n\n/* \u521d\u59cb\u5316\u6808 */\nfunc newArrayStack() *arrayStack {\n return &arrayStack{\n // \u8bbe\u7f6e\u6808\u7684\u957f\u5ea6\u4e3a 0\uff0c\u5bb9\u91cf\u4e3a 16\n data: make([]int, 0, 16),\n }\n}\n\n/* \u6808\u7684\u957f\u5ea6 */\nfunc (s *arrayStack) size() int {\n return len(s.data)\n}\n\n/* \u6808\u662f\u5426\u4e3a\u7a7a */\nfunc (s *arrayStack) isEmpty() bool {\n return s.size() == 0\n}\n\n/* \u5165\u6808 */\nfunc (s *arrayStack) push(v int) {\n // \u5207\u7247\u4f1a\u81ea\u52a8\u6269\u5bb9\n s.data = append(s.data, v)\n}\n\n/* \u51fa\u6808 */\nfunc (s *arrayStack) pop() any {\n val := s.peek()\n s.data = s.data[:len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6\u6808\u9876\u5143\u7d20 */\nfunc (s *arrayStack) peek() any {\n if s.isEmpty() {\n return nil\n }\n val := s.data[len(s.data)-1]\n return val\n}\n\n/* \u83b7\u53d6 Slice \u7528\u4e8e\u6253\u5370 */\nfunc (s *arrayStack) toSlice() []int {\n return s.data\n}\n
array_stack.swift/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private var stack: [Int]\n\n init() {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n stack = []\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n func size() -> Int {\n stack.count\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n func isEmpty() -> Bool {\n stack.isEmpty\n }\n\n /* \u5165\u6808 */\n func push(num: Int) {\n stack.append(num)\n }\n\n /* \u51fa\u6808 */\n @discardableResult\n func pop() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.removeLast()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n func peek() -> Int {\n if isEmpty() {\n fatalError(\"\u6808\u4e3a\u7a7a\")\n }\n return stack.last!\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n func toArray() -> [Int] {\n stack\n }\n}\n
array_stack.js/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n #stack;\n constructor() {\n this.#stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size() {\n return this.#stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty() {\n return this.#stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num) {\n this.#stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top() {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.#stack[this.#stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.#stack;\n }\n}\n
array_stack.ts/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n private stack: number[];\n constructor() {\n this.stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n get size(): number {\n return this.stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n isEmpty(): boolean {\n return this.stack.length === 0;\n }\n\n /* \u5165\u6808 */\n push(num: number): void {\n this.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n pop(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack.pop();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n top(): number | undefined {\n if (this.isEmpty()) throw new Error('\u6808\u4e3a\u7a7a');\n return this.stack[this.stack.length - 1];\n }\n\n /* \u8fd4\u56de Array */\n toArray() {\n return this.stack;\n }\n}\n
array_stack.dart/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n late List<int> _stack;\n ArrayStack() {\n _stack = [];\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n int size() {\n return _stack.length;\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n bool isEmpty() {\n return _stack.isEmpty;\n }\n\n /* \u5165\u6808 */\n void push(int _num) {\n _stack.add(_num);\n }\n\n /* \u51fa\u6808 */\n int pop() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.removeLast();\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n int peek() {\n if (isEmpty()) {\n throw Exception(\"\u6808\u4e3a\u7a7a\");\n }\n return _stack.last;\n }\n\n /* \u5c06\u6808\u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n List<int> toArray() => _stack;\n}\n
array_stack.rs/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nstruct ArrayStack<T> {\n stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n /* \u521d\u59cb\u5316\u6808 */\n fn new() -> ArrayStack<T> {\n ArrayStack::<T> {\n stack: Vec::<T>::new(),\n }\n }\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fn size(&self) -> usize {\n self.stack.len()\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fn is_empty(&self) -> bool {\n self.size() == 0\n }\n\n /* \u5165\u6808 */\n fn push(&mut self, num: T) {\n self.stack.push(num);\n }\n\n /* \u51fa\u6808 */\n fn pop(&mut self) -> Option<T> {\n self.stack.pop()\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fn peek(&self) -> Option<&T> {\n if self.is_empty() {\n panic!(\"\u6808\u4e3a\u7a7a\")\n };\n self.stack.last()\n }\n\n /* \u8fd4\u56de &Vec */\n fn to_array(&self) -> &Vec<T> {\n &self.stack\n }\n}\n
array_stack.c/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\ntypedef struct {\n int *data;\n int size;\n} ArrayStack;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayStack *newArrayStack() {\n ArrayStack *stack = malloc(sizeof(ArrayStack));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5927\u5bb9\u91cf\uff0c\u907f\u514d\u6269\u5bb9\n stack->data = malloc(sizeof(int) * MAX_SIZE);\n stack->size = 0;\n return stack;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayStack(ArrayStack *stack) {\n free(stack->data);\n free(stack);\n}\n\n/* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\nint size(ArrayStack *stack) {\n return stack->size;\n}\n\n/* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\nbool isEmpty(ArrayStack *stack) {\n return stack->size == 0;\n}\n\n/* \u5165\u6808 */\nvoid push(ArrayStack *stack, int num) {\n if (stack->size == MAX_SIZE) {\n printf(\"\u6808\u5df2\u6ee1\\n\");\n return;\n }\n stack->data[stack->size] = num;\n stack->size++;\n}\n\n/* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\nint peek(ArrayStack *stack) {\n if (stack->size == 0) {\n printf(\"\u6808\u4e3a\u7a7a\\n\");\n return INT_MAX;\n }\n return stack->data[stack->size - 1];\n}\n\n/* \u51fa\u6808 */\nint pop(ArrayStack *stack) {\n int val = peek(stack);\n stack->size--;\n return val;\n}\n
array_stack.kt/* \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808 */\nclass ArrayStack {\n // \u521d\u59cb\u5316\u5217\u8868\uff08\u52a8\u6001\u6570\u7ec4\uff09\n private val stack = ArrayList<Int>()\n\n /* \u83b7\u53d6\u6808\u7684\u957f\u5ea6 */\n fun size(): Int {\n return stack.size\n }\n\n /* \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a */\n fun isEmpty(): Boolean {\n return size() == 0\n }\n\n /* \u5165\u6808 */\n fun push(num: Int) {\n stack.add(num)\n }\n\n /* \u51fa\u6808 */\n fun pop(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack.removeAt(size() - 1)\n }\n\n /* \u8bbf\u95ee\u6808\u9876\u5143\u7d20 */\n fun peek(): Int {\n if (isEmpty()) throw IndexOutOfBoundsException()\n return stack[size() - 1]\n }\n\n /* \u5c06 List \u8f6c\u5316\u4e3a Array \u5e76\u8fd4\u56de */\n fun toArray(): Array<Any> {\n return stack.toArray()\n }\n}\n
array_stack.rb[class]{ArrayStack}-[func]{}\n
array_stack.zig// \u57fa\u4e8e\u6570\u7ec4\u5b9e\u73b0\u7684\u6808\nfn ArrayStack(comptime T: type) type {\n return struct {\n const Self = @This();\n\n stack: ?std.ArrayList(T) = null, \n\n // \u6784\u9020\u65b9\u6cd5\uff08\u5206\u914d\u5185\u5b58+\u521d\u59cb\u5316\u6808\uff09\n pub fn init(self: *Self, allocator: std.mem.Allocator) void {\n if (self.stack == null) {\n self.stack = std.ArrayList(T).init(allocator);\n }\n }\n\n // \u6790\u6784\u65b9\u6cd5\uff08\u91ca\u653e\u5185\u5b58\uff09\n pub fn deinit(self: *Self) void {\n if (self.stack == null) return;\n self.stack.?.deinit();\n }\n\n // \u83b7\u53d6\u6808\u7684\u957f\u5ea6\n pub fn size(self: *Self) usize {\n return self.stack.?.items.len;\n }\n\n // \u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\n pub fn isEmpty(self: *Self) bool {\n return self.size() == 0;\n }\n\n // \u8bbf\u95ee\u6808\u9876\u5143\u7d20\n pub fn peek(self: *Self) T {\n if (self.isEmpty()) @panic(\"\u6808\u4e3a\u7a7a\");\n return self.stack.?.items[self.size() - 1];\n } \n\n // \u5165\u6808\n pub fn push(self: *Self, num: T) !void {\n try self.stack.?.append(num);\n } \n\n // \u51fa\u6808\n pub fn pop(self: *Self) T {\n var num = self.stack.?.pop();\n return num;\n } \n\n // \u8fd4\u56de ArrayList\n pub fn toList(self: *Self) std.ArrayList(T) {\n return self.stack.?;\n }\n };\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_stack_and_queue/stack/#513-comparison-of-the-two-implementations","title":"5.1.3 \u00a0 Comparison of the two implementations","text":"Supported Operations
Both implementations support all the operations defined in a stack. The array implementation additionally supports random access, but this is beyond the scope of a stack definition and is generally not used.
Time Efficiency
In the array-based implementation, both push and pop operations occur in pre-allocated contiguous memory, which has good cache locality and therefore higher efficiency. However, if the push operation exceeds the array capacity, it triggers a resizing mechanism, making the time complexity of that push operation \\(O(n)\\).
In the linked list implementation, list expansion is very flexible, and there is no efficiency decrease issue as in array expansion. However, the push operation requires initializing a node object and modifying pointers, so its efficiency is relatively lower. If the elements being pushed are already node objects, then the initialization step can be skipped, improving efficiency.
Thus, when the elements for push and pop operations are basic data types like int
or double
, we can draw the following conclusions:
Space Efficiency
When initializing a list, the system allocates an \"initial capacity,\" which might exceed the actual need; moreover, the expansion mechanism usually increases capacity by a specific factor (like doubling), which may also exceed the actual need. Therefore, the array-based stack might waste some space.
However, since linked list nodes require extra space for storing pointers, the space occupied by linked list nodes is relatively larger.
In summary, we cannot simply determine which implementation is more memory-efficient. It requires analysis based on specific circumstances.
"},{"location":"chapter_stack_and_queue/stack/#514-typical-applications-of-stack","title":"5.1.4 \u00a0 Typical applications of stack","text":"Q: Is the browser's forward and backward functionality implemented with a doubly linked list?
A browser's forward and backward navigation is essentially a manifestation of the \"stack\" concept. When a user visits a new page, the page is added to the top of the stack; when they click the back button, the page is popped from the top of the stack. A double-ended queue (deque) can conveniently implement some additional operations, as mentioned in the \"Double-Ended Queue\" section.
Q: After popping from a stack, is it necessary to free the memory of the popped node?
If the popped node will still be used later, it's not necessary to free its memory. In languages like Java and Python that have automatic garbage collection, manual memory release is not necessary; in C and C++, manual memory release is required.
Q: A double-ended queue seems like two stacks joined together. What are its uses?
A double-ended queue, which is a combination of a stack and a queue or two stacks joined together, exhibits both stack and queue logic. Thus, it can implement all applications of stacks and queues while offering more flexibility.
Q: How exactly are undo and redo implemented?
Undo and redo operations are implemented using two stacks: Stack A for undo and Stack B for redo.
Abstract
The towering tree, full of vitality with its roots deep and leaves lush, branches spreading wide.
It vividly demonstrates the form of data divide-and-conquer.
"},{"location":"chapter_tree/#chapter-contents","title":"Chapter contents","text":"Under the linked list representation, the storage unit of a binary tree is a node TreeNode
, with nodes connected by pointers. The basic operations of binary trees under the linked list representation were introduced in the previous section.
So, can we use an array to represent a binary tree? The answer is yes.
"},{"location":"chapter_tree/array_representation_of_tree/#731-representing-perfect-binary-trees","title":"7.3.1 \u00a0 Representing perfect binary trees","text":"Let's analyze a simple case first. Given a perfect binary tree, we store all nodes in an array according to the order of level-order traversal, where each node corresponds to a unique array index.
Based on the characteristics of level-order traversal, we can deduce a \"mapping formula\" between the index of a parent node and its children: If a node's index is \\(i\\), then the index of its left child is \\(2i + 1\\) and the right child is \\(2i + 2\\). The Figure 7-12 shows the mapping relationship between the indices of various nodes.
Figure 7-12 \u00a0 Array representation of a perfect binary tree
The mapping formula plays a role similar to the node references (pointers) in linked lists. Given any node in the array, we can access its left (right) child node using the mapping formula.
"},{"location":"chapter_tree/array_representation_of_tree/#732-representing-any-binary-tree","title":"7.3.2 \u00a0 Representing any binary tree","text":"Perfect binary trees are a special case; there are often many None
values in the middle levels of a binary tree. Since the sequence of level-order traversal does not include these None
values, we cannot solely rely on this sequence to deduce the number and distribution of None
values. This means that multiple binary tree structures can match the same level-order traversal sequence.
As shown in the Figure 7-13 , given a non-perfect binary tree, the above method of array representation fails.
Figure 7-13 \u00a0 Level-order traversal sequence corresponds to multiple binary tree possibilities
To solve this problem, we can consider explicitly writing out all None
values in the level-order traversal sequence. As shown in the following figure, after this treatment, the level-order traversal sequence can uniquely represent a binary tree. Example code is as follows:
# Array representation of a binary tree\n# Using None to represent empty slots\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* Array representation of a binary tree */\n// Using the maximum integer value INT_MAX to mark empty slots\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* Array representation of a binary tree */\n// Using the Integer wrapper class allows for using null to mark empty slots\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using an any type slice, allowing for nil to mark empty slots\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* Array representation of a binary tree */\n// Using optional Int (Int?) allows for using nil to mark empty slots\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using nullable int (int?) allows for using null to mark empty slots\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* Array representation of a binary tree */\n// Using None to mark empty slots\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* Array representation of a binary tree */\n// Using the maximum int value to mark empty slots, therefore, node values must not be INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* Array representation of a binary tree */\n// Using null to represent empty slots\nval tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
\n
\n
Figure 7-14 \u00a0 Array representation of any type of binary tree
It's worth noting that complete binary trees are very suitable for array representation. Recalling the definition of a complete binary tree, None
appears only at the bottom level and towards the right, meaning all None
values definitely appear at the end of the level-order traversal sequence.
This means that when using an array to represent a complete binary tree, it's possible to omit storing all None
values, which is very convenient. The Figure 7-15 gives an example.
Figure 7-15 \u00a0 Array representation of a complete binary tree
The following code implements a binary tree based on array representation, including the following operations:
class ArrayBinaryTree:\n \"\"\"\u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b\"\"\"\n\n def __init__(self, arr: list[int | None]):\n \"\"\"\u6784\u9020\u65b9\u6cd5\"\"\"\n self._tree = list(arr)\n\n def size(self):\n \"\"\"\u5217\u8868\u5bb9\u91cf\"\"\"\n return len(self._tree)\n\n def val(self, i: int) -> int:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c\"\"\"\n # \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 or i >= self.size():\n return None\n return self._tree[i]\n\n def left(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 1\n\n def right(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return 2 * i + 2\n\n def parent(self, i: int) -> int | None:\n \"\"\"\u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15\"\"\"\n return (i - 1) // 2\n\n def level_order(self) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n self.res = []\n # \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in range(self.size()):\n if self.val(i) is not None:\n self.res.append(self.val(i))\n return self.res\n\n def dfs(self, i: int, order: str):\n \"\"\"\u6df1\u5ea6\u4f18\u5148\u904d\u5386\"\"\"\n if self.val(i) is None:\n return\n # \u524d\u5e8f\u904d\u5386\n if order == \"pre\":\n self.res.append(self.val(i))\n self.dfs(self.left(i), order)\n # \u4e2d\u5e8f\u904d\u5386\n if order == \"in\":\n self.res.append(self.val(i))\n self.dfs(self.right(i), order)\n # \u540e\u5e8f\u904d\u5386\n if order == \"post\":\n self.res.append(self.val(i))\n\n def pre_order(self) -> list[int]:\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"pre\")\n return self.res\n\n def in_order(self) -> list[int]:\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"in\")\n return self.res\n\n def post_order(self) -> list[int]:\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n self.res = []\n self.dfs(0, order=\"post\")\n return self.res\n
array_binary_tree.cpp/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n public:\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(vector<int> arr) {\n tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return INT_MAX;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n vector<int> levelOrder() {\n vector<int> res;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != INT_MAX)\n res.push_back(val(i));\n }\n return res;\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n vector<int> preOrder() {\n vector<int> res;\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n vector<int> inOrder() {\n vector<int> res;\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n vector<int> postOrder() {\n vector<int> res;\n dfs(0, \"post\", res);\n return res;\n }\n\n private:\n vector<int> tree;\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, string order, vector<int> &res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.push_back(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.push_back(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.push_back(val(i));\n }\n};\n
array_binary_tree.java/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private List<Integer> tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n public ArrayBinaryTree(List<Integer> arr) {\n tree = new ArrayList<>(arr);\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n public int size() {\n return tree.size();\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public Integer val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size())\n return null;\n return tree.get(i);\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public Integer parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<Integer> levelOrder() {\n List<Integer> res = new ArrayList<>();\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(); i++) {\n if (val(i) != null)\n res.add(val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private void dfs(Integer i, String order, List<Integer> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\".equals(order))\n res.add(val(i));\n dfs(left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\".equals(order))\n res.add(val(i));\n dfs(right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (\"post\".equals(order))\n res.add(val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<Integer> preOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<Integer> inOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<Integer> postOrder() {\n List<Integer> res = new ArrayList<>();\n dfs(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.cs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(List<int?> arr) {\n List<int?> tree = new(arr);\n\n /* \u5217\u8868\u5bb9\u91cf */\n public int Size() {\n return tree.Count;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n public int? Val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= Size())\n return null;\n return tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n public int Parent(int i) {\n return (i - 1) / 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n public List<int> LevelOrder() {\n List<int> res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < Size(); i++) {\n if (Val(i).HasValue)\n res.Add(Val(i)!.Value);\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void DFS(int i, string order, List<int> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (!Val(i).HasValue)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (order == \"pre\")\n res.Add(Val(i)!.Value);\n DFS(Left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == \"in\")\n res.Add(Val(i)!.Value);\n DFS(Right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == \"post\")\n res.Add(Val(i)!.Value);\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n public List<int> PreOrder() {\n List<int> res = [];\n DFS(0, \"pre\", res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n public List<int> InOrder() {\n List<int> res = [];\n DFS(0, \"in\", res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n public List<int> PostOrder() {\n List<int> res = [];\n DFS(0, \"post\", res);\n return res;\n }\n}\n
array_binary_tree.go/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\ntype arrayBinaryTree struct {\n tree []any\n}\n\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n return &arrayBinaryTree{\n tree: arr,\n }\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nfunc (abt *arrayBinaryTree) size() int {\n return len(abt.tree)\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nfunc (abt *arrayBinaryTree) val(i int) any {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= abt.size() {\n return nil\n }\n return abt.tree[i]\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) left(i int) int {\n return 2*i + 1\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) right(i int) int {\n return 2*i + 2\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n return (i - 1) / 2\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n var res []any\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i := 0; i < abt.size(); i++ {\n if abt.val(i) != nil {\n res = append(res, abt.val(i))\n }\n }\n return res\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if abt.val(i) == nil {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n *res = append(*res, abt.val(i))\n }\n abt.dfs(abt.right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n *res = append(*res, abt.val(i))\n }\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n var res []any\n abt.dfs(0, \"pre\", &res)\n return res\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n var res []any\n abt.dfs(0, \"in\", &res)\n return res\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n var res []any\n abt.dfs(0, \"post\", &res)\n return res\n}\n
array_binary_tree.swift/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n private var tree: [Int?]\n\n /* \u6784\u9020\u65b9\u6cd5 */\n init(arr: [Int?]) {\n tree = arr\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n func size() -> Int {\n tree.count\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n func val(i: Int) -> Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= size() {\n return nil\n }\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func left(i: Int) -> Int {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n func right(i: Int) -> Int {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n func parent(i: Int) -> Int {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n func levelOrder() -> [Int] {\n var res: [Int] = []\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0 ..< size() {\n if let val = val(i: i) {\n res.append(val)\n }\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n private func dfs(i: Int, order: String, res: inout [Int]) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n guard let val = val(i: i) else {\n return\n }\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.append(val)\n }\n dfs(i: left(i: i), order: order, res: &res)\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.append(val)\n }\n dfs(i: right(i: i), order: order, res: &res)\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.append(val)\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n func preOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"pre\", res: &res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n func inOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"in\", res: &res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n func postOrder() -> [Int] {\n var res: [Int] = []\n dfs(i: 0, order: \"post\", res: &res)\n return res\n }\n}\n
array_binary_tree.js/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr) {\n this.#tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n size() {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i) {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder() {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i, order, res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder() {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder() {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder() {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.ts/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n #tree: (number | null)[];\n\n /* \u6784\u9020\u65b9\u6cd5 */\n constructor(arr: (number | null)[]) {\n this.#tree = arr;\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n size(): number {\n return this.#tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n val(i: number): number | null {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= this.size()) return null;\n return this.#tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n left(i: number): number {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n right(i: number): number {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n parent(i: number): number {\n return Math.floor((i - 1) / 2); // \u5411\u4e0b\u6574\u9664\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n levelOrder(): number[] {\n let res = [];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (let i = 0; i < this.size(); i++) {\n if (this.val(i) !== null) res.push(this.val(i));\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n #dfs(i: number, order: Order, res: (number | null)[]): void {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (this.val(i) === null) return;\n // \u524d\u5e8f\u904d\u5386\n if (order === 'pre') res.push(this.val(i));\n this.#dfs(this.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order === 'in') res.push(this.val(i));\n this.#dfs(this.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order === 'post') res.push(this.val(i));\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n preOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n inOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n postOrder(): (number | null)[] {\n const res = [];\n this.#dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.dart/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree {\n late List<int?> _tree;\n\n /* \u6784\u9020\u65b9\u6cd5 */\n ArrayBinaryTree(this._tree);\n\n /* \u5217\u8868\u5bb9\u91cf */\n int size() {\n return _tree.length;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n int? val(int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) {\n return null;\n }\n return _tree[i];\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? left(int i) {\n return 2 * i + 1;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n int? right(int i) {\n return 2 * i + 2;\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n int? parent(int i) {\n return (i - 1) ~/ 2;\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n List<int> levelOrder() {\n List<int> res = [];\n for (int i = 0; i < size(); i++) {\n if (val(i) != null) {\n res.add(val(i)!);\n }\n }\n return res;\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n void dfs(int i, String order, List<int?> res) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(i) == null) {\n return;\n }\n // \u524d\u5e8f\u904d\u5386\n if (order == 'pre') {\n res.add(val(i));\n }\n dfs(left(i)!, order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if (order == 'in') {\n res.add(val(i));\n }\n dfs(right(i)!, order, res);\n // \u540e\u5e8f\u904d\u5386\n if (order == 'post') {\n res.add(val(i));\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n List<int?> preOrder() {\n List<int?> res = [];\n dfs(0, 'pre', res);\n return res;\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n List<int?> inOrder() {\n List<int?> res = [];\n dfs(0, 'in', res);\n return res;\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n List<int?> postOrder() {\n List<int?> res = [];\n dfs(0, 'post', res);\n return res;\n }\n}\n
array_binary_tree.rs/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nstruct ArrayBinaryTree {\n tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(arr: Vec<Option<i32>>) -> Self {\n Self { tree: arr }\n }\n\n /* \u5217\u8868\u5bb9\u91cf */\n fn size(&self) -> i32 {\n self.tree.len() as i32\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fn val(&self, i: i32) -> Option<i32> {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de None \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if i < 0 || i >= self.size() {\n None\n } else {\n self.tree[i as usize]\n }\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn left(&self, i: i32) -> i32 {\n 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fn right(&self, i: i32) -> i32 {\n 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fn parent(&self, i: i32) -> i32 {\n (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fn level_order(&self) -> Vec<i32> {\n let mut res = vec![];\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for i in 0..self.size() {\n if let Some(val) = self.val(i) {\n res.push(val)\n }\n }\n res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fn dfs(&self, i: i32, order: &str, res: &mut Vec<i32>) {\n if self.val(i).is_none() {\n return;\n }\n let val = self.val(i).unwrap();\n // \u524d\u5e8f\u904d\u5386\n if order == \"pre\" {\n res.push(val);\n }\n self.dfs(self.left(i), order, res);\n // \u4e2d\u5e8f\u904d\u5386\n if order == \"in\" {\n res.push(val);\n }\n self.dfs(self.right(i), order, res);\n // \u540e\u5e8f\u904d\u5386\n if order == \"post\" {\n res.push(val);\n }\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fn pre_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"pre\", &mut res);\n res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fn in_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"in\", &mut res);\n res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fn post_order(&self) -> Vec<i32> {\n let mut res = vec![];\n self.dfs(0, \"post\", &mut res);\n res\n }\n}\n
array_binary_tree.c/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7ed3\u6784\u4f53 */\ntypedef struct {\n int *tree;\n int size;\n} ArrayBinaryTree;\n\n/* \u6784\u9020\u51fd\u6570 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n abt->tree = malloc(sizeof(int) * arrSize);\n memcpy(abt->tree, arr, sizeof(int) * arrSize);\n abt->size = arrSize;\n return abt;\n}\n\n/* \u6790\u6784\u51fd\u6570 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n free(abt->tree);\n free(abt);\n}\n\n/* \u5217\u8868\u5bb9\u91cf */\nint size(ArrayBinaryTree *abt) {\n return abt->size;\n}\n\n/* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\nint val(ArrayBinaryTree *abt, int i) {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de INT_MAX \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size(abt))\n return INT_MAX;\n return abt->tree[i];\n}\n\n/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (int i = 0; i < size(abt); i++) {\n if (val(abt, i) != INT_MAX)\n res[index++] = val(abt, i);\n }\n *returnSize = index;\n return res;\n}\n\n/* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (val(abt, i) == INT_MAX)\n return;\n // \u524d\u5e8f\u904d\u5386\n if (strcmp(order, \"pre\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, left(i), order, res, index);\n // \u4e2d\u5e8f\u904d\u5386\n if (strcmp(order, \"in\") == 0)\n res[(*index)++] = val(abt, i);\n dfs(abt, right(i), order, res, index);\n // \u540e\u5e8f\u904d\u5386\n if (strcmp(order, \"post\") == 0)\n res[(*index)++] = val(abt, i);\n}\n\n/* \u524d\u5e8f\u904d\u5386 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"pre\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"in\", res, &index);\n *returnSize = index;\n return res;\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n int *res = (int *)malloc(sizeof(int) * size(abt));\n int index = 0;\n dfs(abt, 0, \"post\", res, &index);\n *returnSize = index;\n return res;\n}\n
array_binary_tree.kt/* \u6570\u7ec4\u8868\u793a\u4e0b\u7684\u4e8c\u53c9\u6811\u7c7b */\nclass ArrayBinaryTree(val tree: List<Int?>) {\n /* \u5217\u8868\u5bb9\u91cf */\n fun size(): Int {\n return tree.size\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u503c */\n fun value(i: Int): Int? {\n // \u82e5\u7d22\u5f15\u8d8a\u754c\uff0c\u5219\u8fd4\u56de null \uff0c\u4ee3\u8868\u7a7a\u4f4d\n if (i < 0 || i >= size()) return null\n return tree[i]\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u5de6\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fun left(i: Int): Int {\n return 2 * i + 1\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u53f3\u5b50\u8282\u70b9\u7684\u7d22\u5f15 */\n fun right(i: Int): Int {\n return 2 * i + 2\n }\n\n /* \u83b7\u53d6\u7d22\u5f15\u4e3a i \u8282\u70b9\u7684\u7236\u8282\u70b9\u7684\u7d22\u5f15 */\n fun parent(i: Int): Int {\n return (i - 1) / 2\n }\n\n /* \u5c42\u5e8f\u904d\u5386 */\n fun levelOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n // \u76f4\u63a5\u904d\u5386\u6570\u7ec4\n for (i in 0..<size()) {\n if (value(i) != null) res.add(value(i))\n }\n return res\n }\n\n /* \u6df1\u5ea6\u4f18\u5148\u904d\u5386 */\n fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n // \u82e5\u4e3a\u7a7a\u4f4d\uff0c\u5219\u8fd4\u56de\n if (value(i) == null) return\n // \u524d\u5e8f\u904d\u5386\n if (\"pre\" == order) res.add(value(i))\n dfs(left(i), order, res)\n // \u4e2d\u5e8f\u904d\u5386\n if (\"in\" == order) res.add(value(i))\n dfs(right(i), order, res)\n // \u540e\u5e8f\u904d\u5386\n if (\"post\" == order) res.add(value(i))\n }\n\n /* \u524d\u5e8f\u904d\u5386 */\n fun preOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"pre\", res)\n return res\n }\n\n /* \u4e2d\u5e8f\u904d\u5386 */\n fun inOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"in\", res)\n return res\n }\n\n /* \u540e\u5e8f\u904d\u5386 */\n fun postOrder(): List<Int?> {\n val res = ArrayList<Int?>()\n dfs(0, \"post\", res)\n return res\n }\n}\n
array_binary_tree.rb[class]{ArrayBinaryTree}-[func]{}\n
array_binary_tree.zig[class]{ArrayBinaryTree}-[func]{}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/array_representation_of_tree/#733-advantages-and-limitations","title":"7.3.3 \u00a0 Advantages and limitations","text":"The array representation of binary trees has the following advantages:
However, the array representation also has some limitations:
None
values in the binary tree, the proportion of node data contained in the array is low, leading to lower space utilization.In the \"Binary Search Tree\" section, we mentioned that after multiple insertions and removals, a binary search tree might degrade to a linked list. In such cases, the time complexity of all operations degrades from \\(O(\\log n)\\) to \\(O(n)\\).
As shown in the Figure 7-24 , after two node removal operations, this binary search tree will degrade into a linked list.
Figure 7-24 \u00a0 Degradation of an AVL tree after removing nodes
For example, in the perfect binary tree shown in the Figure 7-25 , after inserting two nodes, the tree will lean heavily to the left, and the time complexity of search operations will also degrade.
Figure 7-25 \u00a0 Degradation of an AVL tree after inserting nodes
In 1962, G. M. Adelson-Velsky and E. M. Landis proposed the \"AVL Tree\" in their paper \"An algorithm for the organization of information\". The paper detailed a series of operations to ensure that after continuously adding and removing nodes, the AVL tree would not degrade, thus maintaining the time complexity of various operations at \\(O(\\log n)\\) level. In other words, in scenarios where frequent additions, removals, searches, and modifications are needed, the AVL tree can always maintain efficient data operation performance, which has great application value.
"},{"location":"chapter_tree/avl_tree/#751-common-terminology-in-avl-trees","title":"7.5.1 \u00a0 Common terminology in AVL trees","text":"An AVL tree is both a binary search tree and a balanced binary tree, satisfying all properties of these two types of binary trees, hence it is a \"balanced binary search tree\".
"},{"location":"chapter_tree/avl_tree/#1-node-height","title":"1. \u00a0 Node height","text":"Since the operations related to AVL trees require obtaining node heights, we need to add a height
variable to the node class:
class TreeNode:\n \"\"\"AVL tree node\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.height: int = 0 # Node height\n self.left: TreeNode | None = None # Left child reference\n self.right: TreeNode | None = None # Right child reference\n
/* AVL tree node */\nstruct TreeNode {\n int val{}; // Node value\n int height = 0; // Node height\n TreeNode *left{}; // Left child\n TreeNode *right{}; // Right child\n TreeNode() = default;\n explicit TreeNode(int x) : val(x){}\n};\n
/* AVL tree node */\nclass TreeNode {\n public int val; // Node value\n public int height; // Node height\n public TreeNode left; // Left child\n public TreeNode right; // Right child\n public TreeNode(int x) { val = x; }\n}\n
/* AVL tree node */\nclass TreeNode(int? x) {\n public int? val = x; // Node value\n public int height; // Node height\n public TreeNode? left; // Left child reference\n public TreeNode? right; // Right child reference\n}\n
/* AVL tree node */\ntype TreeNode struct {\n Val int // Node value\n Height int // Node height\n Left *TreeNode // Left child reference\n Right *TreeNode // Right child reference\n}\n
/* AVL tree node */\nclass TreeNode {\n var val: Int // Node value\n var height: Int // Node height\n var left: TreeNode? // Left child\n var right: TreeNode? // Right child\n\n init(x: Int) {\n val = x\n height = 0\n }\n}\n
/* AVL tree node */\nclass TreeNode {\n val; // Node value\n height; // Node height\n left; // Left child pointer\n right; // Right child pointer\n constructor(val, left, right, height) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* AVL tree node */\nclass TreeNode {\n val: number; // Node value\n height: number; // Node height\n left: TreeNode | null; // Left child pointer\n right: TreeNode | null; // Right child pointer\n constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val;\n this.height = height === undefined ? 0 : height; \n this.left = left === undefined ? null : left; \n this.right = right === undefined ? null : right; \n }\n}\n
/* AVL tree node */\nclass TreeNode {\n int val; // Node value\n int height; // Node height\n TreeNode? left; // Left child\n TreeNode? right; // Right child\n TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* AVL tree node */\nstruct TreeNode {\n val: i32, // Node value\n height: i32, // Node height\n left: Option<Rc<RefCell<TreeNode>>>, // Left child\n right: Option<Rc<RefCell<TreeNode>>>, // Right child\n}\n\nimpl TreeNode {\n /* Constructor */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n height: 0,\n left: None,\n right: None\n }))\n }\n}\n
/* AVL tree node */\nTreeNode struct TreeNode {\n int val;\n int height;\n struct TreeNode *left;\n struct TreeNode *right;\n} TreeNode;\n\n/* Constructor */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
/* AVL tree node */\nclass TreeNode(val _val: Int) { // Node value\n val height: Int = 0 // Node height\n val left: TreeNode? = null // Left child\n val right: TreeNode? = null // Right child\n}\n
\n
\n
The \"node height\" refers to the distance from that node to its farthest leaf node, i.e., the number of \"edges\" passed. It is important to note that the height of a leaf node is \\(0\\), and the height of a null node is \\(-1\\). We will create two utility functions for getting and updating the height of a node:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef height(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node is not None:\n return node.height\n return -1\n\ndef update_height(self, node: TreeNode | None):\n \"\"\"\u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\"\"\"\n # \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
avl_tree.cpp/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == nullptr ? -1 : node->height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node->height = max(height(node->left), height(node->right)) + 1;\n}\n
avl_tree.java/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.cs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint Height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid UpdateHeight(TreeNode node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
avl_tree.go/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if node != nil {\n return node.Height\n }\n return -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n lh := t.height(node.Left)\n rh := t.height(node.Right)\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if lh > rh {\n node.Height = lh + 1\n } else {\n node.Height = rh + 1\n }\n}\n
avl_tree.swift/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfunc height(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n node?.height ?? -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfunc updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
avl_tree.js/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\n#updateHeight(node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.ts/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nheight(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node === null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nupdateHeight(node: TreeNode): void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.height =\n Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.dart/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node == null ? -1 : node.height;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode? node) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.rs/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n match node {\n Some(node) => node.borrow().height,\n None => -1,\n }\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfn update_height(node: OptionTreeNodeRc) {\n if let Some(node) = node {\n let left = node.borrow().left.clone();\n let right = node.borrow().right.clone();\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nint height(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n if (node != NULL) {\n return node->height;\n }\n return -1;\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nvoid updateHeight(TreeNode *node) {\n int lh = height(node->left);\n int rh = height(node->right);\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n if (lh > rh) {\n node->height = lh + 1;\n } else {\n node->height = rh + 1;\n }\n}\n
avl_tree.kt/* \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6 */\nfun height(node: TreeNode?): Int {\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return node?.height ?: -1\n}\n\n/* \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6 */\nfun updateHeight(node: TreeNode?) {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt()\n}\n
avl_tree.rb[class]{AVLTree}-[func]{height}\n\n[class]{AVLTree}-[func]{update_height}\n
avl_tree.zig// \u83b7\u53d6\u8282\u70b9\u9ad8\u5ea6\nfn height(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n _ = self;\n // \u7a7a\u8282\u70b9\u9ad8\u5ea6\u4e3a -1 \uff0c\u53f6\u8282\u70b9\u9ad8\u5ea6\u4e3a 0\n return if (node == null) -1 else node.?.height;\n}\n\n// \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\nfn updateHeight(self: *Self, node: ?*inc.TreeNode(T)) void {\n // \u8282\u70b9\u9ad8\u5ea6\u7b49\u4e8e\u6700\u9ad8\u5b50\u6811\u9ad8\u5ea6 + 1\n node.?.height = @max(self.height(node.?.left), self.height(node.?.right)) + 1;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-node-balance-factor","title":"2. \u00a0 Node balance factor","text":"The \"balance factor\" of a node is defined as the height of the node's left subtree minus the height of its right subtree, with the balance factor of a null node defined as \\(0\\). We will also encapsulate the functionality of obtaining the node balance factor into a function for easy use later on:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef balance_factor(self, node: TreeNode | None) -> int:\n \"\"\"\u83b7\u53d6\u5e73\u8861\u56e0\u5b50\"\"\"\n # \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node is None:\n return 0\n # \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.left) - self.height(node.right)\n
avl_tree.cpp/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == nullptr)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.java/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null)\n return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.cs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint BalanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return Height(node.left) - Height(node.right);\n}\n
avl_tree.go/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if node == nil {\n return 0\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return t.height(node.Left) - t.height(node.Right)\n}\n
avl_tree.swift/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n guard let node = node else { return 0 }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node: node.left) - height(node: node.right)\n}\n
avl_tree.js/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.ts/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nbalanceFactor(node: TreeNode): number {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node === null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.dart/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode? node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right);\n}\n
avl_tree.rs/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n match node {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n None => 0,\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n Some(node) => {\n Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n }\n }\n}\n
avl_tree.c/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nint balanceFactor(TreeNode *node) {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == NULL) {\n return 0;\n }\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node->left) - height(node->right);\n}\n
avl_tree.kt/* \u83b7\u53d6\u5e73\u8861\u56e0\u5b50 */\nfun balanceFactor(node: TreeNode?): Int {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return height(node.left) - height(node.right)\n}\n
avl_tree.rb[class]{AVLTree}-[func]{balance_factor}\n
avl_tree.zig// \u83b7\u53d6\u5e73\u8861\u56e0\u5b50\nfn balanceFactor(self: *Self, node: ?*inc.TreeNode(T)) i32 {\n // \u7a7a\u8282\u70b9\u5e73\u8861\u56e0\u5b50\u4e3a 0\n if (node == null) return 0;\n // \u8282\u70b9\u5e73\u8861\u56e0\u5b50 = \u5de6\u5b50\u6811\u9ad8\u5ea6 - \u53f3\u5b50\u6811\u9ad8\u5ea6\n return self.height(node.?.left) - self.height(node.?.right);\n}\n
Note
Let the balance factor be \\(f\\), then the balance factor of any node in an AVL tree satisfies \\(-1 \\le f \\le 1\\).
"},{"location":"chapter_tree/avl_tree/#752-rotations-in-avl-trees","title":"7.5.2 \u00a0 Rotations in AVL trees","text":"The characteristic feature of an AVL tree is the \"rotation\" operation, which can restore balance to an unbalanced node without affecting the in-order traversal sequence of the binary tree. In other words, the rotation operation can maintain the property of a \"binary search tree\" while also turning the tree back into a \"balanced binary tree\".
We call nodes with an absolute balance factor \\(> 1\\) \"unbalanced nodes\". Depending on the type of imbalance, there are four kinds of rotations: right rotation, left rotation, right-left rotation, and left-right rotation. Below, we detail these rotation operations.
"},{"location":"chapter_tree/avl_tree/#1-right-rotation","title":"1. \u00a0 Right rotation","text":"As shown in the Figure 7-26 , the first unbalanced node from the bottom up in the binary tree is \"node 3\". Focusing on the subtree with this unbalanced node as the root, denoted as node
, and its left child as child
, perform a \"right rotation\". After the right rotation, the subtree is balanced again while still maintaining the properties of a binary search tree.
Figure 7-26 \u00a0 Steps of right rotation
As shown in the Figure 7-27 , when the child
node has a right child (denoted as grand_child
), a step needs to be added in the right rotation: set grand_child
as the left child of node
.
Figure 7-27 \u00a0 Right rotation with grand_child
\"Right rotation\" is a figurative term; in practice, it is achieved by modifying node pointers, as shown in the following code:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u53f3\u65cb\u64cd\u4f5c\"\"\"\n child = node.left\n grand_child = child.right\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child = node->left;\n TreeNode *grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode rightRotate(TreeNode node) {\n TreeNode child = node.left;\n TreeNode grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? RightRotate(TreeNode? node) {\n TreeNode? child = node?.left;\n TreeNode? grandChild = child?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u53f3\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n child := node.Left\n grandChild := child.Right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.Right = node\n node.Left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u53f3\u65cb\u64cd\u4f5c */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.left\n let grandChild = child?.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child?.right = node\n node?.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u53f3\u65cb\u64cd\u4f5c */\n#rightRotate(node) {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u53f3\u65cb\u64cd\u4f5c */\nrightRotate(node: TreeNode): TreeNode {\n const child = node.left;\n const grandChild = child.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode? rightRotate(TreeNode? node) {\n TreeNode? child = node!.left;\n TreeNode? grandChild = child!.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node;\n node.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u53f3\u65cb\u64cd\u4f5c */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().left.clone().unwrap();\n let grand_child = child.borrow().right.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.borrow_mut().right = Some(node.clone());\n node.borrow_mut().left = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u53f3\u65cb\u64cd\u4f5c */\nTreeNode *rightRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->left;\n grandChild = child->right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child->right = node;\n node->left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.kt/* \u53f3\u65cb\u64cd\u4f5c */\nfun rightRotate(node: TreeNode?): TreeNode {\n val child = node!!.left\n val grandChild = child!!.right\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.right = node\n node.left = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.rb[class]{AVLTree}-[func]{right_rotate}\n
avl_tree.zig// \u53f3\u65cb\u64cd\u4f5c\nfn rightRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.left;\n var grandChild = child.?.right;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u53f3\u65cb\u8f6c\n child.?.right = node;\n node.?.left = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-left-rotation","title":"2. \u00a0 Left rotation","text":"Correspondingly, if considering the \"mirror\" of the above unbalanced binary tree, the \"left rotation\" operation shown in the Figure 7-28 needs to be performed.
Figure 7-28 \u00a0 Left rotation operation
Similarly, as shown in the Figure 7-29 , when the child
node has a left child (denoted as grand_child
), a step needs to be added in the left rotation: set grand_child
as the right child of node
.
Figure 7-29 \u00a0 Left rotation with grand_child
It can be observed that the right and left rotation operations are logically symmetrical, and they solve two symmetrical types of imbalance. Based on symmetry, by replacing all left
with right
, and all right
with left
in the implementation code of right rotation, we can get the implementation code for left rotation:
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u5de6\u65cb\u64cd\u4f5c\"\"\"\n child = node.right\n grand_child = child.left\n # \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grand_child\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n self.update_height(child)\n # \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n
avl_tree.cpp/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child = node->right;\n TreeNode *grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.java/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode leftRotate(TreeNode node) {\n TreeNode child = node.right;\n TreeNode grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.cs/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? LeftRotate(TreeNode? node) {\n TreeNode? child = node?.right;\n TreeNode? grandChild = child?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n UpdateHeight(node);\n UpdateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.go/* \u5de6\u65cb\u64cd\u4f5c */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n child := node.Right\n grandChild := child.Left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.Left = node\n node.Right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n t.updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.swift/* \u5de6\u65cb\u64cd\u4f5c */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n let child = node?.right\n let grandChild = child?.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child?.left = node\n node?.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node: node)\n updateHeight(node: child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.js/* \u5de6\u65cb\u64cd\u4f5c */\n#leftRotate(node) {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.#updateHeight(node);\n this.#updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.ts/* \u5de6\u65cb\u64cd\u4f5c */\nleftRotate(node: TreeNode): TreeNode {\n const child = node.right;\n const grandChild = child.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n this.updateHeight(node);\n this.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.dart/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode? leftRotate(TreeNode? node) {\n TreeNode? child = node!.right;\n TreeNode? grandChild = child!.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node;\n node.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.rs/* \u5de6\u65cb\u64cd\u4f5c */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n match node {\n Some(node) => {\n let child = node.borrow().right.clone().unwrap();\n let grand_child = child.borrow().left.clone();\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.borrow_mut().left = Some(node.clone());\n node.borrow_mut().right = grand_child;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n Self::update_height(Some(node));\n Self::update_height(Some(child.clone()));\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(child)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5de6\u65cb\u64cd\u4f5c */\nTreeNode *leftRotate(TreeNode *node) {\n TreeNode *child, *grandChild;\n child = node->right;\n grandChild = child->left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child->left = node;\n node->right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
avl_tree.kt/* \u5de6\u65cb\u64cd\u4f5c */\nfun leftRotate(node: TreeNode?): TreeNode {\n val child = node!!.right\n val grandChild = child!!.left\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.left = node\n node.right = grandChild\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node)\n updateHeight(child)\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child\n}\n
avl_tree.rb[class]{AVLTree}-[func]{left_rotate}\n
avl_tree.zig// \u5de6\u65cb\u64cd\u4f5c\nfn leftRotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n var child = node.?.right;\n var grandChild = child.?.left;\n // \u4ee5 child \u4e3a\u539f\u70b9\uff0c\u5c06 node \u5411\u5de6\u65cb\u8f6c\n child.?.left = node;\n node.?.right = grandChild;\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.updateHeight(node);\n self.updateHeight(child);\n // \u8fd4\u56de\u65cb\u8f6c\u540e\u5b50\u6811\u7684\u6839\u8282\u70b9\n return child;\n}\n
"},{"location":"chapter_tree/avl_tree/#3-right-left-rotation","title":"3. \u00a0 Right-left rotation","text":"For the unbalanced node 3 shown in the Figure 7-30 , using either left or right rotation alone cannot restore balance to the subtree. In this case, a \"left rotation\" needs to be performed on child
first, followed by a \"right rotation\" on node
.
Figure 7-30 \u00a0 Right-left rotation
"},{"location":"chapter_tree/avl_tree/#4-left-right-rotation","title":"4. \u00a0 Left-right rotation","text":"As shown in the Figure 7-31 , for the mirror case of the above unbalanced binary tree, a \"right rotation\" needs to be performed on child
first, followed by a \"left rotation\" on node
.
Figure 7-31 \u00a0 Left-right rotation
"},{"location":"chapter_tree/avl_tree/#5-choice-of-rotation","title":"5. \u00a0 Choice of rotation","text":"The four kinds of imbalances shown in the Figure 7-32 correspond to the cases described above, respectively requiring right rotation, left-right rotation, right-left rotation, and left rotation.
Figure 7-32 \u00a0 The four rotation cases of AVL tree
As shown in the Table 7-3 , we determine which of the above cases an unbalanced node belongs to by judging the sign of the balance factor of the unbalanced node and its higher-side child's balance factor.
Table 7-3 \u00a0 Conditions for Choosing Among the Four Rotation Cases
Balance factor of unbalanced node Balance factor of child node Rotation method to use \\(> 1\\) (Left-leaning tree) \\(\\geq 0\\) Right rotation \\(> 1\\) (Left-leaning tree) \\(<0\\) Left rotation then right rotation \\(< -1\\) (Right-leaning tree) \\(\\leq 0\\) Left rotation \\(< -1\\) (Right-leaning tree) \\(>0\\) Right rotation then left rotationFor convenience, we encapsulate the rotation operations into a function. With this function, we can perform rotations on various kinds of imbalances, restoring balance to unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef rotate(self, node: TreeNode | None) -> TreeNode | None:\n \"\"\"\u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\"\"\"\n # \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n balance_factor = self.balance_factor(node)\n # \u5de6\u504f\u6811\n if balance_factor > 1:\n if self.balance_factor(node.left) >= 0:\n # \u53f3\u65cb\n return self.right_rotate(node)\n else:\n # \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = self.left_rotate(node.left)\n return self.right_rotate(node)\n # \u53f3\u504f\u6811\n elif balance_factor < -1:\n if self.balance_factor(node.right) <= 0:\n # \u5de6\u65cb\n return self.left_rotate(node)\n else:\n # \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = self.right_rotate(node.right)\n return self.left_rotate(node)\n # \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n
avl_tree.cpp/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int _balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (_balanceFactor > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (_balanceFactor < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.java/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode rotate(TreeNode node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.cs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? Rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int balanceFactorInt = BalanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactorInt > 1) {\n if (BalanceFactor(node?.left) >= 0) {\n // \u53f3\u65cb\n return RightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node!.left = LeftRotate(node!.left);\n return RightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactorInt < -1) {\n if (BalanceFactor(node?.right) <= 0) {\n // \u5de6\u65cb\n return LeftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node!.right = RightRotate(node!.right);\n return LeftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.go/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n // Go \u63a8\u8350\u77ed\u53d8\u91cf\uff0c\u8fd9\u91cc bf \u6307\u4ee3 t.balanceFactor\n bf := t.balanceFactor(node)\n // \u5de6\u504f\u6811\n if bf > 1 {\n if t.balanceFactor(node.Left) >= 0 {\n // \u53f3\u65cb\n return t.rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.Left = t.leftRotate(node.Left)\n return t.rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if bf < -1 {\n if t.balanceFactor(node.Right) <= 0 {\n // \u5de6\u65cb\n return t.leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.Right = t.rightRotate(node.Right)\n return t.leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.swift/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balanceFactor = balanceFactor(node: node)\n // \u5de6\u504f\u6811\n if balanceFactor > 1 {\n if self.balanceFactor(node: node?.left) >= 0 {\n // \u53f3\u65cb\n return rightRotate(node: node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node?.left = leftRotate(node: node?.left)\n return rightRotate(node: node)\n }\n }\n // \u53f3\u504f\u6811\n if balanceFactor < -1 {\n if self.balanceFactor(node: node?.right) <= 0 {\n // \u5de6\u65cb\n return leftRotate(node: node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node?.right = rightRotate(node: node?.right)\n return leftRotate(node: node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.js/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n#rotate(node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.#rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.#leftRotate(node.left);\n return this.#rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.#leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.#rightRotate(node.right);\n return this.#leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.ts/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nrotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n const balanceFactor = this.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (this.balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return this.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = this.leftRotate(node.left);\n return this.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (this.balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return this.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = this.rightRotate(node.right);\n return this.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.dart/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode? rotate(TreeNode? node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int factor = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (factor > 1) {\n if (balanceFactor(node!.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (factor < -1) {\n if (balanceFactor(node!.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.rs/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n let balance_factor = Self::balance_factor(node.clone());\n // \u5de6\u504f\u6811\n if balance_factor > 1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n // \u53f3\u65cb\n Self::right_rotate(Some(node))\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::left_rotate(left);\n Self::right_rotate(Some(node))\n }\n }\n // \u53f3\u504f\u6811\n else if balance_factor < -1 {\n let node = node.unwrap();\n if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n // \u5de6\u65cb\n Self::left_rotate(Some(node))\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::right_rotate(right);\n Self::left_rotate(Some(node))\n }\n } else {\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n node\n }\n}\n
avl_tree.c/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nTreeNode *rotate(TreeNode *node) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n int bf = balanceFactor(node);\n // \u5de6\u504f\u6811\n if (bf > 1) {\n if (balanceFactor(node->left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node->left = leftRotate(node->left);\n return rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (bf < -1) {\n if (balanceFactor(node->right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node->right = rightRotate(node->right);\n return leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
avl_tree.kt/* \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\nfun rotate(node: TreeNode): TreeNode {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n val balanceFactor = balanceFactor(node)\n // \u5de6\u504f\u6811\n if (balanceFactor > 1) {\n if (balanceFactor(node.left) >= 0) {\n // \u53f3\u65cb\n return rightRotate(node)\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.left = leftRotate(node.left)\n return rightRotate(node)\n }\n }\n // \u53f3\u504f\u6811\n if (balanceFactor < -1) {\n if (balanceFactor(node.right) <= 0) {\n // \u5de6\u65cb\n return leftRotate(node)\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.right = rightRotate(node.right)\n return leftRotate(node)\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{rotate}\n
avl_tree.zig// \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\nfn rotate(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {\n // \u83b7\u53d6\u8282\u70b9 node \u7684\u5e73\u8861\u56e0\u5b50\n var balance_factor = self.balanceFactor(node);\n // \u5de6\u504f\u6811\n if (balance_factor > 1) {\n if (self.balanceFactor(node.?.left) >= 0) {\n // \u53f3\u65cb\n return self.rightRotate(node);\n } else {\n // \u5148\u5de6\u65cb\u540e\u53f3\u65cb\n node.?.left = self.leftRotate(node.?.left);\n return self.rightRotate(node);\n }\n }\n // \u53f3\u504f\u6811\n if (balance_factor < -1) {\n if (self.balanceFactor(node.?.right) <= 0) {\n // \u5de6\u65cb\n return self.leftRotate(node);\n } else {\n // \u5148\u53f3\u65cb\u540e\u5de6\u65cb\n node.?.right = self.rightRotate(node.?.right);\n return self.leftRotate(node);\n }\n }\n // \u5e73\u8861\u6811\uff0c\u65e0\u987b\u65cb\u8f6c\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#753-common-operations-in-avl-trees","title":"7.5.3 \u00a0 Common operations in AVL trees","text":""},{"location":"chapter_tree/avl_tree/#1-node-insertion","title":"1. \u00a0 Node insertion","text":"The node insertion operation in AVL trees is similar to that in binary search trees. The only difference is that after inserting a node in an AVL tree, a series of unbalanced nodes may appear along the path from that node to the root node. Therefore, we need to start from this node and perform rotation operations upwards to restore balance to all unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef insert(self, val):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n \"\"\"\u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return TreeNode(val)\n # 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n if val < node.val:\n node.left = self.insert_helper(node.left, val)\n elif val > node.val:\n node.right = self.insert_helper(node.right, val)\n else:\n # \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val)\n node->left = insertHelper(node->left, val);\n else if (val > node->val)\n node->right = insertHelper(node->right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode insertHelper(TreeNode node, int val) {\n if (node == null)\n return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int val) {\n root = InsertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n if (node == null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = InsertHelper(node.left, val);\n else if (val > node.val)\n node.right = InsertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (t *aVLTree) insert(val int) {\n t.root = t.insertHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return NewTreeNode(val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node.Val.(int) {\n node.Left = t.insertHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.insertHelper(node.Right, val)\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(val: Int) {\n root = insertHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return TreeNode(x: val)\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if val < node!.val {\n node?.left = insertHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = insertHelper(node: node?.right, val: val)\n } else {\n return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(val) {\n this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#insertHelper(node, val) {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) node.left = this.#insertHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#insertHelper(node.right, val);\n else return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(val: number): void {\n this.root = this.insertHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return new TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val) {\n node.left = this.insertHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.insertHelper(node.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int val) {\n root = insertHelper(root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? insertHelper(TreeNode? node, int val) {\n if (node == null) return TreeNode(val);\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node.val)\n node.left = insertHelper(node.left, val);\n else if (val > node.val)\n node.right = insertHelper(node.right, val);\n else\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u63d2\u5165\u8282\u70b9 */\nfn insert(&mut self, val: i32) {\n self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n match {\n let node_val = node.borrow().val;\n node_val\n }\n .cmp(&val)\n {\n Ordering::Greater => {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::insert_helper(left, val);\n }\n Ordering::Less => {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::insert_helper(right, val);\n }\n Ordering::Equal => {\n return Some(node); // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => Some(TreeNode::new(val)),\n }\n}\n
avl_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(AVLTree *tree, int val) {\n tree->root = insertHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *insertHelper(TreeNode *node, int val) {\n if (node == NULL) {\n return newTreeNode(val);\n }\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (val < node->val) {\n node->left = insertHelper(node->left, val);\n } else if (val > node->val) {\n node->right = insertHelper(node->right, val);\n } else {\n // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n return node;\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.kt/* \u63d2\u5165\u8282\u70b9 */\nfun insert(value: Int) {\n root = insertHelper(root, value)\n}\n\n/* \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun insertHelper(n: TreeNode?, value: Int): TreeNode {\n if (n == null)\n return TreeNode(value)\n var node = n\n /* 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9 */\n if (value < node.value) node.left = insertHelper(node.left, value)\n else if (value > node.value) node.right = insertHelper(node.right, value)\n else return node // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n\n updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{insert}\n\n[class]{AVLTree}-[func]{insert_helper}\n
avl_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, val: T) !void {\n self.root = (try self.insertHelper(self.root, val)).?;\n}\n\n// \u9012\u5f52\u63d2\u5165\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn insertHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) !?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) {\n var tmp_node = try self.mem_allocator.create(inc.TreeNode(T));\n tmp_node.init(val);\n return tmp_node;\n }\n // 1. \u67e5\u627e\u63d2\u5165\u4f4d\u7f6e\u5e76\u63d2\u5165\u8282\u70b9\n if (val < node.?.val) {\n node.?.left = try self.insertHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = try self.insertHelper(node.?.right, val);\n } else {\n return node; // \u91cd\u590d\u8282\u70b9\u4e0d\u63d2\u5165\uff0c\u76f4\u63a5\u8fd4\u56de\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#2-node-removal","title":"2. \u00a0 Node removal","text":"Similarly, based on the method of removing nodes in binary search trees, rotation operations need to be performed from the bottom up to restore balance to all unbalanced nodes. The code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig avl_tree.pydef remove(self, val: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n \"\"\"\u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\"\"\"\n if node is None:\n return None\n # 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n if val < node.val:\n node.left = self.remove_helper(node.left, val)\n elif val > node.val:\n node.right = self.remove_helper(node.right, val)\n else:\n if node.left is None or node.right is None:\n child = node.left or node.right\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child is None:\n return None\n # \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else:\n node = child\n else:\n # \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp = node.right\n while temp.left is not None:\n temp = temp.left\n node.right = self.remove_helper(node.right, temp.val)\n node.val = temp.val\n # \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n self.update_height(node)\n # 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n return self.rotate(node)\n
avl_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n if (node == nullptr)\n return nullptr;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node->val)\n node->left = removeHelper(node->left, val);\n else if (val > node->val)\n node->right = removeHelper(node->right, val);\n else {\n if (node->left == nullptr || node->right == nullptr) {\n TreeNode *child = node->left != nullptr ? node->left : node->right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == nullptr) {\n delete node;\n return nullptr;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n delete node;\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != nullptr) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode removeHelper(TreeNode node, int val) {\n if (node == null)\n return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode child = node.left != null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int val) {\n root = RemoveHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = RemoveHelper(node.left, val);\n else if (val > node.val)\n node.right = RemoveHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp.left != null) {\n temp = temp.left;\n }\n node.right = RemoveHelper(node.right, temp.val!.Value);\n node.val = temp.val;\n }\n }\n UpdateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (t *aVLTree) remove(val int) {\n t.root = t.removeHelper(t.root, val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node.Val.(int) {\n node.Left = t.removeHelper(node.Left, val)\n } else if val > node.Val.(int) {\n node.Right = t.removeHelper(node.Right, val)\n } else {\n if node.Left == nil || node.Right == nil {\n child := node.Left\n if node.Right != nil {\n child = node.Right\n }\n if child == nil {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n return nil\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n temp := node.Right\n for temp.Left != nil {\n temp = temp.Left\n }\n node.Right = t.removeHelper(node.Right, temp.Val.(int))\n node.Val = temp.Val\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n t.updateHeight(node)\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = t.rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(val: Int) {\n root = removeHelper(node: root, val: val)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n var node = node\n if node == nil {\n return nil\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node!.val {\n node?.left = removeHelper(node: node?.left, val: val)\n } else if val > node!.val {\n node?.right = removeHelper(node: node?.right, val: val)\n } else {\n if node?.left == nil || node?.right == nil {\n let child = node?.left ?? node?.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if child == nil {\n return nil\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else {\n node = child\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node?.right\n while temp?.left != nil {\n temp = temp?.left\n }\n node?.right = removeHelper(node: node?.right, val: temp!.val)\n node?.val = temp!.val\n }\n }\n updateHeight(node: node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node: node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(val) {\n this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\n#removeHelper(node, val) {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val) node.left = this.#removeHelper(node.left, val);\n else if (val > node.val)\n node.right = this.#removeHelper(node.right, val);\n else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.#removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.#updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.#rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(val: number): void {\n this.root = this.removeHelper(this.root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n if (node === null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val) {\n node.left = this.removeHelper(node.left, val);\n } else if (val > node.val) {\n node.right = this.removeHelper(node.right, val);\n } else {\n if (node.left === null || node.right === null) {\n const child = node.left !== null ? node.left : node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child === null) {\n return null;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let temp = node.right;\n while (temp.left !== null) {\n temp = temp.left;\n }\n node.right = this.removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n this.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = this.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int val) {\n root = removeHelper(root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nTreeNode? removeHelper(TreeNode? node, int val) {\n if (node == null) return null;\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node.val)\n node.left = removeHelper(node.left, val);\n else if (val > node.val)\n node.right = removeHelper(node.right, val);\n else {\n if (node.left == null || node.right == null) {\n TreeNode? child = node.left ?? node.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null)\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n else\n node = child;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode? temp = node.right;\n while (temp!.left != null) {\n temp = temp.left;\n }\n node.right = removeHelper(node.right, temp.val);\n node.val = temp.val;\n }\n }\n updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.rs/* \u5220\u9664\u8282\u70b9 */\nfn remove(&self, val: i32) {\n Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n match node {\n Some(mut node) => {\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if val < node.borrow().val {\n let left = node.borrow().left.clone();\n node.borrow_mut().left = Self::remove_helper(left, val);\n } else if val > node.borrow().val {\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, val);\n } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n let child = if node.borrow().left.is_some() {\n node.borrow().left.clone()\n } else {\n node.borrow().right.clone()\n };\n match child {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n None => {\n return None;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n Some(child) => node = child,\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n let mut temp = node.borrow().right.clone().unwrap();\n loop {\n let temp_left = temp.borrow().left.clone();\n if temp_left.is_none() {\n break;\n }\n temp = temp_left.unwrap();\n }\n let right = node.borrow().right.clone();\n node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n node.borrow_mut().val = temp.borrow().val;\n }\n Self::update_height(Some(node.clone())); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = Self::rotate(Some(node)).unwrap();\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n Some(node)\n }\n None => None,\n }\n}\n
avl_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(AVLTree *tree, int val) {\n TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u51fd\u6570\uff09 */\nTreeNode *removeHelper(TreeNode *node, int val) {\n TreeNode *child, *grandChild;\n if (node == NULL) {\n return NULL;\n }\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (val < node->val) {\n node->left = removeHelper(node->left, val);\n } else if (val > node->val) {\n node->right = removeHelper(node->right, val);\n } else {\n if (node->left == NULL || node->right == NULL) {\n child = node->left;\n if (node->right != NULL) {\n child = node->right;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == NULL) {\n return NULL;\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n TreeNode *temp = node->right;\n while (temp->left != NULL) {\n temp = temp->left;\n }\n int tempVal = temp->val;\n node->right = removeHelper(node->right, temp->val);\n node->val = tempVal;\n }\n }\n // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n updateHeight(node);\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
avl_tree.kt/* \u5220\u9664\u8282\u70b9 */\nfun remove(value: Int) {\n root = removeHelper(root, value)\n}\n\n/* \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09 */\nfun removeHelper(n: TreeNode?, value: Int): TreeNode? {\n var node = n ?: return null\n /* 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664 */\n if (value < node.value) node.left = removeHelper(node.left, value)\n else if (value > node.value) node.right = removeHelper(node.right, value)\n else {\n if (node.left == null || node.right == null) {\n val child = if (node.left != null) node.left else node.right\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) return null\n else node = child\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.right\n while (temp!!.left != null) {\n temp = temp.left\n }\n node.right = removeHelper(node.right, temp.value)\n node.value = temp.value\n }\n }\n updateHeight(node) // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n /* 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861 */\n node = rotate(node)\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node\n}\n
avl_tree.rb[class]{AVLTree}-[func]{remove}\n\n[class]{AVLTree}-[func]{remove_helper}\n
avl_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, val: T) void {\n self.root = self.removeHelper(self.root, val).?;\n}\n\n// \u9012\u5f52\u5220\u9664\u8282\u70b9\uff08\u8f85\u52a9\u65b9\u6cd5\uff09\nfn removeHelper(self: *Self, node_: ?*inc.TreeNode(T), val: T) ?*inc.TreeNode(T) {\n var node = node_;\n if (node == null) return null;\n // 1. \u67e5\u627e\u8282\u70b9\u5e76\u5220\u9664\n if (val < node.?.val) {\n node.?.left = self.removeHelper(node.?.left, val);\n } else if (val > node.?.val) {\n node.?.right = self.removeHelper(node.?.right, val);\n } else {\n if (node.?.left == null or node.?.right == null) {\n var child = if (node.?.left != null) node.?.left else node.?.right;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 \uff0c\u76f4\u63a5\u5220\u9664 node \u5e76\u8fd4\u56de\n if (child == null) {\n return null;\n // \u5b50\u8282\u70b9\u6570\u91cf = 1 \uff0c\u76f4\u63a5\u5220\u9664 node\n } else {\n node = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2 \uff0c\u5219\u5c06\u4e2d\u5e8f\u904d\u5386\u7684\u4e0b\u4e2a\u8282\u70b9\u5220\u9664\uff0c\u5e76\u7528\u8be5\u8282\u70b9\u66ff\u6362\u5f53\u524d\u8282\u70b9\n var temp = node.?.right;\n while (temp.?.left != null) {\n temp = temp.?.left;\n }\n node.?.right = self.removeHelper(node.?.right, temp.?.val);\n node.?.val = temp.?.val;\n }\n }\n self.updateHeight(node); // \u66f4\u65b0\u8282\u70b9\u9ad8\u5ea6\n // 2. \u6267\u884c\u65cb\u8f6c\u64cd\u4f5c\uff0c\u4f7f\u8be5\u5b50\u6811\u91cd\u65b0\u6062\u590d\u5e73\u8861\n node = self.rotate(node);\n // \u8fd4\u56de\u5b50\u6811\u7684\u6839\u8282\u70b9\n return node;\n}\n
"},{"location":"chapter_tree/avl_tree/#3-node-search","title":"3. \u00a0 Node search","text":"The node search operation in AVL trees is consistent with that in binary search trees and will not be detailed here.
"},{"location":"chapter_tree/avl_tree/#754-typical-applications-of-avl-trees","title":"7.5.4 \u00a0 Typical applications of AVL trees","text":"As shown in the Figure 7-16 , a \"binary search tree\" satisfies the following conditions.
1.
as well.Figure 7-16 \u00a0 Binary search tree
"},{"location":"chapter_tree/binary_search_tree/#741-operations-on-a-binary-search-tree","title":"7.4.1 \u00a0 Operations on a binary search tree","text":"We encapsulate the binary search tree as a class BinarySearchTree
and declare a member variable root
, pointing to the tree's root node.
Given a target node value num
, one can search according to the properties of the binary search tree. As shown in the Figure 7-17 , we declare a node cur
and start from the binary tree's root node root
, looping to compare the size relationship between the node value cur.val
and num
.
cur.val < num
, it means the target node is in cur
's right subtree, thus execute cur = cur.right
.cur.val > num
, it means the target node is in cur
's left subtree, thus execute cur = cur.left
.cur.val = num
, it means the target node is found, exit the loop and return the node.Figure 7-17 \u00a0 Example of searching for a node in a binary search tree
The search operation in a binary search tree works on the same principle as the binary search algorithm, eliminating half of the possibilities in each round. The number of loops is at most the height of the binary tree. When the binary tree is balanced, it uses \\(O(\\log n)\\) time. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.pydef search(self, num: int) -> TreeNode | None:\n \"\"\"\u67e5\u627e\u8282\u70b9\"\"\"\n cur = self._root\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur is not None:\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n elif cur.val > num:\n cur = cur.left\n # \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else:\n break\n return cur\n
binary_search_tree.cpp/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(int num) {\n TreeNode *cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur->val > num)\n cur = cur->left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.java/* \u67e5\u627e\u8282\u70b9 */\nTreeNode search(int num) {\n TreeNode cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.cs/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? Search(int num) {\n TreeNode? cur = root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur =\n cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.go/* \u67e5\u627e\u8282\u70b9 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n node := bst.root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for node != nil {\n if node.Val.(int) < num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n node = node.Right\n } else if node.Val.(int) > num {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n node = node.Left\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return node\n}\n
binary_search_tree.swift/* \u67e5\u627e\u8282\u70b9 */\nfunc search(num: Int) -> TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if cur!.val > num {\n cur = cur?.left\n }\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else {\n break\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.js/* \u67e5\u627e\u8282\u70b9 */\nsearch(num) {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.ts/* \u67e5\u627e\u8282\u70b9 */\nsearch(num: number): TreeNode | null {\n let cur = this.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > num) cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.dart/* \u67e5\u627e\u8282\u70b9 */\nTreeNode? search(int _num) {\n TreeNode? cur = _root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.val > _num)\n cur = cur.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else\n break;\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.rs/* \u67e5\u627e\u8282\u70b9 */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n let mut cur = self.root.clone();\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => cur = node.borrow().right.clone(),\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => cur = node.borrow().left.clone(),\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n Ordering::Equal => break,\n }\n }\n\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n cur\n}\n
binary_search_tree.c/* \u67e5\u627e\u8282\u70b9 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n TreeNode *cur = bst->root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n if (cur->val < num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else if (cur->val > num) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n } else {\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
binary_search_tree.kt/* \u67e5\u627e\u8282\u70b9 */\nfun search(num: Int): TreeNode? {\n var cur = root\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else if (cur.value > num) cur.left\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n else break\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{search}\n
binary_search_tree.zig// \u67e5\u627e\u8282\u70b9\nfn search(self: *Self, num: T) ?*inc.TreeNode(T) {\n var cur = self.root;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u76ee\u6807\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else if (cur.?.val > num) {\n cur = cur.?.left;\n // \u627e\u5230\u76ee\u6807\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n } else {\n break;\n }\n }\n // \u8fd4\u56de\u76ee\u6807\u8282\u70b9\n return cur;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_search_tree/#2-inserting-a-node","title":"2. \u00a0 Inserting a node","text":"Given an element num
to be inserted, to maintain the property of the binary search tree \"left subtree < root node < right subtree,\" the insertion operation proceeds as shown in the Figure 7-18 .
num
until passing through the leaf node (traversing to None
) then exit the loop.num
and place it where None
was.Figure 7-18 \u00a0 Inserting a node into a binary search tree
In the code implementation, note the following two points.
pre
to save the node from the last loop. This way, when traversing to None
, we can get its parent node, thus completing the node insertion operation.def insert(self, num: int):\n \"\"\"\u63d2\u5165\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self._root is None:\n self._root = TreeNode(num)\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur.val == num:\n return\n pre = cur\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u63d2\u5165\u8282\u70b9\n node = TreeNode(num)\n if pre.val < num:\n pre.right = node\n else:\n pre.left = node\n
binary_search_tree.cpp/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == nullptr) {\n root = new TreeNode(num);\n return;\n }\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = new TreeNode(num);\n if (pre->val < num)\n pre->right = node;\n else\n pre->left = node;\n}\n
binary_search_tree.java/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new TreeNode(num);\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.cs/* \u63d2\u5165\u8282\u70b9 */\nvoid Insert(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = new TreeNode(num);\n return;\n }\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == num)\n return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n\n // \u63d2\u5165\u8282\u70b9\n TreeNode node = new(num);\n if (pre != null) {\n if (pre.val < num)\n pre.right = node;\n else\n pre.left = node;\n }\n}\n
binary_search_tree.go/* \u63d2\u5165\u8282\u70b9 */\nfunc (bst *binarySearchTree) insert(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if cur == nil {\n bst.root = NewTreeNode(num)\n return\n }\n // \u5f85\u63d2\u5165\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n return\n }\n pre = cur\n if cur.Val.(int) < num {\n cur = cur.Right\n } else {\n cur = cur.Left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n node := NewTreeNode(num)\n if pre.Val.(int) < num {\n pre.Right = node\n } else {\n pre.Left = node\n }\n}\n
binary_search_tree.swift/* \u63d2\u5165\u8282\u70b9 */\nfunc insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if root == nil {\n root = TreeNode(x: num)\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if cur!.val == num {\n return\n }\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let node = TreeNode(x: num)\n if pre!.val < num {\n pre?.right = node\n } else {\n pre?.left = node\n }\n}\n
binary_search_tree.js/* \u63d2\u5165\u8282\u70b9 */\ninsert(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre.val < num) pre.right = node;\n else pre.left = node;\n}\n
binary_search_tree.ts/* \u63d2\u5165\u8282\u70b9 */\ninsert(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (this.root === null) {\n this.root = new TreeNode(num);\n return;\n }\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val === num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n const node = new TreeNode(num);\n if (pre!.val < num) pre!.right = node;\n else pre!.left = node;\n}\n
binary_search_tree.dart/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (_root == null) {\n _root = TreeNode(_num);\n return;\n }\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.val == _num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode? node = TreeNode(_num);\n if (pre!.val < _num)\n pre.right = node;\n else\n pre.left = node;\n}\n
binary_search_tree.rs/* \u63d2\u5165\u8282\u70b9 */\npub fn insert(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if self.root.is_none() {\n self.root = Some(TreeNode::new(num));\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n Ordering::Equal => return,\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u63d2\u5165\u8282\u70b9\n let pre = pre.unwrap();\n let node = Some(TreeNode::new(num));\n if num > pre.borrow().val {\n pre.borrow_mut().right = node;\n } else {\n pre.borrow_mut().left = node;\n }\n}\n
binary_search_tree.c/* \u63d2\u5165\u8282\u70b9 */\nvoid insert(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (bst->root == NULL) {\n bst->root = newTreeNode(num);\n return;\n }\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur->val == num) {\n return;\n }\n pre = cur;\n if (cur->val < num) {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n TreeNode *node = newTreeNode(num);\n if (pre->val < num) {\n pre->right = node;\n } else {\n pre->left = node;\n }\n}\n
binary_search_tree.kt/* \u63d2\u5165\u8282\u70b9 */\nfun insert(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (root == null) {\n root = TreeNode(num)\n return\n }\n var cur = root\n var pre: TreeNode? = null\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.value == num) return\n pre = cur\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur.left\n }\n // \u63d2\u5165\u8282\u70b9\n val node = TreeNode(num)\n if (pre?.value!! < num) pre.right = node\n else pre.left = node\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{insert}\n
binary_search_tree.zig// \u63d2\u5165\u8282\u70b9\nfn insert(self: *Self, num: T) !void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u5219\u521d\u59cb\u5316\u6839\u8282\u70b9\n if (self.root == null) {\n self.root = try self.mem_allocator.create(inc.TreeNode(T));\n return;\n }\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u91cd\u590d\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur.?.val == num) return;\n pre = cur;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u63d2\u5165\u4f4d\u7f6e\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u63d2\u5165\u8282\u70b9\n var node = try self.mem_allocator.create(inc.TreeNode(T));\n node.init(num);\n if (pre.?.val < num) {\n pre.?.right = node;\n } else {\n pre.?.left = node;\n }\n}\n
Code Visualization Full Screen >
Similar to searching for a node, inserting a node uses \\(O(\\log n)\\) time.
"},{"location":"chapter_tree/binary_search_tree/#3-removing-a-node","title":"3. \u00a0 Removing a node","text":"First, find the target node in the binary tree, then remove it. Similar to inserting a node, we need to ensure that after the removal operation is completed, the property of the binary search tree \"left subtree < root node < right subtree\" is still satisfied. Therefore, based on the number of child nodes of the target node, we divide it into 0, 1, and 2 cases, performing the corresponding node removal operations.
As shown in the Figure 7-19 , when the degree of the node to be removed is \\(0\\), it means the node is a leaf node, and it can be directly removed.
Figure 7-19 \u00a0 Removing a node in a binary search tree (degree 0)
As shown in the Figure 7-20 , when the degree of the node to be removed is \\(1\\), replacing the node to be removed with its child node is sufficient.
Figure 7-20 \u00a0 Removing a node in a binary search tree (degree 1)
When the degree of the node to be removed is \\(2\\), we cannot remove it directly, but need to use a node to replace it. To maintain the property of the binary search tree \"left subtree < root node < right subtree,\" this node can be either the smallest node of the right subtree or the largest node of the left subtree.
Assuming we choose the smallest node of the right subtree (the next node in in-order traversal), then the removal operation proceeds as shown in the Figure 7-21 .
tmp
.tmp
's value, and recursively remove the node tmp
in the tree.Figure 7-21 \u00a0 Removing a node in a binary search tree (degree 2)
The operation of removing a node also uses \\(O(\\log n)\\) time, where finding the node to be removed requires \\(O(\\log n)\\) time, and obtaining the in-order traversal successor node requires \\(O(\\log n)\\) time. Example code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_search_tree.pydef remove(self, num: int):\n \"\"\"\u5220\u9664\u8282\u70b9\"\"\"\n # \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self._root is None:\n return\n # \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n cur, pre = self._root, None\n while cur is not None:\n # \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur.val == num:\n break\n pre = cur\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur.val < num:\n cur = cur.right\n # \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else:\n cur = cur.left\n # \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur is None:\n return\n\n # \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur.left is None or cur.right is None:\n # \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n child = cur.left or cur.right\n # \u5220\u9664\u8282\u70b9 cur\n if cur != self._root:\n if pre.left == cur:\n pre.left = child\n else:\n pre.right = child\n else:\n # \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self._root = child\n # \u5b50\u8282\u70b9\u6570\u91cf = 2\n else:\n # \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp: TreeNode = cur.right\n while tmp.left is not None:\n tmp = tmp.left\n # \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.val)\n # \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val\n
binary_search_tree.cpp/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == nullptr)\n return;\n TreeNode *cur = root, *pre = nullptr;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != nullptr) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur->val < num)\n cur = cur->right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur->left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == nullptr)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur->left == nullptr || cur->right == nullptr) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre->left == cur)\n pre->left = child;\n else\n pre->right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n // \u91ca\u653e\u5185\u5b58\n delete cur;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != nullptr) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.java/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode child = cur.left != null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.cs/* \u5220\u9664\u8282\u70b9 */\nvoid Remove(int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null)\n return;\n TreeNode? cur = root, pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == num)\n break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null)\n return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n Remove(tmp.val!.Value);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.go/* \u5220\u9664\u8282\u70b9 */\nfunc (bst *binarySearchTree) remove(num int) {\n cur := bst.root\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u4e4b\u524d\u7684\u8282\u70b9\u4f4d\u7f6e\n var pre *TreeNode = nil\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n for cur != nil {\n if cur.Val == num {\n break\n }\n pre = cur\n if cur.Val.(int) < num {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u53f3\u5b50\u6811\u4e2d\n cur = cur.Right\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728\u5de6\u5b50\u6811\u4e2d\n cur = cur.Left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 0 \u6216 1\n if cur.Left == nil || cur.Right == nil {\n var child *TreeNode = nil\n // \u53d6\u51fa\u5f85\u5220\u9664\u8282\u70b9\u7684\u5b50\u8282\u70b9\n if cur.Left != nil {\n child = cur.Left\n } else {\n child = cur.Right\n }\n // \u5220\u9664\u8282\u70b9 cur\n if cur != bst.root {\n if pre.Left == cur {\n pre.Left = child\n } else {\n pre.Right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n bst.root = child\n }\n // \u5b50\u8282\u70b9\u6570\u4e3a 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d\u5f85\u5220\u9664\u8282\u70b9 cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n tmp := cur.Right\n for tmp.Left != nil {\n tmp = tmp.Left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n bst.remove(tmp.Val.(int))\n // \u7528 tmp \u8986\u76d6 cur\n cur.Val = tmp.Val\n }\n}\n
binary_search_tree.swift/* \u5220\u9664\u8282\u70b9 */\nfunc remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if root == nil {\n return\n }\n var cur = root\n var pre: TreeNode?\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while cur != nil {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if cur!.val == num {\n break\n }\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if cur!.val < num {\n cur = cur?.right\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else {\n cur = cur?.left\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur == nil {\n return\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if cur?.left == nil || cur?.right == nil {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n let child = cur?.left ?? cur?.right\n // \u5220\u9664\u8282\u70b9 cur\n if cur !== root {\n if pre?.left === cur {\n pre?.left = child\n } else {\n pre?.right = child\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur?.right\n while tmp?.left != nil {\n tmp = tmp?.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(num: tmp!.val)\n // \u7528 tmp \u8986\u76d6 cur\n cur?.val = tmp!.val\n }\n}\n
binary_search_tree.js/* \u5220\u9664\u8282\u70b9 */\nremove(num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur = this.root,\n pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child = cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre.left === cur) pre.left = child;\n else pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp = cur.right;\n while (tmp.left !== null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.ts/* \u5220\u9664\u8282\u70b9 */\nremove(num: number): void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (this.root === null) return;\n let cur: TreeNode | null = this.root,\n pre: TreeNode | null = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur !== null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val === num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < num) cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur === null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left === null || cur.right === null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n const child: TreeNode | null =\n cur.left !== null ? cur.left : cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur !== this.root) {\n if (pre!.left === cur) pre!.left = child;\n else pre!.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n this.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let tmp: TreeNode | null = cur.right;\n while (tmp!.left !== null) {\n tmp = tmp!.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n this.remove(tmp!.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp!.val;\n }\n}\n
binary_search_tree.dart/* \u5220\u9664\u8282\u70b9 */\nvoid remove(int _num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (_root == null) return;\n TreeNode? cur = _root;\n TreeNode? pre = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.val == _num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.val < _num)\n cur = cur.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else\n cur = cur.left;\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n TreeNode? child = cur.left ?? cur.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != _root) {\n if (pre!.left == cur)\n pre.left = child;\n else\n pre.right = child;\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n _root = child;\n }\n } else {\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode? tmp = cur.right;\n while (tmp!.left != null) {\n tmp = tmp.left;\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.val = tmp.val;\n }\n}\n
binary_search_tree.rs/* \u5220\u9664\u8282\u70b9 */\npub fn remove(&mut self, num: i32) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if self.root.is_none() {\n return;\n }\n let mut cur = self.root.clone();\n let mut pre = None;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while let Some(node) = cur.clone() {\n match num.cmp(&node.borrow().val) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n Ordering::Equal => break,\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n Ordering::Greater => {\n pre = cur.clone();\n cur = node.borrow().right.clone();\n }\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n Ordering::Less => {\n pre = cur.clone();\n cur = node.borrow().left.clone();\n }\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if cur.is_none() {\n return;\n }\n let cur = cur.unwrap();\n let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n match (left_child.clone(), right_child.clone()) {\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n (None, None) | (Some(_), None) | (None, Some(_)) => {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n let child = left_child.or(right_child);\n let pre = pre.unwrap();\n // \u5220\u9664\u8282\u70b9 cur\n if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n let left = pre.borrow().left.clone();\n if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {\n pre.borrow_mut().left = child;\n } else {\n pre.borrow_mut().right = child;\n }\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n self.root = child;\n }\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n (Some(_), Some(_)) => {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n let mut tmp = cur.borrow().right.clone();\n while let Some(node) = tmp.clone() {\n if node.borrow().left.is_some() {\n tmp = node.borrow().left.clone();\n } else {\n break;\n }\n }\n let tmpval = tmp.unwrap().borrow().val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmpval);\n // \u7528 tmp \u8986\u76d6 cur\n cur.borrow_mut().val = tmpval;\n }\n }\n}\n
binary_search_tree.c/* \u5220\u9664\u8282\u70b9 */\n// \u7531\u4e8e\u5f15\u5165\u4e86 stdio.h \uff0c\u6b64\u5904\u65e0\u6cd5\u4f7f\u7528 remove \u5173\u952e\u8bcd\nvoid removeItem(BinarySearchTree *bst, int num) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (bst->root == NULL)\n return;\n TreeNode *cur = bst->root, *pre = NULL;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != NULL) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur->val == num)\n break;\n pre = cur;\n if (cur->val < num) {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u53f3\u5b50\u6811\u4e2d\n cur = cur->right;\n } else {\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 root \u7684\u5de6\u5b50\u6811\u4e2d\n cur = cur->left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == NULL)\n return;\n // \u5224\u65ad\u5f85\u5220\u9664\u8282\u70b9\u662f\u5426\u5b58\u5728\u5b50\u8282\u70b9\n if (cur->left == NULL || cur->right == NULL) {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1 */\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = nullptr / \u8be5\u5b50\u8282\u70b9\n TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre->left == cur) {\n pre->left = child;\n } else {\n pre->right = child;\n }\n // \u91ca\u653e\u5185\u5b58\n free(cur);\n } else {\n /* \u5b50\u8282\u70b9\u6570\u91cf = 2 */\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n TreeNode *tmp = cur->right;\n while (tmp->left != NULL) {\n tmp = tmp->left;\n }\n int tmpVal = tmp->val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n removeItem(bst, tmp->val);\n // \u7528 tmp \u8986\u76d6 cur\n cur->val = tmpVal;\n }\n}\n
binary_search_tree.kt/* \u5220\u9664\u8282\u70b9 */\nfun remove(num: Int) {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (root == null) return\n var cur = root\n var pre: TreeNode? = null\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.value == num) break\n pre = cur\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n cur = if (cur.value < num) cur.right\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n else cur.left\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.left == null || cur.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n val child = if (cur.left != null) cur.left else cur.right\n // \u5220\u9664\u8282\u70b9 cur\n if (cur != root) {\n if (pre!!.left == cur) pre.left = child\n else pre.right = child\n } else {\n // \u82e5\u5220\u9664\u8282\u70b9\u4e3a\u6839\u8282\u70b9\uff0c\u5219\u91cd\u65b0\u6307\u5b9a\u6839\u8282\u70b9\n root = child\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.right\n while (tmp!!.left != null) {\n tmp = tmp.left\n }\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n remove(tmp.value)\n // \u7528 tmp \u8986\u76d6 cur\n cur.value = tmp.value\n }\n}\n
binary_search_tree.rb[class]{BinarySearchTree}-[func]{remove}\n
binary_search_tree.zig// \u5220\u9664\u8282\u70b9\nfn remove(self: *Self, num: T) void {\n // \u82e5\u6811\u4e3a\u7a7a\uff0c\u76f4\u63a5\u63d0\u524d\u8fd4\u56de\n if (self.root == null) return;\n var cur = self.root;\n var pre: ?*inc.TreeNode(T) = null;\n // \u5faa\u73af\u67e5\u627e\uff0c\u8d8a\u8fc7\u53f6\u8282\u70b9\u540e\u8df3\u51fa\n while (cur != null) {\n // \u627e\u5230\u5f85\u5220\u9664\u8282\u70b9\uff0c\u8df3\u51fa\u5faa\u73af\n if (cur.?.val == num) break;\n pre = cur;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u53f3\u5b50\u6811\u4e2d\n if (cur.?.val < num) {\n cur = cur.?.right;\n // \u5f85\u5220\u9664\u8282\u70b9\u5728 cur \u7684\u5de6\u5b50\u6811\u4e2d\n } else {\n cur = cur.?.left;\n }\n }\n // \u82e5\u65e0\u5f85\u5220\u9664\u8282\u70b9\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\n if (cur == null) return;\n // \u5b50\u8282\u70b9\u6570\u91cf = 0 or 1\n if (cur.?.left == null or cur.?.right == null) {\n // \u5f53\u5b50\u8282\u70b9\u6570\u91cf = 0 / 1 \u65f6\uff0c child = null / \u8be5\u5b50\u8282\u70b9\n var child = if (cur.?.left != null) cur.?.left else cur.?.right;\n // \u5220\u9664\u8282\u70b9 cur\n if (pre.?.left == cur) {\n pre.?.left = child;\n } else {\n pre.?.right = child;\n }\n // \u5b50\u8282\u70b9\u6570\u91cf = 2\n } else {\n // \u83b7\u53d6\u4e2d\u5e8f\u904d\u5386\u4e2d cur \u7684\u4e0b\u4e00\u4e2a\u8282\u70b9\n var tmp = cur.?.right;\n while (tmp.?.left != null) {\n tmp = tmp.?.left;\n }\n var tmp_val = tmp.?.val;\n // \u9012\u5f52\u5220\u9664\u8282\u70b9 tmp\n self.remove(tmp.?.val);\n // \u7528 tmp \u8986\u76d6 cur\n cur.?.val = tmp_val;\n }\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_search_tree/#4-in-order-traversal-is-ordered","title":"4. \u00a0 In-order traversal is ordered","text":"As shown in the Figure 7-22 , the in-order traversal of a binary tree follows the \"left \\(\\rightarrow\\) root \\(\\rightarrow\\) right\" traversal order, and a binary search tree satisfies the size relationship \"left child node < root node < right child node\".
This means that in-order traversal in a binary search tree always traverses the next smallest node first, thus deriving an important property: The in-order traversal sequence of a binary search tree is ascending.
Using the ascending property of in-order traversal, obtaining ordered data in a binary search tree requires only \\(O(n)\\) time, without the need for additional sorting operations, which is very efficient.
Figure 7-22 \u00a0 In-order traversal sequence of a binary search tree
"},{"location":"chapter_tree/binary_search_tree/#742-efficiency-of-binary-search-trees","title":"7.4.2 \u00a0 Efficiency of binary search trees","text":"Given a set of data, we consider using an array or a binary search tree for storage. Observing the Table 7-2 , the operations on a binary search tree all have logarithmic time complexity, which is stable and efficient. Only in scenarios of high-frequency addition and low-frequency search and removal, arrays are more efficient than binary search trees.
Table 7-2 \u00a0 Efficiency comparison between arrays and search trees
Unsorted array Binary search tree Search element \\(O(n)\\) \\(O(\\log n)\\) Insert element \\(O(1)\\) \\(O(\\log n)\\) Remove element \\(O(n)\\) \\(O(\\log n)\\)In ideal conditions, the binary search tree is \"balanced,\" thus any node can be found within \\(\\log n\\) loops.
However, continuously inserting and removing nodes in a binary search tree may lead to the binary tree degenerating into a chain list as shown in the Figure 7-23 , at which point the time complexity of various operations also degrades to \\(O(n)\\).
Figure 7-23 \u00a0 Degradation of a binary search tree
"},{"location":"chapter_tree/binary_search_tree/#743-common-applications-of-binary-search-trees","title":"7.4.3 \u00a0 Common applications of binary search trees","text":"A \"binary tree\" is a non-linear data structure that represents the ancestral and descendent relationships, embodying the \"divide and conquer\" logic. Similar to a linked list, the basic unit of a binary tree is a node, each containing a value, a reference to the left child node, and a reference to the right child node.
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZigclass TreeNode:\n \"\"\"Binary tree node\"\"\"\n def __init__(self, val: int):\n self.val: int = val # Node value\n self.left: TreeNode | None = None # Reference to left child node\n self.right: TreeNode | None = None # Reference to right child node\n
/* Binary tree node */\nstruct TreeNode {\n int val; // Node value\n TreeNode *left; // Pointer to left child node\n TreeNode *right; // Pointer to right child node\n TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
/* Binary tree node */\nclass TreeNode {\n int val; // Node value\n TreeNode left; // Reference to left child node\n TreeNode right; // Reference to right child node\n TreeNode(int x) { val = x; }\n}\n
/* Binary tree node */\nclass TreeNode(int? x) {\n public int? val = x; // Node value\n public TreeNode? left; // Reference to left child node\n public TreeNode? right; // Reference to right child node\n}\n
/* Binary tree node */\ntype TreeNode struct {\n Val int\n Left *TreeNode\n Right *TreeNode\n}\n/* \u6784\u9020\u65b9\u6cd5 */\nfunc NewTreeNode(v int) *TreeNode {\n return &TreeNode{\n Left: nil, // Pointer to left child node\n Right: nil, // Pointer to right child node\n Val: v, // Node value\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n var val: Int // Node value\n var left: TreeNode? // Reference to left child node\n var right: TreeNode? // Reference to right child node\n\n init(x: Int) {\n val = x\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n val; // Node value\n left; // Pointer to left child node\n right; // Pointer to right child node\n constructor(val, left, right) {\n this.val = val === undefined ? 0 : val;\n this.left = left === undefined ? null : left;\n this.right = right === undefined ? null : right;\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n val: number;\n left: TreeNode | null;\n right: TreeNode | null;\n\n constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n this.val = val === undefined ? 0 : val; // Node value\n this.left = left === undefined ? null : left; // Reference to left child node\n this.right = right === undefined ? null : right; // Reference to right child node\n }\n}\n
/* Binary tree node */\nclass TreeNode {\n int val; // Node value\n TreeNode? left; // Reference to left child node\n TreeNode? right; // Reference to right child node\n TreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* Binary tree node */\nstruct TreeNode {\n val: i32, // Node value\n left: Option<Rc<RefCell<TreeNode>>>, // Reference to left child node\n right: Option<Rc<RefCell<TreeNode>>>, // Reference to right child node\n}\n\nimpl TreeNode {\n /* \u6784\u9020\u65b9\u6cd5 */\n fn new(val: i32) -> Rc<RefCell<Self>> {\n Rc::new(RefCell::new(Self {\n val,\n left: None,\n right: None\n }))\n }\n}\n
/* Binary tree node */\ntypedef struct TreeNode {\n int val; // Node value\n int height; // \u8282\u70b9\u9ad8\u5ea6\n struct TreeNode *left; // Pointer to left child node\n struct TreeNode *right; // Pointer to right child node\n} TreeNode;\n\n/* \u6784\u9020\u51fd\u6570 */\nTreeNode *newTreeNode(int val) {\n TreeNode *node;\n\n node = (TreeNode *)malloc(sizeof(TreeNode));\n node->val = val;\n node->height = 0;\n node->left = NULL;\n node->right = NULL;\n return node;\n}\n
/* Binary tree node */\nclass TreeNode(val _val: Int) { // Node value\n val left: TreeNode? = null // Reference to left child node\n val right: TreeNode? = null // Reference to right child node\n}\n
\n
\n
Each node has two references (pointers), pointing to the \"left-child node\" and \"right-child node,\" respectively. This node is called the \"parent node\" of these two child nodes. When given a node of a binary tree, we call the tree formed by this node's left child and all nodes under it the \"left subtree\" of this node. Similarly, the \"right subtree\" can be defined.
In a binary tree, except for leaf nodes, all other nodes contain child nodes and non-empty subtrees. As shown in the Figure 7-1 , if \"Node 2\" is considered as the parent node, then its left and right child nodes are \"Node 4\" and \"Node 5,\" respectively. The left subtree is \"the tree formed by Node 4 and all nodes under it,\" and the right subtree is \"the tree formed by Node 5 and all nodes under it.\"
Figure 7-1 \u00a0 Parent Node, child Node, subtree
"},{"location":"chapter_tree/binary_tree/#711-common-terminology-of-binary-trees","title":"7.1.1 \u00a0 Common terminology of binary trees","text":"The commonly used terminology of binary trees is shown in the following figure.
None
.Figure 7-2 \u00a0 Common Terminology of Binary Trees
Tip
Please note that we usually define \"height\" and \"depth\" as \"the number of edges passed,\" but some problems or textbooks may define them as \"the number of nodes passed.\" In this case, both height and depth need to be incremented by 1.
"},{"location":"chapter_tree/binary_tree/#712-basic-operations-of-binary-trees","title":"7.1.2 \u00a0 Basic operations of binary trees","text":""},{"location":"chapter_tree/binary_tree/#1-initializing-a-binary-tree","title":"1. \u00a0 Initializing a binary tree","text":"Similar to a linked list, initialize nodes first, then construct references (pointers).
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py# Initializing a binary tree\n# Initializing nodes\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.cpp/* Initializing a binary tree */\n// Initializing nodes\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.java// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.cs/* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.go/* Initializing a binary tree */\n// Initializing nodes\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// Linking references (pointers) between nodes\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
binary_tree.swift// Initializing nodes\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.js/* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.ts/* Initializing a binary tree */\n// Initializing nodes\nlet n1 = new TreeNode(1),\n n2 = new TreeNode(2),\n n3 = new TreeNode(3),\n n4 = new TreeNode(4),\n n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.dart/* Initializing a binary tree */\n// Initializing nodes\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// Linking references (pointers) between nodes\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.rs// Initializing nodes\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// Linking references (pointers) between nodes\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c/* Initializing a binary tree */\n// Initializing nodes\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// Linking references (pointers) between nodes\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.kt// Initializing nodes\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// Linking references (pointers) between nodes\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.rb\n
binary_tree.zig\n
Code visualization https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
"},{"location":"chapter_tree/binary_tree/#2-inserting-and-removing-nodes","title":"2. \u00a0 Inserting and removing nodes","text":"Similar to a linked list, inserting and removing nodes in a binary tree can be achieved by modifying pointers. The Figure 7-3 provides an example.
Figure 7-3 \u00a0 Inserting and removing nodes in a binary tree
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree.py# Inserting and removing nodes\np = TreeNode(0)\n# Inserting node P between n1 -> n2\nn1.left = p\np.left = n2\n# Removing node P\nn1.left = n2\n
binary_tree.cpp/* Inserting and removing nodes */\nTreeNode* P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
binary_tree.javaTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.cs/* Inserting and removing nodes */\nTreeNode P = new(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.go/* Inserting and removing nodes */\n// Inserting node P between n1 and n2\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// Removing node P\nn1.Left = n2\n
binary_tree.swiftlet P = TreeNode(x: 0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
binary_tree.js/* Inserting and removing nodes */\nlet P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.ts/* Inserting and removing nodes */\nconst P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.dart/* Inserting and removing nodes */\nTreeNode P = new TreeNode(0);\n// Inserting node P between n1 and n2\nn1.left = P;\nP.left = n2;\n// Removing node P\nn1.left = n2;\n
binary_tree.rslet p = TreeNode::new(0);\n// Inserting node P between n1 and n2\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// Removing node P\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c/* Inserting and removing nodes */\nTreeNode *P = newTreeNode(0);\n// Inserting node P between n1 and n2\nn1->left = P;\nP->left = n2;\n// Removing node P\nn1->left = n2;\n
binary_tree.ktval P = TreeNode(0)\n// Inserting node P between n1 and n2\nn1.left = P\nP.left = n2\n// Removing node P\nn1.left = n2\n
binary_tree.rb\n
binary_tree.zig\n
Code visualization https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%E4%BA%8C%E5%8F%89%E6%A0%91%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%B7%A6%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%E5%8F%B3%E5%AD%90%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%8A%82%E7%82%B9%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%EF%BC%88%E6%8C%87%E9%92%88%EF%BC%89%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E4%B8%8E%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%E5%9C%A8%20n1%20-%3E%20n2%20%E4%B8%AD%E9%97%B4%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
Note
It's important to note that inserting nodes may change the original logical structure of the binary tree, while removing nodes usually means removing the node and all its subtrees. Therefore, in a binary tree, insertion and removal are usually performed through a set of operations to achieve meaningful actions.
"},{"location":"chapter_tree/binary_tree/#713-common-types-of-binary-trees","title":"7.1.3 \u00a0 Common types of binary trees","text":""},{"location":"chapter_tree/binary_tree/#1-perfect-binary-tree","title":"1. \u00a0 Perfect binary tree","text":"As shown in the Figure 7-4 , in a \"perfect binary tree,\" all levels of nodes are fully filled. In a perfect binary tree, the degree of leaf nodes is \\(0\\), and the degree of all other nodes is \\(2\\); if the tree's height is \\(h\\), then the total number of nodes is \\(2^{h+1} - 1\\), showing a standard exponential relationship, reflecting the common phenomenon of cell division in nature.
Tip
Please note that in the Chinese community, a perfect binary tree is often referred to as a \"full binary tree.\"
Figure 7-4 \u00a0 Perfect binary tree
"},{"location":"chapter_tree/binary_tree/#2-complete-binary-tree","title":"2. \u00a0 Complete binary tree","text":"As shown in the Figure 7-5 , a \"complete binary tree\" has only the bottom level nodes not fully filled, and the bottom level nodes are filled as far left as possible.
Figure 7-5 \u00a0 Complete binary tree
"},{"location":"chapter_tree/binary_tree/#3-full-binary-tree","title":"3. \u00a0 Full binary tree","text":"As shown in the Figure 7-6 , a \"full binary tree\" has all nodes except leaf nodes having two children.
Figure 7-6 \u00a0 Full binary tree
"},{"location":"chapter_tree/binary_tree/#4-balanced-binary-tree","title":"4. \u00a0 Balanced binary tree","text":"As shown in the Figure 7-7 , in a \"balanced binary tree,\" the absolute difference in height between the left and right subtrees of any node does not exceed 1.
Figure 7-7 \u00a0 Balanced binary tree
"},{"location":"chapter_tree/binary_tree/#714-degeneration-of-binary-trees","title":"7.1.4 \u00a0 Degeneration of binary trees","text":"The Figure 7-8 shows the ideal and degenerate structures of binary trees. When every level of a binary tree is filled, it reaches the \"perfect binary tree\"; when all nodes are biased towards one side, the binary tree degenerates into a \"linked list\".
Figure 7-8 \u00a0 The Best and Worst Structures of Binary Trees
As shown in the Table 7-1 , in the best and worst structures, the number of leaf nodes, total number of nodes, and height of the binary tree reach their maximum or minimum values.
Table 7-1 \u00a0 The Best and Worst Structures of Binary Trees
Perfect binary tree Linked list Number of nodes at level \\(i\\) \\(2^{i-1}\\) \\(1\\) Number of leaf nodes in a tree with height \\(h\\) \\(2^h\\) \\(1\\) Total number of nodes in a tree with height \\(h\\) \\(2^{h+1} - 1\\) \\(h + 1\\) Height of a tree with \\(n\\) total nodes \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 Binary tree traversal","text":"From the perspective of physical structure, a tree is a data structure based on linked lists, hence its traversal method involves accessing nodes one by one through pointers. However, a tree is a non-linear data structure, which makes traversing a tree more complex than traversing a linked list, requiring the assistance of search algorithms to achieve.
Common traversal methods for binary trees include level-order traversal, preorder traversal, inorder traversal, and postorder traversal, among others.
"},{"location":"chapter_tree/binary_tree_traversal/#721-level-order-traversal","title":"7.2.1 \u00a0 Level-order traversal","text":"As shown in the Figure 7-9 , \"level-order traversal\" traverses the binary tree from top to bottom, layer by layer, and accesses nodes in each layer in a left-to-right order.
Level-order traversal essentially belongs to \"breadth-first traversal\", also known as \"breadth-first search (BFS)\", which embodies a \"circumferentially outward expanding\" layer-by-layer traversal method.
Figure 7-9 \u00a0 Level-order traversal of a binary tree
"},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation","title":"1. \u00a0 Code implementation","text":"Breadth-first traversal is usually implemented with the help of a \"queue\". The queue follows the \"first in, first out\" rule, while breadth-first traversal follows the \"layer-by-layer progression\" rule, the underlying ideas of the two are consistent. The implementation code is as follows:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_bfs.pydef level_order(root: TreeNode | None) -> list[int]:\n \"\"\"\u5c42\u5e8f\u904d\u5386\"\"\"\n # \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue: deque[TreeNode] = deque()\n queue.append(root)\n # \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n res = []\n while queue:\n node: TreeNode = queue.popleft() # \u961f\u5217\u51fa\u961f\n res.append(node.val) # \u4fdd\u5b58\u8282\u70b9\u503c\n if node.left is not None:\n queue.append(node.left) # \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if node.right is not None:\n queue.append(node.right) # \u53f3\u5b50\u8282\u70b9\u5165\u961f\n return res\n
binary_tree_bfs.cpp/* \u5c42\u5e8f\u904d\u5386 */\nvector<int> levelOrder(TreeNode *root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue<TreeNode *> queue;\n queue.push(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n vector<int> vec;\n while (!queue.empty()) {\n TreeNode *node = queue.front();\n queue.pop(); // \u961f\u5217\u51fa\u961f\n vec.push_back(node->val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node->left != nullptr)\n queue.push(node->left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node->right != nullptr)\n queue.push(node->right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return vec;\n}\n
binary_tree_bfs.java/* \u5c42\u5e8f\u904d\u5386 */\nList<Integer> levelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new LinkedList<>();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<Integer> list = new ArrayList<>();\n while (!queue.isEmpty()) {\n TreeNode node = queue.poll(); // \u961f\u5217\u51fa\u961f\n list.add(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.offer(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.offer(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.cs/* \u5c42\u5e8f\u904d\u5386 */\nList<int> LevelOrder(TreeNode root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode> queue = new();\n queue.Enqueue(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> list = [];\n while (queue.Count != 0) {\n TreeNode node = queue.Dequeue(); // \u961f\u5217\u51fa\u961f\n list.Add(node.val!.Value); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null)\n queue.Enqueue(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null)\n queue.Enqueue(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.go/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root *TreeNode) []any {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n queue := list.New()\n queue.PushBack(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5207\u7247\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n nums := make([]any, 0)\n for queue.Len() > 0 {\n // \u961f\u5217\u51fa\u961f\n node := queue.Remove(queue.Front()).(*TreeNode)\n // \u4fdd\u5b58\u8282\u70b9\u503c\n nums = append(nums, node.Val)\n if node.Left != nil {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Left)\n }\n if node.Right != nil {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue.PushBack(node.Right)\n }\n }\n return nums\n}\n
binary_tree_bfs.swift/* \u5c42\u5e8f\u904d\u5386 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n var queue: [TreeNode] = [root]\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list: [Int] = []\n while !queue.isEmpty {\n let node = queue.removeFirst() // \u961f\u5217\u51fa\u961f\n list.append(node.val) // \u4fdd\u5b58\u8282\u70b9\u503c\n if let left = node.left {\n queue.append(left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let right = node.right {\n queue.append(right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list\n}\n
binary_tree_bfs.js/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list = [];\n while (queue.length) {\n let node = queue.shift(); // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right) queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list;\n}\n
binary_tree_bfs.ts/* \u5c42\u5e8f\u904d\u5386 */\nfunction levelOrder(root: TreeNode | null): number[] {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const queue = [root];\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n const list: number[] = [];\n while (queue.length) {\n let node = queue.shift() as TreeNode; // \u961f\u5217\u51fa\u961f\n list.push(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left) {\n queue.push(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right) {\n queue.push(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n }\n return list;\n}\n
binary_tree_bfs.dart/* \u5c42\u5e8f\u904d\u5386 */\nList<int> levelOrder(TreeNode? root) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n Queue<TreeNode?> queue = Queue();\n queue.add(root);\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n List<int> res = [];\n while (queue.isNotEmpty) {\n TreeNode? node = queue.removeFirst(); // \u961f\u5217\u51fa\u961f\n res.add(node!.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.add(node.left); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n if (node.right != null) queue.add(node.right); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return res;\n}\n
binary_tree_bfs.rs/* \u5c42\u5e8f\u904d\u5386 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n let mut que = VecDeque::new();\n que.push_back(Rc::clone(&root));\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n let mut vec = Vec::new();\n\n while let Some(node) = que.pop_front() {\n // \u961f\u5217\u51fa\u961f\n vec.push(node.borrow().val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if let Some(left) = node.borrow().left.as_ref() {\n que.push_back(Rc::clone(left)); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if let Some(right) = node.borrow().right.as_ref() {\n que.push_back(Rc::clone(right)); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n };\n }\n vec\n}\n
binary_tree_bfs.c/* \u5c42\u5e8f\u904d\u5386 */\nint *levelOrder(TreeNode *root, int *size) {\n /* \u8f85\u52a9\u961f\u5217 */\n int front, rear;\n int index, *arr;\n TreeNode *node;\n TreeNode **queue;\n\n /* \u8f85\u52a9\u961f\u5217 */\n queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n // \u961f\u5217\u6307\u9488\n front = 0, rear = 0;\n // \u52a0\u5165\u6839\u8282\u70b9\n queue[rear++] = root;\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n /* \u8f85\u52a9\u6570\u7ec4 */\n arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n // \u6570\u7ec4\u6307\u9488\n index = 0;\n while (front < rear) {\n // \u961f\u5217\u51fa\u961f\n node = queue[front++];\n // \u4fdd\u5b58\u8282\u70b9\u503c\n arr[index++] = node->val;\n if (node->left != NULL) {\n // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->left;\n }\n if (node->right != NULL) {\n // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n queue[rear++] = node->right;\n }\n }\n // \u66f4\u65b0\u6570\u7ec4\u957f\u5ea6\u7684\u503c\n *size = index;\n arr = realloc(arr, sizeof(int) * (*size));\n\n // \u91ca\u653e\u8f85\u52a9\u6570\u7ec4\u7a7a\u95f4\n free(queue);\n return arr;\n}\n
binary_tree_bfs.kt/* \u5c42\u5e8f\u904d\u5386 */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n val queue = LinkedList<TreeNode?>()\n queue.add(root)\n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n val list = ArrayList<Int>()\n while (!queue.isEmpty()) {\n val node = queue.poll() // \u961f\u5217\u51fa\u961f\n list.add(node?.value!!) // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) queue.offer(node.left) // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n\n if (node.right != null) queue.offer(node.right) // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n }\n return list\n}\n
binary_tree_bfs.rb[class]{}-[func]{level_order}\n
binary_tree_bfs.zig// \u5c42\u5e8f\u904d\u5386\nfn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {\n // \u521d\u59cb\u5316\u961f\u5217\uff0c\u52a0\u5165\u6839\u8282\u70b9\n const L = std.TailQueue(*inc.TreeNode(T));\n var queue = L{};\n var root_node = try mem_allocator.create(L.Node);\n root_node.data = root;\n queue.append(root_node); \n // \u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u904d\u5386\u5e8f\u5217\n var list = std.ArrayList(T).init(std.heap.page_allocator);\n while (queue.len > 0) {\n var queue_node = queue.popFirst().?; // \u961f\u5217\u51fa\u961f\n var node = queue_node.data;\n try list.append(node.val); // \u4fdd\u5b58\u8282\u70b9\u503c\n if (node.left != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.left.?;\n queue.append(tmp_node); // \u5de6\u5b50\u8282\u70b9\u5165\u961f\n }\n if (node.right != null) {\n var tmp_node = try mem_allocator.create(L.Node);\n tmp_node.data = node.right.?;\n queue.append(tmp_node); // \u53f3\u5b50\u8282\u70b9\u5165\u961f\n } \n }\n return list;\n}\n
Code Visualization Full Screen >
"},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis","title":"2. \u00a0 Complexity analysis","text":"Correspondingly, preorder, inorder, and postorder traversal all belong to \"depth-first traversal\", also known as \"depth-first search (DFS)\", which embodies a \"proceed to the end first, then backtrack and continue\" traversal method.
The Figure 7-10 shows the working principle of performing a depth-first traversal on a binary tree. Depth-first traversal is like walking around the perimeter of the entire binary tree, encountering three positions at each node, corresponding to preorder traversal, inorder traversal, and postorder traversal.
Figure 7-10 \u00a0 Preorder, inorder, and postorder traversal of a binary search tree
"},{"location":"chapter_tree/binary_tree_traversal/#1-code-implementation_1","title":"1. \u00a0 Code implementation","text":"Depth-first search is usually implemented based on recursion:
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRubyZig binary_tree_dfs.pydef pre_order(root: TreeNode | None):\n \"\"\"\u524d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n res.append(root.val)\n pre_order(root=root.left)\n pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n \"\"\"\u4e2d\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n in_order(root=root.left)\n res.append(root.val)\n in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n \"\"\"\u540e\u5e8f\u904d\u5386\"\"\"\n if root is None:\n return\n # \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n post_order(root=root.left)\n post_order(root=root.right)\n res.append(root.val)\n
binary_tree_dfs.cpp/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n vec.push_back(root->val);\n preOrder(root->left);\n preOrder(root->right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left);\n vec.push_back(root->val);\n inOrder(root->right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root) {\n if (root == nullptr)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left);\n postOrder(root->right);\n vec.push_back(root->val);\n}\n
binary_tree_dfs.java/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.add(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode root) {\n if (root == null)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.add(root.val);\n}\n
binary_tree_dfs.cs/* \u524d\u5e8f\u904d\u5386 */\nvoid PreOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.Add(root.val!.Value);\n PreOrder(root.left);\n PreOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid InOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n InOrder(root.left);\n list.Add(root.val!.Value);\n InOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid PostOrder(TreeNode? root) {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n PostOrder(root.left);\n PostOrder(root.right);\n list.Add(root.val!.Value);\n}\n
binary_tree_dfs.go/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n nums = append(nums, node.Val)\n preOrder(node.Left)\n preOrder(node.Right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.Left)\n nums = append(nums, node.Val)\n inOrder(node.Right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(node *TreeNode) {\n if node == nil {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.Left)\n postOrder(node.Right)\n nums = append(nums, node.Val)\n}\n
binary_tree_dfs.swift/* \u524d\u5e8f\u904d\u5386 */\nfunc preOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.append(root.val)\n preOrder(root: root.left)\n preOrder(root: root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunc inOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root: root.left)\n list.append(root.val)\n inOrder(root: root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunc postOrder(root: TreeNode?) {\n guard let root = root else {\n return\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root: root.left)\n postOrder(root: root.right)\n list.append(root.val)\n}\n
binary_tree_dfs.js/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root) {\n if (root === null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.ts/* \u524d\u5e8f\u904d\u5386 */\nfunction preOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.push(root.val);\n preOrder(root.left);\n preOrder(root.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfunction inOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left);\n list.push(root.val);\n inOrder(root.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfunction postOrder(root: TreeNode | null): void {\n if (root === null) {\n return;\n }\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left);\n postOrder(root.right);\n list.push(root.val);\n}\n
binary_tree_dfs.dart/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(node.val);\n preOrder(node.left);\n preOrder(node.right);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(node.left);\n list.add(node.val);\n inOrder(node.right);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode? node) {\n if (node == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(node.left);\n postOrder(node.right);\n list.add(node.val);\n}\n
binary_tree_dfs.rs/* \u524d\u5e8f\u904d\u5386 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n result.push(node.borrow().val);\n result.append(&mut pre_order(node.borrow().left.as_ref()));\n result.append(&mut pre_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n result.append(&mut in_order(node.borrow().left.as_ref()));\n result.push(node.borrow().val);\n result.append(&mut in_order(node.borrow().right.as_ref()));\n }\n result\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n let mut result = vec![];\n\n if let Some(node) = root {\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n result.append(&mut post_order(node.borrow().left.as_ref()));\n result.append(&mut post_order(node.borrow().right.as_ref()));\n result.push(node.borrow().val);\n }\n result\n}\n
binary_tree_dfs.c/* \u524d\u5e8f\u904d\u5386 */\nvoid preOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n arr[(*size)++] = root->val;\n preOrder(root->left, size);\n preOrder(root->right, size);\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nvoid inOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root->left, size);\n arr[(*size)++] = root->val;\n inOrder(root->right, size);\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nvoid postOrder(TreeNode *root, int *size) {\n if (root == NULL)\n return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root->left, size);\n postOrder(root->right, size);\n arr[(*size)++] = root->val;\n}\n
binary_tree_dfs.kt/* \u524d\u5e8f\u904d\u5386 */\nfun preOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n list.add(root.value)\n preOrder(root.left)\n preOrder(root.right)\n}\n\n/* \u4e2d\u5e8f\u904d\u5386 */\nfun inOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n inOrder(root.left)\n list.add(root.value)\n inOrder(root.right)\n}\n\n/* \u540e\u5e8f\u904d\u5386 */\nfun postOrder(root: TreeNode?) {\n if (root == null) return\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n postOrder(root.left)\n postOrder(root.right)\n list.add(root.value)\n}\n
binary_tree_dfs.rb[class]{}-[func]{pre_order}\n\n[class]{}-[func]{in_order}\n\n[class]{}-[func]{post_order}\n
binary_tree_dfs.zig// \u524d\u5e8f\u904d\u5386\nfn preOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u6839\u8282\u70b9 -> \u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811\n try list.append(root.?.val);\n try preOrder(T, root.?.left);\n try preOrder(T, root.?.right);\n}\n\n// \u4e2d\u5e8f\u904d\u5386\nfn inOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u6839\u8282\u70b9 -> \u53f3\u5b50\u6811\n try inOrder(T, root.?.left);\n try list.append(root.?.val);\n try inOrder(T, root.?.right);\n}\n\n// \u540e\u5e8f\u904d\u5386\nfn postOrder(comptime T: type, root: ?*inc.TreeNode(T)) !void {\n if (root == null) return;\n // \u8bbf\u95ee\u4f18\u5148\u7ea7\uff1a\u5de6\u5b50\u6811 -> \u53f3\u5b50\u6811 -> \u6839\u8282\u70b9\n try postOrder(T, root.?.left);\n try postOrder(T, root.?.right);\n try list.append(root.?.val);\n}\n
Code Visualization Full Screen >
Tip
Depth-first search can also be implemented based on iteration, interested readers can study this on their own.
The Figure 7-11 shows the recursive process of preorder traversal of a binary tree, which can be divided into two opposite parts: \"recursion\" and \"return\".
Figure 7-11 \u00a0 The recursive process of preorder traversal
"},{"location":"chapter_tree/binary_tree_traversal/#2-complexity-analysis_1","title":"2. \u00a0 Complexity analysis","text":"Q: For a binary tree with only one node, are both the height of the tree and the depth of the root node \\(0\\)?
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?
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.
Q: Why are there three sequences: pre-order, in-order, and post-order for DFS traversal of a binary tree, and what are their uses?
Similar to sequential and reverse traversal of arrays, pre-order, in-order, and post-order traversals are three methods of traversing a binary tree, allowing us to obtain a traversal result in a specific order. For example, in a binary search tree, since the node sizes satisfy left child node value < root node value < right child node value
, we can obtain an ordered node sequence by traversing the tree in the \"left \u2192 root \u2192 right\" priority.
Q: In a right rotation operation that deals with the relationship between the imbalance nodes node
, child
, grand_child
, isn't the connection between node
and its parent node and the original link of node
lost after the right rotation?
We need to view this problem from a recursive perspective. The right_rotate(root)
operation passes the root node of the subtree and eventually returns the root node of the rotated subtree with return child
. The connection between the subtree's root node and its parent node is established after this function returns, which is outside the scope of the right rotation operation's maintenance.
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.
Q: How do you build a binary search tree from a set of input data? Is the choice of root node very important?
Yes, the method for building the tree is provided in the build_tree()
method in the binary search tree code. As for the choice of the root node, we usually sort the input data and then select the middle element as the root node, recursively building the left and right subtrees. This approach maximizes the balance of the tree.
Q: In Java, do you always have to use the equals()
method for string comparison?
In Java, for primitive data types, ==
is used to compare whether the values of two variables are equal. For reference types, the working principles of the two symbols are different.
==
: Used to compare whether two variables point to the same object, i.e., whether their positions in memory are the same.equals()
: Used to compare whether the values of two objects are equal.Therefore, to compare values, we should use equals()
. However, strings initialized with String a = \"hi\"; String b = \"hi\";
are stored in the string constant pool and point to the same object, so a == b
can also be used to compare the contents of two strings.
Q: Before reaching the bottom level, is the number of nodes in the queue \\(2^h\\) in breadth-first traversal?
Yes, for example, a full binary tree with height \\(h = 2\\) has a total of \\(n = 7\\) nodes, then the bottom level has \\(4 = 2^h = (n + 1) / 2\\) nodes.
"}]} \ No newline at end of file diff --git a/en/sitemap.xml b/en/sitemap.xml index 0b84de6f1..da0417b57 100644 --- a/en/sitemap.xml +++ b/en/sitemap.xml @@ -2,272 +2,272 @@資料結構的世界如同一堵厚實的磚牆。
陣列的磚塊整齊排列,逐個緊貼。鏈結串列的磚塊分散各處,連線的藤蔓自由地穿梭於磚縫之間。
-我們如同迷宮中的探索者,在前進的道路上可能會遇到困難。
回溯的力量讓我們能夠重新開始,不斷嘗試,最終找到通往光明的出口。
-複雜度分析猶如浩瀚的演算法宇宙中的時空嚮導。
它帶領我們在時間與空間這兩個維度上深入探索,尋找更優雅的解決方案。
-資料結構如同一副穩固而多樣的框架。
它為資料的有序組織提供了藍圖,演算法得以在此基礎上生動起來。
-難題被逐層拆解,每一次的拆解都使它變得更為簡單。
分而治之揭示了一個重要的事實:從簡單做起,一切都不再複雜。
-小溪匯入河流,江河匯入大海。
動態規劃將小問題的解彙集成大問題的答案,一步步引領我們走向解決問題的彼岸。
-在生命旅途中,我們就像是一個個節點,被無數看不見的邊相連。
每一次的相識與相離,都在這張巨大的網路圖中留下獨特的印記。
-向日葵朝著太陽轉動,時刻追求自身成長的最大可能。
貪婪策略在一輪輪的簡單選擇中,逐步導向最佳答案。
-在計算機世界中,雜湊表如同一位聰慧的圖書管理員。
他知道如何計算索書號,從而可以快速找到目標圖書。
-堆積就像是山嶽峰巒,層疊起伏、形態各異。
座座山峰高低錯落,而最高的山峰總是最先映入眼簾。
-一位少女翩翩起舞,與資料交織在一起,裙襬上飄揚著演算法的旋律。
她邀請你共舞,請緊跟她的步伐,踏入充滿邏輯與美感的演算法世界。
-演算法猶如美妙的交響樂,每一行程式碼都像韻律般流淌。
願這本書在你的腦海中輕輕響起,留下獨特而深刻的旋律。
-搜尋是一場未知的冒險,我們或許需要走遍神秘空間的每個角落,又或許可以快速鎖定目標。
在這場尋覓之旅中,每一次探索都可能得到一個未曾料想的答案。
-排序猶如一把將混亂變為秩序的魔法鑰匙,使我們能以更高效的方式理解與處理資料。
無論是簡單的升序,還是複雜的分類排列,排序都向我們展示了資料的和諧美感。
-堆疊如同疊貓貓,而佇列就像貓貓排隊。
兩者分別代表先入後出和先入先出的邏輯關係。
-參天大樹充滿生命力,根深葉茂,分枝扶疏。
它為我們展現了資料分治的生動形態。
-