树的算法如下:非递归的算法中,中序和后续遍历稍微难写一些 ,需要多练。其余的基本是模板方式,多横向对比感受下。
import java.util.Stack;
public class SearchTree {
public static void main(String[] args) {
BinaryTreeNode root = new BinaryTreeNode(1);
root.left = new BinaryTreeNode(2);
root.left.left = new BinaryTreeNode(4);
root.left.left.left = new BinaryTreeNode(8);
root.left.left.right = new BinaryTreeNode(9);
root.left.right = new BinaryTreeNode(5);
root.left.right.left = new BinaryTreeNode(10);
root.right = new BinaryTreeNode(3);
root.right.left = new BinaryTreeNode(6);
root.right.right = new BinaryTreeNode(7);
System.out.println("先序遍历的递归和非递归对比:");
noRecursionPreOrder(root);
System.out.println();
recursionPreOrder(root);
System.out.println();
System.out.println("中序遍历的递归和非递归对比:");
noRecursionMidOrder(root);
System.out.println();
recursionMidOrder(root);
System.out.println();
System.out.println("后序遍历的递归和非递归对比:");
noRecursionPostOrder(root);
System.out.println();
recursionPostOrder(root);
System.out.println();
}
// 递归版 --> 二叉树(先序遍历)
public static void noRecursionPreOrder(BinaryTreeNode node) {
// 1. 递归终止条件
if (node == null) return;
// 2. 处理当前层逻辑
System.out.print(node.data + " ");
// 3. 下挖一层
noRecursionPreOrder(node.left);
noRecursionPreOrder(node.right);
}
// 非递归版 --> 二叉树(先序遍历)
public static void recursionPreOrder(BinaryTreeNode root) {
if (root == null) {
return;
}
Stack<BinaryTreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
BinaryTreeNode node = stack.pop();
System.out.print(node.data + " ");
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
}
// 递归版 --> 二叉树(中序遍历)
public static void noRecursionMidOrder(BinaryTreeNode node) {
if (node == null) return;
noRecursionMidOrder(node.left);
System.out.print(node.data + " ");
noRecursionMidOrder(node.right);
}
// 非递归版 --> 二叉树(中序遍历)
public static void recursionMidOrder(BinaryTreeNode node) {
if (node == null) return;
Stack<BinaryTreeNode> stack = new Stack<>();
while (node != null || !stack.isEmpty()) {
if (node != null) {
stack.push(node);
node = node.left;
} else {
node = stack.pop();
System.out.print(node.data + " ");
node = node.right;
}
}
}
// 递归版 --> 二叉树(后序遍历)
public static void noRecursionPostOrder(BinaryTreeNode node) {
if (node == null) return;
noRecursionPostOrder(node.left);
noRecursionPostOrder(node.right);
System.out.print(node.data + " ");
}
// 非递归版 --> 二叉树(后序遍历)
public static void recursionPostOrder(BinaryTreeNode node) {
if (node == null) {
return;
}
Stack<BinaryTreeNode> stack1 = new Stack<>();
Stack<BinaryTreeNode> stack2 = new Stack<>();
stack1.push(node);
while (!stack1.isEmpty()) {
node = stack1.pop();
stack2.push(node);
if (node.left != null) stack1.push(node.left);
if (node.right != null) stack1.push(node.right);
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().data + " ");
}
}
// 递归版 --> 深度遍历
public static void dfs04(TreeNode node) {
if (node == null) return;
System.out.print(node.data + " ");
for (int i = 0; i < node.childNodes.size(); i++) {
dfs04(node.childNodes.get(i));
}
}
}
class BinaryTreeNode {
int data; // 数据域
BinaryTreeNode left;// 左子节点
BinaryTreeNode right; // 右子节点
public BinaryTreeNode(int data) {
this.data = data;
}
}