当前位置:   article > 正文

二叉树 - (前序,中序,后序,层序 遍历实现)_本关任务:以顺序结构存储二叉树,编写前序、中序、后序及层次顺序遍历二叉树的算法

本关任务:以顺序结构存储二叉树,编写前序、中序、后序及层次顺序遍历二叉树的算法

目录

一 . 什么是树

 树的概念

树的表示形式

二.  二叉树

1. 概念 

2. 两种特殊的二叉树

3. 二叉树的性质

4. 二叉树的存储

二叉树的遍历

1. 前序遍历

2. 中序遍历

3 . 后序遍历

二叉树的基本操作

层序遍历

判断完全二叉树


一 . 什么是树

 注意 : 

1.子树是不相交的.

2.除了根节点以外,每个节点有且仅有一个父节点

3,一棵N个节点的树有N-1条边.

 树的概念

1. 结点的度:一个结点含有子树的个数称为该结点的度; 如上图:A的度为6

2. 树的度:一棵树中,所有结点度的最大值称为树的度; 如上图:树的度为

3. 叶子结点或终端结点 :度为 0 的结点称为叶结点; 如上图: B C H I... 等节点为叶结点
4. 双亲结点或父结点 :若一个结点含有子结点,则这个结点称为其子结点的父结点; 如上图: A B 的父结点
5. 孩子结点或子结点 :一个结点含有的子树的根结点称为该结点的子结点; 如上图: B A 的孩子结点
6. 根结点 :一棵树中,没有双亲结点的结点;如上图: A
7. 结点的层次 :从根开始定义起,根为第 1 层,根的子结点为第 2 层,以此类推

8. 树的高度或深度:树中结点的最大层次; 如上图:树的高度为4


树的表示形式

树的表示方式有很多 , 如 : 双亲表示法,孩子表示法,孩子双亲表示法,孩子兄弟表示法.

我们采用最常用的孩子兄弟表示法.

比较简单 : 就像我们之前学的链表一样.

对于二叉树来说 : 有三个值域 : 分别存储 : 值,  左孩子节点地址,  右孩子节点地址

  1. public class Node {
  2. public int val;
  3. public Node left;
  4. public Node right;
  5. public Node(int val) {
  6. this.val = val;
  7. }
  8. }

二.  二叉树

1. 概念 : 

二叉树的每个节点的度不能超过2.

 注意 : 对于任意二叉树都是由以下几种情况复合而成的 : 

2. 两种特殊的二叉树

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

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

3. 二叉树的性质

1. 若规定 根结点的层数为 1 ,则一棵 非空二叉树的第 i 层上最多有 2 ^ (i - 1) (i>0)个结点.
2. 若规定只有 根结点的二叉树的深度为 1 ,则 深度为 K 的二叉树的最大结点数是 (2^k) - 1(k>=0)
3. 对任何一棵二叉树 , 如果其 叶结点个数为 n0, 度为 2 的非叶结点个数为 n2, 则有 n0 n2 1
4. 具有 n 个结点的完全二叉树的深度 k 为 log(n+1) 上取整

5. 对于具有n个结点的完全二叉树,如果按照从上至下从左至右的顺序对所有节点从0开始编号,则对于序号为i的结点有

        1. 若i>0 双亲序号: (i-1)/2 i=0 i 为根结点编号 ,无双亲结点
        2. 若2i+1<n ,左孩子序号: 2i+1 ,否则无左孩子
        3. 若2i+2<n ,右孩子序号: 2i+2,   否则无右孩子

4. 二叉树的存储

二叉树的存储结构 分为: 顺序存储 类似于链表的链式存储
顺序存储用于堆中. 在这里我们使用链式存储

二叉树的遍历

1. 前序遍历

首先我们先用穷举的办法来建一个二叉树 : 

为了方便操作  我们定义一个成员变量root 

  1. public class TreeNode {
  2. static 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. Node root = null;
  11. //创建树
  12. public void createTree() {
  13. Node n1 = new Node(11);
  14. Node n2 = new Node(21);
  15. Node n3 = new Node(31);
  16. Node n4 = new Node(41);
  17. Node n5 = new Node(51);
  18. Node n6 = new Node(61);
  19. n1.left = n2;
  20. n1.right = n3;
  21. n2.left = n4;
  22. n2.right = n5;
  23. n3.right = n6;
  24. this.root = n1;
  25. }
  26. }

将上面的代码 图形化 :

 前序遍历 : 根  左   右   依次去遍历

