All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tk.hongkailiu.test.app.tree.other.BinarySearchTree Maven / Gradle / Ivy

The newest version!
package tk.hongkailiu.test.app.tree.other;

import tk.hongkailiu.test.app.helper.CollectionHelper;
import tk.hongkailiu.test.app.helper.CollectionHelperImpl;

import java.util.List;


public class BinarySearchTree> {

    private static CollectionHelper helper = CollectionHelperImpl.getInstance();

    private SimpleTree tree;

    public BinarySearchTree(SimpleTree tree) {
        super();
        this.tree = tree;
        validate();
    }

    public SimpleTree getTree() {
        return tree;
    }

    private void validate() {
        List list1 = tree.getValuesInOrder();
        if (!helper.isOrdered(list1)) {
            throw new IllegalArgumentException("not ordered");
        }
        List list2 = helper.removeDuplicate(list1);
        if (!helper.elementsEqual(list1, list2)) {
            throw new IllegalArgumentException("duplicate elements not allowed");
        }
    }

    public boolean search(final T toFind) {
        return search(tree, toFind);
    }

    private boolean search(final SimpleTree tree, final T toFind) {
        if (tree == null) {
            return false;
        }
        if (toFind.equals(tree.value)) {
            return true;
        }

        if (toFind.compareTo(tree.value) < 0 && tree.left != null) {
            return search(tree.left, toFind);
        }

        return tree.right != null && search(tree.right, toFind);
    }

    public void insert(final T toInsert) {
        this.tree = insert(this.tree, toInsert);
        validate();
    }

    private SimpleTree insert(final SimpleTree tree, final T toInsert) {
        if (tree == null) {
            return new SimpleTree(toInsert, null, null);
        } else if (toInsert.compareTo(tree.value) < 0) {
            if (tree.left == null) {
                tree.left = new SimpleTree(toInsert, null, null);
            } else {
                insert(tree.left, toInsert);
            }
            return tree;
        } else {
            if (tree.right == null) {
                tree.right = new SimpleTree(toInsert, null, null);
            } else {
                insert(tree.right, toInsert);
            }
            return tree;
        }
    }

    public void delete(final T toDelete) {
        this.tree = delete(tree, toDelete);
        validate();
    }

    /**
     * ref.
     * https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/code/BST.java
     * http://www.algolist.net/Data_structures/Binary_search_tree/Removal
     * hongkai: more to test
     *
     * @param toDelete
     */
    private SimpleTree delete(final SimpleTree tree, final T toDelete) {
        if (tree == null) {
            return null;
        } else if (toDelete.compareTo(tree.value) == 0) {
            if (tree.left != null && tree.right != null) {
                SimpleTree rightMost = findRightMost(tree.left);
                tree.value = rightMost.value;
                tree.left = delete(tree.left, rightMost.value);
                return tree;
            } else {
                if (tree.left == null) {
                    return tree.right;
                } else {
                    return tree.left;
                }
            }

        } else if (toDelete.compareTo(tree.value) < 0) {
            if (tree.left == null) {
                return tree;
            } else {
                tree.left = delete(tree.left, toDelete);
                return tree;
            }
        } else {
            if (tree.right == null) {
                return tree;
            } else {
                tree.right = delete(tree.right, toDelete);
                return tree;
            }
        }
    }

    private SimpleTree findRightMost(final SimpleTree root) {
        if (root == null) {
            return null;
        }
        SimpleTree result = root;
        while (result.right != null) {
            result = result.right;
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy