mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
build
This commit is contained in:
@@ -224,15 +224,15 @@ comments: true
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
/* 查找节点 */
|
||||
TreeNode? search(int num) {
|
||||
TreeNode? search(int _num) {
|
||||
TreeNode? cur = _root;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur.val < num)
|
||||
if (cur.val < _num)
|
||||
cur = cur.right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur.val > num)
|
||||
else if (cur.val > _num)
|
||||
cur = cur.left;
|
||||
// 找到目标节点,跳出循环
|
||||
else
|
||||
@@ -601,10 +601,10 @@ comments: true
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
/* 插入节点 */
|
||||
void insert(int num) {
|
||||
void insert(int _num) {
|
||||
// 若树为空,则初始化根节点
|
||||
if (_root == null) {
|
||||
_root = TreeNode(num);
|
||||
_root = TreeNode(_num);
|
||||
return;
|
||||
}
|
||||
TreeNode? cur = _root;
|
||||
@@ -612,18 +612,18 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur.val == num) return;
|
||||
if (cur.val == _num) return;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur.val < num)
|
||||
if (cur.val < _num)
|
||||
cur = cur.right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else
|
||||
cur = cur.left;
|
||||
}
|
||||
// 插入节点
|
||||
TreeNode? node = TreeNode(num);
|
||||
if (pre!.val < num)
|
||||
TreeNode? node = TreeNode(_num);
|
||||
if (pre!.val < _num)
|
||||
pre.right = node;
|
||||
else
|
||||
pre.left = node;
|
||||
@@ -1234,7 +1234,7 @@ comments: true
|
||||
|
||||
```dart title="binary_search_tree.dart"
|
||||
/* 删除节点 */
|
||||
void remove(int num) {
|
||||
void remove(int _num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (_root == null) return;
|
||||
TreeNode? cur = _root;
|
||||
@@ -1242,10 +1242,10 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur.val == num) break;
|
||||
if (cur.val == _num) break;
|
||||
pre = cur;
|
||||
// 待删除节点在 cur 的右子树中
|
||||
if (cur.val < num)
|
||||
if (cur.val < _num)
|
||||
cur = cur.right;
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user