From d3f7f4efbbb7e5e0ead43bd0167855bcd36e0df1 Mon Sep 17 00:00:00 2001 From: jwansek Date: Fri, 19 Mar 2021 01:00:38 +0000 Subject: added Tree data structure --- Tree.java | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ TreeExample.java | 63 ++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 Tree.java create mode 100644 TreeExample.java diff --git a/Tree.java b/Tree.java new file mode 100644 index 0000000..c71051c --- /dev/null +++ b/Tree.java @@ -0,0 +1,144 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Queue; +import java.util.Stack; +import java.util.LinkedList; +import java.util.Iterator; + +public abstract class Tree> { + + TreeNode root; + + public void findAllLevels() { + throw new UnsupportedOperationException(); + } + + // how to add to a tree? it normally depends on the context... + abstract public boolean add(T element); + + abstract public boolean remove(T element); + + //How to search?? Use BFS or DFS + public boolean contains(T element) { + return iterativeBreadthFirstContains(element); + } + + public boolean iterativeBreadthFirstContains(T element) { + if (root == null) { + return false; + } + Queue> q = new LinkedList>(); + q.add(root); + while (q.isEmpty() == false) { + TreeNode temp = q.remove(); + if (temp.obj.compareTo(element) == 0) { + return true; + } + for (TreeNode child : temp.getAllChildren()) { + q.add(child); + } + } + return false; + } + + public boolean iterativeDepthFirstContains(T element) { + if (root == null) { + return false; + } + Stack> stack = new Stack<>(); + HashSet> seen = new HashSet<>(); + stack.push(root); + while (stack.isEmpty() == false) { + TreeNode temp = stack.peek(); + if (temp.obj.compareTo(element) == 0) { + return true; + } + seen.add(temp); + boolean found = false; + Iterator> iterator = temp.getAllChildren().iterator(); + TreeNode next = null; + while (!found && iterator.hasNext()) { + next = iterator.next(); + if (!seen.contains(next)) { + found = true; + } + } + if (!found) { + stack.pop(); + } else { + stack.push(next); + } + } + return false; + } + + public boolean recursiveDepthFirstContains(T element) { + return depthFirstRecurse(element, root); + } + + private boolean depthFirstRecurse(T e, TreeNode n) { + if (n == null) { + return false; + } + if (n.obj.compareTo(e) == 0) { + return true; + } + for (TreeNode child : n.getAllChildren()) { + if (depthFirstRecurse(e, child)) { + return true; + } + } + return false; + } + + public static class TreeNode { + T obj; + ArrayList> children; + + TreeNode(T o) { + obj = o; + children = new ArrayList<>(); + } + + boolean add(TreeNode tr) { + return children.add(tr); + } + + ArrayList> getAllChildren() { + return children; + } + + public int height() { + throw new UnsupportedOperationException(); + } + } + + public class BreadthFirstIterator implements Iterator, Iterable { + + Queue> q = new LinkedList>(); + + BreadthFirstIterator() { + q.add(root); + } + + @Override + public boolean hasNext() { + if (root == null) { + return false; + } + return !q.isEmpty(); + } + + public T next() { + TreeNode temp = q.remove(); + for (TreeNode child : temp.getAllChildren()) { + q.add(child); + } + return temp.obj; + } + + public Iterator iterator() { + return new BreadthFirstIterator(); + } + } +} \ No newline at end of file diff --git a/TreeExample.java b/TreeExample.java new file mode 100644 index 0000000..ab6eebe --- /dev/null +++ b/TreeExample.java @@ -0,0 +1,63 @@ + + +public class TreeExample> extends Tree { + + // for real implementations this would be much more complicated + public boolean add(T o) { + root.add(new TreeNode(o)); + return true; + } + + public boolean remove(T o) { + throw new UnsupportedOperationException(); + } + + public static void main(String[] args) { + Tree t = new TreeExample<>(); + t.root=new Tree.TreeNode("ARSENAL"); + + t.root.add(new Tree.TreeNode("Forty")); + t.root.add(new Tree.TreeNode("Nine")); + t.root.add(new Tree.TreeNode("Undefeated")); + + t.root.children.get(0).add(new Tree.TreeNode("I")); + t.root.children.get(0).add(new Tree.TreeNode("Bet")); + t.root.children.get(1).add(new Tree.TreeNode("You")); + t.root.children.get(2).add(new Tree.TreeNode("Are")); + t.root.children.get(2).add(new Tree.TreeNode("Sick")); + t.root.children.get(2).add(new Tree.TreeNode("Of")); + t.root.children.get(2).add(new Tree.TreeNode("Arsenal")); + t.root.children.get(2).children.get(1).add(new Tree.TreeNode("Examples")); + + String[] testy={"Arsenal","Totts"}; + System.out.println("iterative breadth first search:"); + for (String s : testy) { + if (t.iterativeBreadthFirstContains(s)) { + System.out.println("Tree contains " + s); + } else { + System.out.println("Tree doesn't contain " + s); + } + } + System.out.println("recursive depth first search:"); + for (String s : testy) { + if (t.recursiveDepthFirstContains(s)) { + System.out.println("Tree contains " + s); + } else { + System.out.println("Tree doesn't contain " + s); + } + } + System.out.println("iterative breadth first search:"); + for (String s : testy) { + if (t.iterativeDepthFirstContains(s)) { + System.out.println("Tree contains " + s); + } else { + System.out.println("Tree doesn't contain " + s); + } + } + + System.out.println("\nbreadth first iteration"); + for (String s : t.new BreadthFirstIterator()) { + System.out.println(s); + } + } +} -- cgit v1.2.3