資料結構(Java語言)——BinarySearchTree簡單實現

來源:互聯網
上載者:User

標籤:資料結構   java   二叉樹   遞迴   二叉搜尋樹   

二叉樹的一個重要應用是它們在尋找中的使用。使二叉樹成為二叉尋找樹的性質是,對於樹中的每個節點X,它的左子樹中所有項的值都大於X中的項。注意,這意味著該樹所有的元素都可以用某種一致的方式排序。

現在給出通常對二叉尋找樹進行的操作的簡單描述。注意,由於樹的遞迴定義,通常是遞迴地編寫這些操作的常式。因為二叉尋找樹的平均深度是O(logN),所以一般不必擔心棧空間耗盡。

二叉尋找樹要求所有的項都能夠排序。要寫出一個一般的類,我們需要提供一個介面來表示這個性質。這個介面就是Comparable,它告訴我們樹中的兩項總可以使用compareTo()方法進行比較。由此我們可以確定所有可能關係,特別是以compareTo()返回0來替代equal()判斷相等。BinarySearchTree類還包含一個嵌套類BinaryNode,用來表示樹的節點。

以下是相關代碼及實現:

public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {private BinaryNode<AnyType> root;private static class BinaryNode<AnyType> {AnyType element;BinaryNode<AnyType> left;BinaryNode<AnyType> right;BinaryNode(AnyType theElement) {this(theElement, null, null);}BinaryNode(AnyType theElement, BinaryNode<AnyType> lt,BinaryNode<AnyType> rt) {element = theElement;left = lt;right = rt;}}public BinarySearchTree() {makeEmpty();}/** * 使樹為空白樹 */public void makeEmpty() {root = null;}/** * 該樹是否為空白樹 *  * @return 是否空 */public boolean isEmpty() {return root == null;}/** * 該樹是否存在含有參數值的節點 *  * @param value *            元素值 * @return 是否含該元素 */public boolean contains(AnyType value) {return contains(value, root);}/** * 某個節點及它的子節點是否存在含有參數值的節點 *  * @param value *            元素值 * @param node *            節點 * @return */private boolean contains(AnyType value, BinaryNode<AnyType> node) {if (node == null) {return false;}int compareResult = value.compareTo(node.element);if (compareResult < 0) { // 插入節點值小於節點值,則遞迴尋找左子樹下return contains(value, node.left);} else if (compareResult > 0) { // 插入節點值大於節點值,則遞迴尋找右子樹下return contains(value, node.right);} else {return true;}}/** * 尋找該樹最小元素值 *  * @return 最小元素值 */public AnyType findMin() {if (isEmpty()) {throw new NullPointerException();}return findMin(root).element;}/** * 尋找某節點及其子樹中的最小元素 *  * @param node *            父節點 * @return 最小元素所在節點 */private BinaryNode<AnyType> findMin(BinaryNode<AnyType> node) {if (node == null) {return null;} else if (node.left == null) {return node;}return findMin(node.left);}/** * 尋找該樹最大元素值 *  * @return 最大元素值 */public AnyType findMax() {if (isEmpty()) {throw new NullPointerException();}return findMavalue(root).element;}/** * 尋找某節點及其子樹中的最大元素 *  * @param node *            父節點 * @return 最大元素 */private BinaryNode<AnyType> findMavalue(BinaryNode<AnyType> node) {if (node == null) {return null;} else if (node.right == null) {return node;}return findMavalue(node.right);}/** * 向樹中插入某元素 *  * @param value *            插入元素值 */public void insert(AnyType value) {root = insert(value, root);}/** * 向某個節點下插入元素 *  * @param value *            元素值 * @param node *            父節點 * @return 元素插入的節點 */private BinaryNode<AnyType> insert(AnyType value, BinaryNode<AnyType> node) {if (node == null) {return new BinaryNode<AnyType>(value);}int compareResult = value.compareTo(node.element);if (compareResult < 0) {node.left = insert(value, node.left);} else if (compareResult > 0) {node.right = insert(value, node.right);}return node;}/** * 向樹中刪除某元素 *  * @param value *            元素值 */public void remove(AnyType value) {root = remove(value, root);}/** * 在某個節點下刪除元素 *  * @param value *            元素值 * @param node *            父節點 * @return 刪除元素的節點 */private BinaryNode<AnyType> remove(AnyType value, BinaryNode<AnyType> node) {if (node == null) {return node;}int compareResult = value.compareTo(node.element);if (compareResult < 0) {node.left = remove(value, node.left);} else if (compareResult > 0) {node.right = remove(value, node.right);} else if (node.left != null && node.right != null) {node.element = findMin(node.right).element;node.right = remove(node.element, node.right);} else {node = (node.left != null) ? node.left : node.right;}return node;}/** * 遍曆輸出樹 */public void printTree() {if (isEmpty()) {System.out.println("Empty tree");} else {printTree(root);}}/** * 先序遍曆輸出某節點下元素 *  * @param node *            節點 */private void printTree(BinaryNode<AnyType> node) {if (node != null) {printTree(node.left);System.out.print(node.element + " ");printTree(node.right);}}public static void main(String[] args) {BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();tree.insert(8);tree.insert(6);tree.insert(2);tree.insert(4);tree.insert(1);tree.insert(3);tree.printTree();tree.remove(2);tree.remove(6);tree.insert(5);tree.insert(7);System.out.println("\n------------");tree.printTree();}}
執行結果:


1 2 3 4 6 8 
------------
1 3 4 5 7 8 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

資料結構(Java語言)——BinarySearchTree簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.