(1)折半查找,需要注意循环终止条件 和 high = mid - 1;以及low = mid + 1;
public int search(int[] data, int key) { int low = 0; int high = data.length - 1; int mid; if (data == null) return -1; if (key > data[high]) { return -1; } if (key < data[low]) { return -1; } while (low <= high) { mid = (low + high) / 2; System.out.println("mid " + mid + " mid value:" + data[mid]);// / if (key < (data[mid])) { high = mid - 1; } else if (key > data[mid]) { low = mid + 1; } else if (key == data[mid]) { return mid; } } return -1; }
(2)二叉树非递归先序遍历
public void preTraversal(BiTree root) { while (root != null || !statck.isEmpty()) { while (root != null) { visit(root); stack.push(root); root = root.leftChild; } if (!statck.isEmpty()) { root = stack.pop(); root = root.rightChild; } } }
(3)二叉树非递归中序遍历
public void MidTraversal(BiTree root) { while (root != null || !statck.isEmpty()) { while (root != null) { stack.push(root); root = root.leftChild; } if (!statck.isEmpty()) { root = stack.pop(); visit(root); root = root.rightChild; } } }