对于二叉树的遍历,使用递归是最好的办法

  1. package demo1;
  2. public class TreeNode {
  3. static class Node {
  4. public int val;
  5. public Node left;
  6. public Node right;
  7. public Node(int val) {
  8. this.val = val;
  9. }
  10. }
  11. Node root = null;
  12. //创建树
  13. public Node createTree() {
  14. Node n1 = new Node(11);
  15. Node n2 = new Node(21);
  16. Node n3 = new Node(31);
  17. Node n4 = new Node(41);
  18. Node n5 = new Node(51);
  19. Node n6 = new Node(61);
  20. n1.left = n2;
  21. n1.right = n3;
  22. n2.left = n4;
  23. n2.right = n5;
  24. n3.right = n6;
  25. return n1;
  26. }
  27. public void preorder(Node root) {
  28. if (root == null) {
  29. return;
  30. }
  31. System.out.print(root.val + " ");
  32. preorder(root.left);
  33. preorder(root.right);
  34. }
  35. }
  36. package demo1;
  37. public class Test {
  38. public static void main(String[] args) {
  39. TreeNode treeNode = new TreeNode();
  40. TreeNode.Node root = treeNode.createTree();
  41. treeNode.preorder(root);
  42. }
  43. }

接下来我们来看一下OJ上这道前序遍历的题 : 144. 二叉树的前序遍历 - 力扣(Leetcode)

  1. public List<Integer> preorderTraversal(TreeNode root) {
  2. }

它的返回值类型是List<Integer> 

解决的思路有两种 : 

1. 遍历思路

  1. List<Integer> list = new ArrayList<>();
  2. public List<Integer> preorderTraversal(Node root) {
  3. pre(root);
  4. return list;
  5. }
  6. public void pre(Node root) {
  7. if (root == null) {
  8. return;
  9. }
  10. list.add(root.val);
  11. pre(root.left);
  12. pre(root.right);
  13. }

2. 子问题思路

  1. public List<Integer> preorderTraversal(Node root) {
  2. List<Integer> list = new ArrayList<>();
  3. if (root == null) {
  4. return list;
  5. }
  6. list.add(root.val);
  7. List<Integer> left = preorderTraversal(root.left);
  8. list.addAll(left);
  9. List<Integer> right = preorderTraversal(root.right);
  10. list.addAll(right);
  11. return list;
  12. }

2. 中序遍历

94. 二叉树的中序遍历 - 力扣(Leetcode)

1. 遍历思路解决

  1. List<Integer> list = new ArrayList<>();
  2. public List<Integer> inorderTraversal(Node root) {
  3. inorder(root);
  4. return list;
  5. }
  6. public void inorder(Node root) {
  7. if (root == null) {
  8. return;
  9. }
  10. inorder(root.left);
  11. list.add(root.val);
  12. inorder(root.right);
  13. }

2. 子问题思路解决

  1. public List<Integer> inorderTraversal(TreeNode root) {
  2. List<Integer> list = new ArrayList<>();
  3. if (root == null) {
  4. return list;
  5. }
  6. List<Integer> left = inorderTraversal(root.left);
  7. list.addAll(left);
  8. list.add(root.val);
  9. List<Integer> right = inorderTraversal(root.right);
  10. list.addAll(right);
  11. return list;
  12. }

3 . 后序遍历

 遍历思路  :  左   右   根

1. 遍历思路

  1. List<Integer> list = new ArrayList<>();
  2. public List<Integer> postorderTraversal(TreeNode root) {
  3. postorder(root);
  4. return list;
  5. }
  6. public void postorder(TreeNode root) {
  7. if (root == null) {
  8. return;
  9. }
  10. postorder(root.left);
  11. postorder(root.right);
  12. list.add(root.val);
  13. }

2. 子问题思路

  1. public List<Integer> postorderTraversal(TreeNode root) {
  2. List<Integer> list = new ArrayList<>();
  3. if (root == null) {
  4. return list;
  5. }
  6. List<Integer> left = postorderTraversal(root.left);
  7. list.addAll(left);
  8. List<Integer> right = postorderTraversal(root.right);
  9. list.addAll(right);
  10. list.add(root.val);
  11. return list;
  12. }

