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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.test.tree;import java.util.Iterator;/*** 編寫TreeSet類的實現(xiàn)程序,其中相關(guān)的迭代器使用二叉查找樹。* 在每個節(jié)點上添加一個指向其父節(jié)點的鏈* @author wyl* @param <T>*/ public class MyTreeSet<T extends Comparable<? super T>> {private BinaryNode<T> root; //根節(jié)點int modCount = 0; //記錄調(diào)整樹結(jié)構(gòu)的次數(shù)public MyTreeSet(){root = null;}/*** 定義二叉查找樹的節(jié)點*/private class BinaryNode<T>{T data; //節(jié)點的值BinaryNode<T> left; //節(jié)點的左節(jié)點BinaryNode<T> right; //節(jié)點右節(jié)點BinaryNode<T> parent; //節(jié)點的父節(jié)點public BinaryNode(T data){this(data, null, null, null);}public BinaryNode(T data, BinaryNode<T> lt, BinaryNode<T> rt, BinaryNode<T> pt) {this.data = data;this.left = lt;this.right = rt;this.parent = pt;}}/*** 定義TreeSet的迭代器* @return*/public Iterator iterator(){return new MyTreeSetIterator();}private class MyTreeSetIterator implements Iterator {private BinaryNode<T> current = findMin(root);private BinaryNode<T> previous;private int expectedModCount = modCount;private boolean okToRemove = false;private boolean atEnd = false;@Overridepublic boolean hasNext() {// TODO Auto-generated method stubreturn !atEnd;}@Overridepublic T next() {// TODO Auto-generated method stubif(modCount != expectedModCount){try {throw new CurrentModificationException();} catch (CurrentModificationException e) {// TODO Auto-generated catch block e.printStackTrace();}}if(!hasNext()){try {throw new NoSuchElementException();} catch (NoSuchElementException e) {// TODO Auto-generated catch block e.printStackTrace();}}T nextItem = current.data;previous = current;if(current.right != null){current = findMin(current.right);}else{BinaryNode<T> child = current;current = current.parent;while(current != null && current.left != child){child = current;current = current.parent;}if(current == null){atEnd = true;}}okToRemove = true;return nextItem;}@Overridepublic void remove(){// TODO Auto-generated method stubif(modCount != expectedModCount){try {throw new CurrentModificationException();} catch (CurrentModificationException e) {// TODO Auto-generated catch block e.printStackTrace();}}if(!okToRemove){throw new IllegalStateException();}try {MyTreeSet.this.remove(previous.data);} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}okToRemove = false;}}public void makeEmpty(){modCount++ ;root = null;}public boolean isEmpty(){return root == null;}public boolean contains(T x){return contains(x, root); }public boolean contains(T x, BinaryNode<T> t){if(t == null){return false;}int compareResult = x.compareTo(t.data);if(compareResult < 0){return contains(x, t.left);}else if(compareResult > 0){return contains(x, t.right);}else{return true;}}public T findMin(){if(isEmpty()){try {throw new UnderflowException();} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}}return findMin(root).data;}public T findMax(){if(isEmpty()){try {throw new UnderflowException();} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}}return findMax(root).data;}public void insert(T x){root = insert(x, root, null);}public void remove(T x) throws UnderflowException{root = remove(x, root);}public void printTree(){if(isEmpty()){System.out.println("Empty tree");}else{printTree(root);}}public void printTree(BinaryNode<T> t){if(t != null){printTree(t.left);System.out.println(t.data);printTree(t.right);}}public BinaryNode<T> remove(T x, BinaryNode<T> t){if(t == null){try {throw new UnderflowException();} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}}int compareResult = x.compareTo(t.data);if(compareResult < 0){t = remove(x, t.left);}else if(compareResult > 0){t = remove(x, t.right);}else if(t.left != null && t.right != null){//要刪除的節(jié)點是含有左右子樹的節(jié)點t.data = findMin(t.right).data;//將右子樹的最小值作為根節(jié)點t.right = remove(t.data, t.right);}else{modCount++ ;BinaryNode<T> oneChild;oneChild = (t.left == null)?t.left:t.right;oneChild.parent = t.parent;t = oneChild;}return t;}public BinaryNode<T> insert(T x, BinaryNode<T> t, BinaryNode<T> parent){if(t == null){modCount++ ;//空樹return new BinaryNode(x, null, null, parent);}int compareResult = x.compareTo(t.data);if(compareResult < 0){//要插入的數(shù)小于節(jié)點值,插入到左子樹t.left = insert(x, t.left, t);}else if(compareResult > 0){//要插入的數(shù)小于節(jié)點值,插入到左子樹t.right = insert(x, t.right, t);}else{}return t;}public BinaryNode<T> findMin(BinaryNode<T> t){// TODO Auto-generated method stubif(t == null){try {throw new UnderflowException();} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}}else if(t.left == null){return t;}return findMin(t.left);}public BinaryNode<T> findMax(BinaryNode<T> t){// TODO Auto-generated method stubif(t == null){try {throw new UnderflowException();} catch (UnderflowException e) {// TODO Auto-generated catch block e.printStackTrace();}}else if(t.right == null){return t;}return findMax(t.right);}public static void main(String[] args) {MyTreeSet<Integer> myTreeSet = new MyTreeSet<Integer>();myTreeSet.insert(24);myTreeSet.insert(23);myTreeSet.insert(16);myTreeSet.insert(20);myTreeSet.insert(28);myTreeSet.insert(29);System.out.println("最小值: "+ myTreeSet.findMin());System.out.println("最大值: "+ myTreeSet.findMax());Iterator iter = myTreeSet.iterator();while(iter.hasNext()){System.out.print(iter.next() + "、");}} } class UnderflowException extends Exception{} class CurrentModificationException extends Exception{} class NoSuchElementException extends Exception{}

?

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

總結(jié)

以上是生活随笔為你收集整理的编写TreeSet类的实现程序,其中相关的迭代器使用二叉查找树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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