backtrack Binary Search Tree

ぃ、小莉子 提交于 2020-06-01 06:04:32

问题



I'm trying to implement the backtrack method on a new class named BacktrackingBST, which consists of the root node of a Binary Search Tree and a stack. the stack is used to store inserted or deleted nodes from the tree in order to allow us to backtrack said methods. i encounter an issue while trying to backtrack deletion , the problem was that while i was to restore the deleted node back to its original place i failed to come up with a solution on how to get the new node in its place like its original place***

public class BacktrackingBST implements Backtrack, ADTSet<BacktrackingBST.Node> {
    BacktrackingBST.Node root = null;
    private Stack stack;
    private Stack redoStack;

    // Do not change the constructor's signature
    public BacktrackingBST(Stack stack, Stack redoStack) {
        this.stack = stack;
        this.redoStack = redoStack;
    }

    public Node getRoot() {
        return root;
    }

    public Node searchNode(BacktrackingBST.Node node, int key) {
        if (node == null || node.key == key)
            return node;
        if (node.key > key)
            return searchNode(node.left, key);
        return searchNode(node.right, key);
    }
        public Node search(int x) {
            return searchNode(root,x);
    }

    public void insert(BacktrackingBST.Node z) {
        if (search(z.getKey()) == null) {
            Node zParent = null;
            var zPlacement = root;
            while (zPlacement != null) {
                zParent = zPlacement;
                if (z.getKey() < zPlacement.getKey())
                    zPlacement = zPlacement.left;
                else
                    zPlacement = zPlacement.right;
            }
            z.parent = zParent;
            if (zParent == null)
                root = z;
            else if (z.getKey() < zParent.getKey())
                zParent.left = z;
            else
                zParent.right = z;
            stack.push(z);
        }
    }
    public void delete(Node x) {
        if(search(x.getKey())!=null){
            stack.push(x);
            if(x.left==null&&x.right==null)
                if(x.parent.getKey()>x.getKey())
                    x.parent.left=null;
                else
                    x.parent.right=null;
            else
            if(x.left==null)
                if(x.parent.getKey()>x.getKey())
                    x.parent.left=x.right;
                else
                    x.parent.right=x.right;
            else
            if(x.right==null)
                if(x.parent.getKey()>x.getKey())
                    x.parent.left=x.left;
                else
                    x.parent.right=x.left;
            else{
                Node suc=successor(x);
                delete(suc);
                stack.pop();
                x.value= suc.getValue();
                x.key = suc.getKey();
            }
        }

    }
    public Node minimumNode(Node z){
        if(z.left != null){
            return minimumNode(z.left);
        }
        return z;
    } public Node maximumNode(Node z){
        if(z.right != null){
            return minimumNode(z.right);
        }
        return z;
    }
    public Node minimum() {
        return minimumNode(root);
    }

    public Node maximum() {
        return maximumNode(root);
    }
    public Node successor(Node x) {
        if (x.right != null) {
            return minimumNode(x.right);
        }
        var parent = x.parent;
        while (parent != null && x == parent.right) {
            x = parent;
            parent = parent.parent;
        }
        return parent;
    }

    public Node predecessor(Node x) {
        if (x.left != null) {
            return maximumNode(x.left);
        }
        var parent = x.parent;
        while (parent != null && x == parent.left) {
            x = parent;
            parent = parent.parent;
        }
        return parent;
    }

    @Override
    public void backtrack() {
        if(!stack.isEmpty()){
        Node pop = (Node) stack.pop();
        if (search(pop.getKey()) == null) {
            search(pop.parent.getKey());


          }
        }
    }

    @Override
    public void print() {
        if (root == null)
            return;
        while(root.left != null){
            root = root.left;
        }while(root.right != null){

        }

        System.out.print(root.key + " ");
    }

    public static class Node {
        public BacktrackingBST.Node left;
        public BacktrackingBST.Node right;

        private BacktrackingBST.Node parent;
        private int key;
        private Object value;

        public Node(int key, Object value) {
            this.key = key;
            this.value = value;
        }

        public int getKey() {
            return key;
        }

        public Object getValue() {
            return value;
        }
    }

}

来源:https://stackoverflow.com/questions/61447029/backtrack-binary-search-tree

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