2.使用 java 实现二叉树的中序优先遍历, 如下二叉树的遍历结果为:ABDECF, 不能使用递归。
这道题目在我开始做的时候,有点纳闷,题目要求是中序优先遍历,也就是中序遍历
中序遍历是先访问左子树,再访问根节点,然后访问右子树。不管怎么走,A都不会是第一个结果。
所以我这道题目写了中序遍历和先序遍历
在service包中有个init方式是初始化建立一颗二叉树
写了递归和非递归两个版本的中序遍历和先序遍历
递归遍历,递归的方式比较简单,主要是看读data是放在哪个位置,函数基本内容相同。
非递归遍历,非递归的遍历是通过栈进行辅助
这道题目在我开始做的时候,有点纳闷,题目要求是中序优先遍历,也就是中序遍历
中序遍历是先访问左子树,再访问根节点,然后访问右子树。不管怎么走,A都不会是第一个结果。
所以我这道题目写了中序遍历和先序遍历
在service包中有个init方式是初始化建立一颗二叉树
写了递归和非递归两个版本的中序遍历和先序遍历
递归遍历,递归的方式比较简单,主要是看读data是放在哪个位置,函数基本内容相同。
非递归遍历,非递归的遍历是通过栈进行辅助
不断压栈,然后执行判断,特定场景出栈
package edu.fjnu.service;
import edu.fjnu.domain.Node;
public class Init {
public Node init(){ //初始化二叉树,由下向上定义
Node E = new Node("E",null,null);
Node D = new Node("D",null,null);
Node F = new Node("F",null,null);
Node B = new Node("B",D,E);
Node C = new Node("C",null,F);
Node A = new Node("A",B,C);
return A; //返回root
}
}
package edu.fjnu.service;
import java.util.Stack;
import edu.fjnu.domain.Node;
public class NodeOrder {
/**
* 先序递归遍历二叉树
* @param root 根结点
* @author Harry
*/
public void preOrderTraByRec(Node root){
if(root != null){
printNodeValue(root);
preOrderTraByRec(root.getLeft());
preOrderTraByRec(root.getRight());
}
}
/**
* 先序非递归遍历二叉树
* @param root 结点
* @author Harry
*/
public void preOrderTraUnRec(Node root){ //非递归,用stack来辅助
Stack<Node> stack = new Stack<Node>();
Node node = root;
while (node != null || stack.size() > 0) { //压栈左结点
if (node != null) {
printNodeValue(node);
stack.push(node);
node = node.getLeft();
} else {
node = stack.pop();
node = node.getRight();
}
}
}
/**
* 中序递归遍历二叉树
* @param root 根结点
* @author Harry
*/
public void midOrderTraByRec(Node root){
if(root != null ){
midOrderTraByRec(root.getLeft());
printNodeValue(root);
midOrderTraByRec(root.getRight());
}
}
/**
* 中序非递归遍历二叉树
* @param root 根结点
* @author Harry
*/
public void midOrderTraUnRec(Node root){ //非递归,用stack来辅助
Stack<Node> stack = new Stack<Node>();
Node node = root;
while(node != null || stack.size() > 0){//压栈左结点
if(node != null) {
stack.push(node);//压栈
node = node.getLeft();
}else {
node = stack.pop();//出栈
printNodeValue(node);
node = node.getRight();
}
}
}
/**
* 打印出每个结点的值
* @param node 结点
* @author Harry
*/
public void printNodeValue(Node node) {
System.out.print(node.getData()+" ");
}
}
package edu.fjnu.domain;
/**
*
* @author Harry
*
*/
public class Node {
private Object data ; //结点值
private Node left ; //左结点
private Node right ; //右结点
public Node(Object data,Node left,Node right){
this.data = data ;
this.left = left ;
this.right = right;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
package edu.fjnu.client;
import edu.fjnu.domain.Node;
import edu.fjnu.service.Init;
import edu.fjnu.service.NodeOrder;
/**
*
* @author Harry
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Node root = new Init().init();
NodeOrder order = new NodeOrder();
System.out.print("先序递归排序为: ");
order.preOrderTraByRec(root);
System.out.println();
System.out.print("先序非递归排序为: ");
order.preOrderTraUnRec(root);
System.out.println();
System.out.print("中序递归排序为: ");
order.midOrderTraByRec(root);
System.out.println();
System.out.print("中序非递归排序为: ");
order.midOrderTraUnRec(root);
System.out.println();
}
}
程序执行的结果:
先序递归排序为: A B D E C F
先序非递归排序为: A B D E C F
中序递归排序为: D B E A C F
中序非递归排序为: D B E A C F