java定义二叉树,二叉树递归实现

  java定义二叉树,二叉树递归实现

  00-1010 1.树形结构1.1概念1.2概念(重要)2。二叉树(重点)2.1概念2.2二叉树的基本形式2.3两种特殊二叉树2.4二叉树的性质2.5二叉树的存储2.6二叉树的基本操作2.7二叉树的序列遍历3 .二叉树的完整代码

  

目录

 

  00-1010树是一种非线性数据结构。它是由n (n=0)个有限节点组成的分层集合。它被称为树是因为它看起来像一棵颠倒的树,也就是说,它的根朝上,叶子朝下。

  00-1010a。节点的度:节点的子树的个数;如上图:A的度数是6,J的度数是2。

  B.树的度:在这棵树中,最大节点的度就是数的度;如上图:树的度是6。

  C.叶节点(终端节点):度为0的节点(没有子树的节点)

  D.父节点/父节点:如上图,D是h的父节点

  子/子节点:如上图:H是d的子节点。

  E.根节点:没有父节点的节点;如上图所示:A

  F.节点的层次结构:从根的定义来看,根是第一级,其子节点是第二级,以此类推;

  G.树的高度或深度:树中节点的最高级别;如上图:树的高度是4。

  

1. 树型结构

 

  00-1010每个节点最多有两个子树,度=2。

  

1.1概念

 

  

1.2 概念(重要)

 

  A.完全二叉树:所有非子叶度都是2

  B.完全二叉树:完全二叉树缺少“右下角”

  

2. 二叉树(重点)

a.满二叉树

 

  1.如果高度为k,则有2个k-1节点。

  2.如果级别是K,则在该级别中有2 (K-1)个节点。

  3.边数=节点数-1

  4.如果n0的次数为0,n2的次数为2,则n0=N2 ^ 1。

  b.完全二叉树

  1.哪里有右孩子,哪里就有左孩子。

  2.只能有一个度数为1的节点。

  00-1010二叉树的存储结构分为顺序存储和类似于链表的链式存储。

  顺序存储:只能存储完整的二叉树。

  链式存储:普通二叉树

  本次展览连锁仓储

  二叉树的链式存储是被节点一一引用的,常见的表示有二进制和三进制。

  以这张图为例,如下:

  //子表示法私有静态类树节点{ charvalTreeNode向左;TreeNode右侧;public TreeNode(char val){ this . val=val;}}初始化:

  公共静态TreeNode build(){ TreeNode nodeA=new TreeNode( A );TreeNode nodeB=新的TreeNode( B );TreeNode nodeC=新的TreeNode( C );TreeNode nodeD=新的TreeNode( D );TreeNode nodeE=新TreeNode( E );TreeNode nodeF=新的TreeNode( F );TreeNode nodeG=新TreeNode( G );TreeNode nodeH=新的TreeNode( H );nodeA.left=nodeBnodeA.right=nodeCnodeB.left=nodeDnodeB.right=nodeEnodeE.right=nodeHnodeC.left=nodeFnodeC.right=nodeG返回nodeA}

  ian">

  

2.6 二叉树的基本操作

2.6.1 二叉树的遍历 (递归)

 

  1. NLR :前序遍历 (Preorder Traversal 亦称先序遍历 )—— 访问根结点 ---> 根的左子树 ---> 根的右子树。

  

 //先序遍历 : 根左右 public static void preOrder(TreeNode root){ if(root==null){ return; } System.out.print(root.val+" "); preOrder(root.left); preOrder(root.right); }

2. LNR :中序遍历 (Inorder Traversal)—— 根的左子树 ---> 根节点 ---> 根的右子树。

 

  

 //中序遍历 public static void inOrder(TreeNode root){ if(root==null){ return; } preOrder(root.left); System.out.print(root.val+" "); preOrder(root.right); }

3. LRN :后序遍历 (Postorder Traversal)—— 根的左子树 ---> 根的右子树 ---> 根节点。

 

  

 //后序遍历 public static void postOrder(TreeNode root){ if(root==null){ return; } preOrder(root.left); preOrder(root.right); System.out.print(root.val+" "); }

