Re: Algorithm for inserting numbers in a list?
- From: Chris F Clark <cfc@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 09 Apr 2008 11:38:27 -0400
I wrote:
On your first pass, you might simply assign consecutive numbers. On
your second pass, if you need to insert a number between two
consecutive elements (n and n+1), you simply add a level, and number
the newly inserted elements n.1, n.2, n.3, etc. On later passes, you
may add numbers with even more components.
Note that the hard case here is where you are continually adding
levels to the left of some sequence, you need a way to add space
there, e.g. by starting counting sequences at 2 (if 1 is your lowest
number, or numbering sequences from 1 if 0 is your lowest number),
reserving 1 (or 0) as the number for entries added to the left edge of
some sub-tree. e.g:
2
3
4
....
then
1.2// added before two
1.3
1.4
...
2
2.2 // added between 2 and 3
2.2.2 // added between 2.2 and 2.3
2.2.3
2.2.3.1.2 // added between 2.2.3 and 2.2.3.2
2.2.3.2 // added between 2.2.3 and 2.2.4
2.2.4
...
2.3
2.4
...
3
4
You can do a similar construction with a modified trinary numbering of
binary trees, 0 means left child, 1 means self, 2 means right child.
0001
001
0021
01
0201
021
0221
1
2001
201
2021
21
2201
221
2221
Note that all numbers of nodes in the tree are a string of 0's and 2's
with a terminal 1. You can use lexicographic ordering to know where
two elements in the tree are relative to each other, and the "key"
(number) is the path to take from the root to find the element in the
tree.
You would also do perfectly well using the constructions Torben and
Andy mentioned (e.g. perhaps better). The rational numbers form a
nice representation of any finite digit sequence. I believe if you
prepend a "0." onto the trinary numbering you have a nice trinary
rational number in the open interval (0,1).
Worth mentioning, is that you can separate the numbering system (how
you form the keys) from the method you use to search for elements in
your "tree". That is, if you number based on an artificial tree that
you never rebalance, you may get quite long keys. However, you can
build a separate search structure which you do rebalance as needed
that allows you to look up the entries quite efficiently and does not
suffer perfromance degradation due to the order elements are added,
except as the actual key comparison does.
Hope this helps,
-Chris
******************************************************************************
Chris Clark Internet: christopher.f.clark@xxxxxxxxxxxxxxxxxxxxxx
Compiler Resources, Inc. or: compres@xxxxxxxxxxxxx
23 Bailey Rd Web Site: http://world.std.com/~compres
Berlin, MA 01503 voice: (508) 435-5016
USA fax: (978) 838-0263 (24 hours)
------------------------------------------------------------------------------
.
- References:
- Algorithm for inserting numbers in a list?
- From: Chris
- Re: Algorithm for inserting numbers in a list?
- From: Chris F Clark
- Algorithm for inserting numbers in a list?
- Prev by Date: Re: How can I tell if F is a string or if it is a number?
- Next by Date: Re: Algorithm for inserting numbers in a list?
- Previous by thread: Re: Algorithm for inserting numbers in a list?
- Next by thread: Re: Algorithm for inserting numbers in a list?
- Index(es):
Relevant Pages
|