添加条件以删除多个BST节点

时间:2016-05-03 22:06:56

标签: java

我终于得到了我的BST工作及其功能,但现在我想要删除所有等于或小于75的密钥的节点。

我尝试将操作添加为:theTree.remove(key<=75);以及其他多种方式,我知道执行此类操作的语法是错误的,但我似乎无法在线查找有关如何执行此操作的相关信息。任何建议都会有所帮助。

代码:

public class DatosTarea9 {

   Node root;
   public void addNode(int key, String name) {
       Node newNode = new Node(key, name);

       if(root == null) {
           root = newNode;
       }
       else {
           Node focusNode = root;
           Node parent;
           while(true) {
               parent = focusNode;
               if(key < focusNode.key) {
                      focusNode = focusNode.leftChild;
                      if(focusNode == null) {
                          parent.leftChild = newNode;
                          return;
           }
         }
               else {
                   focusNode = focusNode.rightChild;
                   if(focusNode == null) {
                       parent.rightChild = newNode;
                       return;
                   }
               }
       }
     }
   }

   public void inOrderTraverseTree(Node focusNode) {
       if(focusNode != null){
           inOrderTraverseTree(focusNode.leftChild);
           System.out.println(focusNode);
           inOrderTraverseTree(focusNode.rightChild);
       }
   }

   public boolean remove(int key) {
       Node focusNode = root;
       Node parent = root;

       boolean isItALeftChild = true;

       while (focusNode.key != key){
           parent = focusNode;
           if(key < focusNode.key){
               isItALeftChild = true;

               focusNode = focusNode.leftChild;
           }
           else {
               isItALeftChild = false;
               focusNode = focusNode.rightChild;
           }
           if(focusNode == null)
                   return false;
       }
       if (focusNode.leftChild == null && focusNode.rightChild == null){
           if(focusNode == root){
               root = null;
           }
           else if(isItALeftChild){
               parent.leftChild = null;   
           }
           else {
               parent.rightChild = null;
           }
       }
       else if(focusNode.rightChild == null){
           if(focusNode == root)
               root = focusNode.leftChild;
           else if(isItALeftChild)
               parent.leftChild = focusNode.leftChild;
           else parent.rightChild = focusNode.leftChild;
       }
       else if(focusNode.leftChild == null){
           if(focusNode == root)
               root = focusNode.rightChild;
           else if(isItALeftChild)
               parent.leftChild = focusNode.rightChild;
           else
               parent.rightChild = focusNode.leftChild;
       }
       else {
           Node replacement = getReplacementNode(focusNode);

           if(focusNode == root)
               root = replacement;

               else if (isItALeftChild)
                   parent.leftChild = replacement;
               else
                   parent.rightChild = replacement;
               replacement.leftChild = focusNode.leftChild;
           }
           return true;
       }


   public Node getReplacementNode(Node replacedNode){
       Node replacementParent = replacedNode;
       Node replacement = replacedNode;

       Node focusNode = replacedNode.rightChild;

       while (focusNode != null){
           replacementParent = replacement;
           replacement = focusNode;
           focusNode = focusNode.leftChild;
       }
       if(replacement != replacedNode.rightChild){
           replacementParent.leftChild = replacement.rightChild;
           replacement.rightChild = replacedNode.rightChild;
       }
       return replacement;
   }

    public static void main(String[] args) {

       DatosTarea9 theTree = new DatosTarea9();
       theTree.addNode(82, "Jorge");
       theTree.addNode(74, "Javier");
       theTree.addNode(66, "Jose");
       theTree.addNode(38, "Jaime");
       theTree.addNode(94, "Andres");
       theTree.addNode(88, "Alejandro");
       theTree.addNode(42, "Adrian");
       theTree.addNode(79, "Alan");

       System.out.println("Remove all keys below 75");
       theTree.remove(key<=75);

       theTree.inOrderTraverseTree(theTree.root);
    }
}

class Node {
    int key;
    String name;

    Node leftChild;
    Node rightChild;

    Node(int key, String name) {
        this.key = key;
        this.name = name;
    }
    public String toString(){
        return name + " has a key " + key;
    }
}

1 个答案:

答案 0 :(得分:0)

在您撰写和发布之前,您不会找到任何文档。这个想法是你必须自己实现的。

举例说明如何将您正在寻找的功能传递给我建议的方法:

public void removeByPredicate(Predicate<Node> predicate) {
    removeByPredicate(root, predicate);
}

调用同名的私有方法来删除传递谓词测试的 节点。您可以使用它:

    Tree t = new Tree();
    t.removeByPredicate(n -> n.key <= 75);

你使用谓词函数,如:

if (predicate.test(node))
    remove(node);

那可能会让你前进。好处是测试可以动态地改变为适合当时的需求。正如评论中指出的那样,需要更多的思考才能删除所有节点。