720 likes | 801 Views
2 1 0. 10 10 10. Base 10 digits {0...9}. 6 1 3. 6 x 100+ 1 x 10 + 3 x 1. =613. Base 2 digits {0, 1}. 3. 2. 1. 0. 2. 2. 2. 2. 1 1 0 1. 1 x 8+ 1 x 4+ 0 x 2+ 1 x 1. = 13.
E N D
2 1 0 10 10 10 Base 10 digits {0...9} 6 1 3 6 x 100+ 1 x 10 + 3 x 1 =613
Base 2 digits {0, 1} 3 2 1 0 2 2 2 2 1 1 0 1 1 x 8+ 1 x 4+ 0 x 2+ 1 x 1 = 13
A binary tree consists of a set of nodes each of which can have at most two children. parent Left child right child
root The top node of the tree is called the root. A leaf is a node with no descendants. leaf leaf leaf
The binary digits (bits) in the computer’s memory are initially set to zero. To represent a number, the appropriate bits are set to 1. 0 0 0 0 3 2 1 0 2 2 2 2 1 1 0 1 13 (dec)
4 (dec) = 000100 Multiplying by 2 in machine language is accomplished by shifting left one bit.
4 (dec) = 000100 Multiplying by 2 in machine language is accomplished by shifting left one bit. 8 (dec) = 001000
4 (dec) = 000100 Multiplying by 2 in machine language is accomplished by shifting left one bit. 8 (dec) = 001000 16 (dec) = 010000
4 (dec) = 000100 5 (dec) = 000101 We obtain the next integer by adding a 1 to the binary number. 8 (dec) = 001000 9 (dec) = 001001 16 (dec) = 010000 17 (dec) = 010001
n 2n 2n+1 Construct a tree using the following: If the parent ‘s node number is n, the left child’s is 2*n and the right child‘s is 2*n + 1.
1 We assign 1 to the root’s node number
1 2 Then, the left child’s node number is 2
1 3 2 And the right child’s node number is 3.
1 0 2 3 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge.
1 0 2 3 0 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge.
1 0 2 3 0 0 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge.
1 1 0 2 3 0 0 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge and a 1 on each right edge of a binary tree.
1 1 0 2 3 1 0 0 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge and a 1 on each right edge of a binary tree.
1 1 0 2 3 1 0 1 0 4 5 6 7 A graphical way of getting the binary equivalents of decimal numbers. Place a 0 on each left edge and a 1 on each right edge of a binary tree.
1 0 2 3 0 1 4 5 6 7 1 To convert 5 to binary, start by writing the lower-most 1 on the path from node 5 to the root.
1 0 2 3 0 1 4 5 6 7 01 To the left of the 1, write the digit for the next edge on the upward path to the root, namely, 0.
1 0 2 3 0 1 4 5 6 7 101 Finally, to the left of the 0, place a 1. This represents the node number of the root, 1, which is the same in binary and decimal.
1 1 0 2 3 1 0 1 0 4 5 6 7 100 101 110 111 The node numbers at the leaves converted to binary numbers.
1 1 0 1 2 3 10 11 1 0 1 0 4 5 6 7 100 101 110 111 Placing a 0 on each left edge is equivalent to shifting left, ie., multiplying by 2. Placing a 1 on the right edge means you are adding 1 to the left child’s value.
Express 67 in decimal 33 1 (bin) 1 67
16 1 33 11 (bin) 1 67
8 0 16 1 33 011 (bin) 1 67
4 0 8 0 16 1 33 0011 (bin) 1 67
2 0 4 0 8 0 16 1 33 00011 (bin) 1 67
1 0 2 0 4 0 8 0 16 1 33 000011 (bin) 1 67
1 0 Place 1 at the left since the root node contains 1. 2 0 4 0 8 0 16 1 33 67 (dec) = 1000011 (bin) 1 67
American Standard Code for Information Interchange The ascii code in decimal and binary for some characters. Thus it requires 7 bits to represent each character.
0 1 Symbol a: 0 The code 001 can be decoded as aac or bc. Thus the code is ambiguous. c a Symbol b: 00 0 Symbol c : 1 b
0 Symbol a: 0 The code 01 is decoded as b. Before, however, you reach the end of the string 01, you would think that 0 corresponds to a. The code requires you to scan ahead. This is called non-instantaneous code and is inefficient as coding scheme. a Symbol b: 01 1 b
If the characters are only in the leaves, the code is unique and instantaneous. Such a code exhibits the prefix property. 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
Let’s decode 10001 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 c 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 c 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 c 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 ca 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 ca 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 ca 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
10001 cab 0 1 Symbol a: 00 c Symbol b: 01 1 0 a Symbol c: 1 b
Letters occurring in a paragraph and their frequency of occurrence. How can we encode these letters so that the resultant code is minimal?
A list of nodes containing the letters and frequencies. The list is sorted by frequency. b, 2 h, 4 g, 9 a, 11
Remove the first two nodes, add their frequencies, and create a parent node with that frequency. Letters will appear in only the leaves of the final tree. *, 6 g, 9 a, 11 b, 2 h, 4
Insert the parent node with its children in its sorted position in the list. This type of list and its operations is called a priority queue. *, 6 g, 9 a, 11 b, 2 h, 4
Remove the first two nodes again, add their frequencies, and create a parent node with that frequency. *, 15 a, 11 *, 6 g, 9 b, 2 h, 4