Sajmon

BSTf

Mar 18th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. package com.sajmon.bst.tree;
  2.  
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class BinarySearchTree implements Serializable {
  8.    
  9.     private static final long serialVersionUID = 1L;
  10.    
  11.     private Node root;
  12.     private List<Node> nodes;
  13.    
  14.     public BinarySearchTree() {
  15.         root = new Node(new GeoPoint(0, 0, null, null, null, null, null), null, null);
  16.         nodes = new ArrayList<Node>();
  17.     }
  18.    
  19.     public void insert(GeoPoint p) {
  20.         insert(new Node(p, null, null));
  21.     }
  22.    
  23.     private void insert(Node n) {
  24.         Node y = null;
  25.         Node temp = root;
  26.        
  27.         while (temp != null) {
  28.             y = temp;
  29.             if (temp.getGeoPoint().compareTo(n.getGeoPoint()) < 0) {
  30.                 temp = temp.left;
  31.             }
  32.             else if (temp.getGeoPoint().compareTo(n.getGeoPoint()) > 0) {
  33.                 temp = temp.right;
  34.             }
  35.             else {
  36.                 break; // Node already exist
  37.             }
  38.         }
  39.        
  40.         if (y == null) {
  41.             root = n;
  42.         }
  43.         else if (y.getGeoPoint().compareTo(n.getGeoPoint()) < 0) {
  44.             y.left = n;
  45.         }
  46.         else if (y.getGeoPoint().compareTo(n.getGeoPoint()) > 0) {
  47.             y.right = n;
  48.         }
  49.     }
  50.    
  51.     public Node search(GeoPoint p) {
  52.         return searchN(p, root);
  53.     }
  54.    
  55.    
  56.     private Node search(GeoPoint p, Node n) {
  57.         while (n != null) {
  58.             if (n.getGeoPoint().compareTo(p) < 0) {
  59.                 System.out.println("Is Lower");
  60.                 n = n.left;
  61.             }
  62.             else if (n.getGeoPoint().compareTo(p) > 0) {
  63.                 System.out.println("Is Upper");
  64.                 n = n.right;
  65.             }
  66.             else {
  67.                 System.out.println("Is Equal");
  68.                 return n;
  69.             }
  70.         }
  71.         return null;
  72.     }
  73.    
  74.     private Node searchN(GeoPoint p, Node current) {
  75.         if (current == null) {
  76.             return null;
  77.         }
  78.         else if (current.getGeoPoint().compareTo(p) == 0) {
  79.             System.out.println("equal");
  80.             return current;
  81.         }
  82.         else {
  83.             System.out.println("ranging");
  84.             checkRange(p, current);
  85.             searchN(p, current.left);
  86.             searchN(p, current.right);
  87.         }
  88.         return null;
  89.     }
  90.    
  91.     private void checkRange(GeoPoint p, Node current) {
  92.         if (current.getGeoPoint().compareTo(p) == -2) {
  93.             nodes.add(current);
  94.         }
  95.         else if (current.getGeoPoint().compareTo(p) == 2) {
  96.             nodes.add(current);
  97.         }
  98.     }
  99.    
  100.     /*
  101.     private List<Node> searchRN(GeoPoint p, Node n) {
  102.         List<Node> nearest = new ArrayList<Node>();
  103.         int temp = 0;
  104.         while (n != null && nearest.size() <= 10) {
  105.             System.out.println("Presiel som whilom");
  106.             if ((temp = n.getGeoPoint().compareTo(p)) < 0) {
  107.                 if (temp == -2) {
  108.                     nearest.add(n);
  109.                 }
  110.                 n = n.left;
  111.             }
  112.             else if ((temp = n.getGeoPoint().compareTo(p)) > 0) {
  113.                 if (temp == 2) {
  114.                     nearest.add(n);
  115.                 }
  116.                 n = n.right;
  117.             }
  118.         }
  119.         return nearest;
  120.     }
  121.     */
  122.  
  123.    
  124.     private Node getNodeValue(Node n) {
  125.         return n;
  126.     }
  127.  
  128.     public List<Node> getNodes() {
  129.         return nodes;
  130.     }  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment