现在的位置: 首页 > 综合 > 正文

Binary Search Tree

2013年12月19日 ⁄ 综合 ⁄ 共 4963字 ⁄ 字号 评论关闭
  1. /**
  2.  * filename: BinarySearchTree.java
  3.  * package: 
  4.  * author: Nick Ma
  5.  * email: nick.ma85@yahoo.com
  6.  * date: Nov 12, 2008
  7.  * description: this class implements an unbalanced binary search tree.
  8.  */
  9. public
     
    class
     BinarySearchTree<T 
    extends
     Comparable<T>> 
    extends
     BinaryTree<T>{
  10.     
  11.     
    // Data Field
  12.     
  13.     
    /** the deleted node */
  14.     
    protected
     BinaryNode<T> deletedNode = 
    null
    ;
  15.     
  16.     
    /**
  17.      * description: search the node with the given data
  18.      * @param target - the data stored in the node
  19.      * @return - the found node
  20.      */
  21.     
    public
     BinaryNode<T> search(T target) {
  22.         
    return
     find(
    this
    .root, target);
  23.     }
  24.     
    /**
  25.      * description: recursive find method
  26.      * @param node - the root of the subtree
  27.      * @param target - the target being sought
  28.      * @return - the node, if found, otherwise, return null
  29.      */
  30.     
    public
     BinaryNode<T> find(BinaryNode<T> node, T target) {
  31.         
    if
    (node == 
    null
    ) {
  32.             
    return
     
    null
    ;
  33.         } 
    else
     {
  34.             
    int
     result = target.compareTo(node.getData());
  35.             
  36.             
    if
    (result < 
    0
    ) {
  37.                 
    // target is less than the data in the local root
  38.                 
    return
     find(node.getLeft(), target);
  39.             } 
    else
     
    if
    (result > 
    0
    ) {
  40.                 
    // target is greater than the data in the local root
  41.                 
    return
     find(node.getRight(), target);
  42.             } 
    else
     {
  43.                 
    // target is equal to the data in the local root
  44.                 
    return
     node;
  45.             }
  46.         }
  47.     }
  48.     
  49.     
    /**
  50.      * description: add new node in the tree
  51.      * @param newNode - the new node
  52.      */
  53.     
    public
     
    void
     add(BinaryNode<T> newNode) {
  54.         
    this
    .root = addAux(
    this
    .root, newNode);
  55.     }
  56.     
  57.     
    /**
  58.      * description: recursive add-auxiliary method
  59.      * @param node - the local root
  60.      * @param newNode - the new node
  61.      * @return - the new local root which contains the new node
  62.      */
  63.     
    private
     BinaryNode<T> addAux(BinaryNode<T> node, BinaryNode<T> newNode) {
  64.         
    if
    (node == 
    null
    ) {
  65.             
    // the tree is empty
  66.             
    return
     newNode;
  67.         } 
    else
     {
  68.             
    int
     result = newNode.getData().compareTo(node.getData());
  69.             
  70.             
    if
    (result < 
    0
    ) {
  71.                 
    // insert new node in the left subtree
  72.                 node.setLeft(addAux(node.getLeft(), newNode));
  73.             } 
    else
     
    if
     (result > 
    0
    ){
  74.                 
    // insert new node in the right subtree
  75.                 node.setRight(addAux(node.getRight(), newNode));
  76.             }
  77.             
    return
     node;
  78.         }
  79.     }
  80.     
  81.     
    /**
  82.      * description: delete the node in the tree
  83.      * @param data - the data of the deleted node
  84.      * @return - the deleted node
  85.      */
  86.     
    public
     BinaryNode<T> delete(T data) {
  87.         
    this
    .root = 
    this
    .deleteAux(
    this
    .root, data);
  88.         
    return
     
    this
    .deletedNode;
  89.     }
  90.     
  91.     
    /**
  92.      * description: recursive delete-auxiliary method
  93.      * @param node - the local root
  94.      * @param target - the data of the deleted node
  95.      * @return - the new local root which deletes the target node
  96.      */
  97.     
    private
     BinaryNode<T> deleteAux(BinaryNode<T> node, T target) {
  98.         
    if
    (node == 
    null
    ) {
  99.             
    // the tree is empty
  100.             
    return
     
    null
    ;
  101.         } 
    else
     {
  102.             
    int
     result = target.compareTo(node.getData());
  103.             
  104.             
    if
    (result < 
    0
    ) {
  105.                 
    // target is less than the data in the local root
  106.                 node.setLeft(deleteAux(node.getLeft(), target));
  107.                 
    return
     node;
  108.             } 
    else
     
    if
     (result > 
    0
    ){
  109.                 
    // target is greater than the data in the local root
  110.                 node.setRight(deleteAux(node.getRight(), target));
  111.                 
    return
     node;
  112.             } 
    else
     {
  113.                 
    // target is equal to the data in the local root
  114.                 
  115.                 deletedNode = node;
  116.         
  117.                 
    if
    (node.getLeft() == 
    null
    ) {
  118.                     
    // there's no left child in the local root
  119.                     
    return
     node.getRight();
  120.                 } 
    else
     
    if
    (node.getRight() == 
    null
    ) {
  121.                     
    // there's no right child in the local root
  122.                     
    return
     node.getLeft();
  123.                 } 
    else
     {
  124.                     
    // the local root has two children
  125.                     
    if
    (node.getLeft().getRight() == 
    null
    ) {
  126.                         node.setData(node.getLeft().getData());
  127.                         node.setLeft(node.getLeft().getLeft());
  128.                         
    return
     node;
  129.                     } 
    else
     {
  130.                         node.setData(
    this
    .findLargestChild(node.getLeft()));
  131.                         
    return
     node;
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     
  138.     
    /**
  139.      * description: find the largest child of the tree
  140.      * @param node - the local root
  141.      * @return - the data in the largest node
  142.      */
  143.     
    protected
     T findLargestChild(BinaryNode<T> node) {
  144.         
    if
    (node == 
    null
    ) {
  145.             
    return
     
    null
    ;
  146.         } 
    else
     
    if
    (node.getRight() == 
    null
    ) {
  147.             
    return
     node.getData();
  148.         } 
    else
     {
  149.             
    // in-order
  150.             
    return
     findLargestChild(node.getRight());
  151.         }
  152.     }
  153. }

抱歉!评论已关闭.