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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

基本数据结构之BinarySearchTree

發布時間:2025/3/17 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基本数据结构之BinarySearchTree 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

問題描述:?

BinarySearchTree


問題分析:?

基本的實現


代碼實現:

package?c04; /***?@project:?DataStructureAndAlgorithmAnalysis*?@filename:?BinarySearchTree.java*?@version:?0.10*?@author:?JM?Han*?@date:?18:38?2015/10/19*?@comment:?Test?Purpose*?@result:*/import?c02.BinarySearch; import?master.UnderflowException; import?java.util.Comparator; import?static?tool.util.*;public?class?BinarySearchTree<AnyType?extends?Comparable<??super?AnyType>>?{private?static?class?BinaryNode<AnyType>{BinaryNode(AnyType?theElement){this(theElement,?null,?null);}BinaryNode(AnyType?theElement,?BinaryNode<AnyType>?lt,?BinaryNode<AnyType>?rt){element?=?theElement;?left?=?lt;?right?=?rt;}AnyType?element;BinaryNode<AnyType>?left;BinaryNode<AnyType>?right;}private?BinaryNode<AnyType>?root;public?BinarySearchTree(){root?=?null;}public?void?makeEmpty(){root?=?null;}public?boolean?isEmpty(){return?null?==?root;}public?boolean?contains(AnyType?x){return?contains(x,?root);}public?AnyType?findMin(){if(isEmpty())throw?new?UnderflowException();return?findMin(root).element;}public?AnyType?findMax(){if(isEmpty())throw?new?UnderflowException();return?findMax(root).element;}public?void?insert(AnyType?x){root?=?insert(x,?root);}public?void?remove(AnyType?x){root?=?remove(x,?root);}public?void?printTree(){if(isEmpty())System.out.println("Empty?Tree");elseprintTree(root);}private?boolean?contains(AnyType?x,?BinaryNode<AnyType>?t){if(t?==?null)return?false;int?comparableResult?=?x.compareTo(t.element);if(comparableResult?<?0)return?contains(x,?t.left);else?if(comparableResult?>?0)return?contains(x,?t.right)?;elsereturn?true;}private?BinaryNode<AnyType>?findMin(BinaryNode<AnyType>?t){if(null?==?t)return?null;else?if(t.left?==?null)return?t;return?findMin(t.left);}private?BinaryNode<AnyType>?findMax(BinaryNode<AnyType>?t){if(t?!=?null)while(t.right?!=?null)t?=?t.right;return?t;}private?BinaryNode<AnyType>?insert(AnyType?x,?BinaryNode<AnyType>?t){if(null?==?t)return?new?BinaryNode<AnyType>(x,?null,?null);int?compareResult?=?x.compareTo(t.element);if(compareResult?<?0)t.left?=?insert(x,?t.left);else?if(compareResult?>?0)t.right?=?insert(x,?t.right);else;return?t;}private?BinaryNode<AnyType>?remove(AnyType?x,?BinaryNode<AnyType>?t){if(null?==?t)return?t;int?compareResult?=?x.compareTo(t.element);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.element?=?findMin(t.right).element;t.right?=?remove(t.element,?t.right);}elset?=?(t.left?!=?null)?t.left:t.right;return?t;}private?void?printTree(BinaryNode<AnyType>?t){if(null?!=?t){printTree(t.left);System.out.println(t.element);printTree(t.right);}}public?static?void?main(String[]?args)?{BinarySearchTree<Integer>?bst?=?new?BinarySearchTree<Integer>();bst.insert(4);bst.insert(3);bst.insert(6);bst.insert(0);bst.insert(2);bst.insert(3);System.out.println("Contains?3:?"?+?bst.contains(3));System.out.println("Max:?"?+?bst.findMax());System.out.println("Min:?"?+?bst.findMin());bst.remove(3);bst.printTree();} }

代碼輸出:

Contains?3:?true Max:?6 Min:?0 0 2 4 6


轉載于:https://my.oschina.net/jimmyhan/blog/519351

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的基本数据结构之BinarySearchTree的全部內容,希望文章能夠幫你解決所遇到的問題。

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