4 ms·
With one of my friends at the university we used to compete for a shorter/nicer implementation of each data structure in the algorithms class we were taking, an
by stiff 14y ago
With one of my friends at the university we used to compete for a shorter/nicer implementation of each data structure in the algorithms class we were taking, and after using pointers-to-pointers in some previous cases (linked lists, binary search trees etc.) and each time ending up with an implementation I really liked, I tried to implement AVL trees[1] the same way, for example an AVL rotation would be done by a call like this:
avl_tree_rotate(avl, &t->left->right->left, &t->left->right, &t->left, t->left->right);
Where avl_tree_rotate looked like this (avl_tree_update just recomputes the height after left or right branch height changes):
static void avl_tree_rotate(avl avl, V **from, V **to, V **parent, V *child) {
if(*from) (*from)->parent = *to ? (*to)->parent : NULL;
if(*to) (*to)->parent = *from ? (*from)->parent : NULL;
avl_tree_swap(avl, from, to);
child->parent = (*parent)->parent;
if(!((*parent)->parent))
avl->root = child;
else if((*parent)->parent->left == *parent)
(*parent)->parent->left = child;
else if((*parent)->parent->right == *parent)
(*parent)->parent->right = child;
(*parent)->parent = child;
*from = *parent;
avl_tree_update(avl, *parent);
avl_tree_update(avl, child);
}
It was one of nicer programming workouts I ever got (the whole class was a great experience and I think every professional programmer should go through implementing all the basic data structures and algorithms in plain C one time), but I never managed to get deletion right, despite spending quite some hours on it, it just went beyond my head. Not that it matters for linked lists, and in this case I made it deliberately harder on myself, but I think the moral of the story is that when you are too clever with implementation details, you might run out of your cleverness when starting to deal with the actual problem domain.
[1] http://en.wikipedia.org/wiki/AVL_tree http://en.wikipedia.org/wiki/AVL_tree
- njs12345 14y agoA fibonacci heap (in almost any language) is also one I would recommend. Definitely the hardest single thing I remember doing in college.
- ZoFreX 14y ago> I think the moral of the story is that when you are too clever with implementation details, you might run out of your cleverness when starting to deal with the actual problem domain. That reminded me of this quote: "Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?" http://en.wikiquote.org/wiki/Brian_Kernighan http://en.wikiquote.org/wiki/Brian_Kernighan
- koenigdavidmj 14y agoThe corollary: code stoned, debug caffeinated, and document drunk.
- SatvikBeri 14y agoBut watch out for the Ballmer peak! I had one particularly bad period of crunch time where I was trying to code a 3-month project in 4 weeks, all while learning a new language/framework. One night, after several hours of being stuck I got sick of it and poured myself a rather large amount of bourbon. Now, I was rather naive when it came to alcohol. I thought the bourbon was 47 proof...turns out it was 47 percent. All I know is that the next day, building maintenance reported hearing singing all through the night, two weeks of snacks were gone, and I woke up in the office at 7 AM, and I found some of the most beautiful and well-written code I'd ever seen, accomplishing about 3 weeks of work in one weird night.