lab8ans

# lab8ans - if (node == null) return 0; else { int heightLeft...

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

LAB 8 - SAMPLE ANSWERS 1. Tree 1 Tree 2 Tree 5 Tree 6 Tree 7 56 56 11 42 (empty) / \ / \ / 34 78 34 78 22 / \ / \ / \ / 23 40 61 92 23 40 33 / \ / 37 49 44 / 55 etc. Tree 3 Tree 4 56 56 / \ / \ 34 78 34 61 / \ / \ \ 23 40 23 40 92 / \ / 37 49 78 2. public int sum() { return sum(root); // call private helper method } private int sum(BTNode<E> node) { if (node == null) return 0; else return node.data + sum(node.left) + sum(node.right); } 3. public int height() { return height(root); } // The height of a tree rooted at the given node is 1 (for that node) // plus the height of its deeper subtree. private int height(BTNode<E> node) {

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if (node == null) return 0; else { int heightLeft = height(node.left); int heightRight = height(node.right); if (heightLeft > heightRight) return 1 + heightLeft; else return 1 + heightRight; } } 4. public boolean isFull() { return isFull(root); } // The binary tree rooted at the given node is full // if each subtree is full and each subtree has the same height. private boolean isFull(BTNode<E> node) { if (node == null) return true; else return (isFull(node.left) && isFull(node.right) && height(node.left)==height(node.right)); }...
View Full Document

## This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reid-miller during the Spring '09 term at Carnegie Mellon.

### Page1 / 2

lab8ans - if (node == null) return 0; else { int heightLeft...

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

View Full Document
Ask a homework question - tutors are online