Thh
This commit is contained in:
28
traversal/.project
Normal file
28
traversal/.project
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>traversal</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
<filteredResources>
|
||||
<filter>
|
||||
<id>1736900497</id>
|
||||
<name></name>
|
||||
<type>30</type>
|
||||
<matcher>
|
||||
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||
</matcher>
|
||||
</filter>
|
||||
</filteredResources>
|
||||
</projectDescription>
|
2
traversal/.settings/org.eclipse.core.resources.prefs
Normal file
2
traversal/.settings/org.eclipse.core.resources.prefs
Normal file
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
11
traversal/.settings/org.eclipse.jdt.core.prefs
Normal file
11
traversal/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
123
traversal/src/traversal/Main.java
Normal file
123
traversal/src/traversal/Main.java
Normal file
@@ -0,0 +1,123 @@
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Queue;
|
||||
|
||||
// a non-empty binary tree
|
||||
abstract class BT<X> {
|
||||
X value;
|
||||
|
||||
BT(X value) { this.value = value; }
|
||||
|
||||
// produce an iterator for the left child, or an empty iterator if one does
|
||||
// not exist
|
||||
abstract Iterator<X> leftIterator(DFTIterator<X> f);
|
||||
|
||||
// produce an iterator for the right child, or an empty iterator if one does
|
||||
// not exist
|
||||
abstract Iterator<X> rightIterator(DFTIterator<X> f);
|
||||
|
||||
// add this node's children, if they exist, to the queue (left then right)
|
||||
abstract void addChildrenToQueue(Queue<BT<X>> queue);
|
||||
}
|
||||
|
||||
class Leaf<X> extends BT<X> {
|
||||
Iterator<X> leftIterator(DFTIterator<X> f) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
Iterator<X> rigtIterator(DFTIterator<X> f) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
void addChildrenToQueue(Queue<BT<X>> queue) { queue.add(this); }
|
||||
|
||||
Leaf(X value) { super(value); }
|
||||
}
|
||||
|
||||
class Node<X> extends BT<X> {
|
||||
BT<X> left;
|
||||
BT<X> right;
|
||||
|
||||
Node(X value, BT<X> left, BT<X> right) {
|
||||
super(value);
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
Iterator<X> leftIterator(DFTIterator<X> f) {
|
||||
return this.left.leftIterator(f);
|
||||
}
|
||||
|
||||
Iterator<X> rightIterator(DFTIterator<X> f) {
|
||||
return this.right.rightIterator(f);
|
||||
}
|
||||
|
||||
void addChildrenToQueue(Queue<BT<X>> queue) {
|
||||
this.left.addChildrenToQueue(queue);
|
||||
this.right.addChildrenToQueue(queue);
|
||||
}
|
||||
}
|
||||
|
||||
// a depth first iterator for a BT
|
||||
interface DFTIterator<X> extends Iterator<X> {
|
||||
// produce a depth first traversal iterator for the given BT
|
||||
DFTIterator<X> iterate(BT<X> x);
|
||||
}
|
||||
|
||||
abstract class DFT<X> implements DFTIterator<X> {
|
||||
BT<X> bt;
|
||||
Iterator<X> left;
|
||||
Iterator<X> right;
|
||||
// has the top-level value of bt been iterated over yet
|
||||
boolean nodeGiven;
|
||||
|
||||
DFT(BT<X> bt) {
|
||||
this.bt = bt;
|
||||
this.left = bt.leftIterator(this);
|
||||
this.right = bt.rightIterator(this);
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return left.hasNext() || !nodeGiven || right.hasNext();
|
||||
}
|
||||
|
||||
// check that there is a next element, wraps the getNext method
|
||||
public X next() {
|
||||
if (!this.hasNext()) { throw new NoSuchElementException(); }
|
||||
return this.getNext();
|
||||
}
|
||||
|
||||
// actually get the next
|
||||
abstract X getNext();
|
||||
}
|
||||
|
||||
class InOrder<X> extends DFT<X> {
|
||||
|
||||
InOrder(BT<X> bt) { super(bt); }
|
||||
|
||||
public DFTIterator<X> iterate(BT<X> x) { return new InOrder<X>(x); }
|
||||
}
|
||||
|
||||
class PreOrder<X> extends DFT<X> {
|
||||
|
||||
PreOrder(BT<X> bt) { super(bt); }
|
||||
|
||||
public DFTIterator<X> iterate(BT<X> x) { return new PreOrder<X>(x); }
|
||||
}
|
||||
|
||||
class PostOrder<X> extends DFT<X> {
|
||||
|
||||
PostOrder(BT<X> bt) { super(bt); }
|
||||
|
||||
public DFTIterator<X> iterate(BT<X> x) { return new PostOrder<X>(x); }
|
||||
}
|
||||
|
||||
// a breadth first iterator for a BT
|
||||
class BFT<X> implements Iterator<X> {
|
||||
|
||||
Queue<BT<X>> queue = new LinkedList<BT<X>>();
|
||||
|
||||
BFT(BT<X> bt) { queue.add(bt); }
|
||||
}
|
Reference in New Issue
Block a user