二叉搜索树的递归遍历
public class BinaryTree {
public int value;
public BinaryTree left;
public BinaryTree right;
public void store(int value) {
if (value < this.value) {
if (left == null) {
left = new BinaryTree();
left.value = value;
} else {
left.store(value);
}
} else if (value > this.value) {
if (right == null) {
right = new BinaryTree();
right.value = value;
} else {
right.store(value);
}
}
}
public boolean find(int value) {
System.out.println("happen" + this.value);
if (value == this.value) {
return true;
} else if (value > this.value) {
if (right == null)
return false;
return right.find(value);
} else {
if (left == null)
return false;
return left.find(value);
}
}
public void preList() {
System.out.print(this.value + ",");
if (left != null)
left.preList();
if (right != null)
right.preList();
}
public void middleList() {
if (left != null)
left.middleList();
System.out.print(this.value + ",");
if (right != null)
right.middleList();
}
public void afterList() {
if (left != null)
left.afterList();
if (right != null)
right.afterList();
System.out.print(this.value + ",");
}
public static void main(String[] args) {
/*
* int[] data = new int[20]; for (int i = 0; i < data.length; i++) { data[i] = (int) (Math.random() * 100) + 1;
* System.out.print(data[i] + ","); }
*/
int[] data = { 7, 5, 8, 3, 6, 4, 9 };
System.out.println();
BinaryTree root = new BinaryTree();
root.value = data[0];
for (int i = 1; i < data.length; i++) {
root.store(data[i]);
}
// root.find(data[19]);
System.out.println("前序遍历");
root.preList();
System.out.println();
System.out.println("中序遍历");
root.middleList();
System.out.println();
System.out.println("后续遍历");
root.afterList();
}
}
非递归遍历方式可以采用栈来实现
// 先序遍历(非递归)---借助于栈实现
public void nrPreOrderTraverse() {
Stack<Node<T>> stack = new Stack<Node<T>>();
Node<T> node = root;
stack.push(node);
while (!stack.isEmpty()) {
node = stack.pop();
System.out.println(node.getValue());
// 右子节点先入栈
if (node.getRight() != null)
stack.push(node.getRight());
if (node.getLeft() != null)
stack.push(node.getLeft());
}
}
// 中序遍历(非递归)----借助于栈实现
public void nrInOrderTraverse() {
Stack<Node<T>> stack = new Stack<Node<T>>();
Node<T> node = root;
while (node != null || !stack.isEmpty()) {
if (node != null) {
stack.push(node);
node = node.getLeft();
} else {
node = stack.pop();
System.out.print(node.getValue() + " ");
node = node.getRight();
}
}
}
// 后续遍历(非递归)
public void nrPostOrderTraverse() {
Stack<Node<T>> stack = new Stack<Node<T>>();
Node<T> node = root;
Node<T> preNode = null;// 表示最近一次访问的节点
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.peek();
if (node.getRight() == null || node.getRight() == preNode) {
System.out.println(node.getValue());
node = stack.pop();
preNode = node;
node = null;
} else {
node = node.getRight();
}
}
}
// 后续遍历(非递归)----借助于两个栈实现
public void nrPostOrder() {
LinkedList<Node<T>> stack1 = new LinkedList<>(), stack2 = new LinkedList<>();
stack1.push(root);
while (!stack1.isEmpty()) {
Node<T> node = stack1.pop();
stack2.push(node);
if (node.getLeft() != null) {
stack1.push(node.getLeft());
}
if (node.getRight() != null) {
stack1.push(node.getRight());
}
}
while (!stack2.isEmpty()) {
Node<T> node = stack2.pop();
System.out.print(node.getValue() + " ");
}
}
层次遍历可以看成为先进先出的结果,利用队列来实现
// 层次遍历--利用队列实现,FIFO
public void levelTraverse(Node<T> node) {
Queue<Node<T>> queue = new LinkedBlockingQueue<Node<T>>();
queue.add(node);
while (!queue.isEmpty()) {
Node<T> temp = queue.poll();
if (temp != null) {
System.out.print(temp.getValue());
if(temp.getLeft()!=null)
queue.add(temp.getLeft());
if(temp.getRight())
queue.add(temp.getRight());
}
}
}