Skip to content

Commit

Permalink
测试
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Apr 26, 2022
1 parent 7ba30d1 commit a416b85
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 135 deletions.
86 changes: 43 additions & 43 deletions encode-n-ary-tree-to-binary-tree/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
import { Node } from "../n-ary-tree-level-order-traversal/Node.ts";

export function encode(root: Node | null): TreeNode | null {
if (!root) {
return root;
}
const nero = new TreeNode(root.val);
const children = root.children;
if (children.length === 0) {
return nero;
} else {
const left = encode(children[0]);
nero.left = left;
if (children.length === 1) {
return nero;
}
let cur: TreeNode | null = left;
for (let i = 1; i < children.length; i++) {
const right = encode(children[i]);
cur && (cur.right = right);
cur = right;
}
return nero;
}
}
export function decode(root: TreeNode | null): Node | null {
if (!root) {
return root;
}
const nero = new Node(root.val);
if (!root.left) {
return nero;
} else {
let cur: TreeNode | null = root.left;
while (cur) {
const child = decode(cur);
child && nero.children.push(child);
cur = cur.right;
}
return nero;
}
}
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
import { Node } from "../n-ary-tree-level-order-traversal/Node.ts";

export function encode(root: Node | null): TreeNode | null {
if (!root) {
return root;
}
const nero = new TreeNode(root.val);
const children = root.children;
if (children.length === 0) {
return nero;
} else {
const left = encode(children[0]);
nero.left = left;
if (children.length === 1) {
return nero;
}
let cur: TreeNode | null = left;
for (let i = 1; i < children.length; i++) {
const right = encode(children[i]);
cur && (cur.right = right);
cur = right;
}
return nero;
}
}
export function decode(root: TreeNode | null): Node | null {
if (!root) {
return root;
}
const nero = new Node(root.val);
if (!root.left) {
return nero;
} else {
let cur: TreeNode | null = root.left;
while (cur) {
const child = decode(cur);
child && nero.children.push(child);
cur = cur.right;
}
return nero;
}
}
184 changes: 92 additions & 92 deletions encode-n-ary-tree-to-binary-tree/test.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,92 @@
import { Node } from "../n-ary-tree-level-order-traversal/Node.ts";
import { assert, assertEquals } from "../deps.ts";
import { decode, encode } from "./index.ts";
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
Deno.test("encode-n-ary-tree-to-binary-tree", () => {
const examples: { node: Node; tree: TreeNode }[] = [
{
node: new Node(1, [
new Node(2),
new Node(3, [
new Node(6),
new Node(7, [new Node(11, [new Node(14)])]),
]),
new Node(4, [new Node(8, [new Node(12)])]),
new Node(5, [new Node(9, [new Node(13)]), new Node(10)]),
]),
tree: {
val: 1,
left: {
val: 2,
left: null,
right: {
val: 3,
left: {
val: 6,
left: null,
right: {
val: 7,
left: {
val: 11,
left: { val: 14, left: null, right: null },
right: null,
},
right: null,
},
},
right: {
val: 4,
left: {
val: 8,
left: { val: 12, left: null, right: null },
right: null,
},
right: {
val: 5,
left: {
val: 9,
left: { val: 13, left: null, right: null },
right: { val: 10, left: null, right: null },
},
right: null,
},
},
},
},
right: null,
},
},
{
node: new Node(1, [
new Node(3, [new Node(5), new Node(6)]),
new Node(2),
new Node(4),
]),
tree: {
val: 1,
left: {
val: 3,
left: {
val: 5,
left: null,
right: { val: 6, left: null, right: null },
},
right: {
val: 2,
left: null,
right: { val: 4, left: null, right: null },
},
},
right: null,
},
},
];
examples.forEach(({ node: node, tree: tree }) => {
const entree = encode(node);
assertEquals(JSON.stringify(tree), JSON.stringify(entree));
// console.log(JSON.stringify(tree));
const cloned = decode(entree);
assert(node !== cloned);
assertEquals(node, cloned);
});
});
import { Node } from "../n-ary-tree-level-order-traversal/Node.ts";
import { assert, assertEquals } from "../deps.ts";
import { decode, encode } from "./index.ts";
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
Deno.test("encode-n-ary-tree-to-binary-tree", () => {
const examples: { node: Node; tree: TreeNode }[] = [
{
node: new Node(1, [
new Node(2),
new Node(3, [
new Node(6),
new Node(7, [new Node(11, [new Node(14)])]),
]),
new Node(4, [new Node(8, [new Node(12)])]),
new Node(5, [new Node(9, [new Node(13)]), new Node(10)]),
]),
tree: {
val: 1,
left: {
val: 2,
left: null,
right: {
val: 3,
left: {
val: 6,
left: null,
right: {
val: 7,
left: {
val: 11,
left: { val: 14, left: null, right: null },
right: null,
},
right: null,
},
},
right: {
val: 4,
left: {
val: 8,
left: { val: 12, left: null, right: null },
right: null,
},
right: {
val: 5,
left: {
val: 9,
left: { val: 13, left: null, right: null },
right: { val: 10, left: null, right: null },
},
right: null,
},
},
},
},
right: null,
},
},
{
node: new Node(1, [
new Node(3, [new Node(5), new Node(6)]),
new Node(2),
new Node(4),
]),
tree: {
val: 1,
left: {
val: 3,
left: {
val: 5,
left: null,
right: { val: 6, left: null, right: null },
},
right: {
val: 2,
left: null,
right: { val: 4, left: null, right: null },
},
},
right: null,
},
},
];
examples.forEach(({ node: node, tree: tree }) => {
const entree = encode(node);
assertEquals(JSON.stringify(tree), JSON.stringify(entree));
// console.log(JSON.stringify(tree));
const cloned = decode(entree);
assert(node !== cloned);
assertEquals(node, cloned);
});
});

0 comments on commit a416b85

Please sign in to comment.