数据结构与算法-二叉查找树(java描述)
生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法-二叉查找树(java描述)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、概述
二叉排序樹(Binary Sort Tree)又稱二叉查找樹(Binary Search Tree),亦稱二叉搜索樹。
1.1、定義
二叉排序樹或者是一棵空樹,或者是具有下列性質的二叉樹:
(1)若左子樹不空,則左子樹上所有結點的值均小于或等于它的根結點的值;
(2)若右子樹不空,則右子樹上所有結點的值均大于或等于它的根結點的值;
(3)左、右子樹也分別為二叉排序樹;
二、二叉查找樹的實現
這里的二叉查找樹的定義是在上篇博客的二叉樹的定義上擴展的,因此這些操作是二叉樹已定義的那些操作的補充。
2.1、抽象實現
BinarySearchTreeADT.java:
package sihai;/*** 二叉查找樹接口*/ public interface BinarySearchTreeADT<T> extends BinaryTreeADT<T> {/** * 在樹中的適當位置添加一個元素*/public void addElement(T element);/** * 根據特定的元素刪除元素*/ public T removeElement(T targetElement);/** * 刪除和目標元素有關的所有元素*/public void removeAllOccurrences(T targetElement);/** * 刪除樹中的最小元素*/public T removeMin();/** * 刪除樹中的最大元素*/ public T removeMax();/** * 查找樹中的最小元素*/ public T findMin();/** *查找樹中的最大元素*/public T findMax(); }三、用鏈表實現二叉查找樹
這里BinaryTreeNode類來表示樹中的每個節點。每個BinaryTreeNode對象要維護一個指向節點所存儲元素的引用,另外還要維護指向節點的每個孩子的引用。
LinkedBinaryTree提供兩個構造函數:一個負責創建空的;另外一個負責創建一顆根節點為特定的元素的鏈接二叉查找樹。
LinkedBinaryTree.java:
package jsjf;import jsjf.exceptions.*; import jsjf.*;/*** 鏈接二叉查找樹實現了鏈接二叉樹接口和二叉查找樹接口*/ public class LinkedBinarySearchTree<T> extends LinkedBinaryTree<T>implements BinarySearchTreeADT<T> {public LinkedBinarySearchTree() {super();}public LinkedBinarySearchTree(T element) {super(element);if (!(element instanceof Comparable))throw new NonComparableElementException("LinkedBinarySearchTree");}/*** 添加元素*/public void addElement(T element) {if (!(element instanceof Comparable))throw new NonComparableElementException("LinkedBinarySearchTree");Comparable<T> comparableElement = (Comparable<T>)element;if (isEmpty())root = new BinaryTreeNode<T>(element);else {if (comparableElement.compareTo(root.getElement()) < 0){if (root.getLeft() == null) this.getRootNode().setLeft(new BinaryTreeNode<T>(element));elseaddElement(element, root.getLeft());}else{if (root.getRight() == null) this.getRootNode().setRight(new BinaryTreeNode<T>(element));elseaddElement(element, root.getRight());}}modCount++;}/*** 在特定的節點插入元素*/private void addElement(T element, BinaryTreeNode<T> node) {Comparable<T> comparableElement = (Comparable<T>)element;if (comparableElement.compareTo(node.getElement()) < 0){if (node.getLeft() == null) node.setLeft(new BinaryTreeNode<T>(element));elseaddElement(element, node.getLeft());}else{if (node.getRight() == null) node.setRight(new BinaryTreeNode<T>(element));elseaddElement(element, node.getRight());}}/*** 刪除目標元素*/public T removeElement(T targetElement)throws ElementNotFoundException {T result = null;if (isEmpty())throw new ElementNotFoundException("LinkedBinarySearchTree");else{BinaryTreeNode<T> parent = null;if (((Comparable<T>)targetElement).equals(root.element)) {result = root.element;BinaryTreeNode<T> temp = replacement(root);if (temp == null)root = null;else {root.element = temp.element;root.setRight(temp.right);root.setLeft(temp.left);}modCount--;}else { parent = root;if (((Comparable)targetElement).compareTo(root.element) < 0)result = removeElement(targetElement, root.getLeft(), parent);elseresult = removeElement(targetElement, root.getRight(), parent);}}return result;}/*** 刪除元素*/private T removeElement(T targetElement, BinaryTreeNode<T> node, BinaryTreeNode<T> parent)throws ElementNotFoundException {T result = null;if (node == null)throw new ElementNotFoundException("LinkedBinarySearchTree");else{if (((Comparable<T>)targetElement).equals(node.element)) {result = node.element;BinaryTreeNode<T> temp = replacement(node);if (parent.right == node)parent.right = temp;else parent.left = temp;modCount--;}else { parent = node;if (((Comparable)targetElement).compareTo(node.element) < 0)result = removeElement(targetElement, node.getLeft(), parent);elseresult = removeElement(targetElement, node.getRight(), parent);}}return result;}/*** 返回一個引用代替將要刪除的元素*/private BinaryTreeNode<T> replacement(BinaryTreeNode<T> node) {BinaryTreeNode<T> result = null;if ((node.left == null) && (node.right == null))result = null;else if ((node.left != null) && (node.right == null))result = node.left;else if ((node.left == null) && (node.right != null))result = node.right;else{BinaryTreeNode<T> current = node.right;BinaryTreeNode<T> parent = node;while (current.left != null){parent = current;current = current.left;}current.left = node.left;if (node.right != current){parent.left = current.right;current.right = node.right;}result = current;}return result;}/*** 刪除目標元素的所有引用*/public void removeAllOccurrences(T targetElement)throws ElementNotFoundException {removeElement(targetElement);try{while (contains((T)targetElement))removeElement(targetElement);}catch (Exception ElementNotFoundException){}}/***刪除最小元素*/public T removeMin() throws EmptyCollectionException {T result = null;if (isEmpty())throw new EmptyCollectionException("LinkedBinarySearchTree");else {if (root.left == null) {result = root.element;root = root.right;}else {BinaryTreeNode<T> parent = root;BinaryTreeNode<T> current = root.left;while (current.left != null) {parent = current;current = current.left;}result = current.element;parent.left = current.right;}modCount--;}return result;}/*** 刪除最大元素*/public T removeMax() throws EmptyCollectionException {// TODO}/*** 查詢二叉查詢樹中的最小值*/public T findMin() throws EmptyCollectionException {// TODO}/*** 查詢二叉查詢樹中的最大值*/public T findMax() throws EmptyCollectionException {// TODO}/*** 查詢目標元素*/public T find(T targetElement) throws ElementNotFoundException {// TODO}/*** 查詢左節點*/public LinkedBinarySearchTree<T> getLeft(){// TODO}/*** 查詢右節點*/public LinkedBinarySearchTree<T> getRight(){// TODO}/*** 查找目標節點*/private BinaryTreeNode<T> findNode(T targetElement, BinaryTreeNode<T> next) {// TODO} }***注意:***其中還有一些沒有完善,日后再慢慢學習完善。
總結
以上是生活随笔為你收集整理的数据结构与算法-二叉查找树(java描述)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构与算法-二叉树(java描述)
- 下一篇: 蓝桥杯-最短路(floyd算法)