From 2336fe1d50e01d29027a3930e589f12a43745ac6 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 17 Jan 2023 01:53:12 +0800 Subject: [PATCH] Update the comments in binary_search_tree and avl_tree. --- codes/c/chapter_tree/avl_tree.c | 4 +- codes/c/chapter_tree/binary_search_tree.c | 8 +-- codes/cpp/chapter_tree/binary_search_tree.cpp | 8 +-- codes/csharp/chapter_tree/avl_tree.cs | 4 +- .../csharp/chapter_tree/binary_search_tree.cs | 8 +-- codes/go/chapter_tree/avl_tree.go | 4 +- codes/go/chapter_tree/binary_search_tree.go | 4 +- codes/java/chapter_tree/avl_tree.java | 4 +- .../java/chapter_tree/binary_search_tree.java | 8 +-- .../chapter_tree/binary_search_tree.js | 8 +-- codes/python/chapter_tree/avl_tree.py | 4 +- .../python/chapter_tree/binary_search_tree.py | 11 ++-- .../chapter_tree/binary_search_tree.ts | 8 +-- docs/chapter_tree/binary_search_tree.md | 55 ++++++++++--------- 14 files changed, 70 insertions(+), 68 deletions(-) diff --git a/codes/c/chapter_tree/avl_tree.c b/codes/c/chapter_tree/avl_tree.c index db97e4474..74b40d3b7 100644 --- a/codes/c/chapter_tree/avl_tree.c +++ b/codes/c/chapter_tree/avl_tree.c @@ -203,10 +203,10 @@ TreeNode *search(avlTree *tree, int val) { // 循环查找,越过叶结点后跳出 while (cur != NULL) { if (cur->val < val) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 cur = cur->right; } else if (cur->val > val) { - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 cur = cur->left; } else { // 找到目标结点,跳出循环 diff --git a/codes/c/chapter_tree/binary_search_tree.c b/codes/c/chapter_tree/binary_search_tree.c index 3429c54ac..b8ddddf48 100644 --- a/codes/c/chapter_tree/binary_search_tree.c +++ b/codes/c/chapter_tree/binary_search_tree.c @@ -53,11 +53,11 @@ TreeNode *search(binarySearchTree *bst, int num) { TreeNode *cur = bst->root; // 循环查找,越过叶结点后跳出 while (cur != NULL) { - // 目标结点在 root 的右子树中 if (cur->val < num) { + // 目标结点在 cur 的右子树中 cur = cur->right; } else if (cur->val > num) { - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 cur = cur->left; } else { // 找到目标结点,跳出循环 @@ -81,10 +81,10 @@ TreeNode *insert(binarySearchTree *bst, int num) { } pre = cur; if (cur->val < num) { - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 cur = cur->right; } else { - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 cur = cur->left; } } diff --git a/codes/cpp/chapter_tree/binary_search_tree.cpp b/codes/cpp/chapter_tree/binary_search_tree.cpp index cd6a712d9..69acb87f0 100644 --- a/codes/cpp/chapter_tree/binary_search_tree.cpp +++ b/codes/cpp/chapter_tree/binary_search_tree.cpp @@ -43,9 +43,9 @@ public: TreeNode* cur = root; // 循环查找,越过叶结点后跳出 while (cur != nullptr) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur->val < num) cur = cur->right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur->val > num) cur = cur->left; // 找到目标结点,跳出循环 else break; @@ -64,9 +64,9 @@ public: // 找到重复结点,直接返回 if (cur->val == num) return nullptr; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur->val < num) cur = cur->right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur->left; } // 插入结点 val diff --git a/codes/csharp/chapter_tree/avl_tree.cs b/codes/csharp/chapter_tree/avl_tree.cs index 6bcb559b7..50a4ba162 100644 --- a/codes/csharp/chapter_tree/avl_tree.cs +++ b/codes/csharp/chapter_tree/avl_tree.cs @@ -193,10 +193,10 @@ namespace hello_algo.chapter_tree // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < val) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > val) cur = cur.left; // 找到目标结点,跳出循环 diff --git a/codes/csharp/chapter_tree/binary_search_tree.cs b/codes/csharp/chapter_tree/binary_search_tree.cs index cb8327693..2821b29bb 100644 --- a/codes/csharp/chapter_tree/binary_search_tree.cs +++ b/codes/csharp/chapter_tree/binary_search_tree.cs @@ -42,9 +42,9 @@ namespace hello_algo.chapter_tree // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -65,9 +65,9 @@ namespace hello_algo.chapter_tree // 找到重复结点,直接返回 if (cur.val == num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; } diff --git a/codes/go/chapter_tree/avl_tree.go b/codes/go/chapter_tree/avl_tree.go index e83b2882b..74e681e9b 100644 --- a/codes/go/chapter_tree/avl_tree.go +++ b/codes/go/chapter_tree/avl_tree.go @@ -195,11 +195,11 @@ func (t *avlTree) search(val int) *TreeNode { cur := t.root // 循环查找,越过叶结点后跳出 for cur != nil { - // 目标结点在 root 的右子树中 if cur.Val < val { + // 目标结点在 cur 的右子树中 cur = cur.Right } else if cur.Val > val { - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 cur = cur.Left } else { // 找到目标结点,跳出循环 diff --git a/codes/go/chapter_tree/binary_search_tree.go b/codes/go/chapter_tree/binary_search_tree.go index 61ed400d5..e4fd08103 100644 --- a/codes/go/chapter_tree/binary_search_tree.go +++ b/codes/go/chapter_tree/binary_search_tree.go @@ -46,10 +46,10 @@ func (bst *binarySearchTree) search(num int) *TreeNode { // 循环查找,越过叶结点后跳出 for node != nil { if node.Val < num { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 node = node.Right } else if node.Val > num { - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 node = node.Left } else { // 找到目标结点,跳出循环 diff --git a/codes/java/chapter_tree/avl_tree.java b/codes/java/chapter_tree/avl_tree.java index 9120262f4..2699bb601 100644 --- a/codes/java/chapter_tree/avl_tree.java +++ b/codes/java/chapter_tree/avl_tree.java @@ -165,10 +165,10 @@ class AVLTree { TreeNode cur = root; // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < val) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > val) cur = cur.left; // 找到目标结点,跳出循环 diff --git a/codes/java/chapter_tree/binary_search_tree.java b/codes/java/chapter_tree/binary_search_tree.java index a0a554e38..7f13b9037 100644 --- a/codes/java/chapter_tree/binary_search_tree.java +++ b/codes/java/chapter_tree/binary_search_tree.java @@ -40,9 +40,9 @@ class BinarySearchTree { TreeNode cur = root; // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -61,9 +61,9 @@ class BinarySearchTree { // 找到重复结点,直接返回 if (cur.val == num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; } // 插入结点 val diff --git a/codes/javascript/chapter_tree/binary_search_tree.js b/codes/javascript/chapter_tree/binary_search_tree.js index d619ab9f8..2fd9092a7 100644 --- a/codes/javascript/chapter_tree/binary_search_tree.js +++ b/codes/javascript/chapter_tree/binary_search_tree.js @@ -37,9 +37,9 @@ function search(num) { let cur = root; // 循环查找,越过叶结点后跳出 while (cur !== null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -58,9 +58,9 @@ function insert(num) { // 找到重复结点,直接返回 if (cur.val === num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; } // 插入结点 val diff --git a/codes/python/chapter_tree/avl_tree.py b/codes/python/chapter_tree/avl_tree.py index 83929981d..79f2e81d5 100644 --- a/codes/python/chapter_tree/avl_tree.py +++ b/codes/python/chapter_tree/avl_tree.py @@ -152,10 +152,10 @@ class AVLTree: cur = self.root # 循环查找,越过叶结点后跳出 while cur is not None: - # 目标结点在 root 的右子树中 + # 目标结点在 cur 的右子树中 if cur.val < val: cur = cur.right - # 目标结点在 root 的左子树中 + # 目标结点在 cur 的左子树中 elif cur.val > val: cur = cur.left # 找到目标结点,跳出循环 diff --git a/codes/python/chapter_tree/binary_search_tree.py b/codes/python/chapter_tree/binary_search_tree.py index fdedf6c9d..c3058c93f 100644 --- a/codes/python/chapter_tree/binary_search_tree.py +++ b/codes/python/chapter_tree/binary_search_tree.py @@ -37,10 +37,10 @@ class BinarySearchTree: cur = self.root # 循环查找,越过叶结点后跳出 while cur is not None: - # 目标结点在 root 的右子树中 + # 目标结点在 cur 的右子树中 if cur.val < num: cur = cur.right - # 目标结点在 root 的左子树中 + # 目标结点在 cur 的左子树中 elif cur.val > num: cur = cur.left # 找到目标结点,跳出循环 @@ -64,10 +64,11 @@ class BinarySearchTree: if cur.val == num: return None pre = cur - - if cur.val < num: # 插入位置在 root 的右子树中 + # 插入位置在 cur 的右子树中 + if cur.val < num: cur = cur.right - else: # 插入位置在 root 的左子树中 + # 插入位置在 cur 的左子树中 + else: cur = cur.left # 插入结点 val diff --git a/codes/typescript/chapter_tree/binary_search_tree.ts b/codes/typescript/chapter_tree/binary_search_tree.ts index a19ea32ab..c210d3cae 100644 --- a/codes/typescript/chapter_tree/binary_search_tree.ts +++ b/codes/typescript/chapter_tree/binary_search_tree.ts @@ -40,9 +40,9 @@ function search(num: number): TreeNode | null { // 循环查找,越过叶结点后跳出 while (cur !== null) { if (cur.val < num) { - cur = cur.right; // 目标结点在 root 的右子树中 + cur = cur.right; // 目标结点在 cur 的右子树中 } else if (cur.val > num) { - cur = cur.left; // 目标结点在 root 的左子树中 + cur = cur.left; // 目标结点在 cur 的左子树中 } else { break; // 找到目标结点,跳出循环 } @@ -66,9 +66,9 @@ function insert(num: number): TreeNode | null { } pre = cur; if (cur.val < num) { - cur = cur.right as TreeNode; // 插入位置在 root 的右子树中 + cur = cur.right as TreeNode; // 插入位置在 cur 的右子树中 } else { - cur = cur.left as TreeNode; // 插入位置在 root 的左子树中 + cur = cur.left as TreeNode; // 插入位置在 cur 的左子树中 } } // 插入结点 val diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 393221b75..75ffb6789 100644 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -43,9 +43,9 @@ comments: true TreeNode cur = root; // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -63,9 +63,9 @@ comments: true TreeNode* cur = root; // 循环查找,越过叶结点后跳出 while (cur != nullptr) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur->val < num) cur = cur->right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur->val > num) cur = cur->left; // 找到目标结点,跳出循环 else break; @@ -83,10 +83,10 @@ comments: true cur = self.root # 循环查找,越过叶结点后跳出 while cur is not None: - # 目标结点在 root 的右子树中 + # 目标结点在 cur 的右子树中 if cur.val < num: cur = cur.right - # 目标结点在 root 的左子树中 + # 目标结点在 cur 的左子树中 elif cur.val > num: cur = cur.left # 找到目标结点,跳出循环 @@ -104,10 +104,10 @@ comments: true // 循环查找,越过叶结点后跳出 for node != nil { if node.Val < num { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 node = node.Right } else if node.Val > num { - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 node = node.Left } else { // 找到目标结点,跳出循环 @@ -127,9 +127,9 @@ comments: true let cur = root; // 循环查找,越过叶结点后跳出 while (cur !== null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -148,9 +148,9 @@ comments: true // 循环查找,越过叶结点后跳出 while (cur !== null) { if (cur.val < num) { - cur = cur.right; // 目标结点在 root 的右子树中 + cur = cur.right; // 目标结点在 cur 的右子树中 } else if (cur.val > num) { - cur = cur.left; // 目标结点在 root 的左子树中 + cur = cur.left; // 目标结点在 cur 的左子树中 } else { break; // 找到目标结点,跳出循环 } @@ -176,9 +176,9 @@ comments: true // 循环查找,越过叶结点后跳出 while (cur != null) { - // 目标结点在 root 的右子树中 + // 目标结点在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 目标结点在 root 的左子树中 + // 目标结点在 cur 的左子树中 else if (cur.val > num) cur = cur.left; // 找到目标结点,跳出循环 else break; @@ -218,9 +218,9 @@ comments: true // 找到重复结点,直接返回 if (cur.val == num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; } // 插入结点 val @@ -244,9 +244,9 @@ comments: true // 找到重复结点,直接返回 if (cur->val == num) return nullptr; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur->val < num) cur = cur->right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur->left; } // 插入结点 val @@ -276,10 +276,11 @@ comments: true if cur.val == num: return None pre = cur - - if cur.val < num: # 插入位置在 root 的右子树中 + # 插入位置在 cur 的右子树中 + if cur.val < num: cur = cur.right - else: # 插入位置在 root 的左子树中 + # 插入位置在 cur 的左子树中 + else: cur = cur.left # 插入结点 val @@ -339,9 +340,9 @@ comments: true // 找到重复结点,直接返回 if (cur.val === num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; } // 插入结点 val @@ -370,9 +371,9 @@ comments: true } pre = cur; if (cur.val < num) { - cur = cur.right as TreeNode; // 插入位置在 root 的右子树中 + cur = cur.right as TreeNode; // 插入位置在 cur 的右子树中 } else { - cur = cur.left as TreeNode; // 插入位置在 root 的左子树中 + cur = cur.left as TreeNode; // 插入位置在 cur 的左子树中 } } // 插入结点 val @@ -407,9 +408,9 @@ comments: true // 找到重复结点,直接返回 if (cur.val == num) return null; pre = cur; - // 插入位置在 root 的右子树中 + // 插入位置在 cur 的右子树中 if (cur.val < num) cur = cur.right; - // 插入位置在 root 的左子树中 + // 插入位置在 cur 的左子树中 else cur = cur.left; }