Re: Calling fire and forget methods.
Frans Bouma <[email protected]> Tue, 26 Feb 2008 14:56:48 +0100
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <00ee01c8787f$6e405a30$4ac10e90$@nl> |
> case of couldn't see the wood for the trees there. for info I don't know
> if the tree will be balanced or not, so the array implementation wouldn't
> fit.
unless you get the tree handed to you in the structure you've to work
with, building the binary tree in an array-based approach is always balanced.
Sure, you've to keep it balanced, though that has many advantages.
I'd seriously look into an array based approach, the code is simpler
and many binary tree implementations in various languages use that approach,
so the algorithm is well documented. A balanced version of a binary tree for
example offers a simple calculation to reach any child. With a node-based
tree, you've to traverse the path.
Though, if balancing isn't required, it might indeed be too much
overhead.
One tip which often occurs with 2/multi path tree structures: if
you're not careful, you're creating clones in your code: code which is similar
for left AND right and as this code is intertwined into eachother, it's hard
to recognize. Not sure if your structure is suitable to make things more
general (i.e. work on left/right regardless of left/right with 1 set of
routines, not 2). The less clones you have in your code, the more maintainable
the code is.
FB
>
> Here's the finished method, shaves 10 seconds off of my previous best
> try.
>
>
> internal void Add(T newItem)
> {
> if (this.value == null) //Short cut for the first pass.
> {
> this.value = newItem;
> this.valueComparer = (IComparable)pi.GetValue(value, null);
> return;
> }
>
>
> TreeNode<T> parent = this; //set up our point to the
> current node we're checking
> IComparable newObject = (IComparable)pi.GetValue(newItem, null);
> //Get an IComparable for property to sort by
> while (true) //we exit with a break.
> {
> if(parent.value == null){ //we've found an empty base
> object, fill it in
> parent.value = newItem;
> parent.valueComparer = newObject; //Pass our IComparable
> to save time.
> break; //the real exit to the while.
> }
> //if the parent value is bigger than the new object.
> if (parent.valueComparer.CompareTo(newObject) > 0)
> {
> //Add it to our left side.
> if (parent.leftNode == null)
> {
> parent.leftNode = new TreeNode<T>(pi); //Add the
> left node
> }
> parent = parent.leftNode; //Move our pointer
> }
> else
> {
> if (parent.rightNode == null) //It's either the same
> or bigger than our parent
> {
> parent.rightNode = new TreeNode<T>(pi); //No node to
> fill? add one.
> }
> parent = parent.rightNode; //Move our pointer
> }
> }
> }
>
> ===================================
> This list is hosted by DevelopMentorR http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com