1. 程式人生 > 實用技巧 >抄襲資料結構之二叉查詢樹

抄襲資料結構之二叉查詢樹

原文地址github

有些功能沒有實現完,大概就這樣了

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ConcurrentLinkedQueue;

// import javax.management.RuntimeErrorException;

// import jdk.nashorn.internal.ir.BinaryNode;

public class BinaryNodeTree<T extends Comparable> {
    public BinaryNode<T> root;
    public BinaryNodeTree()
    {
        this.root = null;
    }

    public BinaryNodeTree(T[] pList, T[] inList, boolean isPreOrder)
    {
        if(pList == null || inList == null)
        {
            throw new RuntimeException("preList or inList can\'t be null");
        }
        if(isPreOrder){
            this.root = createBinarySearchTreeByPreIn(pList, inList, 0, pList.length - 1, 0, inList.length - 1);
        }else{
            this.root = createBinarySearchTreByPostIn(pList, inList, 0, pList.length - 1, 0, inList.length - 1);
        }
    }

    public BinaryNode<T> createBinarySearchTreeByPreIn(T[] pList, T[] inList, int preStart, int preEnd, int inStart, int inEnd)
    {
        BinaryNode<T> p = new BinaryNode<>(pList[preStart]);
        if(preStart == preEnd && inStart == inEnd)
        {
            return p;
        }
        int root = 0;

        for(root = inStart; root < inEnd ;root++)
        {
            if(pList[preStart].compareTo(inList[root]) == 0)
                break;
        }

        int leftLength = root - inStart;
        int rightLength = inEnd - root;

        if(leftLength > 0)
        {
            p.left = createBinarySearchTreeByPreIn(pList, inList, preStart + 1, preStart+leftLength, inStart, root - 1);
        }
        if(rightLength  > 0)
        {
            p.right = createBinarySearchTreeByPreIn(pList, inList, preStart + leftLength + 1, preEnd, root  +1, inEnd);
        }
        return p;
    }

    public BinaryNode<T> createBinarySearchTreByPostIn(T[] pList, T[] inList, int postStart, int postEnd, int inStart, int inEnd)
    {
        BinaryNode<T> p = new BinaryNode<>(pList[postEnd]);

        if(postStart == inStart && inStart == inEnd)
            return p;
        
        int root = 0;

        for(root = inStart; root < inEnd; root++)
        {
            if(pList[postEnd].compareTo(inList[root]) == 0)
                break;
        }

        int leftLength = root - inStart;
        int rightLength = inEnd - root;

        if(leftLength > 0)
        {
            p.left = createBinarySearchTreByPostIn(pList, inList, postStart, postStart + leftLength - 1, inStart, root - 1);

        }
        if(rightLength > 0)
        {
            p.right = createBinarySearchTreByPostIn(pList, inList, postStart + leftLength, postEnd - 1, root + 1, inEnd);
        }

        return p;
    }

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

    public int size()
    {
        return size(root);
    }

    private int size(BinaryNode<T> subTree)
    {
        if(subTree == null)
            return 0;
        else{
            return size(subTree.left) + 1 + size(subTree.right);
        }
    }

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

    private int height(BinaryNode<T> subTree)
    {
        if(subTree == null)
            return 0;
        else{
            int l = height(subTree.left);
            int r = height(subTree.right);
            return (l>r)?(l+1):(r+1);
        }
    }

    public String preOrder(){
        String sb = preOrder(root);
        if(sb.length() > 0)
            sb = sb.substring(0, sb.length() - 1);
        return sb;
    }

    private String preOrder(BinaryNode<T> subTree)
    {
        StringBuilder sb = new StringBuilder();
        if(subTree != null)
        {
            sb.append(subTree.data).append(", ");
            sb.append(preOrder(subTree.left));
            sb.append(preOrder(subTree.right));
        }
        return sb.toString();
    }

    public String inOrder()
    {
        String sb = inOrder(root);
        if(sb.length() > 0)
            sb = sb.substring(0, sb.length() - 2);
        return sb;
    }

    private String inOrder(BinaryNode<T> subTree)
    {
        StringBuilder sb = new StringBuilder();
        if(subTree != null)
        {
            sb.append(inOrder(subTree.left));
            sb.append(subTree.data).append(", ");
            sb.append(inOrder(subTree.right));
        }
        return sb.toString();
    }

    public String postOrder()
    {
        String sb =  postOrder(root);
        if(sb.length() != 0)
        {
            sb = sb.substring(0, sb.length() - 1);
        }
        return sb;
    }

