Java基礎(chǔ)之二叉搜索樹的基本操作
更新時(shí)間:2021年05月23日 09:36:27 作者:保護(hù)眼睛
發(fā)現(xiàn)許多小伙伴還不清楚Java二叉搜索樹的基本操作,今天特地整理了這篇文章,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)Java的小伙伴很有幫助,需要的朋友可以參考下
一、二叉搜索樹插入元素
/** * 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)容請搜索腳本之家以前的文章或繼續(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ā)送功能全過程
電子郵件的應(yīng)用非常廣泛,例如在某網(wǎng)站注冊了一個(gè)賬戶,自動(dòng)發(fā)送一封歡迎郵件,通過郵件找回密碼,自動(dòng)批量發(fā)送活動(dòng)信息等,下面這篇文章主要給大家介紹了關(guān)于如何使用Java自帶的mail?API實(shí)現(xiàn)郵件發(fā)送功能的相關(guān)資料,需要的朋友可以參考下2023-04-04IDEA 啟動(dòng) Tomcat 項(xiàng)目輸出亂碼的解決方法
這篇文章主要介紹了IDEA 啟動(dòng) Tomcat 項(xiàng)目輸出亂碼的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細(xì)介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12