【问题标题】:Binary Index tree in java [closed]java中的二进制索引树[关闭]
【发布时间】:2016-05-11 17:44:23
【问题描述】:

我正在学习如何实现二叉索引树。我应该如何使用一组字符串作为输入来构建二叉树?我相信有两种数据结构 1)常规二叉树 - 我们在其中进行后序排序 2)二叉索引树

public static void main(String[] args)
{
    String[] docs = {"The idea is that all are strings",
                     "position of the node is considered",
                     "Being positive helps",
                     "I want to learn then add and search in the tree"
                    };
}

Bstnode

 public class BSTNode
{
private String key;
private Object value;
private BSTNode left, right;

public BSTNode( String key, Object value )
{
    this.key = key;
    this.value = value;
}

//if key not found in BST then it is added. If jey already exists then that node's value
//is updated.
public void put( String key, Object value )
{
    if ( key.compareTo( this.key ) < 0 )         
    {             
        if ( left != null )             
        {                 
            left.put( key, value );             
        }             
        else             
        {                 
            left = new BSTNode( key, value );             
        }         
    }         
    else if ( key.compareTo( this.key ) > 0 )
    {
        if ( right != null )
        {
            right.put( key, value );
        }
        else
        {
            right = new BSTNode( key, value );
        }
    }
    else
    {
        //update this one
        this.value = value;
    }
}

//find Node with given key and return it's value
public Object get( String key )
{
    if ( this.key.equals( key ) )
    {
        return value;
    }

    if ( key.compareTo( this.key ) < 0 )
    {
        return left == null ? null : left.get( key );
    }
    else
    {
        return right == null ? null : right.get( key );
    }
}

}

请告诉我一个好的教程或开始构建二叉索引树。

【问题讨论】:

  • 注意:binary index trees 或 BIT(也称为 Fenwick 树)与二叉搜索树(又名 BST)几乎没有关系;它们用于有效查询给定点/节点的运行总计或给定范围内的总和等目的。因此,这些野兽与二叉搜索树完全不同。 IOW,二叉搜索树不是二叉索引树,即使它被用作某种索引。

标签: java dictionary binary-tree binary-search-tree b-tree


【解决方案1】:

TreesLists 在 Java 中应该是通用的。将自己限制在Strings 是没有意义的。

任何关于数据结构的好书都会给你一个好的开始。

我已经在 J​​ava 中完成了这项工作。欢迎您访问源代码:

https://github.com/duffymo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 2013-08-09
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多