如何在二叉搜索树中实现重新平衡?

时间:2014-02-28 02:52:23

标签: java recursion binary-search-tree

我已经为二叉搜索树添加了以下方法:

import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.ArrayList;

public class MyTree {

        private class Node
        {
            public String data;
            public int data2;
            public Node left;
            public Node right;
            public Node(String data, Node left, Node right)
            {
                this.data = data;
                this.left = left;
                this.right = right;
            }
        }

private static Node root = null;

private int getHeight(Node subroot)
{
    if (subroot == null)
        return -1;
    int maxLeft = getHeight(subroot.left);
    int maxRight = getHeight(subroot.right);
    return Math.max(maxLeft, maxRight) + 1;
}

public String toString()
{
    return toString(this.root);
}

private String toString(Node subroot)
{
    if (subroot==null)
        return "";
    return toString(subroot.left)+subroot.data+toString(subroot.right);
}

public boolean containsRecursive(String value)
{
    return contains(value, this.root);
}

private boolean contains(String value, Node subroot)
{
    if (subroot==null)
        return false;
    else if (value.equals(subroot.data))
        return true;
    else if (value.compareTo(subroot.data) < 0)
        return contains(value, subroot.left);
    else
        return contains(value, subroot.right);
}

public boolean contains(String value) // not recursive
{
    Node subroot = this.root;
    while (subroot != null)
    {
        if (value.equals(subroot.data))
            return true;
        else if (value.compareTo(subroot.data) < 0)
            subroot = subroot.left;
        else
            subroot = subroot.right;
    }
    return false;
}

public int addUp()
{
    return addUp(this.root);
}

private int addUp(Node subroot)
{
    if (subroot==null)
        return 0;
    return addUp(subroot.left)+subroot.data2+addUp(subroot.right);
} //data = String, data2 = int

public int count()
{
    return count(this.root);
}

private int count(Node subroot)
{
    if (subroot==null)
        return 0;
    return count(subroot.left)+1+count(subroot.right);
}

public int numberLess(int x)
{
    return numberLess(this.root, x);
}

private int numberLess(Node subroot, int x)
{
    if (subroot==null)
        return 0;
    if (x < subroot.data2)
        return numberLess(subroot.left, x)+1+numberLess(subroot.right, x);
    return numberLess(subroot.left, x)+numberLess(subroot.right, x);
}

public int findMax()
{
    return findMax(this.root);
}

private int findMax(Node subroot) throws NoSuchElementException
{
    if (subroot==null)
        throw new NoSuchElementException();
    return Math.max(findMax(subroot.left), findMax(subroot.right));
}

private ArrayList<Integer> addToList(Node subroot, ArrayList<Integer> a)
{
    if (subroot!=null){
    a.add(subroot.data2);
    addToList(subroot.left, a).addAll(addToList(subroot.right, a));
    return a;
    }
    return new ArrayList<Integer>();
}

private ArrayList<Integer> getSortedList(){
    ArrayList<Integer> rawList = addToList(this.root, new ArrayList<Integer>());
    Collections.sort(rawList);
    return rawList;
}

public void rebalance(){
    ArrayList<Integer> list = getSortedList();

}

}

如何使用我已有的结构完成重新平衡方法?我想通过找到中点并递归排序来使用排序的arraylist。我不确定如何使用我设置树的方式(使用内部节点类)来处理这个问题,所以我想对这段代码有所帮助。

1 个答案:

答案 0 :(得分:1)

将阵列分成两个相等大小的部分。将median元素作为新的根节点。 然后再分割两个部分并将中值元素作为第二级节点等。 最好以递归方式实现....