Skip to content

Commit 2336fe1

Browse files
committed
Update the comments in
binary_search_tree and avl_tree.
1 parent 8e2e95a commit 2336fe1

File tree

14 files changed

+70
-68
lines changed

14 files changed

+70
-68
lines changed

codes/c/chapter_tree/avl_tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ TreeNode *search(avlTree *tree, int val) {
203203
// 循环查找,越过叶结点后跳出
204204
while (cur != NULL) {
205205
if (cur->val < val) {
206-
// 目标结点在 root 的右子树中
206+
// 目标结点在 cur 的右子树中
207207
cur = cur->right;
208208
} else if (cur->val > val) {
209-
// 目标结点在 root 的左子树中
209+
// 目标结点在 cur 的左子树中
210210
cur = cur->left;
211211
} else {
212212
// 找到目标结点,跳出循环

codes/c/chapter_tree/binary_search_tree.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ TreeNode *search(binarySearchTree *bst, int num) {
5353
TreeNode *cur = bst->root;
5454
// 循环查找,越过叶结点后跳出
5555
while (cur != NULL) {
56-
// 目标结点在 root 的右子树中
5756
if (cur->val < num) {
57+
// 目标结点在 cur 的右子树中
5858
cur = cur->right;
5959
} else if (cur->val > num) {
60-
// 目标结点在 root 的左子树中
60+
// 目标结点在 cur 的左子树中
6161
cur = cur->left;
6262
} else {
6363
// 找到目标结点,跳出循环
@@ -81,10 +81,10 @@ TreeNode *insert(binarySearchTree *bst, int num) {
8181
}
8282
pre = cur;
8383
if (cur->val < num) {
84-
// 插入位置在 root 的右子树中
84+
// 插入位置在 cur 的右子树中
8585
cur = cur->right;
8686
} else {
87-
// 插入位置在 root 的左子树中
87+
// 插入位置在 cur 的左子树中
8888
cur = cur->left;
8989
}
9090
}

codes/cpp/chapter_tree/binary_search_tree.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class BinarySearchTree {
4343
TreeNode* cur = root;
4444
// 循环查找,越过叶结点后跳出
4545
while (cur != nullptr) {
46-
// 目标结点在 root 的右子树中
46+
// 目标结点在 cur 的右子树中
4747
if (cur->val < num) cur = cur->right;
48-
// 目标结点在 root 的左子树中
48+
// 目标结点在 cur 的左子树中
4949
else if (cur->val > num) cur = cur->left;
5050
// 找到目标结点,跳出循环
5151
else break;
@@ -64,9 +64,9 @@ class BinarySearchTree {
6464
// 找到重复结点,直接返回
6565
if (cur->val == num) return nullptr;
6666
pre = cur;
67-
// 插入位置在 root 的右子树中
67+
// 插入位置在 cur 的右子树中
6868
if (cur->val < num) cur = cur->right;
69-
// 插入位置在 root 的左子树中
69+
// 插入位置在 cur 的左子树中
7070
else cur = cur->left;
7171
}
7272
// 插入结点 val

codes/csharp/chapter_tree/avl_tree.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ public int balanceFactor(TreeNode? node)
193193
// 循环查找,越过叶结点后跳出
194194
while (cur != null)
195195
{
196-
// 目标结点在 root 的右子树中
196+
// 目标结点在 cur 的右子树中
197197
if (cur.val < val)
198198
cur = cur.right;
199-
// 目标结点在 root 的左子树中
199+
// 目标结点在 cur 的左子树中
200200
else if (cur.val > val)
201201
cur = cur.left;
202202
// 找到目标结点,跳出循环

codes/csharp/chapter_tree/binary_search_tree.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public BinarySearchTree(int[] nums) {
4242
// 循环查找,越过叶结点后跳出
4343
while (cur != null)
4444
{
45-
// 目标结点在 root 的右子树中
45+
// 目标结点在 cur 的右子树中
4646
if (cur.val < num) cur = cur.right;
47-
// 目标结点在 root 的左子树中
47+
// 目标结点在 cur 的左子树中
4848
else if (cur.val > num) cur = cur.left;
4949
// 找到目标结点,跳出循环
5050
else break;
@@ -65,9 +65,9 @@ public BinarySearchTree(int[] nums) {
6565
// 找到重复结点,直接返回
6666
if (cur.val == num) return null;
6767
pre = cur;
68-
// 插入位置在 root 的右子树中
68+
// 插入位置在 cur 的右子树中
6969
if (cur.val < num) cur = cur.right;
70-
// 插入位置在 root 的左子树中
70+
// 插入位置在 cur 的左子树中
7171
else cur = cur.left;
7272
}
7373

codes/go/chapter_tree/avl_tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ func (t *avlTree) search(val int) *TreeNode {
195195
cur := t.root
196196
// 循环查找,越过叶结点后跳出
197197
for cur != nil {
198-
// 目标结点在 root 的右子树中
199198
if cur.Val < val {
199+
// 目标结点在 cur 的右子树中
200200
cur = cur.Right
201201
} else if cur.Val > val {
202-
// 目标结点在 root 的左子树中
202+
// 目标结点在 cur 的左子树中
203203
cur = cur.Left
204204
} else {
205205
// 找到目标结点,跳出循环

codes/go/chapter_tree/binary_search_tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ func (bst *binarySearchTree) search(num int) *TreeNode {
4646
// 循环查找,越过叶结点后跳出
4747
for node != nil {
4848
if node.Val < num {
49-
// 目标结点在 root 的右子树中
49+
// 目标结点在 cur 的右子树中
5050
node = node.Right
5151
} else if node.Val > num {
52-
// 目标结点在 root 的左子树中
52+
// 目标结点在 cur 的左子树中
5353
node = node.Left
5454
} else {
5555
// 找到目标结点,跳出循环

codes/java/chapter_tree/avl_tree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ public TreeNode search(int val) {
165165
TreeNode cur = root;
166166
// 循环查找,越过叶结点后跳出
167167
while (cur != null) {
168-
// 目标结点在 root 的右子树中
168+
// 目标结点在 cur 的右子树中
169169
if (cur.val < val)
170170
cur = cur.right;
171-
// 目标结点在 root 的左子树中
171+
// 目标结点在 cur 的左子树中
172172
else if (cur.val > val)
173173
cur = cur.left;
174174
// 找到目标结点,跳出循环

codes/java/chapter_tree/binary_search_tree.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public TreeNode search(int num) {
4040
TreeNode cur = root;
4141
// 循环查找,越过叶结点后跳出
4242
while (cur != null) {
43-
// 目标结点在 root 的右子树中
43+
// 目标结点在 cur 的右子树中
4444
if (cur.val < num) cur = cur.right;
45-
// 目标结点在 root 的左子树中
45+
// 目标结点在 cur 的左子树中
4646
else if (cur.val > num) cur = cur.left;
4747
// 找到目标结点,跳出循环
4848
else break;
@@ -61,9 +61,9 @@ public TreeNode insert(int num) {
6161
// 找到重复结点,直接返回
6262
if (cur.val == num) return null;
6363
pre = cur;
64-
// 插入位置在 root 的右子树中
64+
// 插入位置在 cur 的右子树中
6565
if (cur.val < num) cur = cur.right;
66-
// 插入位置在 root 的左子树中
66+
// 插入位置在 cur 的左子树中
6767
else cur = cur.left;
6868
}
6969
// 插入结点 val

codes/javascript/chapter_tree/binary_search_tree.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function search(num) {
3737
let cur = root;
3838
// 循环查找,越过叶结点后跳出
3939
while (cur !== null) {
40-
// 目标结点在 root 的右子树中
40+
// 目标结点在 cur 的右子树中
4141
if (cur.val < num) cur = cur.right;
42-
// 目标结点在 root 的左子树中
42+
// 目标结点在 cur 的左子树中
4343
else if (cur.val > num) cur = cur.left;
4444
// 找到目标结点,跳出循环
4545
else break;
@@ -58,9 +58,9 @@ function insert(num) {
5858
// 找到重复结点,直接返回
5959
if (cur.val === num) return null;
6060
pre = cur;
61-
// 插入位置在 root 的右子树中
61+
// 插入位置在 cur 的右子树中
6262
if (cur.val < num) cur = cur.right;
63-
// 插入位置在 root 的左子树中
63+
// 插入位置在 cur 的左子树中
6464
else cur = cur.left;
6565
}
6666
// 插入结点 val

codes/python/chapter_tree/avl_tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ def search(self, val: int):
152152
cur = self.root
153153
# 循环查找,越过叶结点后跳出
154154
while cur is not None:
155-
# 目标结点在 root 的右子树中
155+
# 目标结点在 cur 的右子树中
156156
if cur.val < val:
157157
cur = cur.right
158-
# 目标结点在 root 的左子树中
158+
# 目标结点在 cur 的左子树中
159159
elif cur.val > val:
160160
cur = cur.left
161161
# 找到目标结点,跳出循环

codes/python/chapter_tree/binary_search_tree.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def search(self, num: int) -> Optional[TreeNode]:
3737
cur = self.root
3838
# 循环查找,越过叶结点后跳出
3939
while cur is not None:
40-
# 目标结点在 root 的右子树中
40+
# 目标结点在 cur 的右子树中
4141
if cur.val < num:
4242
cur = cur.right
43-
# 目标结点在 root 的左子树中
43+
# 目标结点在 cur 的左子树中
4444
elif cur.val > num:
4545
cur = cur.left
4646
# 找到目标结点,跳出循环
@@ -64,10 +64,11 @@ def insert(self, num: int) -> Optional[TreeNode]:
6464
if cur.val == num:
6565
return None
6666
pre = cur
67-
68-
if cur.val < num: # 插入位置在 root 的右子树中
67+
# 插入位置在 cur 的右子树中
68+
if cur.val < num:
6969
cur = cur.right
70-
else: # 插入位置在 root 的左子树中
70+
# 插入位置在 cur 的左子树中
71+
else:
7172
cur = cur.left
7273

7374
# 插入结点 val

codes/typescript/chapter_tree/binary_search_tree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ function search(num: number): TreeNode | null {
4040
// 循环查找,越过叶结点后跳出
4141
while (cur !== null) {
4242
if (cur.val < num) {
43-
cur = cur.right; // 目标结点在 root 的右子树中
43+
cur = cur.right; // 目标结点在 cur 的右子树中
4444
} else if (cur.val > num) {
45-
cur = cur.left; // 目标结点在 root 的左子树中
45+
cur = cur.left; // 目标结点在 cur 的左子树中
4646
} else {
4747
break; // 找到目标结点,跳出循环
4848
}
@@ -66,9 +66,9 @@ function insert(num: number): TreeNode | null {
6666
}
6767
pre = cur;
6868
if (cur.val < num) {
69-
cur = cur.right as TreeNode; // 插入位置在 root 的右子树中
69+
cur = cur.right as TreeNode; // 插入位置在 cur 的右子树中
7070
} else {
71-
cur = cur.left as TreeNode; // 插入位置在 root 的左子树中
71+
cur = cur.left as TreeNode; // 插入位置在 cur 的左子树中
7272
}
7373
}
7474
// 插入结点 val

0 commit comments

Comments
 (0)