- /**
- * filename: BinarySearchTree.java
- * package:
- * author: Nick Ma
- * email: nick.ma85@yahoo.com
- * date: Nov 12, 2008
- * description: this class implements an unbalanced binary search tree.
- */
- public class BinarySearchTree<T extends Comparable<T>> extends BinaryTree<T>{
- // Data Field
- /** the deleted node */
- protected BinaryNode<T> deletedNode = null ;
- /**
- * description: search the node with the given data
- * @param target - the data stored in the node
- * @return - the found node
- */
- public BinaryNode<T> search(T target) {
- return find( this .root, target);
- }
- /**
- * description: recursive find method
- * @param node - the root of the subtree
- * @param target - the target being sought
- * @return - the node, if found, otherwise, return null
- */
- public BinaryNode<T> find(BinaryNode<T> node, T target) {
- if (node == null ) {
- return null ;
- } else {
- int result = target.compareTo(node.getData());
- if (result < 0 ) {
- // target is less than the data in the local root
- return find(node.getLeft(), target);
- } else if (result > 0 ) {
- // target is greater than the data in the local root
- return find(node.getRight(), target);
- } else {
- // target is equal to the data in the local root
- return node;
- }
- }
- }
- /**
- * description: add new node in the tree
- * @param newNode - the new node
- */
- public void add(BinaryNode<T> newNode) {
- this .root = addAux( this .root, newNode);
- }
- /**
- * description: recursive add-auxiliary method
- * @param node - the local root
- * @param newNode - the new node
- * @return - the new local root which contains the new node
- */
- private BinaryNode<T> addAux(BinaryNode<T> node, BinaryNode<T> newNode) {
- if (node == null ) {
- // the tree is empty
- return newNode;
- } else {
- int result = newNode.getData().compareTo(node.getData());
- if (result < 0 ) {
- // insert new node in the left subtree
- node.setLeft(addAux(node.getLeft(), newNode));
- } else if (result > 0 ){
- // insert new node in the right subtree
- node.setRight(addAux(node.getRight(), newNode));
- }
- return node;
- }
- }
- /**
- * description: delete the node in the tree
- * @param data - the data of the deleted node
- * @return - the deleted node
- */
- public BinaryNode<T> delete(T data) {
- this .root = this .deleteAux( this .root, data);
- return this .deletedNode;
- }
- /**
- * description: recursive delete-auxiliary method
- * @param node - the local root
- * @param target - the data of the deleted node
- * @return - the new local root which deletes the target node
- */
- private BinaryNode<T> deleteAux(BinaryNode<T> node, T target) {
- if (node == null ) {
- // the tree is empty
- return null ;
- } else {
- int result = target.compareTo(node.getData());
- if (result < 0 ) {
- // target is less than the data in the local root
- node.setLeft(deleteAux(node.getLeft(), target));
- return node;
- } else if (result > 0 ){
- // target is greater than the data in the local root
- node.setRight(deleteAux(node.getRight(), target));
- return node;
- } else {
- // target is equal to the data in the local root
- deletedNode = node;
- if (node.getLeft() == null ) {
- // there's no left child in the local root
- return node.getRight();
- } else if (node.getRight() == null ) {
- // there's no right child in the local root
- return node.getLeft();
- } else {
- // the local root has two children
- if (node.getLeft().getRight() == null ) {
- node.setData(node.getLeft().getData());
- node.setLeft(node.getLeft().getLeft());
- return node;
- } else {
- node.setData( this .findLargestChild(node.getLeft()));
- return node;
- }
- }
- }
- }
- }
- /**
- * description: find the largest child of the tree
- * @param node - the local root
- * @return - the data in the largest node
- */
- protected T findLargestChild(BinaryNode<T> node) {
- if (node == null ) {
- return null ;
- } else if (node.getRight() == null ) {
- return node.getData();
- } else {
- // in-order
- return findLargestChild(node.getRight());
- }
- }
- }
Binary Search Tree
最新推荐文章于 2024-05-29 15:37:18 发布