Binary Trees
Binary Trees Trees with no more than two children per node We call the children of a node left child and right child 11/29/2018
Binary Trees Root Left child Right child Right Sub-tree 11/29/2018
Recursive Definition of Binary Trees A binary tree, T, is either empty or such that: T has a root node; T has two sets of nodes, LT and RT, called the left sub-tree and right sub-tree of T, respectively; LT and RT are binary trees Left Sub-tree Right Sub-tree 11/29/2018
Full Binary Tree Every leaf node has the same depth Every non-leaf node has two children Every non-leaf node has two children Every leaf node has the same depth 11/29/2018
Full Binary Tree Not full Not full Non-leaf node with only one child Leaf nodes with different depths Not full Not full Non-leaf node with only one child 11/29/2018
Number of Nodes in a Full Binary Tree Depth Nodes at Depth Total Nodes 1 2 3 4 n 1 N0=1 2 N1=1+2=3 4 N2=3+4=7 8 N3=7+8=15 16 N4=15+16=31 Nn=Nn-1+2n =2n+1-1 2n 11/29/2018
Only one place to add another node Complete Binary Tree Every level but lowest has all possible nodes If lowest level is not full, all nodes as far left as possible Only one place to add another node 11/29/2018
Binary Tree Applications Expression trees Decision Trees Huffman codes Search trees (next topic) 11/29/2018
Arithmetic Expression Tree Binary tree associated with an arithmetic expression non-leaf nodes: operators leaf nodes: operands Example: arithmetic expression tree for the expression (2 (a - 1)) + (3 b) + 2 (a - 1) 3 b 3 b 2 a-1 a 1 - 11/29/2018
Decision Tree Binary tree associated with a decision process non-leaf nodes: questions with yes/no answer leaf nodes: decisions Example: mail service decision 11/29/2018
Decision Tree YES NO YES NO NO YES Is speed of delivery important? Is contents valuable? YES NO Is contents fragile? NO YES Insured air freight Air freight Rail freight Bus parcel express 11/29/2018
Huffman codes Used for signal compression Suppose we have a message consisting of 5 symbols, e.g. [►♣♣♠☻►♣☼►☻] How can we code this message using 0/1 so the coded message will have minimum length (for transmission or saving!) 5 symbols at least 3 bits (fixed length codes) For a simple encoding, length of code is 10*3=30 bits 11/29/2018
Huffman codes Uses variable length codes Intuition: Those symbols that are more frequent should have smaller codes For Huffman code, length of encoded message will be ►♣♣♠☻►♣☼►☻ =3*2 +3*2+2*2+3+3=24bits 11/29/2018
Huffman Coding Algorithm 1. Take the two least probable symbols: Assign them longest codes, equal length, differing in last digit 2. Combine these two symbols into a single Virtual symbol, and repeat. 11/29/2018
Huffman Coding Algorithm Symbols/Probability ={ a/0.25 , b/0.25 , c/0.2 , d/0.15 , e/0.15 } 1.0 1 0.55 1 0.45 1 0.3 1 a 0.25 b 0.25 c 0.2 d 0.15 e 0.15 00 10 11 010 011 11/29/2018
Binary Tree Representations There are two ways to represent a Tree: Arrays Linked nodes 11/29/2018
Array Representation of Binary Trees Number the nodes as follows: Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right. 1 2 3 4 6 5 7 8 9 11/29/2018 10 11 12 13 14 15
Array Representation of Binary Trees Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent. Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child. Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child. 11/29/2018
Array Representation of Binary Trees Number the nodes as described earlier. The node that is numbered i is stored in tree[i]. b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 5 10 a b c d e f g h i j 11/29/2018
Array Representation of Binary Trees 1 3 c 7 d 15 tree[] 5 10 a - b c 15 d An n node binary tree needs an array whose length is between n+1 and 2n. 11/29/2018
Linked Representation of Binary Trees Each binary tree node is represented as an object whose data type is BTNode. class BTNode { Object data; // any type BTNode left; // left sub-Tree BTNode right; //right sub-Tree } 11/29/2018
Linked Representation of Binary Trees class BTNode { Object data; // any type BTNode left; // left sub-Tree BTNode right; //right sub-Tree } 11/29/2018
Generic Binary node structures Use a generic type parameter for the data class BTNode<E> { E data; BTNode left; BTNode right; } 11/29/2018
BTNode class (assuming Object data) Constructor Public BTNode( Object initialData, BTNode initialLeft, BTNode initialRight ) { data = initialData; left = initialLeft; right = initialRight; } 11/29/2018
BTNode class (assuming Object data) Methods regular accessors and manipulators e.g., public BTNode getLeft() //Gets reference to left child { return left; } public void setData(Object newData) //Set data of this node data = newData; 11/29/2018
BTNode class (assuming Object data) Methods properties of node in tree e.g., public boolean isLeaf() { return (left == null) && (right == null); } 11/29/2018
BTNode class (assuming Object data) Methods tree methods e.g., public void preOrderPrint() public BTNode removeLeftmost() static tree methods e.g., public static treeCopy(BTNode source) public static int treeSize(BTNode root) public static int height() // not in Main 11/29/2018
public BTNode removeLeftmost() starting from root, how do you find the leftmost node? how do you remove the leftmost node and repair the tree structure? 11/29/2018
public BTNode removeLeftmost() How do you find the leftmost node? follow left child links to node with no left child 11/29/2018
public BTNode removeLeftmost() How do you remove the leftmost node and repair the tree structure? leftmost node has no left child attach right child (may be null) to parent of leftmost node as left child 11/29/2018
public BTNode removeLeftmost() { if (left==null) return right; left = left.removeLeftmost(); return this; } 11/29/2018
Traversals of Binary Trees Pre-order In-order Post-order 11/29/2018
Traversals of Binary Trees Pre-order Process the root Process the nodes in the left sub-Tree with a recursive call Process the nodes in the right sub-Tree with a recursive call 1 2 3 11/29/2018
Traversals of Binary Trees Pre-order public void printPreOrder() { System.out.println(data); //1 if (left != null) left.printPreOrder(); //2 if (right != null) right.printPreOrder(); //3 } 1 2 3 11/29/2018
Traversals of Binary Trees In-order Process the nodes in the left sub-Tree with a recursive call Process the root Process the nodes in the right sub-Tree with a recursive call 1 3 2 11/29/2018
Traversals of Binary Trees In-order public void printInOrder() { if (left != null) left.printInOrder(); //1 System.out.println(data); //2 if (right != null) right.printInOrder(); //3 } 1 3 2 11/29/2018
Traversals of Binary Trees Post-order Process the nodes in the left sub-Tree with a recursive call Process the nodes in the right sub-Tree with a recursive call Process the root 1 2 3 11/29/2018
Traversals of Binary Trees Post-order public void printPostOrder() { if (left != null) left.printPostOrder(); //1 if (right != null) right.printInOrder(); //2 System.out.println(data); //3 } 1 2 3 11/29/2018
Recursive method: height calculating height of a tree of BTNode: (=height of root) height: length of path to deepest descendent leaf Recursive design: BASE CASES: height of empty tree is -1 height of one-node tree is 0 RECURSIVE CASES: height is 1 more than height of higher child 3 2 1
Recursive view of the node height calculation: HT = Max (HL + 1, HR + 1) 11/29/2018 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
Recursive method: height calculating height of a tree of BTNode: (=height of root) public static int height(BTNode b) { if (b == null) return -1; return 1 + Math.max(height(b.left),height(b.right)); }
Recursive view used to calculate the size, ST, of a tree: ST = SL + SR + 1. 11/29/2018 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
Recursive method: size of tree How many nodes in a tree? write recursive method with header: static int size (BTNode b) { if (b == null ) return 0; return 1 + size(b.left) + size(b.right); }