java中的二进制索引树

时间:2016-05-11 17:44:24

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

我正在学习如何实现二进制索引树。我应该如何构建一个带有一组字符串作为输入的二叉树?我相信有两种数据结构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 );
    }
}

}

请让我知道一个很好的教程或开始构建二进制索引树。

1 个答案:

答案 0 :(得分:1)

TreesLists在Java中应该是通用的。没有理由限制自己Strings

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

我已经用Java完成了这项工作。欢迎您来源:

https://github.com/duffymo