java 二叉查找樹實例代碼
java 二叉查找樹實例代碼
1.左邊<中間<右邊
2.前序遍歷 左中右
3.中序遍歷 中左右
4.后序遍歷 左右中
public class BinaryTree { // 二叉樹的根節(jié)點 public TreeNode rootNode ; // 記錄搜索深度 public int count; /** * 利用傳入一個數(shù)組來建立二叉樹 */ public BinaryTree(int[] data) { for (int i = 0; i < data. length; i++) { addNodeToTree(data[i]); } } /** * 將指定的值加入到二叉樹中適當?shù)墓?jié)點 */ private void addNodeToTree(int value) { TreeNode currentNode = rootNode; // 建立樹根 if (rootNode == null) { rootNode = new TreeNode(value); return; } // 建立二叉樹 while (true) { // 新增的value比節(jié)點的value小,則在左子樹 if (value < currentNode.value ) { if (currentNode.leftNode == null) { currentNode.leftNode = new TreeNode(value); return; } else { currentNode = currentNode.leftNode; } } else { // 新增的value比節(jié)點的value大,在右子樹 if (currentNode.rightNode == null) { currentNode. rightNode = new TreeNode(value); return; } else { currentNode = currentNode. rightNode; } } } } /** * 中序遍歷(左子樹 -樹根- 右子樹) */ public void inOrder(TreeNode node) { if (node != null) { inOrder(node. leftNode); System. out.print("[" + node.value + "]"); inOrder(node. rightNode); } } /** * 前序遍歷(樹根 -左子樹- 右子樹) */ public void preOrder(TreeNode node) { if (node != null) { System. out.print("[" + node.value + "]"); preOrder(node. leftNode); preOrder(node. rightNode); } } /** * 后序遍歷(左子樹 -右子樹- 樹根) */ public void postOrder(TreeNode node) { if (node != null) { postOrder(node. leftNode); postOrder(node. rightNode); System. out.print("[" + node.value + "]"); } } /** * 從二叉樹中查找指定value */ public boolean findTree(TreeNode node, int value) { if (node == null) { System. out.println("共搜索" + count + "次"); return false; } else if (node.value == value) { System. out.println("共搜索" + count + "次"); return true; } else if (value < node.value) { count++; return findTree(node.leftNode , value); } else { count++; return findTree(node.rightNode , value); } } /** * 利用中序遍歷進行排序 */ public void sort() { this.inOrder(rootNode ); } class TreeNode { int value ; TreeNode leftNode; TreeNode rightNode; public TreeNode(int value) { this.value = value; this.leftNode = null; this.rightNode = null; } } public static void main(String[] args) { int[] content = { 50, 35, 27, 45, 40, 48, 78, 56, 90 }; BinaryTree tree = new BinaryTree(content); System. out.println("前序遍歷:" ); tree.preOrder(tree. rootNode); System. out.println("\n中序遍歷:" ); tree.inOrder(tree. rootNode); System. out.println("\n后序遍歷:" ); tree.postOrder(tree. rootNode); System. out.println("\n\n開始搜索:" ); boolean isFind = tree.findTree(tree.rootNode, 48); System. out.println("是否搜索到" + 48 + ":" + isFind); System. out.println("\n進行排序:" ); tree.sort(); } }
前序遍歷:
[50][35][27][45][40][48][78][56][90]
中序遍歷:
[27][35][40][45][48][50][56][78][90]
后序遍歷:
[27][40][48][45][35][56][90][78][50]
開始搜索:
共搜索3次
是否搜索到48:true
進行排序:
[27][35][40][45][48][50][56][78][90]
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- java二叉查找樹的實現(xiàn)代碼
- 詳解Java二叉排序樹
- Java的二叉樹排序以及遍歷文件展示文本格式的文件樹
- Java中二叉樹數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)示例
- 圖解紅黑樹及Java進行紅黑二叉樹遍歷的方法
- java使用歸并刪除法刪除二叉樹中節(jié)點的方法
- Java實現(xiàn)求二叉樹的深度和寬度
- JAVA 實現(xiàn)二叉樹(鏈式存儲結(jié)構(gòu))
- Java 實現(xiàn)二叉搜索樹的查找、插入、刪除、遍歷
- java實現(xiàn)二叉樹的創(chuàng)建及5種遍歷方法(總結(jié))
- 圖解二叉樹的三種遍歷方式及java實現(xiàn)代碼
- Java基于二叉查找樹實現(xiàn)排序功能示例
相關(guān)文章
Java分批將List數(shù)據(jù)導入數(shù)據(jù)庫的解決過程
這篇文章主要給大家介紹了關(guān)于Java分批將List數(shù)據(jù)導入數(shù)據(jù)庫的解決過程,文中通過代碼示例介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友可以參考下2023-08-08Java單例模式利用HashMap實現(xiàn)緩存數(shù)據(jù)
這篇文章主要為大家詳細介紹了Java單例模式利用HashMap實現(xiàn)緩存數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04SpringSecurit鹽值加密的密碼驗證以及強密碼驗證過程
在密碼加密過程中,鹽值的使用可以增強密碼的安全性,如果忘記存儲鹽值,將無法驗證密碼,強密碼應(yīng)包含數(shù)字、字母和特殊字符,長度應(yīng)在8到30位之間,以提高賬戶安全2023-03-03SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空的問題
這篇文章主要介紹了SpringCloud Gateway自定義filter獲取body中的數(shù)據(jù)為空,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10java實現(xiàn)圖片轉(zhuǎn)base64字符串 java實現(xiàn)base64字符串轉(zhuǎn)圖片
這篇文章主要為大家詳細介紹了java實現(xiàn)圖片轉(zhuǎn)base64字符串,java實現(xiàn)base64字符串轉(zhuǎn)圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02Spring Boot啟動過程(五)之Springboot內(nèi)嵌Tomcat對象的start教程詳解
這篇文章主要介紹了Spring Boot啟動過程(五)之Springboot內(nèi)嵌Tomcat對象的start的相關(guān)資料,需要的朋友可以參考下2017-04-04