- Section 17.2

- Each node has â‰¤ 2 children
- Each node has left/right child
- Left/right position matters (unlike in a general tree)
- Decision trees (yes/no children)
- Huffman trees (0/1 encodingâ€”next lecture)
- Binary search trees (lecture after that)
- Expression trees

Consider a tree of height h. What is the smallest number of leaves that the tree can have?

- 1
- h
- 2
^{h - 1} - 2
^{h}- 1

public class BinaryTree { private Node root; public BinaryTree() { root = null; } // An empty tree public BinaryTree(Object rootData, BinaryTree left, BinaryTree right) { root = new Node(); root.data = rootData; root.left = left.root; root.right = right.root; } class Node { public Object data; public Node left; public Node right; } . . . }

What are the leaves of the tree

new BinaryTree("Fred", new BinaryTree("Barney", null, null), new BinaryTree("Dino", new BinaryTree("Wilma", null, null), null));

- Fred
- Wilma
- Barney and Wilma
- Something else

What is wrong with this implementation of the height method of a BinaryTree class?

public class BinaryTree { . . . public int height() { return height(root); } class Node { private int height(Node n) { if (n == null) { return 0; } else { return 1 + Math.max(height(n.left), height(n.right)); } } } . . . }

- The private height method should have been static
- The public height method should have been static
- The returned values are off by one from the actual heights
- There is nothing wrong--the method works correctly

What is wrong with this implementation of the height methodof a BinaryTre class?

public class BinaryTree { . . . public int height() { return height(root); } class Node { private static int height(Node n) { if (n == null) { return 0; } else { return Math.max(height(n.left), height(n.right)); } } } . . . }

- The public height method should have been static
- The method returns the wrong value for every tree
- The method returns the wrong value for every non-empty tree
- There is nothing wrong--the method works correctly