aboutsummaryrefslogtreecommitdiffstats
path: root/TreeExample.java
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2021-03-19 01:00:38 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2021-03-19 01:00:38 +0000
commitd3f7f4efbbb7e5e0ead43bd0167855bcd36e0df1 (patch)
treef50c130ec1a1860e496aaac50a20cb088ac3cc75 /TreeExample.java
parent92c97d1cbf9c7415eec5975119e7588543a280e1 (diff)
downloadJavaDataStructures-Algorithms-d3f7f4efbbb7e5e0ead43bd0167855bcd36e0df1.tar.gz
JavaDataStructures-Algorithms-d3f7f4efbbb7e5e0ead43bd0167855bcd36e0df1.zip
added Tree data structure
Diffstat (limited to 'TreeExample.java')
-rw-r--r--TreeExample.java63
1 files changed, 63 insertions, 0 deletions
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<T extends Comparable<T>> extends Tree<T> {
+
+ // for real implementations this would be much more complicated
+ public boolean add(T o) {
+ root.add(new TreeNode<T>(o));
+ return true;
+ }
+
+ public boolean remove(T o) {
+ throw new UnsupportedOperationException();
+ }
+
+ public static void main(String[] args) {
+ Tree<String> t = new TreeExample<>();
+ t.root=new Tree.TreeNode<String>("ARSENAL");
+
+ t.root.add(new Tree.TreeNode<String>("Forty"));
+ t.root.add(new Tree.TreeNode<String>("Nine"));
+ t.root.add(new Tree.TreeNode<String>("Undefeated"));
+
+ t.root.children.get(0).add(new Tree.TreeNode<String>("I"));
+ t.root.children.get(0).add(new Tree.TreeNode<String>("Bet"));
+ t.root.children.get(1).add(new Tree.TreeNode<String>("You"));
+ t.root.children.get(2).add(new Tree.TreeNode<String>("Are"));
+ t.root.children.get(2).add(new Tree.TreeNode<String>("Sick"));
+ t.root.children.get(2).add(new Tree.TreeNode<String>("Of"));
+ t.root.children.get(2).add(new Tree.TreeNode<String>("Arsenal"));
+ t.root.children.get(2).children.get(1).add(new Tree.TreeNode<String>("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);
+ }
+ }
+}