How to Delete A Node in AVL Tree (not by key but Node Itself)

The title is very self explanatory.

I’m curious about how to delete a Node in an AVL Tree.

The only condition is (that’s also what makes other resources in the internet uselesss), the delete function should take the Node to delete directly as a parameter rather than the key of the Node and replacing the key.

I tried to use my functon that I created for a BST for this usecase. But unfortunately, it didn’t work and I wasn’t able to delete the root.

This is my BST code. I added balance tree at the return of the deleteRoot. But it still doesn’t work.

public void delete(TreeNode x) {
        // Function to delete the Node (x) in the BST
        if (root != null) {  // If it's not an empty tree
            if (root == x) {  // If the Node is the root
                root = deleteRoot(root);  // Directly call the deleteRoot function
            } else {
                deleter(root, x);  // If it's not the root, pass to the deleter
            }
        }
    }

    public void deleter(TreeNode parent, TreeNode target) {
        // Recursive delete function to reach the Node we'd like to delete and call the deleteRoot method
        if (parent.getLeft() != null && target.getKey() < parent.getKey()) {  // If left is not empty and target Node is less than the current parent
            if (target.getKey() == parent.getLeft().getKey()) {  // If the left Node is our target
                parent.setLeft(deleteRoot(parent.getLeft()));  // Call the deleteRoot on the left child of the parent and set the new root to the left child of the parent
            } else {
                deleter(parent.getLeft(), target);  // If we don't have a match, proceed further in the BST
            }
        } else if (parent.getRight() != null && target.getKey() > parent.getKey()) {  // If right is not empty and target Node is more than the current parent
            if (target.getKey() == parent.getRight().getKey()) { // Applying the same procedures but for the right side
                parent.setRight(deleteRoot(parent.getRight()));
            } else {
                deleter(parent.getRight(), target);
            }
        }
    }

    private TreeNode deleteRoot(TreeNode x) {
        // Function to delete the Node in a BST that is the root of the tree
        if (x.getLeft() == null) {
            // If the Node we want to delete is a Node with only one right child (This case also catches Nodes with no child)
            TreeNode temp = x;  // Saving the Node in a temp variable
            x = x.getRight();  // Setting it into its right child
            temp.setRight(null);  // Disconnecting the Node we'd like to delete
            return x;
        } else if (x.getRight() == null) {
            // If the Node we want to delete is a Node with only one left child, we apply the same procedure as before but with the left side
            TreeNode temp = x;
            x = x.getLeft();
            temp.setLeft(null);
            return x;
        } else {
            // If the Node we want to delete is a Node with a child on both sides
            TreeNode temp = getSuccessor(x.getRight());  // Finding the successor
            delete(temp);  // Removing the successor from the tree
            temp.setRight(x.getRight());  // Inserting the successor into the place of the Node we'd like to delete
            temp.setLeft(x.getLeft());
            x.setLeft(null);  // Disconnecting the Node we'd like to delete
            x.setRight(null);
            return temp;
        }
    }

    private TreeNode getSuccessor(TreeNode x) {
        // Function to find the successor of a Node
        while (x.getLeft() != null) {  // While the left side is not null
            x = x.getLeft();
        }
        return x;
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật