二叉查找树的实现
文章目錄
- 前言
前言
??????用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){//測試...} }總結
- 上一篇: FFTW、Eigen库在VisualSt
- 下一篇: LeetCode算法题4:二分查找及扩展