Skip to content

Instantly share code, notes, and snippets.

@nyux
Created August 18, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyux/42baff79b01465665567 to your computer and use it in GitHub Desktop.
Save nyux/42baff79b01465665567 to your computer and use it in GitHub Desktop.

about two years ago, i was taking a class in uni. our last assignment was to implement huffman coding in c. a classmate needed to find some way to represent the path from the root of the tree to a leaf. typically paths are represented as a sequence of 0s and 1s, to determine whether you need to take the left or right child of the node you're at.

the way he represented the path was with a long long. and so there are 255 possible byte values, and a long long is only 64 bytes long. this seems like a bad idea. but he used the digits of a long long instead, and in his scheme a 1 meant to take the left child and a 2 meant to take the right child of a node. his paths were backwards, so '122' meant right -> right -> left. he traversed the tree by dividing the path by 10, and when his path reached '0', he knew he was at a leaf node.

his code still broke when he was given input with too wide a variety of characters. one of the files the professor used to test our code was moby dick, and it made his program spit out nonsense. i spent a few hours trying to help him debug his code, fixed a few other bugs that he hadn't even noticed, but the main problem still remained. so he left me and went to the person who gave him his algorithm in the first place and asked for help. he told him just to make it a uint_128 instead, and it worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment