二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有深度遍历和广度遍历,深度遍历有前序、中序以及后序三种遍历方法,广度遍历即我们平常所说的层次遍历。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁,而对于广度遍历来说,需要其他数据结构的支撑,比如堆了。所以,对于一段代码来说,可读性有时候要比代码本身的效率要重要的多。
四种主要的遍历思想为:
前序遍历:根结点 ---> 左子树 ---> 右子树
中序遍历:左子树---> 根结点 ---> 右子树
后序遍历:左子树 ---> 右子树 ---> 根结点
层次遍历:只需按层次遍历即可
代码实现:
自定义一个二叉树
public class MyTreeNode {
private TreeNode root;
/**
* 添加元素
* @param data
*/
public void add(int data) {
root = addToTreeNode(root, data);
}
/**
* 删除元素
* @param data
*/
public void remove(int data) {
removeTreeNode(root, data);
}
/**
* 获取二叉树的深度
* @return
*/
public int getMaxDepth() {
return maxDepth(root);
}
private TreeNode addToTreeNode(TreeNode root, int data) {
if (null == root) {
TreeNode treeNode = new TreeNode();
treeNode.data = data;
return treeNode;
}
if (data > root.data) {
root.right = addToTreeNode(root.right, data);
} else if (data < root.data) {
root.left = addToTreeNode(root.left, data);
} else {
root.data = data;
}
return root;
}
private TreeNode removeTreeNode(TreeNode root, int data) {
if (null == root) {
return null;
}
int var = root.data;
if (data > var) {
root.right = removeTreeNode(root.right, data);
} else if (data < var) {
root.left = removeTreeNode(root.left, data);
} else {
if (null == root.right && null == root.left) {
root = null;
} else if (null == root.left) {
root = root.right;
} else if (null == root.right) {
root = root.left;
} else {
TreeNode left = root.left;
root = root.right;
TreeNode childLeft = root.left;
TreeNode next = root;
while (null != childLeft) {
next = childLeft;
childLeft = childLeft.left;
}
childLeft = left;
next.left = childLeft;
}
}
return root;
}
private int maxDepth(TreeNode root) {
if (null == root) {
return 0;
}
int left_depth = maxDepth(root.left);
int right_depth = maxDepth(root.right);
return Math.max(left_depth, right_depth) + 1;
}
private class TreeNode {
private int data;
private TreeNode left;
private TreeNode right;
}
}
第一种 :二叉树的前序遍历实现
1、递归的方式实现
private void preOrderTraverse1ByRecurrence(TreeNode root) {
if (null == root) {
return;
}
System.out.print(" data: " + root.data);
preOrderTraverse1ByRecurrence(root.left);
preOrderTraverse1ByRecurrence(root.right);
}
2、队列的方式实现
private void preOrderTraverse1ByQueue(TreeNode root) {
if (null == root) {
return;
}
LinkedList<TreeNode> queue = new LinkedList<>();
while (null != root || !queue.isEmpty()) {
if (null != root) {
System.out.print(" data: " + root.data);
queue.push(root);
root = root.left;
} else {
root = queue.poll();
root = root.right;
}
}
}
第二种:二叉树的中序遍历实现
1、递归的方式实现
private void preOrderTraverse1ByRecurrence(TreeNode root) {
if (null == root) {
return;
}
preOrderTraverse1ByRecurrence(root.left);
System.out.print(" data: " + root.data);
preOrderTraverse1ByRecurrence(root.right);
}
2、队列的方式实现
private void preOrderTraverse1ByQueue(TreeNode root) {
if (null == root) {
return;
}
LinkedList<TreeNode> queue = new LinkedList<>();
while (null != root || !queue.isEmpty()) {
if (null != root) {
queue.push(root);
root = root.left;
} else {
root = queue.poll();
System.out.print(" data: " + root.data);
root = root.right;
}
}
}
第三种:二叉树的后序实现方式
1、递归的方式实现
private void preOrderTraverse1ByRecurrence(TreeNode root) {
if (null == root) {
return;
}
preOrderTraverse1ByRecurrence(root.left);
preOrderTraverse1ByRecurrence(root.right);
System.out.print(" data: " + root.data);
}
2、队列的方式实现
private void preOrderTraverse1ByQueue(TreeNode root) {
if (root != null) {
LinkedList<TreeNode> queue = new LinkedList<>();
queue.push(root);
TreeNode c = null;
while (!queue.isEmpty()) {
c = queue.peek();
if (c.left != null && root != c.left && root != c.right) {
queue.push(c.left);
} else if (c.right != null && root != c.right) {
queue.push(c.right);
} else {
System.out.print(" data: " + queue.pop().data);
root = c;
}
}
}
}
第四种:层次遍历
public void levelTraverse(TreeNode root) {
if (root == null) {
return;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.print(node.val+" ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
终于整理完成了!!!!
参考资料: