set8_sol

set8_sol - CS112 Spring 2011 Problem Set 8 Binary Tree 1...

This preview shows pages 1–3. Sign up to view the full content.

CS112 Spring 2011: Problem Set 8 Binary Tree ------------------------------------------------------------------------ 1. * Answer the following questions in terms of /h/, the height of a binary tree: 1. What is the *minimum* possible number of nodes in a binary tree of height /h/? 2. A /strictly/ binary tree is one in which every node has either no children or two children; in other words, there is *no* node that has exactly one child. What is the *minimum* possible number of nodes in a strictly binary tree of height /h/? 3. A /complete/ binary tree is one in which every level *but* the last has the maximum number of nodes possible at that level; the last level may have any number of nodes. What is the *minimum* possible number of nodes in a complete binary tree of height /h/? *SOLUTION* 1. h+1 - one node at every level, and there are (h+1) levels (levels are numbered 0, 1, . .., h) 2. Every level except the root level has 2 nodes. So, 1 + 2*h 3. Level 0 has 2^0 nodes, level 1 has 2^1 nodes, and so on. Level h-1 has 2^(h-1) nodes. The last level has one node. The total is 2^h -1+1 = 2^h . ------------------------------------------------------------------------ 2. Two binary trees are /isomorphic/ if they have the same structure. Implement the following *recursive* method: public static <T> boolean isomorphic(BTNode<T> T1, BTNode<T> T2) { /* your code here */ } that returns *true* if the trees rooted at T1 and T2 are isomorphic, and false otherwise. BTNode is defined as follows: public class BTNode<T> { T data; BTNode<T> left, right; BTNode(T data, BTNode<T> left, BTNode<T> right) { this.data = data; this.left = left; this.right = right; } } *SOLUTION* public static <T> boolean isomorphic(BTNode<T> T1, BTNode<T> T2) { if (T1 == null & T2 == null) return true; if (T1 == null || T2 == null) return false; if (!isomorphic(T1.left, T2.left)) return false; return isomorphic(T1.right, T2.right); } ------------------------------------------------------------------------ 3. ** Exercise E9.3, page 294 of the textbook.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
The /radix tree/ data structure shown below stores the bit strings 0,1,01,010,100, and 1011 in such a way that each left branch represents a 0 and each right branch represents a 1.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

set8_sol - CS112 Spring 2011 Problem Set 8 Binary Tree 1...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online