|
A stack? A stack is the definition of recursion
No?
Edit: ok looked it up.
So you traverse one million items with nested loops. in the worst case csenario that's million times million loops.
modified 20-Oct-19 21:02pm.
|
|
|
|
|
well recursion uses a stack, but not the other way around
in fact, right now, i need to retool a function in my SortedSplayTreeDictionary to use a stack instead of full recursion to do what it's doing, because otherwise my dictionary can only hold between 5k and 10k items
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
As far as I know the only recursive limitation is the call stack, with a balanced tree (binary as a worst case scenario) the number of nodes is 2^depth of tree. 16K elements is 14 calls as far as I remember?
In C# you can do this:
var stackSize = 10000;
Thread thread = new Thread(new ThreadStart(BigRecursion), stackSize);
Thread Class (System.Threading) | Microsoft Docs
modified 20-Oct-19 21:02pm.
|
|
|
|
|
This is a splay tree. Worst case is different because it's doing tree rotations.
Yes, I could increase the stack size but that's not really the proper way to do this - that's like cleaning your carpet with a blowtorch - i mean sure, it gets rid of the filth, but...
but i am tempted. THis is the function i need to make non-recursive:
_Node _Splay(_Node root, TKey key)
{
int c;
if (null==root || 0==(c = _comparer.Compare(root.Key, key)))
return root;
if (0<c)
{
if (null==root.Left)
return root;
c = _comparer.Compare(root.Left.Key, key);
if (0<c)
{
root.Left.Left = _Splay(root.Left.Left, key);
root = _Ror(root);
}
else if (0>c)
{
root.Left.Right = _Splay(root.Left.Right, key);
if (null!=root.Left.Right)
root.Left = _Rol(root.Left);
}
return (null==root.Left) ? root : _Ror(root);
}
else
{
if (null==root.Right)
return root;
c = _comparer.Compare(root.Right.Key, key);
if (0<c)
{
root.Right.Left = _Splay(root.Right.Left, key);
if (null!=root.Right.Left)
root.Right = _Ror(root.Right);
}
else if (0>c)
{
root.Right.Right = _Splay(root.Right.Right, key);
root = _Rol(root);
}
return (null==root.Right ) ? root : _Rol(root);
}
}
=( =( =(
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
You will end up with nested while loops. It will be vastly less efficient:
Inorder Tree Traversal without Recursion - GeeksforGeeks
void inOrder(struct Node *root)
{
stack<Node *> s;
Node *curr = root;
<pre>
while (curr != NULL || s.empty() == false)
{
/* Reach the left most Node of the
curr Node /
while (curr != NULL)
{
/ place pointer to a tree node on
the stack before traversing
the node's left subtree */
s.push(curr);
curr = curr->left;
}
/* Current must be NULL at this point */
curr = s.top();
s.pop();
cout << curr->data << " ";
/* we have visited the node and its
left subtree. Now, it's right
subtree's turn */
curr = curr->right;
} /* end of while */
}
modified 20-Oct-19 21:02pm.
|
|
|
|
|
I'm not so sure that will work because I need to go right left right right left like that.
your solution looks like once it decided on the left path it would stay on the left path.
(Never mind the above, I read the code wrong. Whoops)
Unfortunately what I think i need is a state machine inside a single while loop, which is rough
While loops inside while loops aren't necessarily performance killers. It depends on how much the while loop is executed and in this case it doesn't matter because for every iteration it was making a function call anyway.
No matter what you do, the correct non-recursive implementation will be at least as, or more likely, more efficient than this version.
The reason is you're using less stack overall and doing the exact same number of pass throughs.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
modified 13-Sep-19 14:31pm.
|
|
|
|
|
|
Message Removed
modified 13-Sep-19 9:26am.
|
|
|
|
|
Pique - the French breath is yellow and black! (10)
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hufflepuff?
One of the school houses (I think)
Pique = huff The French = le Breath = puff
Guessing the house colours are yellow and black ...
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
Is the correct answer - you are up Monday.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
But the same theme:
Have always really rated your pick of two tea-eating ruffians - to start with, anyway! (5, 6)
Real CCC at usual time ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
So close!: "Dotty ..."
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I really thought I was onto something...
|
|
|
|
|
Or was it "Hairy ..."? I'm getting all confused.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ah, that's the one character that I've actually heard of!
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
That's why I didn't use it as the "official CCC"
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Griff - you're a wizard at these things...
This space for rent.
|
|
|
|
|
Nemesea - Sayonara[^]
Deciding on this week's SOTW was hard!
I had a few contestants and the Dutch Nemesea was one of them.
I've been listening their latest album on repeat, which is pretty rare for an entire album!
So I went for Nemesea, but then I had to pick one song.
All songs on the album are good.
I even like the soft songs, like Lion.
I went for Sayonara because it's rather catchy, but I can recommend looking up the entire album on Spotify.
Nemesea started out as gothic metal which fitted perfectly in the Dutch tradition of After Forever, Within Temptation and Epica.
They've strayed from the metal path and have adopted a more poppy melodic rock sound.
The new singer on this album, Sanne Mieloo, also plays in musicals.
Enjoy
|
|
|
|
|
Nemesea sounds great …
but I had only 37 seconds time to listen because someone
opened Pandora's box at my work to please me I suppose,
so I bookmark Nemesea for the weekend …
and my other bookmarks are
- new AILD with TL …
- new Agonist (had no chance to check them yet …)
Cheers,
|
|
|
|
|
I don't really know As I Lay Dying, only by name.
New stuff sounds good though.
|
|
|
|
|
"Your basic 'Prime Instant' service includes a 99% discount on everything Amazon sells ! And:
1 a monthly delivery by courier of twenty foil-packed sets of red and blue gel capsules.
2 a special set of bluetooth-enabled glasses (batteries not included).
Purchase and Check-out:
Select the items you want, go to check-out, approve the order:
1 Put on the glasses, wait until the check-out screen verifies the blue-tooth connection is enabled, and your credit card approves the purchase.
2 When the red background screen is flashing, take one red capsule. Keep your eyes on the screen for thirty seconds until the red screen stops flashing.
3 When the blue screen starts flashing, take one blue capsule. Keep your eyes on the screen for thirty seconds until the blue screen stops flashing.
When the order is complete you may feel a moment of dizziness: do not worry, this is normal.
Now, enjoy the memories of your new, instantly delivered, items being virtually present, and your happy times using them, forever !
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Take a red pill.
There's just one fly in your ointment. Given that Kindle books sell for anything up to 100% of the price of the dead tree edition (you only save on the shipping), why should Amazon give 99% discounts on other virtual items?
Now take the blue pill.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Got a new job. And also a serious case of déjà vu.
- Hey, I've cloned a repo and managed to compile the project. Just wondering... where are tests?
- Well, look for it...
(...)
- So, the client didn't want to pay us for tests.
- And for last three months you've been doing nothing but bug-fixing?
- Pretty much. Look, we all know what you mean, but in the end of the day it's up to the financial department. They pay me for my time. If I'm wasting it on doing shish because of their bad decisions, it's not my problem.
I just... gave up. It's all the same everywhere.
|
|
|
|