使用ArrayList平衡二进制搜索树

时间:2017-12-14 11:53:03

标签: java arraylist tree binary-search-tree tree-balancing

对于类赋值,我需要向提供的BinarySearchTree类添加一个方法,该类将通过将值按顺序存储在数组中并使用这些值构建新树来平衡二叉搜索树。但是,当我尝试运行该方法时,我得到一个nullPointerException。如何更改方法以正确平衡二叉搜索树?

我在下面添加了我的代码(尝试将其修剪为仅针对问题所需的代码);底部的两个方法是我试图用来平衡的方法。

package ch08;

import ch05.queues.*;
import java.util.ArrayList;

public class BinarySearchTree<T extends Comparable<T>> implements BSTInterface<T>{

protected BSTNode<T> root;      // reference to the root of this BST
protected LinkedUnbndQueue<T> inOrderQueue; 
protected ArrayList<T> balanceArray;

public BinarySearchTree(){
    root = null;
}

public int reset(int orderType){
    int numNodes = size();

    if (orderType == INORDER){
        inOrderQueue = new LinkedUnbndQueue<T>();
        inOrder(root);
    }
    return numNodes;
}

public T getNext (int orderType){
    if (orderType == INORDER)
        return inOrderQueue.dequeue();
}

    public void balanceTree() {
    int count = reset(INORDER);

    for(int i = 0; i < count; i++) {
        balanceArray.add(getNext(INORDER));
    }
    BinarySearchTree<T> tree = new BinarySearchTree<T>();
    tree.insertTree(0, count - 1);
    this.root = tree.root;
}

public void insertTree(int low, int high){
    if(low == high) {
        add(balanceArray.get(low));
    }
    else if((low + 1) == high) {
        add(balanceArray.get(low));
        add(balanceArray.get(high));
    }
    else {
        int mid = (low + high) / 2;
        add(balanceArray.get(mid));
        insertTree(low, mid - 1);
        insertTree(mid + 1, high);
    }
 }
}

1 个答案:

答案 0 :(得分:0)

我想NullPointerException来自于你永远不会初始化balanceArray

您不应将inOrderQueuebalanceArray声明为字段(恕我直言),因为它们只与一个操作相关。我会在那里使用论据。

我没有看到班级BSTNode,所以我认为它是这样的:

public class BSTNode<T extends Comparable<T>> {
    T value;
    BSTNode<T> left;
    BSTNode<T> right;

    public BSTNode(T value, BSTNode<T> left, BSTNode<T> right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}

这是我如何进行平衡:

public class BinarySearchTree<T extends Comparable<T>> {
    protected BSTNode<T> root;      // reference to the root of this BST

    public BinarySearchTree() {
        root = null;
    }

    // creates a tree from a sorted list
    private BinarySearchTree(List<T> list) {
        root = buildTree(list, 0, list.size());
    }

    public BinarySearchTree<T> balancedTree()  {
        List<T> list = new ArrayList<>();
        inOrder(root, list);
        return new BinarySearchTree(list);
    }

    private void inOrder(BSTNode<T> node, List<T> list) {
        if (node != null) {
            inOrder(node.left, list);
            list.add(node.value);
            inOrder(node.right, list);            
        }
    }

    private BSTNode<T> buildTree(List<T> list, int i, int size) {
        if (size == 0) {
            return null;
        } else {
            int half = size/2;
            int mid = i+half;
            return new BSTNode<T>(list.get(mid),
                    buildTree(list, i, half),
                    buildTree(list, mid+1, size-half-1));
        }
    }

    ...
}