Lecture #13 Mon 11 Feb 2002 NEC Homework #3 will come out TODAY!!!!!!! Due in one week. There are fewer functions to write, and although they're a bit more complicated, I think it should be less time consuming that Homework #2, or, maybe about the same amount of time. Start early!!!! Reading Assignment in Harel too. 0. Logarithms (Not Algorithms!) 1. Binary Numerals 2. Trees, revisited 0. What's a logarithm? A logarithm function takes in a base and a number and produces the exponent of that base which produces that number. (Here, the base appears as a part of the function.) Examples: log2(2) = 1 log2(64) = 6 log2(1) = 0 log10(100) = 2 logx(z) = y where x^y = z In this course, when we deal with logarithms, we'll mostly deal with base 2, that is log2 functions. You can think of log2 as "number of doublings" if you'd like. Number of Doublings function: ;;nod: number -> number ;;takes in a number n and returns the number of times that 1 ;;can be doubled before it is greater than or equal to n. (define (nod n) (nod-help n 0 1)) ;;nod-help: number number number -> number ;;takes in numbers n doub and count returns the number of times that count ;;can be doubled before it is greater than or equal to n, added to doub. (define (nod-help n doubs count) (if (>= count n) doubs (nod-help n (add1 doubs) (* 2 count)))) Keep in mind, nod doesn't perfectly represent log2, because (nod 65) = 7 and log2(65) =~ 6.02236. But it will work for perfect powers of two, right? i.e. (nod 64) = 6 and log2(64) = 6. 1. Binary Numerals number vs. numeral: number is an abstract idea. numeral is a representation of that number. The system that we use, with arabic numerals, is base 10, or "decimal". This is because there are 10 different digits. Even though we represent the number 9 with one digit and the number 10 with two, there's nothing so different about the abstract concept of these numbers. It is our numeric system that provides this distinction. Aside: Roman Numerals So, what if we only had two digits instead of ten? Well how would we count? Base10: 0, 1, 2, . . . 9, 10, 11, . . . 19, 20, 21, . . . 29, 30, 31 . . . Base2: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, . . . 0 -> 0 1 -> 1 2 -> 10 3 -> 11 4 -> 100 This suggests how to count in binary numerals: Instead of having a ones place, tens place, hundreds place etc, we'll have a ones place, twos place, fours place, eights place, etc. (corresponding to which branch was taking in preceding tree levels:) E.g., 1 1 0 1 ^ ^ ^ ^ | | | \----- one's place | | \------- two's place | \--------- four's place \----------- eight's place It's like we're going to the store and buying 1 eight-pack, 1 four-pack, 0 two-packs, 1 one-pack (1-1-0-1), for a total of thirteen. You can imagine there's a leading 0 in the sixteen's place, since we also bought 0 sixteen-packs. Conversely, how do we express nineteen as a binary numeral? We realize that to buy nineteen, we want to buy: 1 sixteen pack, plus how many eight packs?, plus how many four packs? Right -- 1 sixteen-pack, 0 eight-packs, 0 four-packs, 1 two-pack and 1 one-pack. meaning that the binary numeral 10011 denotes nineteen. This all works for base 10 as well -- consider buying 1234 items which come in thousand-packs, hundred-packs, etc. You can also think about it in terms of exponents/logarithms: 1 1 0 1 = 13 1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 8 + 4 + 0 + 1 = 13 ---------------------------------------------------------------------------- To get a binary representation of 13: We start with 13, and log2(13) = 3.7000439 . . . , so we truncate that to 3, so we know we have have a 1 in the 2^3 place, or eight's place. 13 - 8 = 5, and log2(5) = 2.3219 . . ., so we truncate that to 2, so we know we have a 1 in the 2^2 place, or four's place. 5 - 4 = 1, and log2(1) = 0, so we know we have a 1 in the 2^0 or one's place. Since this required no truncation, we're done, just fill in the unused places with zeroes! 13 = 1101 ----------------------------------------------------------------------------- So, how does this relate to anything? We'll see more binary numerals later in the course, but we also see them in binary trees! Ever heard of a bit? ("bit" = "BInary digiT") There are 8 bits in a byte and . . . 1024 bytes in a kilobyte, 1024 kb in an MB, 1024 MB in a GB, etc . . . Your data is nothing but bits!!!! Anyone know how many bits are in a "word"? Anyone know another term for 4 bits? 2. Trees, revisited Let's step back and ask, what is the correspondence between size and height of a binary tree? Easy: size >= height But can we further constrain the size in the other direction? size height 1 0 3 1 7 2 2 1 5 2 9 3 size <= 2^(height+1) - 1 Equivalently: height >= log2(size+1) - 1 [In a formal CS class, we would prove this more formally.] We'll use this fact relating heights & sizes later. So, the important thing to remember at this point is that the height of a full binary tree is approximately log2(size); this will be important as we start analyzing algorithms. More next time . . .