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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

二叉查找树的实现

發(fā)布時間:2025/6/17 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉查找树的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 前言


前言

??????用Java簡單的實現二叉查找樹。

//實現二叉查找樹import java.nio.BufferUnderflowException; import java.util.ArrayDeque;//T extends Comparable<T> 表示T類必須實現Comparable接口。 //T extends Comparable<? super T> 表示T類或者其父類必須實現Comparable接口。 //如果自定義一個 class NewInteger extends Integer, // 那么Integer extends Comparable<Integer> 時, BinarySearchTree<NewInteger>=null; fail! // 而Integer extends Comparable<? super Integer>時,BinarySearchTree<NewInteger>=null; pass! (因為Integer已經實現了Comparable接口。 public class BinarySearchTree<T extends Comparable<? super T>>{private static class BinaryNode<T> { //節(jié)點T element;BinaryNode<T> left;BinaryNode<T> right;public BinaryNode(T a){element=a;left=right=null;}public BinaryNode(T a, BinaryNode<T> le, BinaryNode<T> ri){element=a;left=le;right=ri;}}private BinaryNode<T> root;//root節(jié)點。public BinarySearchTree(){root=null;}public boolean isEmpty(){return root==null;}public void makeEmpty(){root=null;}public boolean contains(T element){return contains(element,root);}private boolean contains(T element,BinaryNode<T> root){if(root==null)return false;if(element.compareTo(root.element)==0)return true;if(element.compareTo(root.element)<0)return contains(element,root.left);elsereturn contains(element,root.right);}public T findMin(){if(isEmpty())throw new BufferUnderflowException();return findMin(root);}private T findMin(BinaryNode<T> root){while(root.left!=null)root=root.left;return root.element;}public T findMax(){if(isEmpty())throw new BufferUnderflowException();return findMax(root);}private T findMax(BinaryNode<T> root){if(root.right==null)return root.element;return findMax(root.right);}public void insert(T element){root=insert(element,root);}private BinaryNode<T> insert(T element,BinaryNode<T> root){ //插入if(root==null)return new BinaryNode<T>(element);int compareRe=element.compareTo(root.element);if(compareRe<0)root.left=insert(element,root.left);else if(compareRe>0)root.right=insert(element,root.right);else; //遇到重復元素,什么也不做。 當然也可加上一個頻率域表示某個元素出現的次數。return root;}public void remove(T element){root=remove(element,root);}private BinaryNode<T> remove(T element,BinaryNode<T> root){if(root==null)return root;int compareRe=element.compareTo(root.element);if(compareRe<0)root.left=remove(element,root.left);else if(compareRe>0)root.right=remove(element,root.right);else if(root.left!=null&&root.right!=null){//兩個孩子root.element=findMax(root.left);//左側最大或右側最小root.left=remove(root.element,root.left);}else//一個孩子或沒有孩子。root=(root.right==null)?root.left:root.right;return root;}public void printTree(){//分層打印樹節(jié)點。if(root==null)return;ArrayDeque<BinaryNode<T>> queue=new ArrayDeque<>();queue.offerLast(root);int remainingNode=1,nextLayerN=0;while(!queue.isEmpty()){BinaryNode<T> tmp=queue.pollFirst();System.out.print(tmp.element+" ");remainingNode--;if(tmp.left!=null){queue.offerLast(tmp.left);nextLayerN++;}if(tmp.right!=null){queue.offerLast(tmp.right);nextLayerN++;}if(remainingNode==0){System.out.println();remainingNode=nextLayerN;nextLayerN=0;}}}public static void main(String[] args){//測試...} }

總結

以上是生活随笔為你收集整理的二叉查找树的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。