【发布时间】: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