









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










COP 3503 – Computer Science II – CLASS NOTES - DAY # TREES Techniques for defining a tree There are two basic techniques that can be used to define a tree.
Proper descendant: If b is a descendant of a and a b then b is a proper descendant of a and a is a proper ancestor of b. The following diagrams illustrate the various properties of trees. … … There are x total nodes and thus x-1 total edges in the tree shown above. The depth of the tree to node c 1 is 2. Depth(1) = 1. Height(3) = 2, Height(2) = 0 Depth(root) = 0, Height(root) = 3 Node 3 is an ancestor of node 7. Node7 is a proper descendant of node 3. Nodes 4 and 5 are siblings. c b 0 b 1 b n c 0 c 1 c m m m root c 1 2 3 4 5 6 7
Given the conventional tree: This corresponds to the following tree using the first child/next sibling technique. Null pointers are shown in the example above for completeness only, typically they are omitted for clarity.
roo t A B C D E
roo t A B C D G F
Java Implementation public class TreeNode { protected Object data; protected Singly_Linked_List children; protected TreeNode sibling; private TreeNode ( ) { } //defeat improper instantiation public TreeNode (Object x, TreeNode, r) { data = x; sibling = r; children = null; //children = new Singly_Linked_List(null) } //end constructor r null public TreeNode (Object x, Singly_Linked_List sll, TreeNode r) { data = x; children = sll; sibling = r; } //end constructor r header null data children sibling x data children sibling data next null data children sibling x data children sibling
Binary Trees A special case of a general tree in which each node can have at most two children. Called a left child and a right child. Used for many applications such as expression trees, binary searching, Huffman coding, etc.. Expression Trees: have a operator in the root of every subtree (all internal nodes), including the main root and every operand is a leaf node. Huffman Coding Trees: every alphabet symbol is stored in a leaf node and its coding sequence is obtained by following the path from the root node to the leaf. Typically a left link is assigned a “0” code and a right link is assigned a “1” code. Huffman coding is based upon frequency of occurrence for the various letters in the alphabet with more frequently occurring letters assigned to shorter codes thus saving significant amounts of space in data storage, message transmission, etc.. Example: Suppose that we have a four letter alphabet consisting of a, b, c, and d, and e only. To encode four letters requires 2 bits. Suppose that these are assigned as follows: a = 00, b = 01, c = 10, and d = 11. Now suppose that we have a sentence of these letters which is 15 characters long. This sentence will require 30 bits to encode. Suppose that we also have some information about the frequency of occurrence of each of our letters and know that “a” occurs most frequently, followed by b and so on. A Huffman coding tree is built as shown below with the most frequently occurring letters closest to the root. 0 1 0 1 0 1 Reading the new codes from the tree we have: a = 0, b = 10, c = 100, and d = 101. Now suppose our 15 character sentence contains 8 a’s, 4 b’s, 2 c’s, and 1 d. With 1 5 a 7 3 d b b c b
the new code this sentence requires (81) + (42) + (23) + (13) bits = 8 + 8 + 6
Postorder: processed in order left-child/right-child/root public void postorderTrav (BinTree) { if (root != null) { postorderTrav (left); postorderTrav (right); process (root); //assumed method } //endif } //end postorderTrav Expressions An infix expression represented as a tree can be evaluated with an inorder traversal of the tree. public void getExp (BinTreeNode t) { (1) if (t == null) return; (2) if ((t.left != null)) & (t.right != null)) System.out.print (“(“); (3) getEXP (t.left); (4) System.out.print (t.data); (5) getEXP (t.right); (6) if ((t.right.left == null) && (t.right.right == null)) System.out.print (“)”); } //end getEXP Example : Consider the expression “ (a + ((b – c) * d) t
a *
Trace: set t to root of the tree
Tree # Tree #
Tree # 40 30 70 10 32 60 90 35