二叉树的先序遍历 中序遍历 后序遍历 层序遍历

快来打我* 2023-01-10 14:39 371阅读 0赞

两种特殊的二叉树

完全二叉树: 完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树。

满二叉树: 一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树

在这里插入图片描述

二叉树的遍历

先序遍历 :先遍历根节点,再遍历左节点,最后遍历右节点

中序遍历 :先遍历左节点,再遍历右节点,最后遍历根节点

后序遍历 :先遍历左节点,再遍历右节点,最后遍历根节点

层序遍历 : 自上而下,自左至右逐层访问树的结点的过程就是层序遍历

遍历方法的实现

先建立一棵树
在这里插入图片描述
用代码建立以上树

  1. class Node {
  2. public char val;
  3. public Node left;
  4. public Node right;
  5. public Node(char val) {
  6. this.val = val;
  7. }
  8. }
  9. public class TestTree {
  10. public static Node build(){
  11. //手动把一颗树构造出来
  12. Node a = new Node('A');
  13. Node b = new Node('B');
  14. Node c = new Node('C');
  15. Node d = new Node('D');
  16. Node e = new Node('E');
  17. Node f = new Node('F');
  18. Node g = new Node('G');
  19. Node h = new Node('H');
  20. a.left = b;
  21. a.right = c;
  22. b.left = d;
  23. b.right = e;
  24. e.left = g;
  25. g.right = h;
  26. c.right = f;
  27. //返回根节点
  28. return a;
  29. }
  30. }

下面进行先序遍历:

  1. //先序遍历
  2. public static void preOrder(Node root){
  3. if (root == null){
  4. return;
  5. }
  6. System.out.print(root.val+" ");
  7. preOrder(root.left);
  8. preOrder(root.right);
  9. }

下面进行中序遍历

  1. //中序遍历
  2. public static void inOrder(Node root){
  3. if (root == null){
  4. return;
  5. }
  6. inOrder(root.left);
  7. System.out.print(root.val+" ");
  8. inOrder(root.right);
  9. }

下面进行后序遍历

  1. //后序遍历
  2. public static void postOrder(Node root){
  3. if (root == null){
  4. return;
  5. }
  6. postOrder(root.left);
  7. postOrder(root.right);
  8. System.out.print(root.val+" ");
  9. }

下面进行层序遍历

  1. //层序遍历
  2. public void levelOrder(TreeNode root){
  3. //不能使用递归
  4. //可以借助一个队列来完成
  5. Queue<TreeNode> queue = new LinkedList<>();
  6. queue.offer(root);//先把根入队列
  7. while (!queue.isEmpty()){
  8. TreeNode cur = queue.poll();//去队首元素
  9. //访问数据
  10. System.out.println(cur.val+" ");
  11. //把左右子树入队列
  12. if (cur.left != null){
  13. queue.offer(cur.left);
  14. }
  15. if (cur.right != null){
  16. queue.offer(cur.right);
  17. }
  18. }
  19. }

(层序遍历无法使用递归的方法)

补充(非递归实现先序/中序/后续遍历)

  1. import java.util.Stack;
  2. class Node{
  3. public int val;
  4. public Node left;
  5. public Node right;
  6. public Node(int val) {
  7. this.val = val;
  8. }
  9. }
  10. public class TreeNode {
  11. // 二叉树的前序遍历,非递归迭代实现
  12. public static void preOrderByLoop(Node root){
  13. if (root == null){
  14. return;
  15. }
  16. Stack<Node> stack = new Stack<>();
  17. stack.push(root);
  18. while (!stack.isEmpty()){
  19. Node top = stack.pop();
  20. System.out.println(top.val + " ");
  21. //把右子树和左子树分别入栈
  22. if (top.right != null){
  23. stack.push(top.right);
  24. }
  25. if (top.left != null){
  26. stack.push(top.left);
  27. }
  28. }
  29. }
  30. // 二叉树的中序遍历,非递归迭代实现
  31. public static void inOrderByLoop(Node root){
  32. if (root == null){
  33. return;
  34. }
  35. Stack<Node> stack = new Stack<>();
  36. Node cur = root;
  37. while (true){
  38. //1,循环往左找,把路径上遇到的节点都入栈
  39. while (cur != null){
  40. stack.push(cur);
  41. cur = cur.left;
  42. }
  43. //2.如果当前栈为空,遍历就结束了
  44. if (!stack.isEmpty()){
  45. break;
  46. }
  47. Node top = stack.pop();
  48. System.out.println(top.val + " ");
  49. cur = top.right;
  50. }
  51. }
  52. // 二叉树的后序遍历,非递归迭代实现
  53. public static void postOrderByLoop(Node root){
  54. if (root == null){
  55. return;
  56. }
  57. Stack<Node> stack = new Stack<>();
  58. Node cur = root;
  59. //prev表示记录了当前已经访问过的节点中的最后一个节点(即将被访问的元素的前一个结点)
  60. Node prev = null;
  61. while (true){
  62. //1,循环往左找,把路径上遇到的节点都入栈
  63. while (cur != null){
  64. stack.push(cur);
  65. cur = cur.left;
  66. }
  67. //2.如果当前栈为空,遍历就结束了
  68. if (!stack.isEmpty()){
  69. break;
  70. }
  71. //拿出栈顶元素的值
  72. Node top = stack.peek();//拿出来看看
  73. if (top.right == null || prev == top.right){
  74. System.out.println(top.val + " ");
  75. stack.pop();//出栈
  76. prev = top;//时刻维护好prev指向已经遍历完元素的最后一个
  77. }else {
  78. cur = top.right;
  79. }
  80. }
  81. }
  82. }

发表评论

表情:
评论列表 (有 0 条评论,371人围观)

还没有评论,来说两句吧...

相关阅读