test2Fsoln - root.left = removeLeaves(root.left);...

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

View Full Document Right Arrow Icon
1a. ArrayList<String> styles = map.get(name); if (styles == null) { styles = new ArrayList<String>(); map.put(name, styles); } styles.add(breed); 1b. Set<String> allStyles = new TreeSet<String>(); for (ArrayList<String> styles: map.values()) { for (String style: styles) { allStyles.add(style); } } for (String style: allStyles) { System.out.print(style + ":"); for (String name: map.keySet()) { if (map.get(name).contains(style)) { System.out.print(" " + name); } } System.out.println(); } 2. public void removeLeaves() { // Removes all of the leaves in this binary tree. root = removeLeaves(root); } private BTNode<E> removeLeaves(BTNode<E> root) { if (root == null) return null; if (root.left == null & root.right == null) return null;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: root.left = removeLeaves(root.left); root.right = removeLeaves(root.right); return root; } Alternate solution: public void removeLeaves() { // Removes all of the leaves in this binary tree. if (root == null) return; if (root.left == null &amp;&amp; root.right == null) { root = null; return; } removeLeaves(root); } private void removeLeaves(BTNode&lt;E&gt; node) { if (node.left != null) { if (node.left.left == null &amp;&amp; node.left.right == null) { node.left = null; } else { removeLeaves(node.left); } } if (node.right != null) { if (node.right.left == null &amp;&amp; node.right.right == null) { node.right = null; } else { removeLeaves(node.right); } } }...
View Full Document

Page1 / 2

test2Fsoln - root.left = removeLeaves(root.left);...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online