日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二叉查找树的插入,删除,查找

發(fā)布時間:2024/1/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉查找树的插入,删除,查找 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
二叉查找樹的添加,刪除,查找算法:import java.util.Scanner;public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {BiNode root;class BiNode<AnyType>{AnyType data;BiNode left;BiNode right;public BiNode(AnyType data){this.data=data;}public BiNode(AnyType data,BiNode left,BiNode right){this.data=data;this.left=left;this.right=right;}}//添加public void insert(AnyType x){root=insert(x,root);}public BiNode insert(AnyType x,BiNode t){if(t==null)return new BiNode(x,null,null);int compareResult=x.compareTo((AnyType)t.data); if(compareResult>0)t.right=insert(x,t.right);if(compareResult<0)t.left=insert(x,t.left);else;return t;}//查找public boolean contains(AnyType x){return contains(x,root);}public boolean contains(AnyType x,BiNode t){if(t==null)return false;int compareResult=x.compareTo((AnyType)t.data);if(compareResult>0)return contains(x,t.right);if(compareResult<0)return contains(x,t.left);else return true;}//找最大和最小public AnyType findMin(){return (AnyType) findMin(root).data;}public BiNode findMin(BiNode t){if(t==null)return null;else if(t.left==null)return t;elsereturn findMin(t.left);}public AnyType findMax(){ return (AnyType) findMax(root).data;}public BiNode findMax(BiNode t){if(t==null)return null;else if(t.left==null)return t;elsereturn findMax(t.left);}//刪除public void remove(AnyType x){root=remove(x,root); }public BiNode remove(AnyType x,BiNode t){if(t==null)return t;int compareResult=x.compareTo((AnyType)t.data);if(compareResult<0)t.left=remove(x,t.left);else if(compareResult>0)t.right=remove(x,t.right);else if(t.left!=null&&t.right!=null){t.data=findMin(t.right).data;t.right=remove((AnyType)t.data,t.right);}elset=(t.left!=null)?t.left:t.right;return t; }//中序遍歷public void inOrder(){inOrder(root);}public void inOrder(BiNode t){if(t!=null){inOrder(t.left);System.out.print(t.data+" ");inOrder(t.right);}}public static void main(String[] args) {Scanner sc=new Scanner(System.in);BinarySearchTree<Integer> bsh=new BinarySearchTree<Integer>();for(int i=0;i<6;i++){ //輸入六個數(shù),個數(shù)可改變int numble=sc.nextInt();bsh.insert(numble);}bsh.inOrder();}}

版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。

轉(zhuǎn)載于:https://www.cnblogs.com/dingxiaoyue/p/4931860.html

總結(jié)

以上是生活随笔為你收集整理的二叉查找树的插入,删除,查找的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。