2.6.2 二叉树的遍历 (迭代)

 

  1.前序遍历

  

 //方法2(迭代) //先序遍历 (迭代) public static void preOrderNonRecursion(TreeNode root){ if(root==null){ return ; } Deque<TreeNode> stack=new LinkedList<>(); stack.push(root); while (!stack.isEmpty()){ TreeNode cur=stack.pop(); System.out.print(cur.val+" "); if(cur.right!=null){ stack.push(cur.right); } if(cur.left!=null){ stack.push(cur.left); } } }

2.中序遍历

 

  

 //方法2(迭代) //中序遍历 (迭代) public static void inorderTraversalNonRecursion(TreeNode root) { if(root==null){ return ; } Deque<TreeNode> stack=new LinkedList<>(); // 当前走到的节点 TreeNode cur=root; while (!stack.isEmpty() cur!=null){ // 不管三七二十一,先一路向左走到根儿~ while (cur!=null){ stack.push(cur); cur=cur.left; } // 此时cur为空,说明走到了null,此时栈顶就存放了左树为空的节点 cur=stack.pop(); System.out.print(cur.val+" "); // 继续访问右子树 cur=cur.right; } }

3.后序遍历

 

  

 //方法2(迭代) //后序遍历 (迭代) public static void postOrderNonRecursion(TreeNode root){ if(root==null){ return; } Deque<TreeNode> stack=new LinkedList<>(); TreeNode cur=root; TreeNode prev=null; while (!stack.isEmpty() cur!=null){ while (cur!=null){ stack.push(cur); cur=cur.left; } cur=stack.pop(); if(cur.right==null prev==cur.right){ System.out.print(cur.val+" "); prev=cur; cur=null; }else { stack.push(cur); cur=cur.right; } } }

2.6.3 二叉树的基本操作

 

  1.求结点个数(递归&迭代)

  

 //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树中一共有多少个节点,返回节点数 //此时的访问就不再是输出节点值,而是计数器 + 1操作 public static int getNodes(TreeNode root){ if(root==null){ return 0; } return 1+getNodes(root.left)+getNodes(root.right); } //方法2(迭代) //使用层序遍历来统计当前树中的节点个数 public static int getNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque<TreeNode> queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { TreeNode cur = queue.poll(); size++; if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } return size; }

2.求叶子结点个数(递归&迭代)

 

  

 //方法1(递归) //传入一颗二叉树的根节点,就能统计出当前二叉树的叶子结点个数 public static int getLeafNodes(TreeNode root){ if(root==null){ return 0; } if(root.left==null && root.right==null){ return 1; } return getLeafNodes(root.left)+getLeafNodes(root.right); } //方法2(迭代) //使用层序遍历来统计叶子结点的个数 public static int getLeafNodesNoRecursion(TreeNode root){ if(root==null){ return 0; } int size=0; Deque<TreeNode> queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ TreeNode cur=queue.poll(); if(cur.left==null && cur.right==null){ size++; } if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } return size; }

3.求第 k 层结点个数

 

  

 //求出以root为根节点的二叉树第k层的节点个数 public static int getKLevelNodes(TreeNode root,int k){ if(root==null k<=0){ return 0; } if(k==1){ return 1; } return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1); }

4.求树的高度

 

  

 //传入一个以root为根节点的二叉树,就能求出该树的高度 public static int height(TreeNode root){ if(root==null){ return 0; } return 1+ Math.max(height(root.left),height(root.right)); }

5.判断二叉树数中是否存在值为value的节点

 

  

 //判断当前以root为根节点的二叉树中是否包含指定元素val, //若存在返回true,不存在返回false public static boolean contains(TreeNode root,char value){ if(root==null){ return false; } if(root.val==value){ return true; } return contains(root.left,value) contains(root.right,value); }

 

  

2.7 二叉树的层序遍历

 //层序遍历 public static void levelOrder(TreeNode root) { if(root==null){ return ; } // 借助队列来实现遍历过程 Deque<TreeNode> queue =new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()){ int size=queue.size(); for (int i = 0; i < size; i++) { TreeNode cur=queue.poll(); System.out.print(cur.val+" "); if(cur.left!=null){ queue.offer(cur.left); } if(cur.right!=null){ queue.offer(cur.right); } } } }

 

  

3.二叉树完整代码

二叉树完整代码见下节:Java实现二叉树的示例代码(递归&迭代)

 

  以上就是详解Java中二叉树的基础概念(递归&迭代)的详细内容,更多关于Java二叉树的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: