Функция удаления узла двоичного дерева поиска не удаляет - PullRequest
1 голос
/ 30 мая 2020

Мой метод удаления состоит из 4 операторов if, которые обращаются к 4 различным видам удаления в дереве двоичного поиска. Не уверен, где ошибся, но он не удалил ни одного узла, когда я его проверял. Любая помощь, если будет оценена. Заранее спасибо '

Я подозреваю, что проблемы заключаются в том, где я пытаюсь заменить удаление узла на null

public class BinaryTree<T extends Comparable<T>> {

private class Node{

    private T data;
    private Node left;
    private Node right;


    // left and right child do not have to nessary exist
    public Node ( T data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }}

    private Node root;
    private int count = 0;

    public void add( T data) {
        if ( isEmpty()) {
            root = new Node(data);
            count++;
        }
        else {
            insert(data, root);
            count++;
        }
    }

    public boolean isEmpty() { 
        return root == null;
    }

    public T getRoot()  {
        if ( root.data == null) {
            System.out.println("Root is empty");
            return null;
        }
        else {
        return  root.data;
    }}

    public Node getRootNode() {
        return root;
    }

    /*
     * Checking if the data is larger or lesser than the parent's data
     * If the data is smaller than the parent's data, node.left is created
     * If the data is bigger than the parent's data, node.right is created
     */
    private void insert( T data, Node node) {

        /*
         * If 1st obj is less than the 2nd obj return a neg
         * if 1st obj is more than the 2nd obj return a pos
         * if equal return 0
         */
        int compare = data.compareTo(node.data);
        if ( compare < 1 ){
            if (node.left == null ) {
                node.left = new Node(data);

            }

        // make node.left if it is filled  
            else {
                insert(data, node.left);
            }
        }

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

    public int getSize() {
        return count;
    }

    public boolean search ( T data) {

        Node temp = searchInner(data, root);
        if ( temp.data == data) {
            System.out.println(temp.data);
            return true;
        }
        else {
            return false;
        }

    }


    public Node searchInner( T data, Node node) {

        int compare = data.compareTo(node.data);

        if ( getRoot() == data ) {
            return root;
        }

        if ( compare > 0) {
            return searchInner( data, node.right);  
        }

        if ( compare < 0 ) {
            return searchInner(data , node.left);
        }

        if ( compare == 0 ) {
            return node;
        }

        else {
            System.out.println("Not found");
            return node;
        }

    }

    public void remove( T data) {
        remove1( root, data);
    }

    private Node remove1( Node node1, T data) {

        Node parent = root;
        Node node = root;
        Node temp;
        boolean isLeft = true;

        while ( node.data != data) {

            parent = node;

            if ( isEmpty()) {
                System.out.println("Unable to remove, root is empty");
                break;
            }

            if ( compare(data, node.data) < 0) {
                node = node.left;
                isLeft = true;
            }

            if ( compare(data, node.data) > 0) {
                node = node.right;
                isLeft = false;
            }

            else {
                // remove node if left child available
                if ( node.left == null && node.right != null) {

                    if ( isLeft) {
                        parent.left = node.right;
                    }

                    else {
                        parent.right = node.right;
                    }
                    count --;
                    break;
                }

                //remove node if right child available
                if ( node.right == null && node.left != null) {

                    if ( isLeft) {
                        parent.left = node.left;
                    }

                    else {
                        parent.right = node.left;
                    }
                    count --;
                    break;
                }

                // Remove node if 2 child available
                if ( node.left != null && node.right != null ) {

                    node = min(node.right);
                    node.right = remove1(node.right, node.data);

                }

                // remove node if no child available
                 if ( node.left == null && node.right == null) {
                    if (  isLeft ) {
                        parent.left = null;
                    }
                    else {
                        parent.right = null;
                    }
                    count --;
                    break;
                }

            }


        }
            return node;   
        }

    // fine the smallest node in the right subtree
    private Node min ( Node node1 ) {
        while ( node1.left != null) {
            node1 = node1.left;
        }
        return node1;
    }

    private int compare( T data, T data1) {
        return data.compareTo(data1);
    }

    public void printBST(T data) {
        printTree( root, data);
    }

    private void printTree( Node node, T data)
     {
        if(node == null) return;

        System.out.println(data + " + " + node.data);
        printTree(node.left , data);
        printTree(node.right , data);
     }

    public int getHeight() {
        return height(root);
    }

    private int height( Node node) {

        if  (node == null) return 0;
        else
            return 1 + Math.max(height(node.left), height(node.right));
        }

    public void print() {
        println(root);
    }

    private void println ( Node node) {

        LinkedList<T> q = new LinkedList<T>();
        q.add(node.data);

        if ( node == null) {
            return;
        }

        int size = getSize();
        while ( size > 0) {

                System.out.print(q);

            q.clear();
            if ( node.left != null) {
                q.add(node.left.data);
                size --;
            }
            if ( node.right != null) {
                q.add(node.right.data);
                size --;
            }
            if ( node.right != null&& node.left != null) {
                System.out.println();
            }
            if ( size > 1) {
                System.out.println(",");
            }
        }

    }

    public boolean sameTree( Node root1, Node root2) {

        if ( root1 == null && root2 == null) {
            return true;
        }

        if ( root1 != null && root2 != null) {
            return  root1.data == root2.data && sameTree(root1.left,root2.left) && sameTree(root1.right, root2.right);
        }
        else {
            return false;
        }
    }

}

1 Ответ

0 голосов
/ 30 мая 2020

Я переписал ваш класс BinaryTree. Я добавил новый метод удаления, который использует ваш метод min(Node node) и другой, созданный мной, который удаляет только минимальный элемент дерева. Кроме того, я также изменил ваш класс Node, добавив новый конструктор и переменную вашего размера, которая была в классе BinaryTree. Я изменил все это, чтобы метод remove() работал правильно


import java.util.LinkedList;

public class BinaryTree<T extends Comparable<T>> {

