Fix the return type of binary search tree and avl tree

This commit is contained in:
krahets 2023-04-14 05:47:20 +08:00
parent 9c9c8b7574
commit f7ae9c8a02
24 changed files with 247 additions and 451 deletions

View File

@ -132,11 +132,9 @@ TreeNode *insertHelper(TreeNode *node, int val) {
return node; return node;
} }
/* 插入节点 */ /* 插入节点 */
TreeNode *insert(avlTree *tree, int val) { void insert(avlTree *tree, int val) {
tree->root = insertHelper(tree->root, val); tree->root = insertHelper(tree->root, val);
return tree->root;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */ /* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
@ -153,7 +151,7 @@ TreeNode *getInOrderNext(TreeNode *node) {
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
TreeNode *removeHelper(TreeNode *node, int val) { TreeNode *removeHelper(TreeNode *node, int val) {
TreeNode *child, *grandChild, *temp; TreeNode *child, *grandChild;
if (node == NULL) { if (node == NULL) {
return NULL; return NULL;
} }
@ -177,9 +175,13 @@ TreeNode *removeHelper(TreeNode *node, int val) {
} }
} else { } else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
temp = getInOrderNext(node->right); TreeNode *temp = node->right;
while (temp->left != NULL) {
temp = temp->left;
}
int tempVal = temp->val;
node->right = removeHelper(node->right, temp->val); node->right = removeHelper(node->right, temp->val);
node->val = temp->val; node->val = tempVal;
} }
} }
// 更新节点高度 // 更新节点高度
@ -192,9 +194,8 @@ TreeNode *removeHelper(TreeNode *node, int val) {
/* 删除节点 */ /* 删除节点 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词 // 由于引入了 stdio.h ,此处无法使用 remove 关键词
TreeNode *removeNode(avlTree *tree, int val) { void removeNode(avlTree *tree, int val) {
TreeNode *root = removeHelper(tree->root, val); TreeNode *root = removeHelper(tree->root, val);
return root;
} }
/* 查找节点 */ /* 查找节点 */

View File

@ -69,15 +69,16 @@ TreeNode *search(binarySearchTree *bst, int num) {
} }
/* 插入节点 */ /* 插入节点 */
TreeNode *insert(binarySearchTree *bst, int num) { void insert(binarySearchTree *bst, int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (bst->root == NULL) return NULL; if (bst->root == NULL)
return;
TreeNode *cur = bst->root, *pre = NULL; TreeNode *cur = bst->root, *pre = NULL;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != NULL) { while (cur != NULL) {
// 找到重复节点,直接返回 // 找到重复节点,直接返回
if (cur->val == num) { if (cur->val == num) {
return NULL; return;
} }
pre = cur; pre = cur;
if (cur->val < num) { if (cur->val < num) {
@ -95,24 +96,14 @@ TreeNode *insert(binarySearchTree *bst, int num) {
} else { } else {
pre->left = node; pre->left = node;
} }
return node;
}
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
TreeNode *getInOrderNext(TreeNode *root) {
if (root == NULL) return root;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (root->left != NULL) {
root = root->left;
}
return root;
} }
/* 删除节点 */ /* 删除节点 */
// 由于引入了 stdio.h ,此处无法使用 remove 关键词 // 由于引入了 stdio.h ,此处无法使用 remove 关键词
TreeNode *removeNode(binarySearchTree *bst, int num) { void removeNode(binarySearchTree *bst, int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (bst->root == NULL) return NULL; if (bst->root == NULL)
return;
TreeNode *cur = bst->root, *pre = NULL; TreeNode *cur = bst->root, *pre = NULL;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != NULL) { while (cur != NULL) {
@ -128,9 +119,8 @@ TreeNode *removeNode(binarySearchTree *bst, int num) {
} }
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == NULL) { if (cur == NULL)
return NULL; return;
}
// 判断待删除节点是否存在子节点 // 判断待删除节点是否存在子节点
if (cur->left == NULL || cur->right == NULL) { if (cur->left == NULL || cur->right == NULL) {
/* 子节点数量 = 0 or 1 */ /* 子节点数量 = 0 or 1 */
@ -145,14 +135,16 @@ TreeNode *removeNode(binarySearchTree *bst, int num) {
} else { } else {
/* 子节点数量 = 2 */ /* 子节点数量 = 2 */
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
TreeNode *nex = getInOrderNext(cur->right); TreeNode *tmp = cur->right;
int tmp = nex->val; while (tmp->left != NULL) {
// 递归删除节点 nex tmp = tmp->left;
removeNode(bst, nex->val); }
// 将 nex 的值复制给 cur int tmpVal = tmp->val;
cur->val = tmp; // 递归删除节点 tmp
removeNode(bst, tmp->val);
// 用 tmp 覆盖 cur
cur->val = tmpVal;
} }
return cur;
} }
/* Driver Code */ /* Driver Code */

View File

@ -93,17 +93,6 @@ class AVLTree {
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
TreeNode *getInOrderNext(TreeNode *node) {
if (node == nullptr)
return node;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (node->left != nullptr) {
node = node->left;
}
return node;
}
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
TreeNode *removeHelper(TreeNode *node, int val) { TreeNode *removeHelper(TreeNode *node, int val) {
if (node == nullptr) if (node == nullptr)
@ -128,7 +117,10 @@ class AVLTree {
} }
} else { } else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
TreeNode *temp = getInOrderNext(node->right); TreeNode *temp = node->right;
while (temp->left != nullptr) {
temp = temp->left;
}
int tempVal = temp->val; int tempVal = temp->val;
node->right = removeHelper(node->right, temp->val); node->right = removeHelper(node->right, temp->val);
node->val = tempVal; node->val = tempVal;
@ -158,15 +150,13 @@ class AVLTree {
} }
/* 插入节点 */ /* 插入节点 */
TreeNode *insert(int val) { void insert(int val) {
root = insertHelper(root, val); root = insertHelper(root, val);
return root;
} }
/* 删除节点 */ /* 删除节点 */
TreeNode *remove(int val) { void remove(int val) {
root = removeHelper(root, val); root = removeHelper(root, val);
return root;
} }
/* 查找节点 */ /* 查找节点 */
@ -209,6 +199,8 @@ void testRemove(AVLTree &tree, int val) {
cout << "\n删除节点 " << val << "AVL 树为" << endl; cout << "\n删除节点 " << val << "AVL 树为" << endl;
printTree(tree.root); printTree(tree.root);
} }
/* Driver Code */
int main() { int main() {
/* 初始化空 AVL 树 */ /* 初始化空 AVL 树 */
AVLTree avlTree; AVLTree avlTree;

View File

@ -59,16 +59,16 @@ class BinarySearchTree {
} }
/* 插入节点 */ /* 插入节点 */
TreeNode *insert(int num) { void insert(int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == nullptr) if (root == nullptr)
return nullptr; return;
TreeNode *cur = root, *pre = nullptr; TreeNode *cur = root, *pre = nullptr;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != nullptr) { while (cur != nullptr) {
// 找到重复节点,直接返回 // 找到重复节点,直接返回
if (cur->val == num) if (cur->val == num)
return nullptr; return;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur->val < num) if (cur->val < num)
@ -83,14 +83,13 @@ class BinarySearchTree {
pre->right = node; pre->right = node;
else else
pre->left = node; pre->left = node;
return node;
} }
/* 删除节点 */ /* 删除节点 */
TreeNode *remove(int num) { void remove(int num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == nullptr) if (root == nullptr)
return nullptr; return;
TreeNode *cur = root, *pre = nullptr; TreeNode *cur = root, *pre = nullptr;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != nullptr) { while (cur != nullptr) {
@ -107,7 +106,7 @@ class BinarySearchTree {
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == nullptr) if (cur == nullptr)
return nullptr; return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur->left == nullptr || cur->right == nullptr) { if (cur->left == nullptr || cur->right == nullptr) {
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点 // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
@ -123,25 +122,16 @@ class BinarySearchTree {
// 子节点数量 = 2 // 子节点数量 = 2
else { else {
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
TreeNode *nex = getInOrderNext(cur->right); TreeNode *tmp = cur->right;
int tmp = nex->val; while (tmp->left != nullptr) {
// 递归删除节点 nex tmp = tmp->left;
remove(nex->val);
// 将 nex 的值复制给 cur
cur->val = tmp;
} }
return cur; int tmpVal = tmp->val;
// 递归删除节点 tmp
remove(tmp->val);
// 用 tmp 覆盖 cur
cur->val = tmpVal;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
TreeNode *getInOrderNext(TreeNode *root) {
if (root == nullptr)
return root;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (root->left != nullptr) {
root = root->left;
}
return root;
} }
}; };
@ -158,7 +148,7 @@ int main() {
cout << endl << "查找到的节点对象为 " << node << ",节点值 = " << node->val << endl; cout << endl << "查找到的节点对象为 " << node << ",节点值 = " << node->val << endl;
/* 插入节点 */ /* 插入节点 */
node = bst->insert(16); bst->insert(16);
cout << endl << "插入节点 16 后,二叉树为\n" << endl; cout << endl << "插入节点 16 后,二叉树为\n" << endl;
printTree(bst->getRoot()); printTree(bst->getRoot());

View File

@ -107,10 +107,9 @@ class AVLTree
} }
/* 插入节点 */ /* 插入节点 */
public TreeNode? insert(int val) public void insert(int val)
{ {
root = insertHelper(root, val); root = insertHelper(root, val);
return root;
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -132,10 +131,9 @@ class AVLTree
} }
/* 删除节点 */ /* 删除节点 */
public TreeNode? remove(int val) public void remove(int val)
{ {
root = removeHelper(root, val); root = removeHelper(root, val);
return root;
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -162,7 +160,11 @@ class AVLTree
else else
{ {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
TreeNode? temp = getInOrderNext(node.right); TreeNode? temp = node.right;
while (temp.left != null)
{
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val); node.right = removeHelper(node.right, temp.val);
node.val = temp.val; node.val = temp.val;
} }
@ -174,18 +176,6 @@ class AVLTree
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private TreeNode? getInOrderNext(TreeNode? node)
{
if (node == null) return node;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (node.left != null)
{
node = node.left;
}
return node;
}
/* 查找节点 */ /* 查找节点 */
public TreeNode? search(int val) public TreeNode? search(int val)
{ {

View File

@ -57,16 +57,16 @@ class BinarySearchTree
} }
/* 插入节点 */ /* 插入节点 */
public TreeNode? insert(int num) public void insert(int num)
{ {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == null) return null; if (root == null) return;
TreeNode? cur = root, pre = null; TreeNode? cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != null) while (cur != null)
{ {
// 找到重复节点,直接返回 // 找到重复节点,直接返回
if (cur.val == num) return null; if (cur.val == num) return;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num) cur = cur.right;
@ -81,15 +81,14 @@ class BinarySearchTree
if (pre.val < num) pre.right = node; if (pre.val < num) pre.right = node;
else pre.left = node; else pre.left = node;
} }
return node;
} }
/* 删除节点 */ /* 删除节点 */
public TreeNode? remove(int num) public void remove(int num)
{ {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root == null) return null; if (root == null) return;
TreeNode? cur = root, pre = null; TreeNode? cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur != null) while (cur != null)
@ -103,7 +102,7 @@ class BinarySearchTree
else cur = cur.left; else cur = cur.left;
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur == null || pre == null) return null; if (cur == null || pre == null) return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) if (cur.left == null || cur.right == null)
{ {
@ -123,29 +122,16 @@ class BinarySearchTree
else else
{ {
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
TreeNode? nex = getInOrderNext(cur.right); TreeNode? tmp = cur.right;
if (nex != null) while (tmp.left != null)
{ {
int tmp = nex.val; tmp = tmp.left;
// 递归删除节点 nex
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
} }
// 递归删除节点 tmp
remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
} }
return cur;
}
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private TreeNode? getInOrderNext(TreeNode? root)
{
if (root == null) return root;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (root.left != null)
{
root = root.left;
}
return root;
} }
} }
@ -165,7 +151,7 @@ public class binary_search_tree
Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node.val); Console.WriteLine("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
/* 插入节点 */ /* 插入节点 */
node = bst.insert(16); bst.insert(16);
Console.WriteLine("\n插入节点 16 后,二叉树为\n"); Console.WriteLine("\n插入节点 16 后,二叉树为\n");
PrintUtil.PrintTree(bst.getRoot()); PrintUtil.PrintTree(bst.getRoot());

View File

@ -94,9 +94,8 @@ class AVLTree {
} }
/* 插入节点 */ /* 插入节点 */
TreeNode? insert(int val) { void insert(int val) {
root = insertHelper(root, val); root = insertHelper(root, val);
return root;
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -117,9 +116,8 @@ class AVLTree {
} }
/* 删除节点 */ /* 删除节点 */
TreeNode? remove(int val) { void remove(int val) {
root = removeHelper(root, val); root = removeHelper(root, val);
return root;
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -141,7 +139,10 @@ class AVLTree {
node = child; node = child;
} else { } else {
// = 2 // = 2
TreeNode? temp = getInOrderNext(node.right); TreeNode? temp = node.right;
while (temp!.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp!.val); node.right = removeHelper(node.right, temp!.val);
node.val = temp.val; node.val = temp.val;
} }
@ -153,16 +154,6 @@ class AVLTree {
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
TreeNode? getInOrderNext(TreeNode? node) {
if (node == null) return node;
// 访
while (node!.left != null) {
node = node.left;
}
return node;
}
/* 查找节点 */ /* 查找节点 */
TreeNode? search(int val) { TreeNode? search(int val) {
TreeNode? cur = root; TreeNode? cur = root;

View File

@ -53,15 +53,15 @@ TreeNode? search(int num) {
} }
/* 插入节点 */ /* 插入节点 */
TreeNode? insert(int num) { void insert(int num) {
// //
if (root == null) return null; if (root == null) return;
TreeNode? cur = root; TreeNode? cur = root;
TreeNode? pre = null; TreeNode? pre = null;
// //
while (cur != null) { while (cur != null) {
// //
if (cur.val == num) return null; if (cur.val == num) return;
pre = cur; pre = cur;
// cur // cur
if (cur.val < num) if (cur.val < num)
@ -76,13 +76,12 @@ TreeNode? insert(int num) {
pre.right = node; pre.right = node;
else else
pre.left = node; pre.left = node;
return node;
} }
/* 删除节点 */ /* 删除节点 */
TreeNode? remove(int num) { void remove(int num) {
// //
if (root == null) return null; if (root == null) return;
TreeNode? cur = root; TreeNode? cur = root;
TreeNode? pre = null; TreeNode? pre = null;
@ -99,7 +98,7 @@ TreeNode? remove(int num) {
cur = cur.left; cur = cur.left;
} }
// //
if (cur == null) return null; if (cur == null) return;
// = 0 or 1 // = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {
// = 0 / 1 child = null / // = 0 / 1 child = null /
@ -112,24 +111,15 @@ TreeNode? remove(int num) {
} else { } else {
// = 2 // = 2
// cur // cur
TreeNode? nex = getInOrderNext(cur.right); TreeNode? tmp = cur.right;
int tem = nex!.val; while (tmp!.left != null) {
// nex tmp = tmp.left;
remove(nex.val);
// nex cur
cur.val = tem;
} }
return cur; // tmp
remove(tmp.val);
// tmp cur
cur.val = tmp.val;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
TreeNode? getInOrderNext(TreeNode? root) {
if (root == null) return null;
// 访
while (root!.left != null) {
root = root.left;
}
return root;
} }
/* Driver Code */ /* Driver Code */

View File

@ -107,9 +107,8 @@ func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
} }
/* 插入节点 */ /* 插入节点 */
func (t *aVLTree) insert(val int) *TreeNode { func (t *aVLTree) insert(val int) {
t.root = t.insertHelper(t.root, val) t.root = t.insertHelper(t.root, val)
return t.root
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -135,9 +134,8 @@ func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
} }
/* 删除节点 */ /* 删除节点 */
func (t *aVLTree) remove(val int) *TreeNode { func (t *aVLTree) remove(val int) {
root := t.removeHelper(t.root, val) t.root = t.removeHelper(t.root, val)
return root
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -156,8 +154,8 @@ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
if node.Right != nil { if node.Right != nil {
child = node.Right child = node.Right
} }
// 子节点数量 = 0 ,直接删除 node 并返回
if child == nil { if child == nil {
// 子节点数量 = 0 ,直接删除 node 并返回
return nil return nil
} else { } else {
// 子节点数量 = 1 ,直接删除 node // 子节点数量 = 1 ,直接删除 node
@ -165,7 +163,10 @@ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
} }
} else { } else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
temp := t.getInOrderNext(node.Right) temp := node.Right
for temp.Left != nil {
temp = temp.Left
}
node.Right = t.removeHelper(node.Right, temp.Val) node.Right = t.removeHelper(node.Right, temp.Val)
node.Val = temp.Val node.Val = temp.Val
} }
@ -178,18 +179,6 @@ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
return node return node
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
func (t *aVLTree) getInOrderNext(node *TreeNode) *TreeNode {
if node == nil {
return node
}
// 循环访问左子节点,直到叶节点时为最小节点,跳出
for node.Left != nil {
node = node.Left
}
return node
}
/* 查找节点 */ /* 查找节点 */
func (t *aVLTree) search(val int) *TreeNode { func (t *aVLTree) search(val int) *TreeNode {
cur := t.root cur := t.root

View File

@ -42,18 +42,6 @@ func (bst *binarySearchTree) getRoot() *TreeNode {
return bst.root return bst.root
} }
/* 获取中序遍历的下一个节点(仅适用于 root 有左子节点的情况) */
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
if node == nil {
return node
}
// 循环访问左子节点,直到叶节点时为最小节点,跳出
for node.Left != nil {
node = node.Left
}
return node
}
/* 查找节点 */ /* 查找节点 */
func (bst *binarySearchTree) search(num int) *TreeNode { func (bst *binarySearchTree) search(num int) *TreeNode {
node := bst.root node := bst.root
@ -75,18 +63,18 @@ func (bst *binarySearchTree) search(num int) *TreeNode {
} }
/* 插入节点 */ /* 插入节点 */
func (bst *binarySearchTree) insert(num int) *TreeNode { func (bst *binarySearchTree) insert(num int) {
cur := bst.root cur := bst.root
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if cur == nil { if cur == nil {
return nil return
} }
// 待插入节点之前的节点位置 // 待插入节点之前的节点位置
var pre *TreeNode = nil var pre *TreeNode = nil
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
for cur != nil { for cur != nil {
if cur.Val == num { if cur.Val == num {
return nil return
} }
pre = cur pre = cur
if cur.Val < num { if cur.Val < num {
@ -102,15 +90,14 @@ func (bst *binarySearchTree) insert(num int) *TreeNode {
} else { } else {
pre.Left = node pre.Left = node
} }
return cur
} }
/* 删除节点 */ /* 删除节点 */
func (bst *binarySearchTree) remove(num int) *TreeNode { func (bst *binarySearchTree) remove(num int) {
cur := bst.root cur := bst.root
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if cur == nil { if cur == nil {
return nil return
} }
// 待删除节点之前的节点位置 // 待删除节点之前的节点位置
var pre *TreeNode = nil var pre *TreeNode = nil
@ -130,7 +117,7 @@ func (bst *binarySearchTree) remove(num int) *TreeNode {
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if cur == nil { if cur == nil {
return nil return
} }
// 子节点数为 0 或 1 // 子节点数为 0 或 1
if cur.Left == nil || cur.Right == nil { if cur.Left == nil || cur.Right == nil {
@ -150,14 +137,15 @@ func (bst *binarySearchTree) remove(num int) *TreeNode {
// 子节点数为 2 // 子节点数为 2
} else { } else {
// 获取中序遍历中待删除节点 cur 的下一个节点 // 获取中序遍历中待删除节点 cur 的下一个节点
next := bst.getInOrderNext(cur) tmp := cur.Right
temp := next.Val for tmp.Left != nil {
// 递归删除节点 next tmp = tmp.Left
bst.remove(next.Val) }
// 将 next 的值复制给 cur // 递归删除节点 tmp
cur.Val = temp bst.remove(tmp.Val)
// 用 tmp 覆盖 cur
cur.Val = tmp.Val
} }
return cur
} }
/* 打印二叉搜索树 */ /* 打印二叉搜索树 */

View File

@ -24,7 +24,7 @@ func TestBinarySearchTree(t *testing.T) {
fmt.Println("查找到的节点对象为", node, ",节点值 =", node.Val) fmt.Println("查找到的节点对象为", node, ",节点值 =", node.Val)
// 插入节点 // 插入节点
node = bst.insert(16) bst.insert(16)
fmt.Println("\n插入节点后 16 的二叉树为:") fmt.Println("\n插入节点后 16 的二叉树为:")
bst.print() bst.print()

View File

@ -92,9 +92,8 @@ class AVLTree {
} }
/* 插入节点 */ /* 插入节点 */
public TreeNode insert(int val) { public void insert(int val) {
root = insertHelper(root, val); root = insertHelper(root, val);
return root;
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -116,9 +115,8 @@ class AVLTree {
} }
/* 删除节点 */ /* 删除节点 */
public TreeNode remove(int val) { public void remove(int val) {
root = removeHelper(root, val); root = removeHelper(root, val);
return root;
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -141,7 +139,10 @@ class AVLTree {
node = child; node = child;
} else { } else {
// 子节点数量 = 2 则将中序遍历的下个节点删除并用该节点替换当前节点 // 子节点数量 = 2 则将中序遍历的下个节点删除并用该节点替换当前节点
TreeNode temp = getInOrderNext(node.right); TreeNode temp = node.right;
while (temp.left != null) {
temp = temp.left;
}
node.right = removeHelper(node.right, temp.val); node.right = removeHelper(node.right, temp.val);
node.val = temp.val; node.val = temp.val;
} }
@ -153,17 +154,6 @@ class AVLTree {
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private TreeNode getInOrderNext(TreeNode node) {
if (node == null)
return node;
// 循环访问左子节点直到叶节点时为最小节点跳出
while (node.left != null) {
node = node.left;
}
return node;
}
/* 查找节点 */ /* 查找节点 */
public TreeNode search(int val) { public TreeNode search(int val) {
TreeNode cur = root; TreeNode cur = root;

View File

@ -56,16 +56,16 @@ class BinarySearchTree {
} }
/* 插入节点 */ /* 插入节点 */
public TreeNode insert(int num) { public void insert(int num) {
// 若树为空直接提前返回 // 若树为空直接提前返回
if (root == null) if (root == null)
return null; return;
TreeNode cur = root, pre = null; TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出 // 循环查找越过叶节点后跳出
while (cur != null) { while (cur != null) {
// 找到重复节点直接返回 // 找到重复节点直接返回
if (cur.val == num) if (cur.val == num)
return null; return;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur.val < num) if (cur.val < num)
@ -80,14 +80,13 @@ class BinarySearchTree {
pre.right = node; pre.right = node;
else else
pre.left = node; pre.left = node;
return node;
} }
/* 删除节点 */ /* 删除节点 */
public TreeNode remove(int num) { public void remove(int num) {
// 若树为空直接提前返回 // 若树为空直接提前返回
if (root == null) if (root == null)
return null; return;
TreeNode cur = root, pre = null; TreeNode cur = root, pre = null;
// 循环查找越过叶节点后跳出 // 循环查找越过叶节点后跳出
while (cur != null) { while (cur != null) {
@ -104,7 +103,7 @@ class BinarySearchTree {
} }
// 若无待删除节点则直接返回 // 若无待删除节点则直接返回
if (cur == null) if (cur == null)
return null; return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) { if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 child = null / 该子节点 // 当子节点数量 = 0 / 1 child = null / 该子节点
@ -118,14 +117,15 @@ class BinarySearchTree {
// 子节点数量 = 2 // 子节点数量 = 2
else { else {
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
TreeNode nex = getInOrderNext(cur.right); TreeNode tmp = cur.right;
int tmp = nex.val; while (tmp.left != null) {
// 递归删除节点 nex tmp = tmp.left;
remove(nex.val); }
// nex 的值复制给 cur // 递归删除节点 tmp
cur.val = tmp; remove(tmp.val);
// tmp 覆盖 cur
cur.val = tmp.val;
} }
return cur;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */ /* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
@ -153,7 +153,7 @@ public class binary_search_tree {
System.out.println("\n查找到的节点对象为 " + node + ",节点值 = " + node.val); System.out.println("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
/* 插入节点 */ /* 插入节点 */
node = bst.insert(16); bst.insert(16);
System.out.println("\n插入节点 16 后,二叉树为\n"); System.out.println("\n插入节点 16 后,二叉树为\n");
PrintUtil.printTree(bst.getRoot()); PrintUtil.printTree(bst.getRoot());

View File

@ -95,7 +95,6 @@ class AVLTree {
/* 插入节点 */ /* 插入节点 */
insert(val) { insert(val) {
this.root = this.#insertHelper(this.root, val); this.root = this.#insertHelper(this.root, val);
return this.root;
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -115,7 +114,6 @@ class AVLTree {
/* 删除节点 */ /* 删除节点 */
remove(val) { remove(val) {
this.root = this.#removeHelper(this.root, val); this.root = this.#removeHelper(this.root, val);
return this.root;
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -133,7 +131,10 @@ class AVLTree {
else node = child; else node = child;
} else { } else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
const temp = this.#getInOrderNext(node.right); let temp = node.right;
while (temp.left !== null) {
temp = temp.left;
}
node.right = this.#removeHelper(node.right, temp.val); node.right = this.#removeHelper(node.right, temp.val);
node.val = temp.val; node.val = temp.val;
} }
@ -145,16 +146,6 @@ class AVLTree {
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
#getInOrderNext(node) {
if (node === null) return node;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (node.left !== null) {
node = node.left;
}
return node;
}
/* 查找节点 */ /* 查找节点 */
search(val) { search(val) {
let cur = this.root; let cur = this.root;

View File

@ -51,12 +51,12 @@ function search(num) {
/* 插入节点 */ /* 插入节点 */
function insert(num) { function insert(num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root === null) return null; if (root === null) return;
let cur = root, pre = null; let cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur !== null) { while (cur !== null) {
// 找到重复节点,直接返回 // 找到重复节点,直接返回
if (cur.val === num) return null; if (cur.val === num) return;
pre = cur; pre = cur;
// 插入位置在 cur 的右子树中 // 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right; if (cur.val < num) cur = cur.right;
@ -67,13 +67,12 @@ function insert(num) {
let node = new TreeNode(num); let node = new TreeNode(num);
if (pre.val < num) pre.right = node; if (pre.val < num) pre.right = node;
else pre.left = node; else pre.left = node;
return node;
} }
/* 删除节点 */ /* 删除节点 */
function remove(num) { function remove(num) {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root === null) return null; if (root === null) return;
let cur = root, pre = null; let cur = root, pre = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur !== null) { while (cur !== null) {
@ -86,7 +85,7 @@ function remove(num) {
else cur = cur.left; else cur = cur.left;
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur === null) return null; if (cur === null) return;
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left === null || cur.right === null) { if (cur.left === null || cur.right === null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点 // 当子节点数量 = 0 / 1 时, child = null / 该子节点
@ -98,24 +97,15 @@ function remove(num) {
// 子节点数量 = 2 // 子节点数量 = 2
else { else {
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
let nex = getInOrderNext(cur.right); let tmp = cur.right;
let tmp = nex.val; while (tmp.left !== null) {
// 递归删除节点 nex tmp = tmp.left;
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
} }
return cur; // 递归删除节点 tmp
remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
function getInOrderNext(root) {
if (root === null) return root;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (root.left !== null) {
root = root.left;
}
return root;
} }
/* Driver Code */ /* Driver Code */
@ -130,7 +120,7 @@ let node = search(7);
console.log("\n查找到的节点对象为 " + node + ",节点值 = " + node.val); console.log("\n查找到的节点对象为 " + node + ",节点值 = " + node.val);
/* 插入节点 */ /* 插入节点 */
node = insert(16); insert(16);
console.log("\n插入节点 16 后,二叉树为\n"); console.log("\n插入节点 16 后,二叉树为\n");
printTree(getRoot()); printTree(getRoot());

View File

@ -92,10 +92,9 @@ class AVLTree:
# 平衡树,无需旋转,直接返回 # 平衡树,无需旋转,直接返回
return node return node
def insert(self, val) -> TreeNode: def insert(self, val) -> None:
"""插入节点""" """插入节点"""
self.__root = self.__insert_helper(self.__root, val) self.__root = self.__insert_helper(self.__root, val)
return self.__root
def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode: def __insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:
"""递归插入节点(辅助方法)""" """递归插入节点(辅助方法)"""
@ -114,10 +113,9 @@ class AVLTree:
# 2. 执行旋转操作,使该子树重新恢复平衡 # 2. 执行旋转操作,使该子树重新恢复平衡
return self.__rotate(node) return self.__rotate(node)
def remove(self, val: int) -> TreeNode | None: def remove(self, val: int) -> None:
"""删除节点""" """删除节点"""
self.__root = self.__remove_helper(self.__root, val) self.__root = self.__remove_helper(self.__root, val)
return self.__root
def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None: def __remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:
"""递归删除节点(辅助方法)""" """递归删除节点(辅助方法)"""
@ -137,8 +135,11 @@ class AVLTree:
# 子节点数量 = 1 ,直接删除 node # 子节点数量 = 1 ,直接删除 node
else: else:
node = child node = child
else: # 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 else:
temp = self.__get_inorder_next(node.right) # 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
temp = node.right
while temp.left is not None:
temp = temp.left
node.right = self.__remove_helper(node.right, temp.val) node.right = self.__remove_helper(node.right, temp.val)
node.val = temp.val node.val = temp.val
# 更新节点高度 # 更新节点高度
@ -146,15 +147,6 @@ class AVLTree:
# 2. 执行旋转操作,使该子树重新恢复平衡 # 2. 执行旋转操作,使该子树重新恢复平衡
return self.__rotate(node) return self.__rotate(node)
def __get_inorder_next(self, node: TreeNode | None) -> TreeNode | None:
"""获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)"""
if node is None:
return None
# 循环访问左子节点,直到叶节点时为最小节点,跳出
while node.left is not None:
node = node.left
return node
def search(self, val: int) -> TreeNode | None: def search(self, val: int) -> TreeNode | None:
"""查找节点""" """查找节点"""
cur = self.__root cur = self.__root

View File

@ -57,18 +57,18 @@ class BinarySearchTree:
break break
return cur return cur
def insert(self, num: int) -> TreeNode | None: def insert(self, num: int) -> None:
"""插入节点""" """插入节点"""
# 若树为空,直接提前返回 # 若树为空,直接提前返回
if self.__root is None: if self.__root is None:
return None return
# 循环查找,越过叶节点后跳出 # 循环查找,越过叶节点后跳出
cur, pre = self.__root, None cur, pre = self.__root, None
while cur is not None: while cur is not None:
# 找到重复节点,直接返回 # 找到重复节点,直接返回
if cur.val == num: if cur.val == num:
return None return
pre = cur pre = cur
# 插入位置在 cur 的右子树中 # 插入位置在 cur 的右子树中
if cur.val < num: if cur.val < num:
@ -83,13 +83,12 @@ class BinarySearchTree:
pre.right = node pre.right = node
else: else:
pre.left = node pre.left = node
return node
def remove(self, num: int) -> TreeNode | None: def remove(self, num: int) -> None:
"""删除节点""" """删除节点"""
# 若树为空,直接提前返回 # 若树为空,直接提前返回
if self.__root is None: if self.__root is None:
return None return
# 循环查找,越过叶节点后跳出 # 循环查找,越过叶节点后跳出
cur, pre = self.__root, None cur, pre = self.__root, None
@ -98,13 +97,15 @@ class BinarySearchTree:
if cur.val == num: if cur.val == num:
break break
pre = cur pre = cur
if cur.val < num: # 待删除节点在 cur 的右子树中 # 待删除节点在 cur 的右子树中
if cur.val < num:
cur = cur.right cur = cur.right
else: # 待删除节点在 cur 的左子树中 # 待删除节点在 cur 的左子树中
else:
cur = cur.left cur = cur.left
# 若无待删除节点,则直接返回 # 若无待删除节点,则直接返回
if cur is None: if cur is None:
return None return
# 子节点数量 = 0 or 1 # 子节点数量 = 0 or 1
if cur.left is None or cur.right is None: if cur.left is None or cur.right is None:
@ -118,22 +119,13 @@ class BinarySearchTree:
# 子节点数量 = 2 # 子节点数量 = 2
else: else:
# 获取中序遍历中 cur 的下一个节点 # 获取中序遍历中 cur 的下一个节点
nex: TreeNode = self.get_inorder_next(cur.right) tmp: TreeNode = cur.right
tmp: int = nex.val while tmp.left is not None:
# 递归删除节点 nex tmp = tmp.left
self.remove(nex.val) # 递归删除节点 tmp
# 将 nex 的值复制给 cur self.remove(tmp.val)
cur.val = tmp # 用 tmp 覆盖 cur
return cur cur.val = tmp.val
def get_inorder_next(self, root: TreeNode | None) -> TreeNode | None:
"""获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)"""
if root is None:
return root
# 循环访问左子节点,直到叶节点时为最小节点,跳出
while root.left is not None:
root = root.left
return root
"""Driver Code""" """Driver Code"""
@ -149,7 +141,7 @@ if __name__ == "__main__":
print("\n查找到的节点对象为: {},节点值 = {}".format(node, node.val)) print("\n查找到的节点对象为: {},节点值 = {}".format(node, node.val))
# 插入节点 # 插入节点
node = bst.insert(16) bst.insert(16)
print("\n插入节点 16 后,二叉树为\n") print("\n插入节点 16 后,二叉树为\n")
print_tree(bst.root) print_tree(bst.root)

View File

@ -90,9 +90,8 @@ class AVLTree {
/* */ /* */
@discardableResult @discardableResult
func insert(val: Int) -> TreeNode? { func insert(val: Int) {
root = insertHelper(node: root, val: val) root = insertHelper(node: root, val: val)
return root
} }
/* */ /* */
@ -118,9 +117,8 @@ class AVLTree {
/* */ /* */
@discardableResult @discardableResult
func remove(val: Int) -> TreeNode? { func remove(val: Int) {
root = removeHelper(node: root, val: val) root = removeHelper(node: root, val: val)
return root
} }
/* */ /* */
@ -147,7 +145,10 @@ class AVLTree {
} }
} else { } else {
// = 2 // = 2
let temp = getInOrderNext(node: node?.right) let temp = node?.right
while temp?.left != nil {
temp = temp?.left
}
node?.right = removeHelper(node: node?.right, val: temp!.val) node?.right = removeHelper(node: node?.right, val: temp!.val)
node?.val = temp!.val node?.val = temp!.val
} }
@ -159,19 +160,6 @@ class AVLTree {
return node return node
} }
/* root */
private func getInOrderNext(node: TreeNode?) -> TreeNode? {
var node = node
if node == nil {
return node
}
// 访
while node?.left != nil {
node = node?.left
}
return node
}
/* */ /* */
func search(val: Int) -> TreeNode? { func search(val: Int) -> TreeNode? {
var cur = root var cur = root

View File

@ -57,10 +57,10 @@ class BinarySearchTree {
} }
/* */ /* */
func insert(num: Int) -> TreeNode? { func insert(num: Int) {
// //
if root == nil { if root == nil {
return nil return
} }
var cur = root var cur = root
var pre: TreeNode? var pre: TreeNode?
@ -68,7 +68,7 @@ class BinarySearchTree {
while cur != nil { while cur != nil {
// //
if cur!.val == num { if cur!.val == num {
return nil return
} }
pre = cur pre = cur
// cur // cur
@ -87,15 +87,14 @@ class BinarySearchTree {
} else { } else {
pre?.left = node pre?.left = node
} }
return node
} }
/* */ /* */
@discardableResult @discardableResult
func remove(num: Int) -> TreeNode? { func remove(num: Int) {
// //
if root == nil { if root == nil {
return nil return
} }
var cur = root var cur = root
var pre: TreeNode? var pre: TreeNode?
@ -117,7 +116,7 @@ class BinarySearchTree {
} }
// //
if cur == nil { if cur == nil {
return nil return
} }
// = 0 or 1 // = 0 or 1
if cur?.left == nil || cur?.right == nil { if cur?.left == nil || cur?.right == nil {
@ -133,27 +132,15 @@ class BinarySearchTree {
// = 2 // = 2
else { else {
// cur // cur
let nex = getInOrderNext(root: cur?.right) let tmp = cur?.right
let tmp = nex!.val while tmp?.left != nil {
// nex tmp = tmp?.left
remove(num: nex!.val)
// nex cur
cur?.val = tmp
} }
return cur // tmp
remove(num: tmp!.val)
// tmp cur
cur?.val = tmp!.val
} }
/* root */
func getInOrderNext(root: TreeNode?) -> TreeNode? {
var root = root
if root == nil {
return root
}
// 访
while root?.left != nil {
root = root?.left
}
return root
} }
} }
@ -172,7 +159,7 @@ enum _BinarySearchTree {
print("\n查找到的节点对象为 \(node!),节点值 = \(node!.val)") print("\n查找到的节点对象为 \(node!),节点值 = \(node!.val)")
/* */ /* */
node = bst.insert(num: 16) bst.insert(num: 16)
print("\n插入节点 16 后,二叉树为\n") print("\n插入节点 16 后,二叉树为\n")
PrintUtil.printTree(root: bst.getRoot()) PrintUtil.printTree(root: bst.getRoot())

View File

@ -94,9 +94,8 @@ class AVLTree {
} }
/* 插入节点 */ /* 插入节点 */
insert(val: number): TreeNode { insert(val: number): void {
this.root = this.insertHelper(this.root, val); this.root = this.insertHelper(this.root, val);
return this.root;
} }
/* 递归插入节点(辅助方法) */ /* 递归插入节点(辅助方法) */
@ -118,9 +117,8 @@ class AVLTree {
} }
/* 删除节点 */ /* 删除节点 */
remove(val: number): TreeNode { remove(val: number): void {
this.root = this.removeHelper(this.root, val); this.root = this.removeHelper(this.root, val);
return this.root;
} }
/* 递归删除节点(辅助方法) */ /* 递归删除节点(辅助方法) */
@ -143,7 +141,10 @@ class AVLTree {
} }
} else { } else {
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点 // 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
const temp = this.getInOrderNext(node.right); let temp = node.right;
while (temp.left !== null) {
temp = temp.left;
}
node.right = this.removeHelper(node.right, temp.val); node.right = this.removeHelper(node.right, temp.val);
node.val = temp.val; node.val = temp.val;
} }
@ -155,16 +156,6 @@ class AVLTree {
return node; return node;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
private getInOrderNext(node: TreeNode): TreeNode {
if (node === null) return node;
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (node.left !== null) {
node = node.left;
}
return node;
}
/* 查找节点 */ /* 查找节点 */
search(val: number): TreeNode { search(val: number): TreeNode {
let cur = this.root; let cur = this.root;

View File

@ -52,17 +52,17 @@ function search(num: number): TreeNode | null {
} }
/* 插入节点 */ /* 插入节点 */
function insert(num: number): TreeNode | null { function insert(num: number): void {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root === null) { if (root === null) {
return null; return;
} }
let cur = root, let cur = root,
pre: TreeNode | null = null; pre: TreeNode | null = null;
// 循环查找,越过叶节点后跳出 // 循环查找,越过叶节点后跳出
while (cur !== null) { while (cur !== null) {
if (cur.val === num) { if (cur.val === num) {
return null; // 找到重复节点,直接返回 return; // 找到重复节点,直接返回
} }
pre = cur; pre = cur;
if (cur.val < num) { if (cur.val < num) {
@ -78,14 +78,13 @@ function insert(num: number): TreeNode | null {
} else { } else {
pre!.left = node; pre!.left = node;
} }
return node;
} }
/* 删除节点 */ /* 删除节点 */
function remove(num: number): TreeNode | null { function remove(num: number): void {
// 若树为空,直接提前返回 // 若树为空,直接提前返回
if (root === null) { if (root === null) {
return null; return;
} }
let cur = root, let cur = root,
pre: TreeNode | null = null; pre: TreeNode | null = null;
@ -104,7 +103,7 @@ function remove(num: number): TreeNode | null {
} }
// 若无待删除节点,则直接返回 // 若无待删除节点,则直接返回
if (cur === null) { if (cur === null) {
return null; return;
} }
// 子节点数量 = 0 or 1 // 子节点数量 = 0 or 1
if (cur.left === null || cur.right === null) { if (cur.left === null || cur.right === null) {
@ -120,26 +119,15 @@ function remove(num: number): TreeNode | null {
// 子节点数量 = 2 // 子节点数量 = 2
else { else {
// 获取中序遍历中 cur 的下一个节点 // 获取中序遍历中 cur 的下一个节点
let next = getInOrderNext(cur.right); let tmp = cur.right;
let tmp = next!.val; while (tmp.left !== null) {
// 递归删除节点 nex tmp = tmp.left;
remove(next!.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
} }
return cur; // 递归删除节点 tmp
remove(tmp!.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
} }
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
function getInOrderNext(root: TreeNode | null): TreeNode | null {
if (root === null) {
return null;
}
// 循环访问左子节点,直到叶节点时为最小节点,跳出
while (root.left !== null) {
root = root.left;
}
return root;
} }
/* Driver Code */ /* Driver Code */
@ -154,7 +142,7 @@ let node = search(7);
console.log('\n查找到的节点对象为 ' + node + ',节点值 = ' + node!.val); console.log('\n查找到的节点对象为 ' + node + ',节点值 = ' + node!.val);
/* 插入节点 */ /* 插入节点 */
node = insert(16); insert(16);
console.log('\n插入节点 16 后,二叉树为\n'); console.log('\n插入节点 16 后,二叉树为\n');
printTree(getRoot()); printTree(getRoot());

View File

@ -108,9 +108,8 @@ pub fn AVLTree(comptime T: type) type {
} }
// //
fn insert(self: *Self, val: T) !?*inc.TreeNode(T) { fn insert(self: *Self, val: T) void {
self.root = try self.insertHelper(self.root, val); self.root = try self.insertHelper(self.root, val);
return self.root;
} }
// //
@ -137,9 +136,8 @@ pub fn AVLTree(comptime T: type) type {
} }
// //
fn remove(self: *Self, val: T) ?*inc.TreeNode(T) { fn remove(self: *Self, val: T) void {
self.root = self.removeHelper(self.root, val); self.root = self.removeHelper(self.root, val);
return self.root;
} }
// //
@ -163,7 +161,10 @@ pub fn AVLTree(comptime T: type) type {
} }
} else { } else {
// = 2 // = 2
var temp = self.getInOrderNext(node.?.right); var temp = node.?.right;
while (temp.?.left != null) {
temp = temp.?.left;
}
node.?.right = self.removeHelper(node.?.right, temp.?.val); node.?.right = self.removeHelper(node.?.right, temp.?.val);
node.?.val = temp.?.val; node.?.val = temp.?.val;
} }
@ -175,18 +176,6 @@ pub fn AVLTree(comptime T: type) type {
return node; return node;
} }
// root
fn getInOrderNext(self: *Self, node_: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
_ = self;
var node = node_;
if (node == null) return node;
// 访
while (node.?.left != null) {
node = node.?.left;
}
return node;
}
// //
fn search(self: *Self, val: T) ?*inc.TreeNode(T) { fn search(self: *Self, val: T) ?*inc.TreeNode(T) {
var cur = self.root; var cur = self.root;

View File

@ -69,15 +69,15 @@ pub fn BinarySearchTree(comptime T: type) type {
} }
// //
fn insert(self: *Self, num: T) !?*inc.TreeNode(T) { fn insert(self: *Self, num: T) !void {
// //
if (self.root == null) return null; if (self.root == null) return;
var cur = self.root; var cur = self.root;
var pre: ?*inc.TreeNode(T) = null; var pre: ?*inc.TreeNode(T) = null;
// //
while (cur != null) { while (cur != null) {
// //
if (cur.?.val == num) return null; if (cur.?.val == num) return;
pre = cur; pre = cur;
// cur // cur
if (cur.?.val < num) { if (cur.?.val < num) {
@ -95,13 +95,12 @@ pub fn BinarySearchTree(comptime T: type) type {
} else { } else {
pre.?.left = node; pre.?.left = node;
} }
return node;
} }
// //
fn remove(self: *Self, num: T) ?*inc.TreeNode(T) { fn remove(self: *Self, num: T) !void {
// //
if (self.root == null) return null; if (self.root == null) return;
var cur = self.root; var cur = self.root;
var pre: ?*inc.TreeNode(T) = null; var pre: ?*inc.TreeNode(T) = null;
// //
@ -118,7 +117,7 @@ pub fn BinarySearchTree(comptime T: type) type {
} }
} }
// //
if (cur == null) return null; if (cur == null) return;
// = 0 or 1 // = 0 or 1
if (cur.?.left == null or cur.?.right == null) { if (cur.?.left == null or cur.?.right == null) {
// = 0 / 1 child = null / // = 0 / 1 child = null /
@ -132,26 +131,16 @@ pub fn BinarySearchTree(comptime T: type) type {
// = 2 // = 2
} else { } else {
// cur // cur
var nex = self.getInOrderNext(cur.?.right); var tmp = cur.?.right;
var tmp = nex.?.val; while (tmp.?.left != null) {
// nex tmp = tmp.?.left;
_ = self.remove(nex.?.val);
// nex cur
cur.?.val = tmp;
} }
return cur; var tmpVal = tmp.?.val;
// tmp
_ = self.remove(tmp.?.val);
// tmp cur
cur.?.val = tmpVal;
} }
// root
fn getInOrderNext(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
_ = self;
var node_tmp = node;
if (node_tmp == null) return null;
// 访
while (node_tmp.?.left != null) {
node_tmp = node_tmp.?.left;
}
return node_tmp;
} }
}; };
} }

View File

@ -180,9 +180,9 @@
当待删除节点的子节点数量 $= 2$ 时,删除操作分为三步: 当待删除节点的子节点数量 $= 2$ 时,删除操作分为三步:
1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 nex 1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 `tmp`
2. 在树中递归删除节点 `nex` 2. 在树中递归删除节点 `tmp`
3. 使用 `nex` 替换待删除节点 3. `tmp` 的值覆盖待删除节点的值
=== "<1>" === "<1>"
![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png) ![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png)