Java基礎(chǔ)之二叉搜索樹的基本操作
一、二叉搜索樹插入元素
/** * user:ypc; * date:2021-05-18; * time: 15:09; */ class Node { int val; Node left; Node right; Node(int val) { this.val = val; } } public void insert(int key) { Node node = new Node(key); if (this.root == null) { root = node; } Node cur = root; Node parent = null; while (cur != null) { if (cur.val == key) { //System.out.println("元素已經(jīng)存在"); return; } else if (cur.val > key) { parent = cur; cur = cur.left; } else { parent = cur; cur = cur.right; } } if (key > parent.val) { parent.right = node; } else { parent.left = node; } }
二、搜索指定節(jié)點(diǎn)
public boolean search(int key) { Node cur = root; while (cur != null) { if (cur.val == key) { return true; } else if (cur.val > key) { cur = cur.left; } else { cur = cur.right; } } return false; }
三、刪除節(jié)點(diǎn)方式一
public void removenode1(Node parent, Node cur) { if (cur.left == null) { if (cur == root) { root = cur.right; } else if (cur == parent.right) { parent.left = cur.right; } else { parent.right = cur.right; } } else if (cur.right == null) { if (cur == root) { root.left = cur; } else if (cur == parent.right) { parent.right = cur.left; } else { parent.left = cur.left; } } else { Node tp = cur; Node t = cur.right; while (t.left != null) { tp = t; t = t.left; } if (tp.left == t) { cur.val = t.val; tp.left = t.right; } if (tp.right == t) { cur.val = t.val; tp.right = t.right; } } } public void remove(int key) { Node cur = root; Node parent = null; while (cur != null) { if (cur.val == key) { removenode1(parent, cur); //removenode2(parent, cur); return; } else if (key > cur.val) { parent = cur; cur = cur.right; } else { parent = cur; cur = cur.left; } } }
四、刪除節(jié)點(diǎn)方式二
public void removenode2(Node parent, Node cur) { if (cur.left == null) { if (cur == root) { root = cur.right; } else if (cur == parent.right) { parent.left = cur.right; } else { parent.right = cur.right; } } else if (cur.right == null) { if (cur == root) { root.left = cur; } else if (cur == parent.right) { parent.right = cur.left; } else { parent.left = cur.left; } } else { Node tp = cur; Node t = cur.left; while (t.right != null) { tp = t; t = t.right; } if (tp.right == t) { cur.val = t.val; tp.right = t.left; } if (tp.left == t) { cur.val = t.val; tp.left = t.left; } } }
五、運(yùn)行結(jié)果
/** * user:ypc; * date:2021-05-18; * time: 15:09; */ class TestBinarySearchTree { public static void main(String[] args) { int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9}; BinarySearchTree binarySearchTree = new BinarySearchTree(); for (int i = 0; i < a.length; i++) { binarySearchTree.insert(a[i]); } binarySearchTree.inOrderTree(binarySearchTree.root); System.out.println(); binarySearchTree.preOrderTree(binarySearchTree.root); binarySearchTree.remove(7); System.out.println(); System.out.println("方法一刪除后"); binarySearchTree.inOrderTree(binarySearchTree.root); System.out.println(); binarySearchTree.preOrderTree(binarySearchTree.root); } }
到此這篇關(guān)于Java基礎(chǔ)之二叉搜索樹的基本操作的文章就介紹到這了,更多相關(guān)二叉搜索樹的基本操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringFox實(shí)現(xiàn)自動(dòng)生成RESTful?API文檔
在開發(fā)?RESTful?API?時(shí),編寫?API?文檔是一個(gè)重要的任務(wù),這篇文章為大家介紹了如何使用?SpringFox?自動(dòng)生成?RESTful?API?文檔,并提供示例代碼,需要的可以參考一下2023-06-06使用Java自帶的mail?API實(shí)現(xiàn)郵件發(fā)送功能全過(guò)程
電子郵件的應(yīng)用非常廣泛,例如在某網(wǎng)站注冊(cè)了一個(gè)賬戶,自動(dòng)發(fā)送一封歡迎郵件,通過(guò)郵件找回密碼,自動(dòng)批量發(fā)送活動(dòng)信息等,下面這篇文章主要給大家介紹了關(guān)于如何使用Java自帶的mail?API實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,需要的朋友可以參考下2023-04-04JVM垃圾回收機(jī)制和垃圾回收器詳細(xì)解說(shuō)
這篇文章主要介紹了JVM垃圾回收機(jī)制和垃圾回收器,為了讓程序員更加專注于代碼的實(shí)現(xiàn),而不用過(guò)多的考慮內(nèi)存釋放的問(wèn)題,所以在Java語(yǔ)言中,有了自動(dòng)的垃圾回收機(jī)制,也是我們常常提及的GC,需要的朋友可以參考下2022-07-07IDEA 啟動(dòng) Tomcat 項(xiàng)目輸出亂碼的解決方法
這篇文章主要介紹了IDEA 啟動(dòng) Tomcat 項(xiàng)目輸出亂碼的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Java8中字符串處理庫(kù)strman-java的使用示例
除了Java本身的字符串處理方式外,我們還可以使用Apache Common Langs里的StringUtils來(lái)簡(jiǎn)化String的操作。但以上兩種方式對(duì)于我們?nèi)粘>幊讨凶钊菀着龅降淖址幚韥?lái)說(shuō),仍然顯得有些不足。所以這篇文章給大家介紹Java8中字符串處理庫(kù)strman-java的使用。2016-09-09JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細(xì)介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12