所謂分治算法:就是把一個複雜的問題分紅兩個或更多的相同或類似的子問題,再把子問題分紅更小的子問題……直到最後子問題能夠簡單的直接求解node
下面展現一個有序數組轉二分查找樹的實現算法
1 /** 2 * 二叉樹的典型結構實現 3 * 4 * @author kangye 5 * @param <T> 6 */ 7 public class BinaryNode<T> { 8 9 private final T data; 10 private BinaryNode<T> left; 11 private BinaryNode<T> right; 12 13 public BinaryNode(T data) { 14 this.data = data; 15 } 16 17 public T getData() { 18 return data; 19 } 20 21 public BinaryNode<T> getLeft() { 22 return left; 23 } 24 25 public void setLeft(BinaryNode<T> left) { 26 this.left = left; 27 } 28 29 public BinaryNode<T> getRight() { 30 return right; 31 } 32 33 public void setRight(BinaryNode<T> right) { 34 this.right = right; 35 } 36 37 public boolean hasLeft() { 38 return left != null; 39 } 40 41 public boolean hasRight() { 42 return right != null; 43 } 44 45 @Override 46 public boolean equals(Object o) { 47 if (this == o) 48 return true; 49 if (!(o instanceof BinaryNode)) 50 return false; 51 52 BinaryNode that = (BinaryNode) o; 53 54 if (!data.equals(that.data)) 55 return false; 56 if (left != null ? !left.equals(that.left) : that.left != null) 57 return false; 58 if (right != null ? !right.equals(that.right) : that.right != null) 59 return false; 60 61 return true; 62 } 63 64 @Override 65 public int hashCode() { 66 int result = data.hashCode(); 67 result = 31 * result + (left != null ? left.hashCode() : 0); 68 result = 31 * result + (right != null ? right.hashCode() : 0); 69 return result; 70 } 71 72 @Override 73 public String toString() { 74 return "BinaryNode{" + "data=" + data + '}'; 75 } 76 }
1 /** 2 * 將有序數組轉換成一顆二叉查找樹 3 * 4 * @author kangye 5 */ 6 public class SortedArrayToBST { 7 8 /** 9 * 驗證 10 * 11 * @author kangye 12 * @param sortedArray 13 * @return 14 */ 15 public BinaryNode<Integer> transform(Integer[] sortedArray) { 16 if (sortedArray == null || sortedArray.length == 0) { 17 throw new IllegalArgumentException("You can't use a null or empty array."); 18 } 19 return transformToBST(sortedArray, 0, sortedArray.length - 1); 20 } 21 22 /** 23 * 分治算法 把一個複雜的問題分紅:兩個或更多的相同或類似的子問題,再把子問題分紅更小的子問題……直到最後子問題能夠簡單的直接求解 24 * 25 * @author kangye 26 * @param sortedArray 有序數組, 在分支中仍然保留完整數組 27 * @param bottom 起始位置 28 * @param top 結束爲止 29 * @return 30 */ 31 private static BinaryNode<Integer> transformToBST(Integer[] sortedArray, int bottom, int top) { 32 int center = (top + bottom) / 2; 33 if (sortedArray.length == 1) { 34 return new BinaryNode<Integer>(sortedArray[0]); 35 } else if (bottom > top) { 36 return null; 37 } else { 38 BinaryNode node = new BinaryNode<Integer>(sortedArray[center]); 39 node.setLeft(transformToBST(sortedArray, bottom, center - 1)); 40 node.setRight(transformToBST(sortedArray, center + 1, top)); 41 return node; 42 } 43 } 44 45 }
1 /** 2 * @author kangye. 3 */ 4 public class SortedArrayToBSTTest { 5 6 private SortedArrayToBST sortedArrayToBST; 7 8 @Before 9 public void setUp() { 10 sortedArrayToBST = new SortedArrayToBST(); 11 } 12 13 @Test(expected = IllegalArgumentException.class) 14 public void shouldNotAcceptNullArrays() { 15 sortedArrayToBST.transform(null); 16 } 17 18 @Test(expected = IllegalArgumentException.class) 19 public void shouldNotAcceptAnEmptyArray() { 20 Integer[] emptyArray = new Integer[0]; 21 sortedArrayToBST.transform(emptyArray); 22 } 23 24 @Test 25 public void shouldReturnJustOneNodeIfTheArrayContainsJustOneElement() { 26 Integer[] array = { 1 }; 27 28 BinaryNode<Integer> result = sortedArrayToBST.transform(array); 29 30 assertEquals(new Integer(1), result.getData()); 31 } 32 }
一個有意思的問題,轉換成Python是個什麼樣子?數組