Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package real;
- import java.util.ArrayList;
- import java.util.Collection;
- import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;
- public class TreeSearch {
- private ArrayList<TreeNode> array[];
- private static int rear;
- public TreeSearch() {
- rear = 4;
- array = (ArrayList<TreeNode>[]) new ArrayList[4];
- for (int i = 0; i < 4; i++) {
- array[i] = new ArrayList<TreeNode>();
- }
- }
- public void insert(TreeNode node) {
- if (node.getItem() % 2 == 0) {
- array[1].add(node);
- }
- if (node.getItem() % 3 == 0) {
- array[2].add(node);
- }
- if (node.getItem() % 5 == 0) {
- array[3].add(node);
- }
- }
- public boolean isEmpty() {
- if (array[0] != null) {
- }
- return false;
- }
- public int size() {
- int size = 0;
- for (int i = 0; i < rear; i++)
- size += array[i].size();
- return size;
- }
- public String find(TreeNode node) {
- String result = "NOT_FOUND";
- if (node.getItem() % 2 == 0) {
- for (int y = 0; y < array[1].size(); y++) {
- if (node == array[1].get(y)) {
- result = 1 + " " + y;
- node.increase();
- quickSort(array[1], 0, array.length - 1);
- }
- }
- }
- if (node.getItem() % 3 == 0) {
- for (int y = 0; y < array[2].size(); y++) {
- if (node == array[2].get(y)) {
- result = 2 + " " + y;
- node.increase();
- }
- }
- }
- if (node.getItem() % 5 == 0) {
- for (int y = 0; y < array[3].size(); y++) {
- if (node == array[3].get(y)) {
- result = 3 + " " + y;
- node.increase();
- }
- }
- }
- return result;
- }
- public String toString() {
- String result = "";
- for (int i = 0; i < rear; i++)
- for (int y = 0; y < array[i].size(); y++)
- result += array[i].get(y) + " ArrayNumber: " + i
- + " Element in the array: " + y + "\n ";
- return result;
- }
- public static void quickSort(ArrayList<TreeNode> list, int first, int last) {
- int g = first, h = last;
- int midIndex = (first + last) / 2;
- TreeNode node = list.get(midIndex);
- do {
- while (list.get(g).equals(node))
- g++;
- while (list.get(h).equals(node))
- h--;
- if (g <= h) {
- // swap(list[g], list[h]);
- swap(list, g, h);
- g++;
- h--;
- }
- }
- while (g < h);
- if (h > first)
- quickSort(list, first, h);
- if (g < last)
- quickSort(list, g, last);
- }
- public static void swap(ArrayList<TreeNode> list, int g, int h) {
- TreeNode temp = list.get(g);
- list.set(g, list.get(h));
- list.set(h, temp);
- }
- public static void print(int[] array) {
- for (int i = 0; i < array.length; i++) {
- System.out.print(array[i]);
- if (i < array.length - 1)
- {
- System.out.print(",");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement