当前位置:   article > 正文

【Java--数据结构】二叉树

【Java--数据结构】二叉树

欢迎关注个人主页:逸狼


创造不易,可以点点赞吗~

如有错误,欢迎指出~



树结构

树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合

注意:树形结构中,子树之间不能有交集,否则就不是树形结构

常见概念 

 节点的度:一个节点含有子树的个数,如A的度为6

树的度:一颗树所有节点的度的最大值,如上图中树的度为6

叶子节点(终端节点):度为0的节点,如P,Q节点

父节点(双亲节点):有子节点的节点,如A是B的父节点

子节点(孩子节点):与父节点相反,如B是A的子节点

根节点:没有父节点的节点,如A

节点的层次:从根节点为第1层 ,往下数,以此类推。

树的高度:树的最大层次数,如上图树的高度为4

树的应用

 二叉树

一棵二叉树是结点的一个有限集合,该集合: 为空或者是由一个根节点加上两棵别称为左子树和右子树的二叉树组成

 注意:每个节点最多有两颗子树,且有左右之分。

 二叉树的各种情况

满二叉树 与 完全二叉树

满二叉树:每层的节点都达到最大值。(若满二叉树的层数为k,则节点个数为2^K-1)

完全二叉树:每一层从左到右数,直到最后一个节点的前面必须是满的。(满二叉树是特殊的完全二叉树)

二叉树的性质:

前、中、后、层序遍历

前序遍历:根、左、右

中序遍历:左、根、右

后序遍历:左、右、根

层序遍历:从上到下,从左到右,依次遍历 

创建二叉树

二叉树的节点

  1. public class TestBinaryTree {
  2. static class TreeNode{
  3. public char val;
  4. public TreeNode left;
  5. public TreeNode right;
  6. public TreeNode(char val){
  7. this.val=val;
  8. }
  9. }
  10. }

 手动搭建一个二叉树

  1. public TreeNode createTree(){
  2. TreeNode A=new TreeNode('A');
  3. TreeNode B=new TreeNode('B');
  4. TreeNode C=new TreeNode('C');
  5. TreeNode D=new TreeNode('D');
  6. TreeNode E=new TreeNode('E');
  7. TreeNode F=new TreeNode('F');
  8. TreeNode G=new TreeNode('G');
  9. TreeNode H=new TreeNode('H');
  10. A.left=B;
  11. A.right=C;
  12. B.left=D;
  13. B.right=E;
  14. E.right=H;
  15. C.left=F;
  16. C.right=G;
  17. return A;
  18. }

  1. public void preOrder (TreeNode root){
  2. if(root==null){
  3. return;
  4. }
  5. System.out.print(root.val+" ");
  6. //递归遍历左子树
  7. preOrder(root.left);
  8. //递归遍历右子树
  9. preOrder(root.right);
  10. }
  11. public void inOrder(TreeNode root){
  12. if(root==null){
  13. return;
  14. }
  15. inOrder(root.left);
  16. System.out.print(root.val+" ");
  17. inOrder(root.right);
  18. }
  19. public void postOrder(TreeNode root){
  20. if(root==null){
  21. return;
  22. }
  23. postOrder(root.left);
  24. postOrder(root.right);
  25. System.out.print(root.val+" ");
  26. }

测试

  1. TestBinaryTree testBinaryTree=new TestBinaryTree();
  2. TestBinaryTree.TreeNode root= testBinaryTree.createTree();
  3. testBinaryTree.preOrder(root);
  4. System.out.println();
  5. testBinaryTree.inOrder(root);
  6. System.out.println();
  7. testBinaryTree.postOrder(root);

根据 前序遍历 和 中序遍历

或者 后序遍历 和 中序遍历 可以创建二叉树

而 前序 和 后序 不能

获取整棵树的节点数size

子问题思路

左子树节点数+右子树节点数+1=整棵树的

  1. //求整个树的节点个数
  2. public int size(TreeNode root){
  3. if(root==null){
  4. return 0;
  5. }
  6. int ret=size(root.left)+size(root.right)+1;
  7. return ret;
  8. }

遍历思路

每遍历一个节点就++

  1. //遍历思路
  2. public int nodeSize;
  3. public void size2(TreeNode root){
  4. if(root==null){
  5. return;
  6. }
  7. nodeSize++;
  8. size2(root.right);
  9. size2(root.left);
  10. }

求叶子节点的个数

子问题思路

 整棵树的叶子节点个数=左树的+右树的

  1. public int getLeafNodeCount(TreeNode root){
  2. if (root==null){
  3. return 0;
  4. }
  5. if (root.right==null&&root.left==null){
  6. return 1;
  7. }
  8. return getLeafNodeCount(root.right)+getLeafNodeCount(root.left);
  9. }

 遍历思路

  1. public int leafSize;
  2. public void getLeafNodeCount2(TreeNode root) {
  3. if(root == null) {
  4. return ;
  5. }
  6. if(root.left == null && root.right == null) {
  7. leafSize++;
  8. }
  9. getLeafNodeCount2(root.left);
  10. getLeafNodeCount2(root.right);
  11. }

 计算第k层有多少个节点

已知前提:k是合法的

  1. public int getKLevalNodeCount(TreeNode root,int k){
  2. if(root==null){
  3. return 0;
  4. }
  5. if(k==1){
  6. return 1;
  7. }
  8. return getKLevalNodeCount(root.left,k-1)+getKLevalNodeCount(root.right,k-1);
  9. }

 求树的高度

整棵树的高度=Math.max(左树的高度和右树高度)+1

  1. //求树的高度
  2. public int getHeight(TreeNode root){
  3. if(root==null){
  4. return 0;
  5. }
  6. int leftHeight=getHeight(root.left);
  7. int rightHeight=getHeight(root.right);
  8. // return Math.max(leftHeight,rightHeight)+1;
  9. return leftHeight>rightHeight?
  10. leftHeight+1:rightHeight+1;
  11. }

找值为val的节点

  1. // 找值为val的节点
  2. public TreeNode find(TreeNode root,char val){
  3. if(root==null){
  4. return null;
  5. }
  6. if(root.val==val){
  7. return root;
  8. }
  9. TreeNode ret=find(root.left,val);
  10. if(ret!=null){
  11. return ret;
  12. }
  13. ret=find(root.right,val);
  14. if(ret!=null){
  15. return ret;
  16. }
  17. return null;
  18. }

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

闽ICP备14008679号