1 / 13

CSE 246 Data Structures and Algorithms

CSE 246 Data Structures and Algorithms. Spring2012 Lecture#17. Binary tree representation using Array. Binary tree Traversal. Three classic way to traverse a binary tree Preorder=53 30 14 9 23 39 34 47 72 61 84 79 Inorder = 9 14 23 30 34 39 47 53 61 72 79 84

meg
Download Presentation

CSE 246 Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 246Data Structures and Algorithms Spring2012 Lecture#17

  2. Binary tree representation using Array Quratulain

  3. Binary tree Traversal • Three classic way to traverse a binary tree • Preorder=53 30 14 9 23 39 34 47 72 61 84 79 • Inorder= 9 14 23 30 34 39 47 53 61 72 79 84 • Postorder= 9 23 14 34 47 39 30 61 79 84 72 53 Quratulain

  4. Delete to Binary Search Trees • There are three possibilities: • To delete a leaf node (no children): disconnect it. • To delete a node with one child: bypass the node and directly connect to the child. • To delete a node with two children: find the smallest node in its right subtree (or the largest node in its left subtree), copy the minimum value into the info field of the "node to be deleted" and then delete the minimum node instead, which can only have a right child, so the situation becomes one of the above two.

  5. Binary Search Trees vs. Arrays • Same O(log2N) search • Better insertion time: O(log2N) vs. O(N) • Better deletion • What is worse? O(N) • BST requires more space - 2 references for each data element

  6. Conversion of degenerated tree to a binary balance tree • If the tree is degenerated then it may be completely or partially sorted. • Store the tree into array in a sorted order. • Find the first mid and store it as root of a tree. • The first mid divide the list into two halves • Repeat the process to find mid of each sub-list and include as nodes in tree Quratulain

  7. Conversion of degenerated tree to a binary balance tree 66 44 55 88 55 44 77 66 77 88 Quratulain

  8. Huffman Codes • Binary trees can be used in an interesting way to construct minimal length encodings for messages when the frequency of letters used in the messages is known. • A special kind of binary tree, called a Huffman coding tree is used to accomplish this.

  9. Huffman Codes • Huffman is a coding algorithm presented by David Huffman in 1952. It's an algorithm which works with integer length codes. • Huffman is the best option because it's optimal. • The position of the symbol depends on its probability. Then it assigns a code based on its position in the tree. The codes have the prefix property and are instantaneously decodable thus they are well suited for compression and decompression.

  10. Example • suppose we know that the frequency of occurrences for six letters in a message are as given below: • To build the Huffman tree, we sort the frequencies into increasing order (4, 5, 7, 8, 12, 29). Then we choose the two smallest values, 4 and 5, and construct a binary tree with labeled edges:

  11. Next, we replace the two smallest values 4 and 5 with their sum, getting a new sequence, (7, 8, 9, 12, 29). We again take the two smallest values and construct a labeled binary tree. • We now have the frequencies (15, 9, 12, 29) which must be sorted into (9, 12, 15, 29) and the two lowest are selected once again. • Now, we combine the two lowest which are 15 and 21 to give the tree.

  12. The two remaining frequencies, 36 and 29, are now combined into the final tree. Notice that it does not make any difference which one is placed as the left subtree and which in the right subtree.

  13. From this final tree, we find the encoding for this alphabet • Using this code, a message like SENT would be coded as 01111000001 • If the message had been coded in the "normal" way, each letter would have required 3 bits. The entire message is 65 characters long so 195 bits would be needed to code the message (3*65). Using the Huffman code, the message requires 1*29+4*5+3*12+3*7+4*4+3*8=146 bits. • This code can be applied to the English Language by using average frequency counts for the letters.

More Related