程序员的信仰
金麟岂是池中物,一遇风云便化龙
金麟岂是池中物,一遇风云便化龙
Mar 14th
Mar 12th
今天下午看见一条推
RT: @rtmeme: RT @foxzzz RT @junyu: 看到 @windbreaker 老师在Buzz说:今天是植树节,写一棵二叉树庆祝之 (via @yangfannet)
于是兴起,花了半个多小时写了棵自认为(从工程而非算法上)比较牛逼的排序二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | package com.jayxu; import java.util.Iterator; import java.util.Random; import java.util.Stack; /** * * @author ijay */ public class BinaryTree<T extends Comparable<T>> implements Iterable<T> { private static final char[] FIRST_CHAR = {'B', 'C', 'K', 'F', 'D', 'G'}; private static final char[] FOLLOWING_CHAR = {'a', 'k', 'l', 'p', 'r', 't', 'n', 'm', 's', 'e'}; private static Random r = new Random(System.currentTimeMillis()); private TreeNode root; public void addNode(T value) { root = addToNode(root, value); } private TreeNode addToNode(TreeNode node, T value) { if (node == null) { node = new TreeNode(); node.value = value; } else if (value.compareTo(node.value) <= 0) { node.left = addToNode(node.left, value); } else { node.right = addToNode(node.right, value); } return node; } public Iterator<T> iterator() { return new PreOrderIterator(); } private class TreeNode { TreeNode left; TreeNode right; T value; } private class PreOrderIterator implements Iterator<T> { private Stack<TreeNode> stack = new Stack<TreeNode>(); private PreOrderIterator() { TreeNode current = root; while (current != null) { stack.push(current); current = current.left; } } public boolean hasNext() { return !stack.isEmpty(); } public T next() { TreeNode n = stack.pop(); TreeNode current = n.right; while (current != null) { stack.push(current); current = current.left; } return n.value; } public void remove() { throw new UnsupportedOperationException(); } } public static void main(String[] args) { for (int i = 8; i < Integer.MAX_VALUE; i *= 2) { buildNTraverseTree(buildRawStrings(i), false); } } private static String[] buildRawStrings(int capacity) { String[] s = new String[capacity]; s[0] = "Jay"; s[1] = "Blader"; s[2] = "Fred"; s[3] = "Gavin"; s[4] = "Alex"; for (int i = 5; i < capacity; i++) { StringBuilder sb = new StringBuilder(); sb.append(FIRST_CHAR[r.nextInt(FIRST_CHAR.length)]); int len = r.nextInt(10); for (int j = 0; j < len; j++) { sb.append(FOLLOWING_CHAR[r.nextInt(FOLLOWING_CHAR.length)]); } s[i] = sb.toString(); } return s; } private static void buildNTraverseTree(String[] strings, boolean output) { System.out.print("Current capacity: " + strings.length + "\t"); long start = System.currentTimeMillis(); BinaryTree<String> tree = new BinaryTree<String>(); for (String s : strings) { tree.addNode(s); } for (String s : tree) { if (output) { System.out.println(s); } } System.out.println("took " + (System.currentTimeMillis() - start) + " ms"); } } |
该树特点:
Mar 7th