Skip to content

Commit

Permalink
add tree init func: initWithArray
Browse files Browse the repository at this point in the history
  • Loading branch information
knewbie committed Apr 17, 2017
1 parent a846764 commit 172011b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class BST<T> {
this.root = null;
}

public initWithArray(arr:T[]) {
for(let a of arr) {
this.insert(a);
}
}

public insert(d: T) {
let node = new TreeNode(d);
if (this.root == null) {
Expand Down
13 changes: 9 additions & 4 deletions test_Tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { BST, TreeNode} from "./Tree";
import {print} from "./util";


let arr = [23,45,16,37,3,99,22, 26,40,38,42];
let arr1 = ['b', 'a','g','2','h','z','y','7','n'];

let t = new BST<number>();
t.initWithArray(arr);

let t1 = new BST<string>();
t1.initWithArray(arr1);
print(t1.inOrder(t1.root));

let arr = [23,45,16,37,3,99,22, 26,40,38,42];
for(let d of arr) {
t.insert(d);
}

print("Inorder traversal: ");
print(t.inOrder(t.root));
Expand All @@ -28,3 +31,5 @@ t.remove(37);
print(t.inOrder(t.root));

print(t.count(), t.edges());


0 comments on commit 172011b

Please sign in to comment.