Reputation: 4897
If one were to implement a tree using a 4GL like C#, and storing it in a database, such as SQL Server 2008, what would the schema/design look like?
In other words, what role would the database play in such an implementation?
Upvotes: 7
Views: 1896
Reputation: 52107
There are several options:
NOTE: In the case of the suffix array, you won't be storing any characters, you'd just store the indexes describing each element, something like this:
CREATE TABLE SUFFIX_ARRAY (
ORDER INT PRIMARY KEY, -- Position in the suffix array.
START INT NOT NULL, -- Position of the starting character of the suffix within the target string.
LONGEST_COMMON_PREFIX INT NOT NULL -- If useful for your application.
)
You'd also have to separately store the "target" string (e.g. in a CLOB in another table).
Upvotes: 6
Reputation: 13046
Tree structures in an RDBMS is usually handled with a combination of cross-reference tables and recursive queries.
Text
================
id -- autoincrement
text -- varchar
Text_Suffix
=================
startingTextId -- fk reference to Text.id
suffixPartId -- fk reference to Text.id
So... with this example data -
Text
=================
1 | lay
2 | er
3 | ing
4 | s
Text_Suffix
==================
1 | 2
1 | 3
1 | 4
2 | 4
You'd use a query like this:
WITH All_Suffixes (id, text) as (SELECT id, text
FROM Text as a
EXCEPTION JOIN Text_Suffix as b
ON b.suffixPartId = a.id
UNION ALL
SELECT b.suffixPartId, a.text + c.text
FROM All_Suffixes as a
JOIN Text_Suffix as b
ON b.startingTextId = a.id
JOIN Text as c
ON c.id = b.suffixPartId)
SELECT *
FROM All_Suffixes
Which should generates results like so:
1 | lay
2 | layer
3 | laying
4 | lays
4 | layers
Upvotes: 4