Class TernaryTree
Ternary Search Tree.
A ternary search tree is a hybrid between a binary tree and a digital search
tree (trie). Keys are limited to strings. A data value of type char is stored
in each leaf node. It can be used as an index (or pointer) to the data.
Branches that only contain one key are compressed to one node by storing a
pointer to the trailer substring of the key. This class is intended to serve
as base class or helper class to implement Dictionary collections or the
like. Ternary trees have some nice properties as the following: the tree can
be traversed in sorted order, partial matches (wildcard) can be implemented,
retrieval of all keys within a given distance from the target, etc. The
storage requirements are higher than a binary tree but a lot less than a
trie. Performance is comparable with a hash table, sometimes it outperforms a
hash function (most of the time can determine a miss faster than a hash).
The main purpose of this java port is to serve as a base for implementing
TeX's hyphenation algorithm (see The TeXBook, appendix H). Each language
requires from 5000 to 15000 hyphenation patterns which will be keys in this
tree. The strings patterns are usually small (from 2 to 5 characters), but
each char in the tree is stored in a node. Thus memory usage is the main
concern. We will sacrifice 'elegance' to keep memory requirements to the
minimum. Using java's char type as pointer (yes, I know pointer it is a
forbidden word in java) we can keep the size of the node to be just 8 bytes
(3 pointers and the data char). This gives room for about 65000 nodes. In my
tests the english patterns took 7694 nodes and the german patterns 10055
nodes, so I think we are safe.
All said, this is a map with strings as keys and char as value. Pretty
limited!. It can be extended to a general map by using the string
representation of an object and using the char value as an index to an array
that contains the object values.
This class has been taken from the Apache FOP project (http://xmlgraphics.apache.org/fop/). They have been slightly modified.
Inheritance
System.Object
TernaryTree
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Assembly: Lucene.Net.Analysis.Common.dll
Syntax
Fields
|
Improve this Doc
View Source
BLOCK_SIZE
Declaration
protected const int BLOCK_SIZE = 2048
Field Value
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
m_eq
Pointer to equal branch and to data when this node is a string terminator.
Declaration
Field Value
Type |
Description |
System.Char[] |
|
|
Improve this Doc
View Source
m_freenode
Declaration
protected char m_freenode
Field Value
Type |
Description |
System.Char |
|
|
Improve this Doc
View Source
m_hi
Declaration
Field Value
Type |
Description |
System.Char[] |
|
|
Improve this Doc
View Source
m_kv
This vector holds the trailing of the keys when the branch is compressed.
Declaration
protected CharVector m_kv
Field Value
|
Improve this Doc
View Source
m_length
Declaration
Field Value
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
m_lo
Pointer to low branch and to rest of the key when it is stored directly in
this node, we don't have unions in java!
Declaration
Field Value
Type |
Description |
System.Char[] |
|
|
Improve this Doc
View Source
m_root
Declaration
Field Value
Type |
Description |
System.Char |
|
|
Improve this Doc
View Source
m_sc
The character stored in this node: splitchar. Two special values are
reserved:
- 0x0000 as string terminator
- 0xFFFF to indicate that the branch starting at this node is compressed
This shouldn't be a problem if we give the usual semantics to strings since
0xFFFF is guaranteed not to be an Unicode character.
Declaration
Field Value
Type |
Description |
System.Char[] |
|
Properties
|
Improve this Doc
View Source
Length
Declaration
public virtual int Length { get; }
Property Value
Type |
Description |
System.Int32 |
|
Methods
|
Improve this Doc
View Source
Balance()
Balance the tree for best search performance
Declaration
public virtual void Balance()
|
Improve this Doc
View Source
Clone()
Declaration
public virtual object Clone()
Returns
Type |
Description |
System.Object |
|
|
Improve this Doc
View Source
Find(Char[], Int32)
Declaration
public virtual int Find(char[] key, int start)
Parameters
Type |
Name |
Description |
System.Char[] |
key |
|
System.Int32 |
start |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
Find(String)
Declaration
public virtual int Find(string key)
Parameters
Type |
Name |
Description |
System.String |
key |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
Init()
Declaration
protected virtual void Init()
|
Improve this Doc
View Source
Insert(Char[], Int32, Char)
Declaration
public virtual void Insert(char[] key, int start, char val)
Parameters
Type |
Name |
Description |
System.Char[] |
key |
|
System.Int32 |
start |
|
System.Char |
val |
|
|
Improve this Doc
View Source
Insert(String, Char)
Branches are initially compressed, needing one node per key plus the size
of the string key. They are decompressed as needed when another key with
same prefix is inserted. This saves a lot of space, specially for long
keys.
Declaration
public virtual void Insert(string key, char val)
Parameters
Type |
Name |
Description |
System.String |
key |
|
System.Char |
val |
|
|
Improve this Doc
View Source
InsertBalanced(String[], Char[], Int32, Int32)
Recursively insert the median first and then the median of the lower and
upper halves, and so on in order to get a balanced tree. The array of keys
is assumed to be sorted in ascending order.
Declaration
protected virtual void InsertBalanced(string[] k, char[] v, int offset, int n)
Parameters
Type |
Name |
Description |
System.String[] |
k |
|
System.Char[] |
v |
|
System.Int32 |
offset |
|
System.Int32 |
n |
|
|
Improve this Doc
View Source
Keys()
Declaration
public virtual IEnumerator<string> Keys()
Returns
Type |
Description |
System.Collections.Generic.IEnumerator<System.String> |
|
|
Improve this Doc
View Source
Knows(String)
Declaration
public virtual bool Knows(string key)
Parameters
Type |
Name |
Description |
System.String |
key |
|
Returns
Type |
Description |
System.Boolean |
|
|
Improve this Doc
View Source
PrintStats(TextWriter)
Declaration
public virtual void PrintStats(TextWriter out)
Parameters
Type |
Name |
Description |
System.IO.TextWriter |
out |
|
|
Improve this Doc
View Source
StrCmp(Char[], Int32, Char[], Int32)
Compares 2 null terminated char arrays
Declaration
public static int StrCmp(char[] a, int startA, char[] b, int startB)
Parameters
Type |
Name |
Description |
System.Char[] |
a |
|
System.Int32 |
startA |
|
System.Char[] |
b |
|
System.Int32 |
startB |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
StrCmp(String, Char[], Int32)
Compares a string with null terminated char array
Declaration
public static int StrCmp(string str, char[] a, int start)
Parameters
Type |
Name |
Description |
System.String |
str |
|
System.Char[] |
a |
|
System.Int32 |
start |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
StrCpy(Char[], Int32, Char[], Int32)
Declaration
public static void StrCpy(char[] dst, int di, char[] src, int si)
Parameters
Type |
Name |
Description |
System.Char[] |
dst |
|
System.Int32 |
di |
|
System.Char[] |
src |
|
System.Int32 |
si |
|
|
Improve this Doc
View Source
StrLen(Char[])
Declaration
public static int StrLen(char[] a)
Parameters
Type |
Name |
Description |
System.Char[] |
a |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
StrLen(Char[], Int32)
Declaration
public static int StrLen(char[] a, int start)
Parameters
Type |
Name |
Description |
System.Char[] |
a |
|
System.Int32 |
start |
|
Returns
Type |
Description |
System.Int32 |
|
|
Improve this Doc
View Source
TrimToSize()
Each node stores a character (splitchar) which is part of some key(s). In a
compressed branch (one that only contain a single string key) the trailer
of the key which is not already in nodes is stored externally in the kv
array. As items are inserted, key substrings decrease. Some substrings may
completely disappear when the whole branch is totally decompressed. The
tree is traversed to find the key substrings actually used. In addition,
duplicate substrings are removed using a map (implemented with a
TernaryTree!).
Declaration
public virtual void TrimToSize()