java基礎(chǔ)二叉搜索樹(shù)圖文詳解
概念
二叉搜索樹(shù)又稱二叉排序樹(shù),它或者是一棵空樹(shù),或者是具有以下性質(zhì)的二叉樹(shù):
1、若它的左子樹(shù)不為空,則左子樹(shù)上所有節(jié)點(diǎn)的值都小于根結(jié)點(diǎn)的值。
2、若它的右子樹(shù)不為空,則右子樹(shù)上所有節(jié)點(diǎn)的值都大于根結(jié)點(diǎn)的值。
3、它的左右子樹(shù)也分別為二叉搜索樹(shù)
直接實(shí)踐
準(zhǔn)備工作:定義一個(gè)樹(shù)節(jié)點(diǎn)的類,和二叉搜索樹(shù)的類。
搜索二叉樹(shù)的查找功能
假設(shè)我們已經(jīng)構(gòu)造好了一個(gè)這樣的二叉樹(shù),如下圖
我們要思考的第一個(gè)問(wèn)題是如何查找某個(gè)值是否在該二叉樹(shù)中?
根據(jù)上述的邏輯,我們來(lái)把搜索的方法進(jìn)行完善。
搜索二叉樹(shù)的插入操作
根據(jù)上述邏輯,我們來(lái)寫一個(gè)插入節(jié)點(diǎn)的代碼。
搜索二叉樹(shù) 刪除節(jié)點(diǎn)的操作 - 難點(diǎn)
再來(lái)分析一下:curDummy 和 parentDummy 是怎么找到“替罪羊”的。
總程序 - 模擬實(shí)現(xiàn)二叉搜索樹(shù)
class TreeNode{ public int val; public TreeNode left; public TreeNode right; public TreeNode(int val){ this.val = val; } } public class BinarySearchTree { TreeNode root; //在二叉樹(shù)中 尋找指定 val 值的節(jié)點(diǎn) // 找到了,返回其節(jié)點(diǎn)地址;沒(méi)找到返回 null public TreeNode search(int key){ TreeNode cur = this.root; while(cur != null){ if(cur.val == key){ return cur; }else if(cur.val < key){ cur = cur.right; }else{ cur = cur.left; } } return null; } // 插入操作 public boolean insert(int key){ if(this.root == null){ this.root = new TreeNode(key); return true; } TreeNode cur = this.root; TreeNode parent = null; while(cur!=null){ if(key > cur.val){ parent = cur; cur = cur.right; }else if(cur.val == key){ return false; }else{ parent = cur; cur = cur.left; } } TreeNode node = new TreeNode(key); if(parent .val > key){ parent.left = node; }else{ parent.right = node; } return true; } // 刪除操作 public void remove(int key){ TreeNode cur = root; TreeNode parent = null; // 尋找 刪除節(jié)點(diǎn)位置。 while(cur!=null){ if(cur.val == key){ removeNode(cur,parent);// 真正刪除節(jié)點(diǎn)的代碼 break; }else if(cur.val < key){ parent = cur; cur = cur.right; }else{ parent = cur; cur = cur.left; } } } // 輔助刪除方法:真正刪除節(jié)點(diǎn)的代碼 private void removeNode(TreeNode cur,TreeNode parent){ // 情況一 if(cur.left == null){ if(cur == this.root){ this.root = this.root.right; }else if( cur == parent.left){ parent.left = cur.right; }else{ parent.right = cur.right; } // 情況二 }else if(cur.right == null){ if(cur == this.root){ this.root = root.left; }else if(cur == parent.left){ parent.left = cur.left; }else{ parent.right = cur.left; } // 情況三 }else{ // 第二種方法:在刪除節(jié)點(diǎn)的右子樹(shù)中尋找最小值, TreeNode parentDummy = cur; TreeNode curDummy = cur.right; while(curDummy.left != null){ parentDummy = curDummy; curDummy = curDummy.left; } // 此時(shí) curDummy 指向的 cur 右子樹(shù) cur.val = curDummy.val; if(parentDummy.left != curDummy){ parentDummy.right = curDummy.right; }else{ parentDummy.left = curDummy.right; } } } // 中序遍歷 public void inorder(TreeNode root){ if(root == null){ return; } inorder(root.left); System.out.print(root.val+" "); inorder(root.right); } public static void main(String[] args) { int[] array = {10,8,19,3,9,4,7}; BinarySearchTree binarySearchTree = new BinarySearchTree(); for (int i = 0; i < array.length; i++) { binarySearchTree.insert(array[i]); } binarySearchTree.inorder(binarySearchTree.root); System.out.println();// 換行 System.out.print("插入重復(fù)的數(shù)據(jù) 9:" + binarySearchTree.insert(9)); System.out.println();// 換行 System.out.print("插入不重復(fù)的數(shù)據(jù) 1:" + binarySearchTree.insert(1)); System.out.println();// 換行 binarySearchTree.inorder(binarySearchTree.root); System.out.println();// 換行 binarySearchTree.remove(19); System.out.print("刪除元素 19 :"); binarySearchTree.inorder(binarySearchTree.root); System.out.println();// 換行 System.out.print("查找不存在的數(shù)據(jù)50 :"); System.out.println(binarySearchTree.search(50)); System.out.print("查找存在的數(shù)據(jù) 7:"); System.out.println(binarySearchTree.search(7)); } }
性能分析
插入和刪除操作都必須先查找,查找效率代表了二叉搜索樹(shù)中各個(gè)操作的性能。
對(duì)有n個(gè)結(jié)點(diǎn)的二叉搜索樹(shù),若每個(gè)元素查找的概率相等,則二叉搜索樹(shù)平均查找長(zhǎng)度是結(jié)點(diǎn)在二叉搜索樹(shù)的深度的函數(shù),即結(jié)點(diǎn)越深,則比較次數(shù)越多。
但對(duì)于同一個(gè)關(guān)鍵碼集合,如果各關(guān)鍵碼插入的次序不同,可能得到不同結(jié)構(gòu)的二叉搜索樹(shù):
如果我們能保證 二叉搜索樹(shù)的左右子樹(shù)高度差不超過(guò)1。盡量滿足高度平衡條件。
這就成 AVL 樹(shù)了(高度平衡的二叉搜索樹(shù))。而AVL樹(shù),也有缺點(diǎn):需要一個(gè)頻繁的旋轉(zhuǎn)。浪費(fèi)很多效率。
至此 紅黑樹(shù)就誕生了,避免更多的旋轉(zhuǎn)。
和 java 類集的關(guān)系
TreeMap 和 TreeSet 即 java 中利用搜索樹(shù)實(shí)現(xiàn)的 Map 和 Set;實(shí)際上用的是紅黑樹(shù),而紅黑樹(shù)是一棵近似平衡的二叉搜索樹(shù),即在二叉搜索樹(shù)的基礎(chǔ)之上 + 顏色以及紅黑樹(shù)性質(zhì)驗(yàn)證,關(guān)于紅黑樹(shù)的內(nèi)容,等博主學(xué)了,會(huì)寫博客的。
總結(jié)
到此這篇關(guān)于java基礎(chǔ)二叉搜索樹(shù)的文章就介紹到這了,更多相關(guān)java二叉搜索樹(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java創(chuàng)建二叉搜索樹(shù),實(shí)現(xiàn)搜索,插入,刪除的操作實(shí)例
- java實(shí)現(xiàn) 二叉搜索樹(shù)功能
- Java刪除二叉搜索樹(shù)的任意元素的方法詳解
- Java二叉搜索樹(shù)基礎(chǔ)原理與實(shí)現(xiàn)方法詳解
- 利用java實(shí)現(xiàn)二叉搜索樹(shù)
- Java實(shí)現(xiàn)二叉搜索樹(shù)的插入、刪除功能
- Java基礎(chǔ)之二叉搜索樹(shù)的基本操作
- Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹(shù)的核心方法
相關(guān)文章
Java POI讀取excel中數(shù)值精度損失問(wèn)題解決
這篇文章主要介紹了Java POI讀取excel中數(shù)值精度損失問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04詳解mybatis foreach collection示例
這篇文章主要介紹了詳解mybatis foreach collection的相關(guān)資料,需要的朋友可以參考下2017-10-10mybatis plus saveBatch方法方法執(zhí)行慢導(dǎo)致接口發(fā)送慢解決分析
這篇文章主要為大家介紹了mybatis plus saveBatch方法導(dǎo)致接口發(fā)送慢解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10基于java時(shí)區(qū)轉(zhuǎn)換夏令時(shí)的問(wèn)題及解決方法
下面小編就為大家分享一篇基于java時(shí)區(qū)轉(zhuǎn)換夏令時(shí)的問(wèn)題及解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11spring boot配置讀寫分離的完整實(shí)現(xiàn)步驟
數(shù)據(jù)庫(kù)配置主從之后,如何在代碼層面實(shí)現(xiàn)讀寫分離?所以下面這篇文章主要給大家介紹了關(guān)于spring boot配置讀寫分離的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2018-09-09SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼校驗(yàn)方法思路詳解
最近做項(xiàng)目遇到這樣的需求,前端是基于BootStrap,html代碼中有BootStrap樣式實(shí)現(xiàn)的,具體后臺(tái)實(shí)現(xiàn)代碼大家通過(guò)本文一起學(xué)習(xí)吧2017-08-08