Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将常规二叉搜索树转换为平衡二叉搜索树_Java_Binary Tree_Binary Search Tree_B Tree_Avl Tree - Fatal编程技术网

Java 将常规二叉搜索树转换为平衡二叉搜索树

Java 将常规二叉搜索树转换为平衡二叉搜索树,java,binary-tree,binary-search-tree,b-tree,avl-tree,Java,Binary Tree,Binary Search Tree,B Tree,Avl Tree,我能够编写自己的二元搜索树,但我在想如何将其转换为平衡的二元搜索树时遇到了很多困难 有人能帮我用普通的二叉树实现一个平衡的二叉搜索树代码吗 我想我成功地改变了我的TreeNode类,使之有了必要的改变 我添加了另一个键和另一个值以及另一个TreeNode中间值,以便在到达树中的3节点时保持中间指针 然后,我添加了另一个构造函数来保存案例(如果它是3节点)。我相信我做得对 public class TreeNode<V> { public int key; public int ke

我能够编写自己的二元搜索树,但我在想如何将其转换为平衡的二元搜索树时遇到了很多困难

有人能帮我用普通的二叉树实现一个平衡的二叉搜索树代码吗

我想我成功地改变了我的TreeNode类,使之有了必要的改变

我添加了另一个键和另一个值以及另一个TreeNode中间值,以便在到达树中的3节点时保持中间指针

然后,我添加了另一个构造函数来保存案例(如果它是3节点)。我相信我做得对

public class TreeNode<V> 
{
public int key;
public int key1;
public V value;
public V value1;
public TreeNode<V> left;
public TreeNode<V> right;
public TreeNode<V> middle;

public TreeNode(int key, V value)
{
    this.key = key;
    this.value = value;
    this.left = null;
    this.right = null;
}

public TreeNode(int key, V value, int key1, V value1)
{
    this.key = key;
    this.key1 = key1;
    this.value = value;
    this.value1 = value1;       
    this.left = null;
    this.right = null;
    this.middle = null;
}
公共类树节点
{
公钥;
公共int键1;
公共价值观;
公共价值1;
公共树节点左;
公共树节点权;
公共树状中间;
公共树节点(int键,V值)
{
this.key=key;
这个值=值;
this.left=null;
this.right=null;
}
公共树节点(int键,V值,int键1,V值1)
{
this.key=key;
此参数为0.key1=key1;
这个值=值;
此值为1.value1=value1;
this.left=null;
this.right=null;
this.middle=null;
}
困难的部分是当我需要更改实际的BST类时。我知道put将发生很大的变化,因为我们必须检查它是2节点还是3节点,以及检查父节点是什么

以下是我到目前为止的情况:

public class BST<V>
{
private TreeNode<V> root;

public BST()
{
    this.root = null;
}

public V get(int key)
{
    return get(root, key);
}

private V get(TreeNode<V> current, int key)
{
    if (current == null)
        return null;
    else if (key == current.key)
        return current.value;
    else if (key < current.key)
        return get(current.left, key);
    else
        return get(current.right, key);
}

public void put(int key, V value)
{
    if (root == null)
        root = new TreeNode<>(key, value);
    else
        put(root, key, value);
}

private void put(TreeNode<V> current, int key, V value)
{
    if (key == current.key)
    {
        current.value = value;
        return;
    }
    else if (key < current.key)
    {
        if (current.left == null)
        {
            current.left = new TreeNode<>(key, value);
            return;
        }
        else
            put(current.left, key, value);
    }
    else
    {
        if (current.right == null)
        {
            current.right = new TreeNode<>(key, value);
            return;
        }
        else
            put(current.right, key, value);
    }
  } 
}
公共类BST
{
独活根;
公共BST()
{
this.root=null;
}
公共V get(int键)
{
返回get(root,key);
}
私有V get(树节点当前,int键)
{
如果(当前==null)
返回null;
else if(key==current.key)
返回当前值;
否则如果(键<当前键)
返回get(current.left,key);
其他的
返回get(current.right,key);
}
公共void put(int键,V值)
{
if(root==null)
根=新树节点(键,值);
其他的
put(根、键、值);
}
私有void put(树节点当前,int键,V值)
{
if(key==current.key)
{
当前值=当前值;
返回;
}
否则如果(键<当前键)
{
if(current.left==null)
{
current.left=新树节点(键,值);
返回;
}
其他的
put(当前、左侧、键、值);
}
其他的
{
if(current.right==null)
{
current.right=新树节点(键,值);
返回;
}
其他的
put(当前、右侧、键、值);
}
} 
}

最困难的是递归。我理解基本递归是如何工作的,但用它来实现平衡的二元搜索树似乎比最初想象的要困难得多。

你只想要一个二元搜索树,对吗?如果是这样,就不需要键(用于M元树)

这不是一个确切的答案,但希望这将有助于简化您的代码至少一点