    private class Node<T> { //Here we specify what the node contains

        private T data;
        private Node<T> left;
        private Node<T> right;
        private int size;

        public Node(T value) {
            this(value, null, null);
        }

        // left and right child do not have to nessary exist
        public Node(T data, Node<T> left, Node<T> right) {
            this.data = data;
            this.left = null;
            this.right = null;
            size = 1;
            if (left != null) {
                size += left.size;
            }
            if (right != null) {
                size += right.size;
            }
        }
    }
    private Node root;

    public BinaryTree() { //Added a constructor to set the root node to null
        root = null;
    }

    public boolean isEmpty() {
        return root == null;
    }

    public T getRootData() { //Changed the name to other more clear
        if (root.data == null) {
            System.out.println("Root is empty");
            return null;
        } else {
            return (T) root.data;
        }
    }

    public Node getRootNode() {
        return root;
    }

    public void insert(T x) { //The new insert method
        root = insert(x, root);
    }

    protected Node<T> insert(T x, Node<T> actual) {
        //We check if the node exists, in case not we just create a new node
        if (actual == null) {
            return new Node<T>(x);
        }
        int cmp = compare(x, actual.data);
        if (cmp < 0) {
            actual.left = insert(x, actual.left);
        } else if (cmp > 0) {
            actual.right = insert(x, actual.right);
        } else {
            // If the node exists we just update his content
            actual.data = x;
        }
        actual.size = 1 + getSize(actual.left) + getSize(actual.right);
        return actual;
    }

     public int getSize() { //New method
        return getSize(root);
    }

    private int getSize(Node<T> actual) {
        if (actual == null) {
            return 0;
        } else {
            return actual.size;
        }
    }

    public boolean search(T data) {

        Node temp = searchInner(data, root);
        if (temp.data == data) {
            System.out.println(temp.data);
            return true;
        } else {
            return false;
        }

    }

    public Node searchInner(T data, Node node) {

        int compare = data.compareTo((T) node.data);

        if (getRootData() == data) {
            return root;
        }

        if (compare > 0) {
            return searchInner(data, node.right);
        }

        if (compare < 0) {
            return searchInner(data, node.left);
        }

        if (compare == 0) {
            return node;
        } else {
            System.out.println("Not found");
            return node;
        }

    }

    public void remove(T data) {

        remove1(root, data);
    }

    private Node remove1(Node actual, T data) {
        if (actual == null) {
            return actual;
        }
        int cmp = compare(data, (T) actual.data);
        //Check whether the value is lesser greater or equal than the one we are just visiting
        if (cmp < 0) {
            actual.left = remove1(actual.left, data);
        } else if (cmp > 0) {
            actual.right = remove1(actual.right, data);
        } else {
            if (actual.right == null) {
                return actual.left;
            }
            if (actual.left == null) {
                return actual.right;
            }
            actual.data = min(actual.right).data;
            actual.right = removeMin(actual.right);
        }
        return actual;
    }

    public Node removeMin() {
        //A new method to remove the minimum element
        Node min = min(root);
        root = removeMin(root);
        return min;
    }

    private Node removeMin(Node actual) {
        if (actual.left == null) {
            return actual.right;
        }
        actual.left = removeMin(actual.left);
        actual.size--;
        return actual;
    }

    // fine the smallest node in the right subtree
    private Node min(Node node1) {
        while (node1.left != null) {
            node1 = node1.left;
        }
        return node1;
    }

    private int compare(T data, T data1) {
        return data.compareTo(data1);
    }

    public void printBST(T data) {
        printTree(root, data);
    }

    private void printTree(Node node, T data) {
        if (node == null) {
            return;
        }

        System.out.println(data + " + " + node.data);
        printTree(node.left, data);
        printTree(node.right, data);
    }

    public int getHeight() {
        return height(root);
    }

    private int height(Node node) {

        if (node == null) {
            return 0;
        } else {
            return 1 + Math.max(height(node.left), height(node.right));
        }
    }

    public void print() {
        println(root);
    }

    private void println(Node node) {

        LinkedList<T> q = new LinkedList<T>();
        q.add((T) node.data);

        if (node == null) {
            return;
        }

        int size = getSize();
        while (size > 0) {

            System.out.print(q);

            q.clear();
            if (node.left != null) {
                q.add((T) node.left.data);
                size--;
            }
            if (node.right != null) {
                q.add((T) node.right.data);
                size--;
            }
            if (node.right != null && node.left != null) {
                System.out.println();
            }
            if (size > 1) {
                System.out.println(",");
            }
        }

    }

    public boolean sameTree(Node root1, Node root2) {

        if (root1 == null && root2 == null) {
            return true;
        }

        if (root1 != null && root2 != null) {
            return root1.data == root2.data && sameTree(root1.left, root2.left) && sameTree(root1.right, root2.right);
        } else {
            return false;
        }

    }
}

Надеюсь, это вам поможет

...