Some quick questions here.
a) Is there a handy dandy equation to work out how many nodes, for a complete binary search tree of a given height?
b) and something to reverse that equation, given X nodes, what would be the height?
c) Any good references on AVL Trees would be appreciated. (My textbook is freaking horrible!)
d) Is it forgivable to use some recursive functions in Kernel code?
e) Any advice on how to make recursive calls as... friendly to the stack as possible would be appreciated, as Zenobit currently only has a 4KiB Stack and I dont particularly want to increase it drastically!
Much thanks!
~Zeii.
Binary Tree + Insanity.
Recursion in kernel code is ok, as long as you don't recurse too far, so it really depends a lot on what you expect your maximum tree size to be.
Ofcourse searching a binary tree is easy to do without recursion (well this is the obvious tail-recursive implementation rewritten for explicit iteration):
Doing inserts / deletes without rebalancing is similarly easy to do without recursion. If you can afford a back-link into parent, then it's pretty easy to walk upwards the tree, which allows rotations for rebalancing.
If you can't, then you can take advantage of the fact that if you know the previous node and the current node, then the pointer from the previous node to the current node is redundant, and you can use it to temporarily store the back-pointer upwards instead.
This will allow you to walk back up without any extra storage, although it will require locking the whole tree for the duration of the operation, because looking down from the root the tree won't make sense before you walk it back up and restore the pointers to how they should be.
....
As for information... mm.... Wikipedia has decent information on both AVLs and Red-Blacks (which I personally found easier to figure out, and which sometimes require a bit less rotations by being not quite so strict about balancing, but can lose in average lookup times for the same reason).
....
Anyway, I would suggest you first implement your tree-routines recursively, test that they actually work, and then convert them to interative versions one operation at a time.
Ofcourse searching a binary tree is easy to do without recursion (well this is the obvious tail-recursive implementation rewritten for explicit iteration):
Code: Select all
// compare should return -1, 0 or 1, depending on <, ==, >
tree * find(tree * root, void * value, int (*compare)(void *, void*)) {
tree * current = root;
while(1) switch(compare(value, current->value)) {
case -1: current = current->left; break;
case 0: return current;
case 1: current = current->right; break;
}
}
If you can't, then you can take advantage of the fact that if you know the previous node and the current node, then the pointer from the previous node to the current node is redundant, and you can use it to temporarily store the back-pointer upwards instead.
This will allow you to walk back up without any extra storage, although it will require locking the whole tree for the duration of the operation, because looking down from the root the tree won't make sense before you walk it back up and restore the pointers to how they should be.
....
As for information... mm.... Wikipedia has decent information on both AVLs and Red-Blacks (which I personally found easier to figure out, and which sometimes require a bit less rotations by being not quite so strict about balancing, but can lose in average lookup times for the same reason).
....
Anyway, I would suggest you first implement your tree-routines recursively, test that they actually work, and then convert them to interative versions one operation at a time.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
Hey guys, Thank you for the information.
I've been tinkering away for the past few days, constantly trying to get an AVL tree working in a variety of crazy ways.
And, I think I finally got it. The only part I have to do now is :
- Removal (Shouldn't be too hectic)
- Left-Right cases (Left right left, left right right)
- Right-Left cases (Right left right, right left left).
( Fixing the balances after rotation).
So far, It seems to work okay.
I would really appreciate it if someone would take a look at my current code.
It is my attempt at an Iterative AVL tree, completely in C.
If anyone is interested in checking / helping me understand AVL better, PM me and I will email my code.
If you aren't into looking at other people's code, No problem - Add me on MSN or ICQ . Contact details are in my profile.
~Zeii
I've been tinkering away for the past few days, constantly trying to get an AVL tree working in a variety of crazy ways.
And, I think I finally got it. The only part I have to do now is :
- Removal (Shouldn't be too hectic)
- Left-Right cases (Left right left, left right right)
- Right-Left cases (Right left right, right left left).
( Fixing the balances after rotation).
So far, It seems to work okay.
I would really appreciate it if someone would take a look at my current code.
It is my attempt at an Iterative AVL tree, completely in C.
If anyone is interested in checking / helping me understand AVL better, PM me and I will email my code.
If you aren't into looking at other people's code, No problem - Add me on MSN or ICQ . Contact details are in my profile.
~Zeii
- Attachments
-
- zcapp-avl-0116190307.tar.gz
- (2.92 KiB) Downloaded 62 times