Java 如何在二叉搜索树中实现再平衡?

Java 如何在二叉搜索树中实现再平衡?,java,recursion,binary-search-tree,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;

我已经为二进制搜索树提供了以下方法:

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();

}

}
import java.util.Collections;
导入java.util.NoSuchElementException;
导入java.util.ArrayList;
公共类MyTree{
私有类节点
{
公共字符串数据;
公共int数据2;
公共节点左;
公共节点权;
公共节点(字符串数据、左节点、右节点)
{
这个数据=数据;
this.left=左;
这个。右=右;
}
}
私有静态节点根=null;
私有int getHeight(节点子节点)
{
如果(子轨迹==null)
返回-1;
int maxleet=getHeight(subroot.left);
int maxRight=getHeight(subroot.right);
返回Math.max(maxLeft,maxRight)+1;
}
公共字符串toString()
{
返回到字符串(this.root);
}
私有字符串到字符串(节点子角色)
{
如果(子轨迹==null)
返回“”;
返回toString(子程序左)+子程序数据+toString(子程序右);
}
公共布尔值包含草书(字符串值)
{
返回包含(值,this.root);
}
私有布尔包含(字符串值、节点子轨迹)
{
如果(子轨迹==null)
返回false;
else if(值等于(子轨迹数据))
返回true;
else if(值比较(子轨迹数据)<0)
返回包含(值,subroot.left);
其他的
返回包含(值,子轨迹。右侧);
}
公共布尔包含(字符串值)//非递归
{
Node subroot=this.root;
while(子轨迹!=null)
{
if(值等于(子轨迹数据))
返回true;
else if(值比较(子轨迹数据)<0)
副回转=副回转。左;
其他的
subroot=subroot.right;
}
返回false;
}
公共整数加总()
{
返回加法(this.root);
}
专用整数加总(节点子程序)
{
如果(子轨迹==null)
返回0;
返回加总(subroot.left)+subroot.data2+加总(subroot.right);
}//data=String,data2=int
公共整数计数()
{
返回计数(this.root);
}
专用整数计数(节点子轨迹)
{
如果(子轨迹==null)
返回0;
返回计数(左副循环)+1+计数(右副循环);
}
公共整数(整数x)
{
返回numberness(this.root,x);
}
专用整数编号(节点子轨迹,整数x)
{
如果(子轨迹==null)
返回0;
如果(x

如何使用我已有的结构完成再平衡方法?我想通过查找中点并递归地对它们排序来使用排序后的arraylist。我不确定如何使用我设置树的方式(使用内部节点类)来实现这一点,因此我希望获得有关这段代码的帮助。

将数组分成两个大小相等的部分。将中间元素作为新的根节点。 然后再次拆分这两部分,并将中间元素作为第二级节点,等等。
最佳递归实现…

为什么要重新发明自行车?查看
AVL
red-black