用Java实现树的基本遍历操作,包括递归和非递归方式:
Tree结点类:
package dataStruct;
/**
* 树的结点
* @author WQC
*
*/
public class TreeNode{
public int value;
public TreeNode leftChild;
public TreeNode rightChild;
public TreeNode() {
}
public TreeNode(int value) {
this.value = value;
}
public TreeNode(int value, TreeNode leftChild, TreeNode rightChild) {
super();
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
}
Tree实现类:
package dataStruct;
import java.util.Scanner;
import java.util.Stack;
public class Tree {
/**
* 前序遍历二叉树--递归
*
* @param tree
* @param function
* @return
*/
public static boolean preOrderTree(TreeNode tree, Function function) {
if (function == null) {
return false;
}
if (tree != null) {
if (function.visit(tree)) {
if (preOrderTree(tree.leftChild, function)) {
if (preOrderTree(tree.rightChild, function)) {
return true;
}
}
} else {
return false;
}
}
return true;
}
/**
* 中序遍历二叉树--递归
*
* @param tree
* @param function
* @return
*/
public static boolean inOrderTree(TreeNode tree, Function function) {
if (function == null) {
return false;
}
// 检查结点是否为空
if (tree != null) {
// 中序遍历左子树
if (inOrderTree(tree.leftChild, function)) {
// 打印根节点
if (function.visit(tree)) {
// 中序遍历右子树
if (inOrderTree(tree.rightChild, function)) {
return true;
} else {
function.visit(tree);
}
}
} else {
function.visit(tree);
return false;
}
}
return true;
}
/**
* 后续遍历树--递归
*
* @param treeRoot
* @param function
*/
public static boolean postOrderTree(TreeNode treeRoot, Function function) {
if (function == null) {
return false;
}
// 检查结点是否为空
if (treeRoot != null) {
// 后续遍历左子树
if (postOrderTree(treeRoot.leftChild, function)) {
//后续遍历右子树
if (postOrderTree(treeRoot.rightChild, function)) {
if (function.visit(treeRoot)) {
return true;
}
}else{
function.visit(treeRoot);
return true;
}
} else {
function.visit(treeRoot);
return false;
}
}
return true;
}
/**
* 中序遍历--非递归
* @param root
* @param function
* @return
*/
public static boolean inOrderNoRecursion(TreeNode root,Function function){
if(root==null || function == null){
return false;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
//向左走到尽头
while(stack.peek()!=null ){
stack.push(root.leftChild);
root = root.leftChild;
}
//空指针退栈
root = stack.pop();
if(!stack.isEmpty()){
root = stack.pop();
if(!function.visit(root)){
return false;
}
root = root.rightChild;
stack.push(root.rightChild);
}
}
return true;
}
/**
* 先序遍历创建一棵树
*
* @return
*/
public static TreeNode createTreeByPre() {
TreeNode node;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入节点值");
String dataStr = scanner.next();
if (dataStr.equals("$")) {
node = null;
} else {
node = new TreeNode();
int data = Integer.valueOf(dataStr);
node.value = data;
node.leftChild = createTreeByPre();
node.rightChild = createTreeByPre();
}
return node;
}
/**
* 对树的结点的操作接口
* @author WQC
*
*/
public interface Function {
public boolean visit(TreeNode treeNode);
}
}