二叉树的基本操作

  1. package demo2;
  2. public class BinaryTree {
  3. static class Node {
  4. public int val;
  5. public Node left;
  6. public Node right;
  7. public Node(int val) {
  8. this.val = val;
  9. }
  10. }
  11. Node root = null;
  12. //创建树
  13. public Node createTree() {
  14. Node n1 = new Node(11);
  15. Node n2 = new Node(21);
  16. Node n3 = new Node(31);
  17. Node n4 = new Node(41);
  18. Node n5 = new Node(51);
  19. Node n6 = new Node(61);
  20. n1.left = n2;
  21. n1.right = n3;
  22. n2.left = n4;
  23. n2.right = n5;
  24. n3.right = n6;
  25. return n1;
  26. }
  27. int count = 0;
  28. // 获取树中节点的个数
  29. int size(Node root) {
  30. preorder(root);
  31. return count;
  32. }
  33. public void preorder(Node root) {
  34. if (root == null) {
  35. return;
  36. }
  37. count++;
  38. preorder(root.left);
  39. preorder(root.right);
  40. }
  41. // 获取叶子节点的个数
  42. // 子问题思路-求叶子结点个数
  43. int getLeafNodeCount(Node root) {
  44. if (root == null){
  45. return 0;
  46. }
  47. if (root.left == null && root.right == null) {
  48. return 1;
  49. }
  50. return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
  51. }
  52. // 获取第K层节点的个数
  53. int getKLevelNodeCount(Node root,int k) {
  54. if (root == null) {
  55. return 0;
  56. }
  57. if (k == 1) {
  58. return 1;
  59. }
  60. return getKLevelNodeCount(root.left,k - 1) + getKLevelNodeCount(root.right,k - 1);
  61. }
  62. // 获取二叉树的高度
  63. int getHeight(Node root) {
  64. if (root == null) {
  65. return 0;
  66. }
  67. int leftHeight = getHeight(root.left);
  68. int rightHeight = getHeight(root.left);
  69. return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
  70. }
  71. // 检测值为value的元素是否存在
  72. Node find(Node root, int val) {
  73. if (root == null) {
  74. return null;
  75. }
  76. if (root.val == val) {
  77. return root;
  78. }
  79. Node left = find(root.left,val);
  80. if (left != null) {
  81. return left;
  82. }
  83. Node right = find(root.right,val);
  84. if (right != null) {
  85. return right;
  86. }
  87. return null;
  88. }
  89. }
  1. package demo2;
  2. public class Test {
  3. public static void main(String[] args) {
  4. BinaryTree binaryTree = new BinaryTree();
  5. BinaryTree.Node root = binaryTree.createTree();
  6. System.out.print("树的节点个数 : ");
  7. System.out.println(binaryTree.size(root));
  8. System.out.print("树的叶子节点个数 : ");
  9. System.out.println(binaryTree.getLeafNodeCount(root));
  10. System.out.print("第3层的节点个数 : ");
  11. System.out.println(binaryTree.getKLevelNodeCount(root,3));
  12. System.out.print("树的高度 : ");
  13. System.out.println(binaryTree.getHeight(root));
  14. System.out.println("找到21这个节点 : ");
  15. BinaryTree.Node node = binaryTree.find(root,21);
  16. System.out.println(node.val);
  17. }
  18. }

层序遍历

思路 : 从上往下  从左往右 依次遍历

  1. public List<List<Integer>> levelOrder(TreeNode root) {
  2. List<List<Integer>> lists = new ArrayList<>();
  3. Queue<TreeNode> queue = new LinkedList<>();
  4. if (root != null) {
  5. queue.offer(root);
  6. }
  7. while (!queue.isEmpty()) {
  8. int size = queue.size();
  9. List<Integer> list = new ArrayList<>();
  10. while (size-- > 0) {
  11. TreeNode node = queue.poll();
  12. list.add(node.val);
  13. if (node.left != null) {
  14. queue.offer(node.left);
  15. }
  16. if (node.right != null) {
  17. queue.offer(node.right);
  18. }
  19. }
  20. lists.add(list);
  21. }
  22. return lists;
  23. }

判断完全二叉树

 

  1. // 判断一棵树是不是完全二叉树
  2. boolean isCompleteTree(Node root) {
  3. Queue<Node> queue = new LinkedList<>();
  4. if (root == null) {
  5. return true;
  6. }
  7. queue.offer(root);
  8. while (!queue.isEmpty()) {
  9. BinaryTree.Node node = queue.poll();
  10. if (node == null) {
  11. break;
  12. }
  13. queue.offer(node.left);
  14. queue.offer(node.right);
  15. }
  16. while (!queue.isEmpty()) {
  17. if (queue.poll() != null) {
  18. return false;
  19. }
  20. }
  21. return true;
  22. }

希望可以帮到大家~~~~~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/737590
推荐阅读
相关标签
  

闽ICP备14008679号