/** * Leaf node of PRQuadTree. Contains a * LinkedList of points to access the data from. * @author nickgrifasi * @version 10/15/21 * */ public class LeafNode implements BaseNode { /** * linkedList containing points in leaf */ private LinkedList points; /** * Default constructor of LeafNode */ public LeafNode() { points = new LinkedList(); } /** * Loaded constructor that takes argument for * Point object which will be stored in LeafNode. * @param point to be stored in list */ public LeafNode(Point point) { points = new LinkedList(point); } /** * MergeTree method will alter the PRQuadTree based on decomposition * rules: * * leaf node contains >3 points, not all leafs have same x,y value * @param x x coordinate * @param y y coordinate * @param width width * @return root node */ @Override public BaseNode mergeTree(int x, int y, int width) { if (points.getSize() > 3 && !points.duplicate()) { InternalNode node = new InternalNode(); while (points.getHeadNode() != null) { node.insert(x, y, width, points.removeFirst()); } return node; } else if (points.getSize() == 0) { return PRQuadtree.FLY_WEIGHT; } else { points.duplicateFix(); return this; } } /** * Inserts point in node * @param x x coordinate * @param y y coordinate * @param width width * @param point point to be inserted * @return root of subtree after merging */ @Override public BaseNode insert(int x, int y, int width, Point point) { points.insert(point); return mergeTree(x, y, width); } /** * Removes leaf and merges the tree if necessary */ @Override public Point remove(int x, int y, int width, Point point, boolean skip) { Point temp = points.remove(point, skip); mergeTree(x, y, width); return temp; } /** * @param x x coordinate * @param y y coordinate * @param width width * @param level of the tree * @return number of nodes * Dump method for FlyWeightNode * prints nodes preorder traversal order, * one node per line, each line indent 2 spaces by level */ @Override public int dump(int x, int y, int width, int level) { String temp = ""; int i = 0; int j = 0; //each level of the tree is 2 space indent while (i < level) { temp = temp + " "; i++; } temp = temp + "Node at " + x + ", " + y + ", " + width + ":"; System.out.println(temp); Node node = points.getHeadNode(); temp = ""; while (j < level) { temp = temp + " "; j++; } while (node != null) { String newString = temp + node.getPoint().toString(); System.out.println(newString); node = node.getNode(); } return 1; } /** * finds the duplicates */ @Override public void findDuplicates() { points.printDupe(); } /** * getter that returns number of * nonDupe * @return number of non duplicates */ @Override public int getNonDupe() { return points.getSize(); } /** * returns the list of point data * @return List of point */ @Override public LinkedList getPoint() { return points; } }