Deleting an node in BST

可紊 提交于 2019-12-13 05:18:33

问题


This is not an homework. I am just totally blocked on this. I know what to do but I am having difficulty manipulating the tree. please help.

I am trying to delete and node from an BST. I am able to lookup and find the parent and store it an tree.

package com.test.binarytree;


public class BinaryTreeDelete {
private Node root;

//create null binary tree
public BinaryTreeDelete(){
    root = null;
}

//delete
public void delete(int target){
    root = delete(root, target);
}

public Node delete(Node node, int target){  
    NodeWithParent temp = lookupFindParent(root, null, target);

    if( node == null){
        return null;
    }
    else{
        if( node.left == null || node.right == null) //leaf node
        {
            //WHAT DO I DO HERE
            //temp.parent.left = null;
            //temp.parent.right = null;
            //return null; 
        }
        if( node.left != null && node.right == null ) //one child only on left
        {
            //WHAT DO I DO HERE
        }
        if( node.right != null && node.left == null ) //one child only on right
        {
            //WHAT DO I DO HERE
        }
        if( node.left != null && node.right != null ) //two children
        {
            //WHAT DO I DO HERE
        }
    }

    return null;
}

private NodeWithParent lookupFindParent(Node node, Node parentNode, int target){
    if( node == null ){
        return null;
    }

    if( node.data == target){
        return new NodeWithParent(node, parentNode);
    }
    else if( node.data > target ){
        parentNode = node;
        return lookupFindParent(node.left, parentNode, target);
    }
    else{
        parentNode = node;
        return lookupFindParent(node.right, parentNode, target);
    }
}

//insert
public void insert(int data){
    root = insert(root, data);
}

public Node insert (Node node, int data){
    if(node == null){
        node = new Node(data);
    }
    else{
        if( data <= node.data ){
            node.left = insert(node.left, data);
        }
        else{
            node.right = insert(node.right, data);
        }
    }

    return node;
}

//print tree
public void printTree(){
    printTree(root);
    System.out.println();
}

//print tree
private void printTree(Node node) {
     if (node == null) return;

     // left, node itself, right
     printTree(node.left);
     System.out.print(node.data + "  ");
     printTree(node.right);
} 
//node class
public static class Node{
    Node left;
    Node right;
    int data;

    Node(int newNode){
        data = newNode;
        left = null;
        right = null;
    }
}

//node class
public static class NodeWithParent{
    Node current;
    Node parent;

    NodeWithParent(Node current, Node parent){
        this.current = current;
        this.parent = parent;
    }
}

public static void main(String[] args) {
    BinaryTreeDelete bt = new BinaryTreeDelete();   

    //insert with inserts - tree increases on right if inserted in order
    bt = new BinaryTreeDelete();
    bt.insert(5);
    bt.insert(3);
    bt.insert(7);
    bt.insert(1);
    bt.insert(4);
    bt.insert(6);
    bt.insert(9);
    bt.printTree();
    //bt.delete(3);
    //bt.delete(4);
    //bt.delete(6);
    bt.delete(9);
    //bt.delete(5);
    bt.printTree();
}

}

回答1:


I'm going to provide you the logic (that means you have to write the code yourself) of how to delete a node in a BST.

There are three cases.

  1. Node to be deleted has both left and right child as null: Delete the node and make the parent point to null.
  2. Node to be deleted has either left or right child (but not both) as null: Delete the node but make sure that the parent points to the valid child of the to-be-deleted node.
  3. Node to be deleted has nether left child nor right child as null: In this case, you have to find the next greater element of the to-be-deleted node. This next greater element is the least element of the right subtree of the to-be-deleted node. Since this is the least element, it has at least one of its child as null. So swap the values of the to-be-deleted node with the next greater node. After you swap, delete this next greater node using points 1 and 2 (whichever is fitting to the situation). Now, why the next greater node and not any node. Because if you replace a node with its next greater node, the BST remains a BST. Try it out in an example and it will be clear.


来源:https://stackoverflow.com/questions/23024610/deleting-an-node-in-bst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!