Consider a tree of height h. What is the smallest number of leaves that the tree can have?
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));
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)); } } } . . . }
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)); } } } . . . }