詳解Java中二叉樹的基礎(chǔ)概念(遞歸&迭代)
1. 樹型結(jié)構(gòu)
1.1概念
樹是一種 非線性 的數(shù)據(jù)結(jié)構(gòu),它是由 n ( n>=0 )個(gè)有限結(jié)點(diǎn)組成一個(gè)具有層次關(guān)系的集合。 把它叫做樹是因?yàn)樗?起來像一棵倒掛的樹,也就是說它是根朝上,而葉朝下的 。
1.2 概念(重要)
a.節(jié)點(diǎn)的度:該節(jié)點(diǎn)子樹的個(gè)數(shù);如上圖:A的度為6,J的度為2
b.樹的度:該樹中,最大結(jié)點(diǎn)的度就是該數(shù)的度;如上圖:樹的度為6
c.葉子節(jié)點(diǎn)(終端節(jié)點(diǎn)):度為0的節(jié)點(diǎn)(沒有子樹的節(jié)點(diǎn))
d.雙親結(jié)點(diǎn)/父節(jié)點(diǎn):如上圖:D是H的父節(jié)點(diǎn)
孩子節(jié)點(diǎn)/子節(jié)點(diǎn):如上圖:H是D的子節(jié)點(diǎn)
e.根節(jié)點(diǎn):沒有雙親的節(jié)點(diǎn);如上圖:A
f.節(jié)點(diǎn)的層次:從根開始定義起,根為第1層,根的子節(jié)點(diǎn)為第2層,以此類推;
g.樹的高度或深度:樹中節(jié)點(diǎn)的最大層次; 如上圖:樹的高度為4
2. 二叉樹(重點(diǎn))
2.1 概念
每個(gè)節(jié)點(diǎn)最多只有兩顆子樹,度<=2.
2.2 二叉樹的基本形態(tài)
2.3 兩種特殊的二叉樹
a.滿二叉樹:非子葉度都為2
b.完全二叉樹:滿二叉樹缺了“右下角”
2.4 二叉樹的性質(zhì)
a.滿二叉樹
1.高度為K,則有2^k-1個(gè)節(jié)點(diǎn)
2.層次為K,則該層有2^(k-1)個(gè)節(jié)點(diǎn)
3.邊個(gè)數(shù) = 節(jié)點(diǎn)個(gè)數(shù) - 1
4.度為0有n0個(gè),度為2有n2個(gè),則 n0 = n2 + 1
b.完全二叉樹
1.有右孩子必有左孩子
2.只可能有一個(gè)度為1的節(jié)點(diǎn)
2.5 二叉樹的存儲(chǔ)
二叉樹的存儲(chǔ)結(jié)構(gòu)分為:順序存儲(chǔ)和類似于鏈表的鏈?zhǔn)酱鎯?chǔ)。
順序存儲(chǔ):只能存完全二叉樹
鏈?zhǔn)酱鎯?chǔ):普通二叉樹
本次展示鏈?zhǔn)酱鎯?chǔ)
二叉樹的鏈?zhǔn)酱鎯?chǔ)是通過一個(gè)一個(gè)的節(jié)點(diǎn)引用起來的,常見的表示方式有二叉和三叉表示方式 ,
以此圖為例, 具體如下:
// 孩子表示法 private static class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(char val) { this.val = val; } }
初始化:
public static TreeNode build(){ TreeNode nodeA=new TreeNode('A'); TreeNode nodeB=new TreeNode('B'); TreeNode nodeC=new TreeNode('C'); TreeNode nodeD=new TreeNode('D'); TreeNode nodeE=new TreeNode('E'); TreeNode nodeF=new TreeNode('F'); TreeNode nodeG=new TreeNode('G'); TreeNode nodeH=new TreeNode('H'); nodeA.left=nodeB; nodeA.right=nodeC; nodeB.left=nodeD; nodeB.right=nodeE; nodeE.right=nodeH; nodeC.left=nodeF; nodeC.right=nodeG; return nodeA; }
2.6 二叉樹的基本操作
2.6.1 二叉樹的遍歷 (遞歸)
1. NLR :前序遍歷 (Preorder Traversal 亦稱先序遍歷 )—— 訪問根結(jié)點(diǎn) ---> 根的左子樹 ---> 根的右子樹。
//先序遍歷 : 根左右 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)—— 根的左子樹 ---> 根節(jié)點(diǎn) ---> 根的右子樹。
//中序遍歷 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)—— 根的左子樹 ---> 根的右子樹 ---> 根節(jié)點(diǎn)。
//后序遍歷 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<>(); // 當(dāng)前走到的節(jié)點(diǎn) TreeNode cur=root; while (!stack.isEmpty() || cur!=null){ // 不管三七二十一,先一路向左走到根兒~ while (cur!=null){ stack.push(cur); cur=cur.left; } // 此時(shí)cur為空,說明走到了null,此時(shí)棧頂就存放了左樹為空的節(jié)點(diǎn) cur=stack.pop(); System.out.print(cur.val+" "); // 繼續(xù)訪問右子樹 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.求結(jié)點(diǎn)個(gè)數(shù)(遞歸&迭代)
//方法1(遞歸) //傳入一顆二叉樹的根節(jié)點(diǎn),就能統(tǒng)計(jì)出當(dāng)前二叉樹中一共有多少個(gè)節(jié)點(diǎn),返回節(jié)點(diǎn)數(shù) //此時(shí)的訪問就不再是輸出節(jié)點(diǎn)值,而是計(jì)數(shù)器 + 1操作 public static int getNodes(TreeNode root){ if(root==null){ return 0; } return 1+getNodes(root.left)+getNodes(root.right); } //方法2(迭代) //使用層序遍歷來統(tǒng)計(jì)當(dāng)前樹中的節(jié)點(diǎn)個(gè)數(shù) 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.求葉子結(jié)點(diǎn)個(gè)數(shù)(遞歸&迭代)
//方法1(遞歸) //傳入一顆二叉樹的根節(jié)點(diǎn),就能統(tǒng)計(jì)出當(dāng)前二叉樹的葉子結(jié)點(diǎn)個(gè)數(shù) 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(迭代) //使用層序遍歷來統(tǒng)計(jì)葉子結(jié)點(diǎn)的個(gè)數(shù) 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 層結(jié)點(diǎn)個(gè)數(shù)
//求出以root為根節(jié)點(diǎn)的二叉樹第k層的節(jié)點(diǎn)個(gè)數(shù) 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.求樹的高度
//傳入一個(gè)以root為根節(jié)點(diǎn)的二叉樹,就能求出該樹的高度 public static int height(TreeNode root){ if(root==null){ return 0; } return 1+ Math.max(height(root.left),height(root.right)); }
5.判斷二叉樹數(shù)中是否存在值為value的節(jié)點(diǎn)
//判斷當(dāng)前以root為根節(jié)點(diǎn)的二叉樹中是否包含指定元素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 ; } // 借助隊(duì)列來實(shí)現(xiàn)遍歷過程 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.二叉樹完整代碼
二叉樹完整代碼見下節(jié):Java實(shí)現(xiàn)二叉樹的示例代碼(遞歸&迭代)
以上就是詳解Java中二叉樹的基礎(chǔ)概念(遞歸&迭代)的詳細(xì)內(nèi)容,更多關(guān)于Java二叉樹的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Java編程實(shí)現(xiàn)深度優(yōu)先遍歷與連通分量代碼示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)深度優(yōu)先遍歷與連通分量代碼示例,2017-11-11java微信掃碼支付模式一線下支付功能實(shí)現(xiàn)
本篇文章主要介紹了JAVA微信掃碼支付模式一線下支付功能實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11RestTemplate發(fā)送form-data請(qǐng)求上傳rul資源文件及對(duì)象參數(shù)方式
這篇文章主要介紹了RestTemplate發(fā)送form-data請(qǐng)求上傳rul資源文件及對(duì)象參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié)
轉(zhuǎn)發(fā)和重定向是JavaWeb中常用的兩種頁(yè)面跳轉(zhuǎn)方式,它們?cè)趯?shí)現(xiàn)上有一些區(qū)別,本文主要介紹了JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10