tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
22
Task/Same-Fringe/D/same-fringe-1.d
Normal file
22
Task/Same-Fringe/D/same-fringe-1.d
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
struct Node(T) {
|
||||
T data;
|
||||
Node* L, R;
|
||||
}
|
||||
|
||||
bool sameFringe(T)(Node!T* t1, Node!T* t2) {
|
||||
T[] scan(Node!T* t) {
|
||||
if (!t) return [];
|
||||
return (!t.L && !t.R) ? [t.data] : scan(t.L) ~ scan(t.R);
|
||||
}
|
||||
return scan(t1) == scan(t2);
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
alias N = Node!int;
|
||||
auto t1 = new N(10, new N(20, new N(30, new N(40), new N(50))));
|
||||
auto t2 = new N(1, new N(2, new N(3, new N(40), new N(50))));
|
||||
writeln(sameFringe(t1, t2));
|
||||
auto t3 = new N(1, new N(2, new N(3, new N(40), new N(51))));
|
||||
writeln(sameFringe(t1, t3));
|
||||
}
|
||||
194
Task/Same-Fringe/D/same-fringe-2.d
Normal file
194
Task/Same-Fringe/D/same-fringe-2.d
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
import std.array: empty;
|
||||
import std.algorithm: equal;
|
||||
|
||||
|
||||
// Replace with an efficient stack when available in Phobos.
|
||||
struct Stack(T) {
|
||||
private T[] data;
|
||||
|
||||
public @property bool empty() const pure nothrow {
|
||||
return data.empty;
|
||||
}
|
||||
|
||||
// Can't be const if T isn't a value or const.
|
||||
public @property T head() const pure nothrow
|
||||
in {
|
||||
assert(!data.empty);
|
||||
} body {
|
||||
return data[$ - 1];
|
||||
}
|
||||
|
||||
public void push(T x) pure nothrow {
|
||||
data ~= x;
|
||||
}
|
||||
|
||||
public void pop() pure nothrow
|
||||
in {
|
||||
assert(!data.empty);
|
||||
} body {
|
||||
data.length--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct BinaryTreeNode(T) {
|
||||
T data;
|
||||
BinaryTreeNode* left, right;
|
||||
}
|
||||
|
||||
|
||||
struct Fringe(T) {
|
||||
alias const(BinaryTreeNode!T)* BT;
|
||||
private Stack!BT stack;
|
||||
|
||||
pure nothrow invariant() {
|
||||
assert(stack.empty || isLeaf(stack.head));
|
||||
}
|
||||
|
||||
public this(BT t) pure nothrow {
|
||||
if (t != null) {
|
||||
stack.push(t);
|
||||
if (!isLeaf(t)) {
|
||||
// Here invariant() doesn't hold.
|
||||
// invariant() isn't called for private methods.
|
||||
nextLeaf();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public @property bool empty() const pure nothrow {
|
||||
return stack.empty;
|
||||
}
|
||||
|
||||
public @property T front() const pure nothrow
|
||||
in {
|
||||
assert(!stack.empty && stack.head != null);
|
||||
} body {
|
||||
return stack.head.data;
|
||||
}
|
||||
|
||||
public void popFront() pure nothrow
|
||||
in {
|
||||
assert(!stack.empty);
|
||||
} body {
|
||||
stack.pop();
|
||||
if (!empty())
|
||||
nextLeaf();
|
||||
}
|
||||
|
||||
private static bool isLeaf(in BT t) pure nothrow {
|
||||
return t != null && t.left == null && t.right == null;
|
||||
}
|
||||
|
||||
private void nextLeaf() pure nothrow
|
||||
in {
|
||||
assert(!stack.empty);
|
||||
} body {
|
||||
auto t = stack.head;
|
||||
|
||||
while (!stack.empty && !isLeaf(t)) {
|
||||
stack.pop();
|
||||
if (t.right != null)
|
||||
stack.push(t.right);
|
||||
if (t.left != null)
|
||||
stack.push(t.left);
|
||||
t = stack.head;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool sameFringe(T)(in BinaryTreeNode!T* t1, in BinaryTreeNode!T* t2)
|
||||
pure nothrow {
|
||||
return Fringe!T(t1).equal(Fringe!T(t2));
|
||||
}
|
||||
|
||||
|
||||
unittest {
|
||||
alias BinaryTreeNode!int N;
|
||||
|
||||
static N* n(in int x, N* l=null, N* r=null) pure nothrow {
|
||||
return new N(x, l, r);
|
||||
}
|
||||
|
||||
{
|
||||
N* t;
|
||||
assert(sameFringe(t, t));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10);
|
||||
const t2 = n(10);
|
||||
assert(sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10);
|
||||
const t2 = n(20);
|
||||
assert(!sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20));
|
||||
const t2 = n(30, n(20));
|
||||
assert(sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20));
|
||||
const t2 = n(10, n(30));
|
||||
assert(!sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20), n(30));
|
||||
const t2 = n(5, n(20), n(30));
|
||||
assert(sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20), n(30));
|
||||
const t2 = n(5, n(20), n(35));
|
||||
assert(!sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20, n(30)));
|
||||
const t2 = n(1, n(2, n(30)));
|
||||
assert(sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20, n(30, n(40), n(50))));
|
||||
const t2 = n(1, n(2, n(3, n(40), n(50))));
|
||||
assert(sameFringe(t1, t2));
|
||||
}
|
||||
|
||||
{
|
||||
const t1 = n(10, n(20, n(30, n(40), n(50))));
|
||||
const t2 = n(1, n(2, n(3, n(40), n(51))));
|
||||
assert(!sameFringe(t1, t2));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
alias N = BinaryTreeNode!int;
|
||||
|
||||
static N* n(in int x, N* l=null, N* r=null) pure nothrow {
|
||||
return new N(x, l, r);
|
||||
}
|
||||
|
||||
const t1 = n(10, n(20, n(30, n(40), n(50))));
|
||||
writeln("fringe(t1): ", Fringe!int(t1));
|
||||
|
||||
const t2 = n(1, n(2, n(3, n(40), n(50))));
|
||||
writeln("fringe(t2): ", Fringe!int(t2));
|
||||
|
||||
const t3 = n(1, n(2, n(3, n(40), n(51))));
|
||||
writeln("fringe(t3): ", Fringe!int(t3));
|
||||
|
||||
writeln("sameFringe(t1, t2): ", sameFringe(t1, t2));
|
||||
writeln("sameFringe(t1, t3): ", sameFringe(t1, t3));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue