简体   繁体   English

将常规的二进制搜索树变成平衡的二进制搜索树

[英]Turning a regular Binary Search Tree into an Balanced Binary Search Tree

I was able to write my own Binary Search Tree, but I am having a lot of trouble figuring out how to turn that into a Balanced Binary Search tree. 我能够编写自己的二进制搜索树,但是在弄清楚如何将其转换为平衡二进制搜索树时遇到了很多麻烦。

Could someone help me implement a Balanced binary search tree code with the regular binary tree. 有人可以帮我用常规的二叉树实现平衡二叉树的代码。

I think I was successful in changing my TreeNode class to have the necessary changes. 我认为我成功地将TreeNode类更改为必要的更改。

I added another key and another value along with another TreeNode middle to hold the middle pointer when you get to a 3 node in the tree. 当您到达树中的3个节点时,我添加了另一个键和另一个值以及另一个TreeNode中点,以保持中间指针。

I then added another constructor to hold the case if it was a 3 node. 然后,如果它是一个3节点,那么我添加了另一个构造函数来保存该案例。 I believe I did this right. 我相信我做对了。

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;
}

The tough part comes to when I need to change the actual BST Class. 当我需要更改实际的BST类时,困难的部分就来了。 I know the put is going to change quite a bit because we have to check and see if it is a 2 node or a 3 node, as well as check for what the parent node is. 我知道put将会发生很大的变化,因为我们必须检查它是2节点还是3节点,并检查父节点是什么。

Here is what I have so far: 这是我到目前为止的内容:

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

My difficultly comes most with the recursion. 我最难以实现的是递归。 I understand how basic recursion works, but using it to implement a balanced binary search tree is seeming a much more difficult talk than originally thought. 我了解基本递归的工作原理,但是用它来实现平衡的二进制搜索树似乎比最初想的要困难得多。

You only want a binary search tree, correct? 您只想要一个二叉搜索树,对吗? If so, there isn't really a need for keys (Which are used for M-ary trees). 如果是这样,则实际上并不需要密钥(用于Mary树)。

This isn't exactly an answer, but hopefully this will help simplify your code at least a little bit. 这不是一个确切的答案,但是希望这将至少有助于简化您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM