用java实现二叉树相关操作(前序建树,前中后递归非递归遍历,层序遍历)

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack;

//二叉树的建树,前中后 递归非递归遍历 层序遍历

//Node节点
class Node {
	int element;
	Node left;
	Node right;

	public Node() {

	}

	public Node(int element) {
		this.element = element;
	}
}
// BinaryTree

public class Tree {

	// creat tree from array
	public static Node creatTree(int[] data, int i) {
		if (i >= data.length || data[i] == -1)
			return null;
		Node temp = new Node(data[i]);
		temp.left = creatTree(data, i * 2 + 1);
		temp.right = creatTree(data, i * 2 + 2);

		return temp;
	}

	// pre前序遍历递归
	public static void pre(Node temp) {
		if (temp == null)
			return;
		System.out.print(temp.element + "  ");
		pre(temp.left);
		pre(temp.right);

	}

	// mid中序遍历递归
	public static void mid(Node temp) {
		if (temp == null)
			return;
		mid(temp.left);
		System.out.print(temp.element + "  ");
		mid(temp.right);
	}

	// last后序遍历递归
	public static void last(Node temp) {
		if (temp == null)
			return;
		last(temp.left);
		last(temp.right);
		System.out.print(temp.element + "  ");
	}

	// pre1前序遍历非递归
	public static void pre1(Node temp) {
		Stack<Node> stack = new Stack<>();
		while (temp != null || !stack.isEmpty()) {
			while (temp != null) {
				stack.push(temp);
				System.out.print(temp.element + "  ");
				temp = temp.left;
			}
			if (!stack.isEmpty()) {
				temp = stack.pop().right;
			}
		}
	}

	// mid1中序遍历非递归
	public static void mid1(Node temp) {
		Stack<Node> stack = new Stack<>();
		while (temp != null || !stack.isEmpty()) {
			while (temp != null) {
				stack.push(temp);
				temp = temp.left;
			}
			if (!stack.isEmpty()) {
				temp = stack.pop();
				System.out.print(temp.element + "  ");
				temp = temp.right;
			}
		}
	}

	// last1后序遍历非递归
	public static void last1(Node temp) {
		Stack<Node> stack = new Stack<>();
		Stack<Node> stack2 = new Stack<>();
		while (temp != null || !stack.isEmpty()) {
			while (temp != null) {
				stack.push(temp);
				stack2.push(temp);
				temp = temp.right;
			}
			if (!stack.isEmpty()) {
				temp = stack.pop().left;
			}
		}
		while (!stack2.isEmpty())
			System.out.print(stack2.pop().element + "  ");
	}

	// ceng层序遍历
	public static void ceng(Node temp) {
		if (temp == null)
			return;
		Queue<Node> queue = new ArrayDeque<>();
		queue.offer(temp);
		while (!queue.isEmpty()) {
			temp = queue.poll();
			System.out.print(temp.element + "  ");
			if (temp.left != null)
				queue.offer(temp.left);
			if (temp.right != null)
				queue.offer(temp.right);
		}
	}
//Demo
	public static void main(String[] args) {

		int[] array = { 1, 2, 3, 4, 5, 6, 7, -1, -1, 10, -1, -1, 13 };
		Node tree = creatTree(array, 0);
		pre(tree);
		System.out.println();
		pre1(tree);
		System.out.println();
		mid(tree);
		System.out.println();
		mid1(tree);
		System.out.println();
		last(tree);
		System.out.println();
		last1(tree);
		System.out.println();
		ceng(tree);

	}

}

树与二叉树的转换可以通过遍历树来实现。具体来说,可以将树的根节点作为二叉树的根节点,将树的每个子节点作为二叉树的左子树,将树的兄弟节点作为二叉树的右子树。这样就可以将树转换为二叉树。 树的前序遍历可以通过递归非递归的方式实现递归实现时,先访问根节点,然后递归访问左子树和右子树。非递归实现时,可以使用栈来保存待访问的节点。先将根节点入栈,然后循环执行以下操作:弹出栈顶节点并访问,将右子树入栈(如果存在),将左子树入栈(如果存在)。 树的后序遍历也可以通过递归非递归的方式实现递归实现时,先递归访问左子树和右子树,然后访问根节点。非递归实现时,可以使用两个栈来实现。先将根节点入栈1,然后循环执行以下操作:从栈1弹出节点并将其入栈2,将左子树和右子树入栈1(如果存在)。最后将栈2中的节点依次弹出并访问即可。 树的层次序遍历可以通过非递归的方式实现。可以使用队列来保存待访问的节点。先将根节点入队,然后循环执行以下操作:从队列中弹出节点并访问,将其子节点入队(如果存在)。直到队列为空为止。 建树实现可以通过递归非递归的方式实现递归实现时,先读入当前节点的值,然后递归读入左子树和右子树。非递归实现时,可以使用栈来保存待处理的节点。先读入根节点的值并入栈,然后循环执行以下操作:从栈中弹出节点并读入其子节点的值,将子节点入栈(如果存在)。直到栈为空为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值