daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { map } from 'ramda'
  2. import * as assert from 'assert'
  3. //console.log(map(x=> x ** 2, [1, 2, 3, 4, 5]))
  4.  
  5. interface BinTree {
  6.     root: number;
  7.     left?: BinTree;
  8.     right?: BinTree;
  9. };
  10. // 2.1.1
  11. type treeFunc = (t:BinTree) => number[];
  12. const TreePreArray : treeFunc =
  13. t=> {
  14.     let result:number[]= [];
  15.     result.push(t.root);
  16.     if(t.left!==undefined)
  17.         result = result.concat(TreePreArray(t.left));
  18.     if(t.right!==undefined)
  19.         result = result.concat(TreePreArray(t.right));
  20.     return result;
  21. }
  22. // 2.1.2
  23. const TreeInArray : treeFunc =
  24. t=> {
  25.     let result:number[]= [];
  26.     if(t.left!==undefined)
  27.         result = result.concat(TreePreArray(t.left));
  28.     result.push(t.root);
  29.     if(t.right!==undefined)
  30.         result = result.concat(TreePreArray(t.right));
  31.     return result;
  32. }
  33. // 2.1.3
  34. const TreePostArray : treeFunc =
  35. t=> {
  36.     let result:number[]= [];
  37.     if(t.left!==undefined)
  38.         result = result.concat(TreePreArray(t.left));
  39.     if(t.right!==undefined)
  40.         result = result.concat(TreePreArray(t.right));
  41.     result.push(t.root);
  42.     return result;
  43. }
  44.  
  45.  
  46. let t1:BinTree = {root:1,left:{root:2,left:undefined,right:undefined},right:{root:3,left:undefined,right:undefined}}
  47. let t2:BinTree = {root:1,left:{root:2,left:undefined,right:undefined},right:undefined}
  48. let t3:BinTree = {root:1,left:undefined,right:{root:3,left:undefined,right:undefined}}
  49.  
  50. // 2.1.1 tests
  51. assert.deepEqual(TreePreArray(t1), [1, 2, 3], "Preorder - Test1 - BinTrees Not equal");
  52. assert.deepEqual(TreePreArray(t2), [1, 2], "Preorder - Test2 - BinTrees Not equal");
  53. assert.deepEqual(TreePreArray(t3), [1, 3], "Preorder - Test3 - BinTrees Not equal");
  54. // 2.1.2 tests
  55. assert.deepEqual(TreeInArray(t1), [2, 1, 3], "Inorder - Test1 - BinTrees Not equal");
  56. assert.deepEqual(TreeInArray(t2), [2, 1], "Inorder - Test2 - BinTrees Not equal");
  57. assert.deepEqual(TreeInArray(t3), [1, 3], "Inorder - Test3 - BinTrees Not equal");
  58. // 2.1.3 tests
  59. assert.deepEqual(TreePostArray(t1), [2, 3, 1], "Postorder - Test1 - BinTrees Not equal");
  60. assert.deepEqual(TreePostArray(t2), [2, 1], "Postorder - Test2 - BinTrees Not equal");
  61. assert.deepEqual(TreePostArray(t3), [3, 1], "Postorder - Test3 - BinTrees Not equal");
  62.  
  63.  
  64. interface GBinTree<T> {
  65.     root: T;
  66.     left?: GBinTree<T>;
  67.     right?: GBinTree<T>;
  68. };
  69. // 2.1.4
  70.  
  71. type GtreeFunc = <T>(t:GBinTree<T>) => T[];
  72. const GTreePreArray : GtreeFunc =
  73. t => {
  74.     if(t.root === undefined)
  75.         return [];
  76.     type arrayType = typeof t.root;
  77.     let result:arrayType[] = [];
  78.     result.push(t.root);
  79.     if(t.left!==undefined)
  80.         result = result.concat(GTreePreArray(t.left));
  81.     if(t.right!==undefined)
  82.         result = result.concat(GTreePreArray(t.right));
  83.     return result;
  84. }
  85.  
  86. // 2.1.5
  87. const GTreeInArray : GtreeFunc =
  88. t=> {
  89.     if(t.root === undefined)
  90.         return [];
  91.     type arrayType = typeof t.root;
  92.     let result:arrayType[] = [];
  93.     if(t.left!==undefined)
  94.         result = result.concat(GTreePreArray(t.left));
  95.     result.push(t.root);
  96.     if(t.right!==undefined)
  97.         result = result.concat(GTreePreArray(t.right));
  98.     return result;
  99. }
  100. // 2.1.6
  101. const GTreePostArray : GtreeFunc =
  102. t=> {
  103.     if(t.root === undefined)
  104.         return [];
  105.     type arrayType = typeof t.root;
  106.     let result:arrayType[] = [];
  107.     if(t.left!==undefined)
  108.         result = result.concat(GTreePreArray(t.left));
  109.     if(t.right!==undefined)
  110.         result = result.concat(GTreePreArray(t.right));
  111.     result.push(t.root);
  112.     return result;
  113. }
  114.  
  115. let s1:GBinTree<String> = {root:"ab",left:{root:"cd",left:undefined,right:undefined},right:{root:"ef",left:undefined,right:undefined}}
  116. let s2:GBinTree<String> = {root:"ab",left:{root:"cd",left:undefined,right:undefined},right:undefined}
  117. let s3:GBinTree<String> = {root:"ab",left:undefined,right:{root:"ef",left:undefined,right:undefined}}
  118. let s4:GBinTree<String> = {root:undefined,left:undefined,right:undefined}
  119. //assert.deepEqual(GTreePreArray(s4), [], "empty check");
  120. //console.log(GTreePreArray(s4));
  121. // 2.1.4 tests
  122. assert.deepEqual(GTreePreArray(s1), ["ab", "cd", "ef"], "Preorder - Test1 - GBinTrees Not equal");
  123. assert.deepEqual(GTreePreArray(s2), ["ab", "cd"], "Preorder - Test2 - GBinTrees Not equal");
  124. assert.deepEqual(GTreePreArray(s3), ["ab", "ef"], "Preorder - Test3 - GBinTrees Not equal");
  125. // 2.1.5 tests
  126. assert.deepEqual(GTreeInArray(s1), ["cd", "ab", "ef"], "Inorder - Test1 - GBinTrees Not equal");
  127. assert.deepEqual(GTreeInArray(s2), ["cd", "ab"], "Inorder - Test2 - GBinTrees Not equal");
  128. assert.deepEqual(GTreeInArray(s3), ["ab", "ef"], "Inorder - Test3 - GBinTrees Not equal");
  129. // 2.1.6 tests
  130. assert.deepEqual(GTreePostArray(s1), ["cd", "ef", "ab"], "Postorder - Test1 - GBinTrees Not equal");
  131. assert.deepEqual(GTreePostArray(s2), ["cd", "ab"], "Postorder - Test2 - GBinTrees Not equal");
  132. assert.deepEqual(GTreePostArray(s3), ["ef", "ab"], "Postorder - Test3 - GBinTrees Not equal");
  133.  
  134. type T4=<T>(f:((x:T)=>any[]),a:T[])=>any[]
  135. const Flatmap:T4=(f,a)=>{
  136.     let newarr=a.map(f)
  137.     newarr=newarr.reduce((acc, cur)=>acc.concat(cur), [])
  138.     return newarr
  139. }
  140.  
  141.  
  142.  
  143. console.log(Flatmap((x)=>x[0], [[[1,2], [3,4]], [[5,6], [7,8]]]))
RAW Paste Data
Top