    private String postOrder(BinaryNode<T> subTree)
    {
        StringBuilder sb = new StringBuilder();
        if(subTree != null)
        {
            sb.append(postOrder(subTree.left));
            sb.append(postOrder(subTree.right));
            sb.append(subTree.data).append(", ");
        }
        return sb.toString();
    }

    public String preOrderTraverse()
    {
        StringBuilder sb = new StringBuilder();

        Stack<BinaryNode<T>> stack= new Stack<>();

        BinaryNode<T> p = this.root;

        while(p!=null || !stack.isEmpty())
        {
            if(p != null)
            {
                sb.append(p.data).append(", ");
                stack.push(p);
                p = p.left;
            }else{
                p = stack.pop();
                p = p.right;
            }
        }

        if(sb.length()>0){
            return sb.toString().substring(0,sb.length()-2);
        }else {
            return sb.toString();
        }

    }

    public String inOrderTraverse()
    {
        StringBuilder sb = new StringBuilder();
        Stack<BinaryNode<T>> stack = new Stack<>();
        BinaryNode<T> p = this.root;
        while(p != null || !stack.isEmpty())
        {
            if(p != null)
            {
                stack.push(p);
                p = p.left;
            }else{
                p = stack.pop();
                sb.append(p.data).append(", ");
                p = p.right;
            }
        }
        if(sb.length()>0){
            return sb.toString().substring(0,sb.length()-2);
        }else {
            return sb.toString();
        }
    }

    public String postOrderTraverse()
    {
        StringBuilder sb = new StringBuilder();
        Stack<BinaryNode<T>> stack = new Stack<>();
        BinaryNode<T> p = this.root;
        BinaryNode<T> currentNode = this.root;
        while(currentNode != null || !stack.isEmpty())
        {
            while(currentNode != null)
            {
                stack.push(currentNode);
                currentNode = currentNode.left;
            }
            if(!stack.isEmpty())
            {
                BinaryNode<T> temp = stack.peek().right;

                if(temp == null || temp == p)
                {
                    currentNode = stack.pop();
                    sb.append(currentNode.data + ", ");
                    p = currentNode;
                    currentNode = null;
                }else{
                    currentNode = temp;
                }
            }
        }
        if(sb.length()>0){
            return sb.toString().substring(0,sb.length()-2);
        }else {
            return sb.toString();
        }
    }

    public String levelOrder()
    {
        Queue<BinaryNode<T>> queue = new ConcurrentLinkedQueue<BinaryNode<T>>();

        StringBuilder sb = new StringBuilder();

        BinaryNode<T> p = this.root;

        queue.add(p);
        while(!queue.isEmpty())
        {
            p = queue.poll();
            sb.append(p.data + ", ");
            if(p.left != null)
                queue.add(p.left);
            if(p.right != null)
                queue.add(p.right);            
        }
        if(sb.length()>0){
            return sb.toString().substring(0,sb.length()-2);
        }else {
            return sb.toString();
        }
    }

    public void insert(T data)
    {
        if(data == null)
            throw new RuntimeException("data is null!!!");
        this.root = insert(data, this.root);
    }

    private BinaryNode<T> insert(T data, BinaryNode<T> p)
    {
        if( p ==  null)
            p = new BinaryNode<>(data, null, null);
        
        int comparatoResult = data.compareTo(p.data);
        if(comparatoResult < 0)
        {
            p.left = insert(data, p.left);  
        }
        else if(comparatoResult > 0)
        {
            p.right = insert(data, p.right);
        }else{
            ;
        }
        return p;
    }

    public void remove(T data)
    {
        if(data == null)
            throw new RuntimeException("data is null!!!");
        this.root = remove(data, this.root);
    }

    private BinaryNode<T> remove(T data, BinaryNode<T> p)
    {
        if(p == null)
            return p;
        
        int compareResult = data.compareTo(p.data);

        if(compareResult < 0)
            p.left = remove(data, p.left);
        else if(compareResult > 0)
            p.right = remove(data, p.right);
        else if(p.left != null && p.right != null)
        {
            p.data = findMin(p.right).data;
            p.right = remove(p.data, p.right);
        }else{
            p = (p.right != null) ? p.right: p.left;
        }

        return p;
    }

    public T findMin()
    {
        return findMin(root).data;
    }

    private BinaryNode<T> findMin(BinaryNode<T> p)
    {
        if(p == null)
            return null;
        else if(p.left == null)
            return p;
        return findMin(p.left);
    }

    public void clear()
    {
        this.root = null;
    }
}