/*
* @description: 二叉树的前中后序遍历以及已知中前、中后求后、前
* @author: 逍遥
* */
public class Main {
// 前序遍历 递归实现
public void preOrderTraverse1(MyTreeNode root) {
if (root != null) {
printfTreeNode(root);
preOrderTraverse1(root.left);
preOrderTraverse1(root.right);
}
}
//中序遍历 递归实现
public void inOrderTraverse(MyTreeNode root) {
if (root != null) {
inOrderTraverse(root.left);
printfTreeNode(root);
inOrderTraverse(root.right);
}
}
//后序遍历 递归实现
public void postOrderTraverse(MyTreeNode root) {
if (root != null) {
postOrderTraverse(root.left);
postOrderTraverse(root.right);
printfTreeNode(root);
}
}
//广度优先遍历 队列
public void breadthFirstSearch(MyTreeNode node){
if(node==null){
System.out.print("empty tree");
return;
}
ArrayDeque<MyTreeNode> deque = new ArrayDeque<MyTreeNode>();
deque.add(node);
while(!deque.isEmpty()){
MyTreeNode rnode = deque.remove();
printfTreeNode(rnode);
if(rnode.left!=null){
deque.add(rnode.left);
}
if(rnode.right!=null){
deque.add(rnode.right);
}
}
}
//深度优先遍历 堆栈
public void depthTraversal(MyTreeNode node) {
if (node == null) {
System.out.print("empty tree");
return;
}
Stack<MyTreeNode> stack = new Stack<MyTreeNode>();
stack.push(node);
while (!stack.isEmpty()) {
MyTreeNode rnode = stack.pop();
printfTreeNode(rnode);
if (rnode.right != null) {
stack.push(rnode.right);
}
if (rnode.left != null) {
stack.push(rnode.left);
}
}
}
/**
* @param pre_bg_index 前序数组中,开始的索引(具体到左(右)子树中,数组开始索引+长度len精确指定了某一个左(右)子树)
* @param in_bg_index 中序数组中,开始的索引
* @param len 树(左(右)子树对应到数组中的长度)
* @param pre_Order_Array 前序数组
* @param in_Order_Array 中序数组 (这里一直把前序中序数组当做参数,是由于在递归过程中,没有动过数组,只是动态的去其中的某些项。)
*/
public static void postOrderSolution(int pre_bg_index, int in_bg_index, int len, char[] pre_Order_Array, char[] in_Order_Array) {
if (len == 0)
return;
char root = pre_Order_Array[pre_bg_index];
if (len==1){//递归出口。
System.out.print(root+" ");
return;
}
int i = 0;
while (root != in_Order_Array[in_bg_index + i]) {//找到中序根节点的位置。
i++;
}
//左右中,标准的后序遍历的形式。
postOrderSolution(pre_bg_index+1, in_bg_index, i, pre_Order_Array, in_Order_Array);
postOrderSolution(pre_bg_index+i+1, in_bg_index+i+1, len-i-1, pre_Order_Array, in_Order_Array);
System.out.print(root+" ");
}
/**
*
* @param post_end_index 后序数组中,结束的索引(具体到左(右)子树中,数组结束索引+长度len精确指定了某一个左(右)子树)
* @param in_bg_index 中序数组中,开始的索引
* @param len 树(左(右)子树对应到数组中的长度)
* @param post_Order_Array 后序数组
* @param in_Order_Array 中序数组 (这里一直把前序中序数组当做参数,是由于在递归过程中,没有动过数组,只是动态的去其中的某些项。)
*/
public static void preOrderSolution(int post_end_index, int in_bg_index, int len, char[] post_Order_Array, char[] in_Order_Array) {
if (len == 0)
return;
char root = post_Order_Array[post_end_index];
if (len==1){//递归出口。
System.out.print(root+" ");
return;
}
int i = 0;
while (root != in_Order_Array[in_bg_index + i]) {//找到中序根节点的位置。
i++;
}
//中左右,标准的前序遍历的形式。注意左子树中后序结束的索引是从后往前减的,中间减掉的(len-i-1)是右子树对应的数组长度。
System.out.print(root+" ");
preOrderSolution(post_end_index-(len-i-1)-1, in_bg_index, i, post_Order_Array, in_Order_Array);
preOrderSolution(post_end_index-1, in_bg_index+i+1, len-i-1, post_Order_Array, in_Order_Array);
}
/**
* @param pre_bg_index 前序数组中,开始的索引(具体到左(右)子树中,数组开始索引+长度len精确指定了某一个左(右)子树)
* @param in_bg_index 中序数组中,开始的索引
* @param len 树(左(右)子树对应到数组中的长度)
* @param pre_Order_Array 前序数组
* @param in_Order_Array 中序数组 (这里一直把前序中序数组当做参数,是由于在递归过程中,没有动过数组,只是动态的去其中的某些项。)
*/
public static MyTreeNode PreInreConstruct(int pre_bg_index, int in_bg_index, int len, char[] pre_Order_Array, char[] in_Order_Array) {
if (len == 0)
return null;
char root = pre_Order_Array[pre_bg_index];
if (len == 1) {//递归出口。
MyTreeNode newNode = new MyTreeNode(root);
return newNode;
}
int i = 0;
while (root != in_Order_Array[in_bg_index + i]) {//找到中序根节点的位置。
i++;
}
MyTreeNode node = new MyTreeNode<Character>(root);
node.left = PreInreConstruct(pre_bg_index + 1, in_bg_index, i, pre_Order_Array, in_Order_Array);
node.right = PreInreConstruct(pre_bg_index + i + 1, in_bg_index + i + 1, len - i - 1, pre_Order_Array, in_Order_Array);
return node;
}
/**
*
* @param post_end_index 后序数组中,结束的索引(具体到左(右)子树中,数组结束索引+长度len精确指定了某一个左(右)子树)
* @param in_bg_index 中序数组中,开始的索引
* @param len 树(左(右)子树对应到数组中的长度)
* @param post_Order_Array 后序数组
* @param in_Order_Array 中序数组 (这里一直把前序中序数组当做参数,是由于在递归过程中,没有动过数组,只是动态的去其中的某些项。)
*/
public static MyTreeNode EndInreConstruct(int post_end_index, int in_bg_index, int len, char[] post_Order_Array, char[] in_Order_Array) {
if (len == 0)
return null;
char root = post_Order_Array[post_end_index];
if (len==1){//递归出口。
MyTreeNode newNode = new MyTreeNode(root);
return newNode;
}
int i = 0;
while (root != in_Order_Array[in_bg_index + i]) {//找到中序根节点的位置。
i++;
}
MyTreeNode node = new MyTreeNode<Character>(root);
node.left = EndInreConstruct(post_end_index-(len-i-1)-1, in_bg_index, i, post_Order_Array, in_Order_Array);
node.right = EndInreConstruct(post_end_index-1, in_bg_index+i+1, len-i-1, post_Order_Array, in_Order_Array);
return node;
}
public static void main(String[] args){
// char[] pre_Order_Array = {'A','B','D','E','H','C','F','G','I'};
// char[] in_Order_Array = {'D','B','H','E','A','F','C','G','I'};
char[] post_Order_Array = {'D','H','E','B','F','I','G','C','A'};
char[] in_Order_Array = {'D','B','H','E','A','F','C','G','I'};
// postOrderSolution(0, 0, pre_Order_Array.length, pre_Order_Array, in_Order_Array);
preOrderSolution(post_Order_Array.length-1, 0, post_Order_Array.length, post_Order_Array, in_Order_Array);
}
//打印节点
public void printfTreeNode(MyTreeNode node) {
System.out.println(node.val);
}
//创建树节点
static class MyTreeNode<E> {
private MyTreeNode left;
private MyTreeNode right;
private E val;
public MyTreeNode() {
}
public MyTreeNode(E val) {
this.val = val;
}
public MyTreeNode(MyTreeNode left, MyTreeNode right, E val) {
this.left = left;
this.right = right;
this.val = val;
}
public MyTreeNode getLeft() {
return left;
}
public void setLeft(MyTreeNode left) {
this.left = left;
}
public MyTreeNode getRight() {
return right;
}
public void setRight(MyTreeNode right) {
this.right = right;
}
public Object getVal() {
return val;
}
public void setVal(E val) {
this.val = val;
}
}
}
二叉树的前中后序遍历以及互求
最新推荐文章于 2021-02-28 23:56:05 发布