@Test public void shouldReturnRootElementIfTheTreeContainsTFiveElements() { BinaryNode<Integer> root = new BinaryNode<Integer>(2); BinaryNode<Integer> n1 = new BinaryNode<Integer>(1); BinaryNode<Integer> n3 = new BinaryNode<Integer>(3); BinaryNode<Integer> n4 = new BinaryNode<Integer>(4); BinaryNode<Integer> n5 = new BinaryNode<Integer>(-1); root.setLeft(n1); root.setRight(n3); n3.setRight(n4); n1.setLeft(n5); double median = avlTreeMedian.find(root); assertEquals(2, median, DELTA); }
public double find(BinaryNode<Integer> root) { if (root == null) { throw new IllegalArgumentException("You can't pass a null binary tree to this method."); } List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root); double median = 0; if (sortedElements.size() % 2 == 0) { median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get( sortedElements.size() / 2 - 1).getData()) / 2; } else { median = sortedElements.get(sortedElements.size() / 2).getData(); } return median; }
AvlTreeMedian { public double find(BinaryNode<Integer> root) { if (root == null) { throw new IllegalArgumentException("You can't pass a null binary tree to this method."); } List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root); double median = 0; if (sortedElements.size() % 2 == 0) { median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get( sortedElements.size() / 2 - 1).getData()) / 2; } else { median = sortedElements.get(sortedElements.size() / 2).getData(); } return median; } }
AvlTreeMedian { public double find(BinaryNode<Integer> root) { if (root == null) { throw new IllegalArgumentException("You can't pass a null binary tree to this method."); } List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root); double median = 0; if (sortedElements.size() % 2 == 0) { median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get( sortedElements.size() / 2 - 1).getData()) / 2; } else { median = sortedElements.get(sortedElements.size() / 2).getData(); } return median; } AvlTreeMedian(); }
AvlTreeMedian { public double find(BinaryNode<Integer> root) { if (root == null) { throw new IllegalArgumentException("You can't pass a null binary tree to this method."); } List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root); double median = 0; if (sortedElements.size() % 2 == 0) { median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get( sortedElements.size() / 2 - 1).getData()) / 2; } else { median = sortedElements.get(sortedElements.size() / 2).getData(); } return median; } AvlTreeMedian(); double find(BinaryNode<Integer> root); }
AvlTreeMedian { public double find(BinaryNode<Integer> root) { if (root == null) { throw new IllegalArgumentException("You can't pass a null binary tree to this method."); } List<BinaryNode<Integer>> sortedElements = binaryTreeInOrder.getIterative(root); double median = 0; if (sortedElements.size() % 2 == 0) { median = (sortedElements.get(sortedElements.size() / 2).getData() + sortedElements.get( sortedElements.size() / 2 - 1).getData()) / 2; } else { median = sortedElements.get(sortedElements.size() / 2).getData(); } return median; } AvlTreeMedian(); double find(BinaryNode<Integer> root);  }