Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sajmon.bst.tree;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- public class BinarySearchTree implements Serializable {
- private static final long serialVersionUID = 1L;
- private Node root;
- private List<Node> nodes;
- public BinarySearchTree() {
- root = new Node(new GeoPoint(0, 0, null, null, null, null, null), null, null);
- nodes = new ArrayList<Node>();
- }
- public void insert(GeoPoint p) {
- insert(new Node(p, null, null));
- }
- private void insert(Node n) {
- Node y = null;
- Node temp = root;
- while (temp != null) {
- y = temp;
- if (temp.getGeoPoint().compareTo(n.getGeoPoint()) < 0) {
- temp = temp.left;
- }
- else if (temp.getGeoPoint().compareTo(n.getGeoPoint()) > 0) {
- temp = temp.right;
- }
- else {
- break; // Node already exist
- }
- }
- if (y == null) {
- root = n;
- }
- else if (y.getGeoPoint().compareTo(n.getGeoPoint()) < 0) {
- y.left = n;
- }
- else if (y.getGeoPoint().compareTo(n.getGeoPoint()) > 0) {
- y.right = n;
- }
- }
- public Node search(GeoPoint p) {
- return searchN(p, root);
- }
- private Node search(GeoPoint p, Node n) {
- while (n != null) {
- if (n.getGeoPoint().compareTo(p) < 0) {
- System.out.println("Is Lower");
- n = n.left;
- }
- else if (n.getGeoPoint().compareTo(p) > 0) {
- System.out.println("Is Upper");
- n = n.right;
- }
- else {
- System.out.println("Is Equal");
- return n;
- }
- }
- return null;
- }
- private Node searchN(GeoPoint p, Node current) {
- if (current == null) {
- return null;
- }
- else if (current.getGeoPoint().compareTo(p) == 0) {
- System.out.println("equal");
- return current;
- }
- else {
- System.out.println("ranging");
- checkRange(p, current);
- searchN(p, current.left);
- searchN(p, current.right);
- }
- return null;
- }
- private void checkRange(GeoPoint p, Node current) {
- if (current.getGeoPoint().compareTo(p) == -2) {
- nodes.add(current);
- }
- else if (current.getGeoPoint().compareTo(p) == 2) {
- nodes.add(current);
- }
- }
- /*
- private List<Node> searchRN(GeoPoint p, Node n) {
- List<Node> nearest = new ArrayList<Node>();
- int temp = 0;
- while (n != null && nearest.size() <= 10) {
- System.out.println("Presiel som whilom");
- if ((temp = n.getGeoPoint().compareTo(p)) < 0) {
- if (temp == -2) {
- nearest.add(n);
- }
- n = n.left;
- }
- else if ((temp = n.getGeoPoint().compareTo(p)) > 0) {
- if (temp == 2) {
- nearest.add(n);
- }
- n = n.right;
- }
- }
- return nearest;
- }
- */
- private Node getNodeValue(Node n) {
- return n;
- }
- public List<Node> getNodes() {
- return nodes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment