adds ts for tree searches
This commit is contained in:
parent
f3b0be9500
commit
488ea7960b
|
@ -0,0 +1,38 @@
|
||||||
|
import { tree } from "./tree"
|
||||||
|
|
||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function bfs(head: BinaryNode<number> | null, needle: number): boolean {
|
||||||
|
const q: (BinaryNode<number> | null)[] = [head];
|
||||||
|
|
||||||
|
while (q.length) {
|
||||||
|
const current = q.shift() as BinaryNode<number>;
|
||||||
|
|
||||||
|
if (current.value === needle) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.left) {
|
||||||
|
q.push(current.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current.right) {
|
||||||
|
q.push(current.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test("bfs search works a number of values", function () {
|
||||||
|
expect(bfs(tree, 20)).toEqual(true);
|
||||||
|
expect(bfs(tree, -20)).toEqual(false);
|
||||||
|
expect(bfs(tree, 10)).toEqual(true);
|
||||||
|
expect(bfs(tree, 7)).toEqual(true);
|
||||||
|
})
|
|
@ -0,0 +1,47 @@
|
||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const tree: BinaryNode<number> = {
|
||||||
|
value: 20,
|
||||||
|
right: {
|
||||||
|
value: 50,
|
||||||
|
right: {
|
||||||
|
value: 100,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 30,
|
||||||
|
right: {
|
||||||
|
value: 45,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 29,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 10,
|
||||||
|
right: {
|
||||||
|
value: 15,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: {
|
||||||
|
value: 5,
|
||||||
|
right: {
|
||||||
|
value: 7,
|
||||||
|
right: null,
|
||||||
|
left: null,
|
||||||
|
},
|
||||||
|
left: null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -0,0 +1,44 @@
|
||||||
|
import { tree } from "./tree"
|
||||||
|
|
||||||
|
type BinaryNode<T> = {
|
||||||
|
value: T;
|
||||||
|
left: BinaryNode<T> | null;
|
||||||
|
right: BinaryNode<T> | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// pre order
|
||||||
|
function walk(current: BinaryNode<number> | null, path: number[]): number[] {
|
||||||
|
if (!current) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
path.push(current.value);
|
||||||
|
walk(current.left, path);
|
||||||
|
walk(current.right, path);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function pre_order_search(head: BinaryNode<number>): number[] {
|
||||||
|
const path: number[] = [];
|
||||||
|
walk(head, path);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
test("Pre order", function () {
|
||||||
|
expect(pre_order_search(tree)).toEqual([
|
||||||
|
20,
|
||||||
|
10,
|
||||||
|
5,
|
||||||
|
7,
|
||||||
|
15,
|
||||||
|
50,
|
||||||
|
30,
|
||||||
|
29,
|
||||||
|
45,
|
||||||
|
100,
|
||||||
|
]);
|
||||||
|
});
|
Loading…
Reference in New Issue