java數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹
本文實例為大家分享了java數(shù)據(jù)結(jié)構(gòu)之搜索二叉樹的具體代碼,供大家參考,具體內(nèi)容如下
搜索二叉樹的定義是:在一個二叉樹上,左節(jié)點一定比父節(jié)點小,右節(jié)點一定比父節(jié)點大,其他定義跟二叉樹相同。
代碼實現(xiàn):
public class node {
? ? int data;
? ? public node left, right=null;
?
? ? public node(int data) {
? ? ? ? this.data = data;
?
? ? }
?
? ? public node(int data, node left, node right) {
? ? ? ? this.data = data;
? ? ? ? this.right = right;
? ? ? ? this.left = left;
? ? }
? ? //二叉搜索樹
? ? public static void insert(node root, node node) {
?
? ? ? ? if (root.data >= node.data) {
?
? ? ? ? ? ? if (root.right != null) {
? ? ? ? ? ? ? ? insert(root.right, node);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? root.right=node;
? ? ? ? ? ? }
?
? ? ? ? } else {
?
? ? ? ? ? ? if (root.left != null) {
? ? ? ? ? ? ? ? insert(root.left,node);
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? root.left=node;
? ? ? ? ? ? }
? ? ? ? }
?
? ? }
?
? ? //前序遍歷
? ? public static void before(node root) {
? ? ? ? if (root == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? System.out.println("data:" + root.data);
? ? ? ? before(root.left);
? ? ? ? before(root.right);
? ? }
?
? ? //中序遍歷
? ? public static void mid(node root) {
? ? ? ? if (root == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? mid(root.left);
? ? ? ? System.out.println("data:" + root.data);
? ? ? ? mid(root.right);
? ? }
?
? ? //后序遍歷
? ? public static void after(node root) {
? ? ? ? if (root == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? after(root.left);
? ? ? ? after(root.right);
? ? ? ? System.out.println("data:" + root.data);
?
? ? }
?
? ? public static boolean search(int target, node root) {
? ? ? ? if(root == null) {
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? if (root.data > target) {
? ? ? ? ? ? search(target, root.left);
? ? ? ? } else if (root.data < target) {
? ? ? ? ? ? search(target, root.right);
? ? ? ? } else {
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
?
?
}node.java中:data 節(jié)點存放的數(shù)據(jù),left,right 左右子節(jié)點
before() after() mid()為三種前序遍歷,中序遍歷,后序遍歷。關(guān)鍵方法 insert() search()
insert():參數(shù):root node root為你的根節(jié)點,node為你要插入的節(jié)點。遞歸調(diào)用insert()當(dāng)遞歸到某個節(jié)點的右節(jié)點為空時表示可以插入數(shù)據(jù)
流程:

這里有六個節(jié)點作為示例:圓中為數(shù)據(jù),簡單的一個節(jié)點。選定3為根節(jié)點,隨機插入0 2 1 4 5 6

第一步,根節(jié)點3,第二步分別插入021 比三大的數(shù)跟這個類似,不做展示了。
插入0的時候沒有問題,放在3的左邊,插入2的時候,遞歸,2<3,2>0先看當(dāng)前節(jié)點(也就是3)的右邊是否有數(shù)據(jù),為什么不看當(dāng)前節(jié)點左子節(jié)點的數(shù)據(jù),因為,當(dāng)前節(jié)點的左子節(jié)點一定比當(dāng)前節(jié)點大,所以只找當(dāng)前節(jié)點右邊的數(shù)據(jù)。當(dāng)右邊節(jié)點為空的時候,才會插入數(shù)據(jù),這樣2就插入完成了,現(xiàn)在輪到1了,對于1,跟上面類似..
但是這樣會造成一個問題:這樣的查找效率很低,對于這樣特定的數(shù)據(jù),所以要使用平衡二叉樹中的旋轉(zhuǎn),重新選定節(jié)點來平衡二叉樹。關(guān)于二叉樹的文章,過幾天發(fā)布。
主函數(shù):
public class main {
? ? public static void main(String[] args) {
? ? ? ? node root = new node(0);
? ? ? ? node root1 = new node(2);
? ? ? ? node root2 = new node(1);
? ? ? ? node root3 = new node(3);
? ? ? ? node root4 = new node(4);
? ? ? ? node root5 = new node(5);
? ? ? ? node root6 = new node(6);
? ? ? ? node.insert(root3,root);
? ? ? ? node.insert(root3,root2);
? ? ? ? node.insert(root3,root1);
? ? ? ? node.insert(root3,root4);
? ? ? ? node.insert(root3,root5);
? ? ? ? node.insert(root3,root6);
? ? ? ? node.mid(root3);
? ? ? ? boolean i= node.search(10,root3);
? ? ? ? System.out.println(i);
? ? ?
? ? }
?
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的實現(xiàn)詳解
- 詳解Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹
- Java數(shù)據(jù)結(jié)構(gòu)最清晰圖解二叉樹前 中 后序遍歷
- Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹的原理與實現(xiàn)
- 帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹
- Java 數(shù)據(jù)結(jié)構(gòu)中二叉樹前中后序遍歷非遞歸的具體實現(xiàn)詳解
- Java數(shù)據(jù)結(jié)構(gòu)二叉樹難點解析
- Java?數(shù)據(jù)結(jié)構(gòu)進(jìn)階二叉樹題集下
相關(guān)文章
用Java集合中的Collections.sort方法如何對list排序(兩種方法)
本文通過兩種方法給大家介紹java集合中的Collections.sort方法對list排序,第一種方式是list中的對象實現(xiàn)Comparable接口,第二種方法是根據(jù)Collections.sort重載方法實現(xiàn),對collections.sort方法感興趣的朋友一起學(xué)習(xí)吧2015-10-10
Java面試官最喜歡問的關(guān)鍵字之volatile詳解
這篇文章主要給大家介紹了關(guān)于Java面試官最喜歡問的關(guān)鍵字之volatile的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
myBatis實現(xiàn)三級嵌套復(fù)雜對象的賦值問題
這篇文章主要介紹了myBatis實現(xiàn)三級嵌套復(fù)雜對象的賦值問題,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
解決Springboot集成Redis集群配置公網(wǎng)IP連接報私網(wǎng)IP連接失敗問題
在Springboot 集成 Redis集群配置公網(wǎng)IP連接報私網(wǎng)IP連接失敗,一直報私有IP連接失敗,所以本文小編給大家介紹了如何解決報錯問題,如果有遇到相同問題的同學(xué),可以參考閱讀本文2023-10-10
搭建MyBatis-Plus框架并進(jìn)行數(shù)據(jù)庫增刪改查功能
這篇文章主要介紹了搭建MyBatis-Plus框架并進(jìn)行數(shù)據(jù)庫增刪改查,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Java調(diào)用opencv實現(xiàn)圖片矯正功能
這篇文章主要為大家詳細(xì)介紹了Java如何調(diào)用opencv實現(xiàn)圖片矯正功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
Java HttpURLConnection超時和IO異常處理
這篇文章主要介紹了Java HttpURLConnection超時和IO異常處理的相關(guān)資料,需要的朋友可以參考下2016-09-09

