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()當遞歸到某個節(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先看當前節(jié)點(也就是3)的右邊是否有數(shù)據(jù),為什么不看當前節(jié)點左子節(jié)點的數(shù)據(jù),因為,當前節(jié)點的左子節(jié)點一定比當前節(jié)點大,所以只找當前節(jié)點右邊的數(shù)據(jù)。當右邊節(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é)習有所幫助,也希望大家多多支持腳本之家。
- 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)進階二叉樹題集下
相關(guān)文章
Java編程數(shù)組中最大子矩陣簡便解法實現(xiàn)代碼
這篇文章主要介紹了Java編程數(shù)組中最大子矩陣簡便解法實現(xiàn)代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01應(yīng)用Java泛型和反射導(dǎo)出CSV文件的方法
這篇文章主要介紹了應(yīng)用Java泛型和反射導(dǎo)出CSV文件的方法,通過一個自定義函數(shù)結(jié)合泛型與反射的應(yīng)用實現(xiàn)導(dǎo)出CSV文件的功能,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12SpringBoot自動初始化數(shù)據(jù)庫的方法分享
我們在項目中應(yīng)該經(jīng)常遇到過初始化數(shù)據(jù)的場景,特別是項目部署或者交付的時候,那么有什么方式可以在項目啟動的時候自動初始化數(shù)據(jù)庫呢,下面小編就來和大家分享幾個方法吧2023-08-08