import java.util.LinkedList; import java.util.Queue; public class IntTreeSolution { private IntTreeNode root; public int specialSumTree() { return specialSumTree(root); } /* * return a sum of the elements in the tree such that if an element is even, * it is counted once, and if an element is odd, it's counted twice */ private int specialSumTree(IntTreeNode current) { int result = 0; if (current != null) { if (current.data % 2 == 1) { result += current.data; } result += current.data + specialSumTree(current.left) + specialSumTree(current.right); } return result; } /* edit the tree such that if a node is not even, it is multiplied by two */ public void makeEvenTree() { this.root = makeEvenTree(this.root); } public IntTreeNode makeEvenTree(IntTreeNode current) { if (current != null) { if (current.data % 2 == 1) { current = new IntTreeNode(current.data * 2, makeEvenTree(current.left), makeEvenTree(current.right)); } else { current.left = makeEvenTree(current.left); current.right = makeEvenTree(current.right); } } return current; } public IntTreeSolution() { this.root = new IntTreeNode(0, new IntTreeNode(1, new IntTreeNode(2), null), new IntTreeNode(3, null, new IntTreeNode(4))); } public boolean contains(int value) { return contains(this.root, value); } private boolean contains(IntTreeNode current, int value) { if (current == null) { return false; } else if (current.data == value) { return true; } else { boolean containedLeft = contains(current.left, value); boolean containedRight = contains(current.right, value); return containedLeft || containedRight; } } public void print() { print(this.root); System.out.println(); } private void print(IntTreeNode current) { if (current != null) { print(current.left); System.out.print(current.data + " "); print(current.right); } } /** This function returns the height of this tree */ public int height() { return height(this.root); } /** This function returns the height of the tree rooted at current */ private int height(IntTreeNode current) { if (current == null) { return 0; } else { return 1 + Math.max(height(current.left), height(current.right)); } } /** This function returns a string made up of n spaces */ private String getSpaces(int n) { String result = ""; for (int i = 0; i < n; i++) { result += " "; } return result; } /** This function prints an ASCII art picture of this tree */ public void printTree() { Queue q = new LinkedList(); q.add(this.root); printTree(q, height()); } /** * This function prints an ASCII art picture of the tree made up of nodes in * q. */ private void printTree(Queue q, int height) { int size = q.size(); String tree = ""; String branches = ""; boolean hasNonNull = false; /* For each node we haven't explored... */ for (int i = 0; i < size; i++) { /* Explore the next node. */ IntTreeNode current = q.remove(); /* Figure out how many spaces should go before and after it */ String before = getSpaces((int) Math.pow(2, height - 1) - 1); String after = getSpaces((int) Math.pow(2, height - 1)); /* * Construct the next part of the line of the actual tree (e.g. the * values in the tree) */ tree += before; if (current != null) { hasNonNull = true; tree += current.data; q.add(current.left); q.add(current.right); } else { tree += " "; q.add(null); q.add(null); } tree += after; /* * Construct the diagonal lines connecting the different rows of the * tree. */ if (before.length() > 0) { branches += before.substring(1); if (current != null && current.left != null) { branches += "/ "; } else { branches += " "; } if (current != null && current.right != null) { branches += "\\"; } else { branches += " "; } } if (after.length() > 0) { branches += after.substring(1); } } /* Print the row we constructed above. */ if (tree.length() > 0) { System.out.println(tree); } if (branches.length() > 0) { System.out.println(branches); } /* * If there are more nodes to explore (and they aren't all null, explore * the rest of the tree recursively. */ if (!q.isEmpty() && hasNonNull) { printTree(q, height - 1); } } public class IntTreeNode { public int data; public IntTreeNode left; public IntTreeNode right; public IntTreeNode(int data) { this(data, null, null); } public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) { this.data = data; this.left = left; this.right = right; } } }