1 二叉树的创建
今天看了一下二叉树,突然发现书上讲了它的遍历方式,却没有讲如何创建二叉树,如果不先创建的话,我感觉即使写出了遍历的代码,也没法遍历啊。真的是觉得很奇怪!这里附上我写的代码,首先我这棵树我是用数组先存的,按照完全二叉树存的,没有节点的地方值为0
结构如下:
int[] arr = { 1, 2, 3, 4, 5, 0, 0, 0, 0, 6, 7 };
int index = 0;
//测试用的方法
@Test
public void test() {
TreeNode root = null;
root = CreateTree(arr, 0);
System.out.println("先序遍历");
preOrderRecur(root);
System.out.println();
System.out.println("中序遍历");
inOrderRecur(root);
System.out.println();
System.out.println("后序遍历");
posOrderRecur(root);
}
// 创建树
public TreeNode CreateTree(int[] arr, int index) {
TreeNode node = null;
if (index < arr.length && arr[index] != 0) {
node = new TreeNode(arr[index]);
node.setLeft(CreateTree(arr, 2 * index + 1));
node.setRight(CreateTree(arr, 2 * index + 2));
}
return node;
}
树的定义
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
//省略了get和set方法
}
2先序遍历
2.1 先序遍历的递归实现
先读取根节点的值,然后再递归的遍历左子树和又子树,就是先序遍历
// 先序遍历树的递归算法
public void preOrderRecur(TreeNode head) {
if (head == null) {
return;
}
System.out.print(head.val + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
2.2 先序遍历非递归实现
用非递归的过程的具体过程如下:
- 申请一个新栈,记为stack,然后将头节点head压入stack中。
- 从stack中弹出新节点,记为cur,然后打印cur节点的值,再将cur节点的右孩子(如果不为空的话)先压入栈中,最后将cur的左孩子(不为空的话)压入栈中。
- 不断重复步骤2,直到stack为空,全部过程结束
具体代码如下:
// 先序遍历的非递归算法
public void preOrderUnRecur(TreeNode head) {
if (head != null) {
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.add(head);
while (!stack.isEmpty()) {
head = stack.pop();
System.out.print(head.val + " ");
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
}
}
3 中序遍历
3.1 中序遍历的递归算法
// 中序遍历树的递归算法
public void inOrderRecur(TreeNode head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.val + " ");
inOrderRecur(head.right);
}
3.2 中序遍历的非递归算法
具体过程如下:
- 申请一个新的栈,记为stack,初始时,令变量cur=head
- 先把cur节点压入栈中,对以cur节点为头的整棵子树来说,一次把左边界压入栈中,即不停的令cur=cur.left,然后重复步骤2
- 不断重复步骤2,直到发现cur为空,此时从stack中弹出一个节点,记为node,打印node的值,并且让cur=nod.ight,然后重复步骤2,
- 当stack为空且cur为空时,整个过程停止。
具体代码如下:
// 中序遍历的非递归算法
public void inOrderUnRecur(TreeNode head) {
if (head != null) {
Stack<TreeNode> stack = new Stack<TreeNode>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.val + " ");
head = head.right;
}
}
}
}
4 后序遍历
4.1 后序遍历的递归算法
// 后序遍历树的递归算法
public void posOrderRecur(TreeNode head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.val + " ");
}
4.2 后序遍历的非递归算法
后序遍历的非递归实现方法,有很多种,这里记录用两个栈实现的,我感觉一个栈实现的有点难,实际上还是自己太菜
具体过程如下:
- 申请两个栈,记为s1和s2,然后将头节点head压入s1中。
- 从s1中弹出的节点记为cur,然后依次将cur的左孩子和右孩子压入s1中。
- 在整个过程中,每一个从s1中弹出的节点都放进s2中。
- 不断重复步骤2和步骤3,直到s1为空,过程停止
- 从s2中依次弹出节点并打印,打印的顺序就是后序遍历的顺序
具体实现代码如下:
// 后序遍历的非递归算法
public void posOrderUnRecur(TreeNode head) {
if (head != null) {
Stack<TreeNode> s1 = new Stack<TreeNode>();
Stack<TreeNode> s2 = new Stack<TreeNode>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
s2.push(head);
if (head.left != null) {
s1.push(head.left);
}
if (head.right != null) {
s1.push(head.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().val + " ");
}
}
}