mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	fix(tree): add c codes
This commit is contained in:
		@ -201,8 +201,8 @@ TreeNode *search(avlTree *tree, int val) {
 | 
				
			|||||||
    TreeNode *cur = tree->root;
 | 
					    TreeNode *cur = tree->root;
 | 
				
			||||||
    // 循环查找,越过叶结点后跳出
 | 
					    // 循环查找,越过叶结点后跳出
 | 
				
			||||||
    while (cur != NULL) {
 | 
					    while (cur != NULL) {
 | 
				
			||||||
        // 目标结点在 root 的右子树中
 | 
					 | 
				
			||||||
        if (cur->val < val) {
 | 
					        if (cur->val < val) {
 | 
				
			||||||
 | 
					            // 目标结点在 root 的右子树中
 | 
				
			||||||
            cur = cur->right;
 | 
					            cur = cur->right;
 | 
				
			||||||
        } else if (cur->val > val) {
 | 
					        } else if (cur->val > val) {
 | 
				
			||||||
            // 目标结点在 root 的左子树中
 | 
					            // 目标结点在 root 的左子树中
 | 
				
			||||||
 | 
				
			|||||||
@ -54,11 +54,15 @@ TreeNode *search(binarySearchTree *bst, int num) {
 | 
				
			|||||||
    // 循环查找,越过叶结点后跳出
 | 
					    // 循环查找,越过叶结点后跳出
 | 
				
			||||||
    while (cur != NULL) {
 | 
					    while (cur != NULL) {
 | 
				
			||||||
        // 目标结点在 root 的右子树中
 | 
					        // 目标结点在 root 的右子树中
 | 
				
			||||||
        if (cur->val < num) cur = cur->right;
 | 
					        if (cur->val < num) {
 | 
				
			||||||
 | 
					            cur = cur->right;
 | 
				
			||||||
 | 
					        } else if (cur->val > num) {
 | 
				
			||||||
            // 目标结点在 root 的左子树中
 | 
					            // 目标结点在 root 的左子树中
 | 
				
			||||||
        else if (cur->val > num) cur = cur->left;
 | 
					            cur = cur->left;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
            // 找到目标结点,跳出循环
 | 
					            // 找到目标结点,跳出循环
 | 
				
			||||||
        else break;
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // 返回目标结点
 | 
					    // 返回目标结点
 | 
				
			||||||
    return cur;
 | 
					    return cur;
 | 
				
			||||||
@ -72,17 +76,25 @@ TreeNode *insert(binarySearchTree *bst, int num) {
 | 
				
			|||||||
    // 循环查找,越过叶结点后跳出
 | 
					    // 循环查找,越过叶结点后跳出
 | 
				
			||||||
    while (cur != NULL) {
 | 
					    while (cur != NULL) {
 | 
				
			||||||
        // 找到重复结点,直接返回
 | 
					        // 找到重复结点,直接返回
 | 
				
			||||||
        if (cur->val == num) return NULL;
 | 
					        if (cur->val == num) {
 | 
				
			||||||
 | 
					            return NULL;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        pre = cur;
 | 
					        pre = cur;
 | 
				
			||||||
        // 插入位置在 root 的右子树中
 | 
					        if (cur->val < num) {
 | 
				
			||||||
        if (cur->val < num) cur = cur->right;
 | 
					            // 插入位置在 root 的右子树中
 | 
				
			||||||
 | 
					            cur = cur->right;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
            // 插入位置在 root 的左子树中
 | 
					            // 插入位置在 root 的左子树中
 | 
				
			||||||
        else cur = cur->left;
 | 
					            cur = cur->left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // 插入结点 val
 | 
					    // 插入结点 val
 | 
				
			||||||
    TreeNode *node = newTreeNode(num);
 | 
					    TreeNode *node = newTreeNode(num);
 | 
				
			||||||
    if (pre->val < num) pre->right = node;
 | 
					    if (pre->val < num) {
 | 
				
			||||||
    else pre->left = node;
 | 
					        pre->right = node;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        pre->left = node;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    return node;
 | 
					    return node;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -97,6 +109,7 @@ TreeNode *getInOrderNext(TreeNode *root) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 删除结点 */
 | 
					/* 删除结点 */
 | 
				
			||||||
 | 
					// 由于引入了 stdio.h ,此处无法使用 remove 关键词
 | 
				
			||||||
TreeNode *removeNode(binarySearchTree *bst, int num) {
 | 
					TreeNode *removeNode(binarySearchTree *bst, int num) {
 | 
				
			||||||
    // 若树为空,直接提前返回
 | 
					    // 若树为空,直接提前返回
 | 
				
			||||||
    if (bst->root == NULL) return NULL;
 | 
					    if (bst->root == NULL) return NULL;
 | 
				
			||||||
@ -106,23 +119,31 @@ TreeNode *removeNode(binarySearchTree *bst, int num) {
 | 
				
			|||||||
        // 找到待删除结点,跳出循环
 | 
					        // 找到待删除结点,跳出循环
 | 
				
			||||||
        if (cur->val == num) break;
 | 
					        if (cur->val == num) break;
 | 
				
			||||||
        pre = cur;
 | 
					        pre = cur;
 | 
				
			||||||
        // 待删除结点在 root 的右子树中
 | 
					        if (cur->val < num) {
 | 
				
			||||||
        if (cur->val < num) cur = cur->right;
 | 
					            // 待删除结点在 root 的右子树中
 | 
				
			||||||
 | 
					            cur = cur->right;
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
            // 待删除结点在 root 的左子树中
 | 
					            // 待删除结点在 root 的左子树中
 | 
				
			||||||
        else cur = cur->left;
 | 
					            cur = cur->left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // 若无待删除结点,则直接返回
 | 
					    // 若无待删除结点,则直接返回
 | 
				
			||||||
    if (cur == NULL) return NULL;
 | 
					    if (cur == NULL) {
 | 
				
			||||||
    // 子结点数量 = 0 or 1
 | 
					        return NULL;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    // 判断待删除结点是否存在子结点
 | 
				
			||||||
    if (cur->left == NULL || cur->right == NULL) {
 | 
					    if (cur->left == NULL || cur->right == NULL) {
 | 
				
			||||||
 | 
					        /* 子结点数量 = 0 or 1 */
 | 
				
			||||||
        // 当子结点数量 = 0 / 1 时, child = nullptr / 该子结点
 | 
					        // 当子结点数量 = 0 / 1 时, child = nullptr / 该子结点
 | 
				
			||||||
        TreeNode *child = cur->left != NULL ? cur->left : cur->right;
 | 
					        TreeNode *child = cur->left != NULL ? cur->left : cur->right;
 | 
				
			||||||
        // 删除结点 cur
 | 
					        // 删除结点 cur
 | 
				
			||||||
        if (pre->left == cur) pre->left = child;
 | 
					        if (pre->left == cur) {
 | 
				
			||||||
        else pre->right = child;
 | 
					            pre->left = child;
 | 
				
			||||||
    }
 | 
					        } else {
 | 
				
			||||||
        // 子结点数量 = 2
 | 
					            pre->right = child;
 | 
				
			||||||
    else {
 | 
					        }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        /* 子结点数量 = 2 */
 | 
				
			||||||
        // 获取中序遍历中 cur 的下一个结点
 | 
					        // 获取中序遍历中 cur 的下一个结点
 | 
				
			||||||
        TreeNode *nex = getInOrderNext(cur->right);
 | 
					        TreeNode *nex = getInOrderNext(cur->right);
 | 
				
			||||||
        int tmp = nex->val;
 | 
					        int tmp = nex->val;
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user