Best JavaScript code snippet using cucumber-gherkin
dfs.js
Source:dfs.js
1// Depth-first search code starts at line 1692class Node {3 constructor(value) {4 this.value = value;5 this.left = null;6 this.right = null;7 }8}9class BinarySearchTree {10 constructor() {11 this.root = null;12 }13 insert(value) {14 const newNode = new Node(value);15 if (this.root === null) {16 this.root = newNode;17 } else {18 let currentNode = this.root;19 while (true) {20 if (value < currentNode.value) {21 // go left22 if (!currentNode.left) {23 currentNode.left = newNode;24 return this;25 }26 currentNode = currentNode.left;27 } else {28 // go right29 if (!currentNode.right) {30 currentNode.right = newNode;31 return this;32 }33 currentNode = currentNode.right;34 }35 }36 }37 }38 lookup(value) {39 if (!this.root) {40 return false;41 }42 let currentNode = this.root;43 while (currentNode) {44 if (value < currentNode.value) {45 currentNode = currentNode.left;46 } else if (value > currentNode.value) {47 currentNode = currentNode.right;48 } else if (currentNode.value === value) {49 return currentNode;50 }51 }52 return false;53 }54 remove(value) {55 if (!this.root) {56 return false;57 }58 let currentNode = this.root;59 let parentNode = null;60 while (currentNode) {61 if (value < currentNode.value) {62 parentNode = currentNode;63 currentNode = currentNode.left;64 } else if (value > currentNode.value) {65 parentNode = currentNode;66 currentNode = currentNode.right;67 } else if (currentNode.value === value) {68 // Option 1: no right child69 if (currentNode.right === null) {70 if (parentNode === null) {71 this.root = currentNode.left;72 } else {73 // if parent > current value, make current left child a child of parent74 if (currentNode.value < parentNode.value) {75 parentNode.left = currentNode.left;76 // if parent < current value, make left child a right child of parent77 } else if (currentNode.value > parentNode.value) {78 parentNode.right = currentNode.left;79 }80 }81 //Option 2: right child which doesnt have a left child82 } else if (currentNode.right.left === null) {83 if (parentNode === null) {84 this.root = currentNode.left;85 } else {86 currentNode.right.left = currentNode.left;87 // if parent > current, make right child of the left the parent88 if (currentNode.value < parentNode.value) {89 parentNode.left = currentNode.right;90 // if parent < current, make right child a right child of the parent91 } else if (currentNode.value > parentNode.value) {92 parentNode.right = currentNode.right;93 }94 }95 //Option 3: right child that has a left child96 } else {97 // find the right child's leftmost child98 let leftmost = currentNode.right.left;99 let leftmostParent = currentNode.right;100 while (leftmost.left !== null) {101 leftmostParent = leftmost;102 leftmost = leftmost.left;103 }104 // parent's left subtree is now leftmost's right subtree105 leftmostParent.left = leftmost.right;106 leftmost.left = currentNode.left;107 leftmost.right = currentNode.right;108 if (parentNode === null) {109 this.root = leftmost;110 } else {111 if (currentNode.value < parentNode.value) {112 parentNode.left = leftmost;113 } else if (currentNode.value > parentNode.value) {114 parentNode.right = leftmost;115 }116 }117 }118 return true;119 }120 }121 }122 // Breadth-first search123 bfs() {124 let currentNode = this.root;125 let list = [];126 let queue = [];127 queue.push(currentNode);128 while (queue.length > 0) {129 currentNode = queue.shift();130 console.log(currentNode.value);131 list.push(currentNode.value);132 if (currentNode.left) {133 queue.push(currentNode.left);134 }135 if (currentNode.right) {136 queue.push(currentNode.right);137 }138 }139 return list;140 }141 // BFS recursive142 bfsRecursive(queue, list) {143 if (!queue.length) {144 return list;145 }146 let currentNode = queue.shift();147 list.push(currentNode.value);148 if (currentNode.left) {149 queue.push(currentNode.left);150 }151 if (currentNode.right) {152 queue.push(currentNode.right);153 }154 return this.bfsRecursive(queue, list);155 }156 // Depth-first search In Order157 dfsInOrder() {158 return traverseInOrder(this.root, []);159 }160 // DFS post order161 dfsPreOrder() {162 return traversePreOrder(this.root, []);163 }164 // DFS pre order165 dfsPostOrder() {166 return traversePostOrder(this.root, []);167 }168}169function traverseInOrder(node, list) {170 //console.log(node.value) // check the path taken171 if (node.left) {172 traverseInOrder(node.left, list);173 }174 list.push(node.value);175 if (node.right) {176 traverseInOrder(node.right, list);177 }178 return list;179}180function traversePreOrder(node, list) {181 //console.log(node.value) // check the path taken182 list.push(node.value);183 if (node.left) {184 traversePreOrder(node.left, list);185 }186 if (node.right) {187 traversePreOrder(node.right, list);188 }189 return list;190}191function traversePostOrder(node, list) {192 //console.log(node.value) // check the path taken193 if (node.left) {194 traversePostOrder(node.left, list);195 }196 if (node.right) {197 traversePostOrder(node.right, list);198 }199 list.push(node.value);200 return list;201}202function traverse(node) {203 const tree = { value: node.value };204 tree.left = node.left === null ? null :205 traverse(node.left);206 tree.right = node.right === null ? null :207 traverse(node.right);208 return tree;209}210const bst = new BinarySearchTree();211bst.insert(9);212bst.insert(4);213bst.insert(6);214bst.insert(20);215bst.insert(170);216bst.insert(15);217bst.insert(1);218console.log(bst.dfsInOrder());219console.log(bst.dfsPreOrder());220console.log(bst.dfsPostOrder());221// 9222// 4 20223//1 6 15 170224// 3 Types of DFS ordering/traversal225// InOrder - [1,4,6,9,15,20,170]226// PreOrder - [9,4,1,6,20,15,170]...
breadth_first_search.js
Source:breadth_first_search.js
1class Node {2 constructor(value) {3 this.left = null;4 this.right = null;5 this.value = value;6 }7}8class BinarySearchTree {9 constructor() {10 this.root = null;11 }12 insert(value) {13 const newNode = new Node(value)14 if (this.root === null) {15 this.root = newNode16 } else {17 let currentNode = this.root;18 while (true) {19 if (value < currentNode.value) {20 if (!currentNode.left) {21 currentNode.left = newNode;22 return this;23 }24 currentNode = currentNode.left;25 } else {26 if (!currentNode.right) {27 currentNode.right = newNode;28 return this;29 }30 currentNode = currentNode.right;31 }32 }33 }34 }35 lookup(value) {36 if (this.root === null) {37 return false38 }39 let currentNode = this.root;40 while (currentNode) {41 if (value < currentNode.value) {42 currentNode = currentNode.left43 } else if (value > currentNode.value) {44 currentNode = currentNode.right45 } else if (currentNode.value === value) {46 return currentNode;47 }48 }49 return false;50 }51 remove(value) {52 if (!this.root) {53 return false;54 }55 let currentNode = this.root;56 let parentNode = null;57 while (currentNode) {58 if (value < currentNode.value) {59 parentNode = currentNode;60 currentNode = currentNode.left61 } else if (value > currentNode.value) {62 parentNode = currentNode63 currentNode = currentNode.right64 } else if (currentNode.value === value) {65 // We ave a match66 // option 1 : No right child:67 if (currentNode.right === null) {68 if (parentNode === null) {69 this.root = currentNode.left;70 } else {71 // if parent > current value, make current left chuld a child of parent72 if (currentNode.value < parentNode.value) {73 parentNode.left = currentNode.left;74 // if parent < current value, make left child a right child of parent75 } else if (currentNode.value > parentNode.value) {76 parentNode.right = currentNode.left77 }78 }79 // option 2: Right child which doesn't have a left child80 } else if (currentNode.right.left === null) {81 if (parentNode === null) {82 this.root = currentNode.left;83 } else {84 currentNode.right.left = currentNode.left;85 // if parent > current, make right child of the left the parent86 if (currentNode.value < parentNode.value) {87 parentNode.left = currentNode.right;88 // if parent < current, make right child a right child of the parent89 } else if (currentNode.value > parentNode.value) {90 parentNode.right = currentNode.right;91 }92 }93 // option 3: Right child that has a left child94 } else {95 // find the right child's left most child96 let leftmost = currentNode.right.left;97 let leftmostParent = currentNode.right;98 while (leftmost.eft !== null) {99 leftmostParent = leftmost;100 leftmost = leftmost.left;101 }102 // parent's left subtree is now left's modt right subtree103 leftmostParent.left = leftmost.right;104 leftmost.left = currentNode.left;105 leftmost.right = currentNode.right;106 if (parentNode === null) {107 this.root = leftmost;108 } else {109 if (currentNode.value < parentNode.value) {110 parentNide.left = leftmost;111 } else if (currentNode.value > parentNode.value) {112 parentNode.right = leftmost;113 }114 }115 }116 return true117 }118 }119 }120 breadthFirstSearch() {121 let currentNode = this.root;122 let list = [];123 let queue = [];124 queue.push(currentNode);125 while (queue.length > 0) {126 currentNode = queue.shift();127 list.push(currentNode.value);128 if (currentNode.left) {129 queue.push(currentNode.left)130 }131 if (currentNode.right) {132 queue.push(currentNode.right)133 }134 }135 return list;136 }137 breadthFirstSearchR(queue, list) {138 if (!queue.length) {139 return list;140 }141 let currentNode = queue.shift();142 list.push(currentNode.value)143 if (currentNode.left) {144 queue.push(currentNode.left)145 }146 if (currentNode.right) {147 queue.push(currentNode.right)148 }149 return this.breadthFirstSearchR(queue, list)150 }151 DFSInOrder() {152 return traverseInorder(this.root, [])153 }154 DFSPostOrder() {155 return traversePostorder(this.root, [])156 }157 DFSPreOrder() {158 return traversePreorder(this.root, [])159 }160}161function traverseInorder(node, list) {162 if (node.left) {163 traverseInorder(node.left, list)164 }165 list.push(node.value)166 if (node.right) {167 traverseInorder(node.right, list)168 }169 return list;170}171function traversePreorder(node, list) {172 list.push(node.value)173 if (node.left) {174 traversePreorder(node.left, list)175 }176 if (node.right) {177 traversePreorder(node.right, list)178 }179 return list;180}181function traversePostorder(node, list) {182 if (node.left) {183 traversePostorder(node.left, list)184 }185 if (node.right) {186 traversePostorder(node.right, list)187 }188 list.push(node.value)189 return list;190}191const tree = new BinarySearchTree();192tree.insert(9);193tree.insert(4)194tree.insert(6)195tree.insert(20)196tree.insert(170)197tree.insert(15)198tree.insert(1)199 // tree.remove(170)200 // console.log(tree.breadthFirstSearch())201 // console.log(tree.breadthFirstSearch([tree.root], []));202console.log(tree.DFSPostOrder())203function traverse(node) {204 const tree = { value: node.value };205 tree.left = node.left === null ? null :206 traverse(node.left);207 tree.right = node.right === null ? null :208 traverse(node.right);209 return tree;...
bfs.js
Source:bfs.js
1// Breadth-first search code starts at line 1302class Node {3 constructor(value) {4 this.value = value;5 this.left = null;6 this.right = null;7 }8}9class BinarySearchTree {10 constructor() {11 this.root = null;12 }13 insert(value) {14 const newNode = new Node(value);15 if (this.root === null) {16 this.root = newNode;17 } else {18 let currentNode = this.root;19 while (true) {20 if (value < currentNode.value) {21 // go left22 if (!currentNode.left) {23 currentNode.left = newNode;24 return this;25 }26 currentNode = currentNode.left;27 } else {28 // go right29 if (!currentNode.right) {30 currentNode.right = newNode;31 return this;32 }33 currentNode = currentNode.right;34 }35 }36 }37 }38 lookup(value) {39 if (!this.root) {40 return false;41 }42 let currentNode = this.root;43 while (currentNode) {44 if (value < currentNode.value) {45 currentNode = currentNode.left;46 } else if (value > currentNode.value) {47 currentNode = currentNode.right;48 } else if (currentNode.value === value) {49 return currentNode;50 }51 }52 return false;53 }54 remove(value) {55 if (!this.root) {56 return false;57 }58 let currentNode = this.root;59 let parentNode = null;60 while (currentNode) {61 if (value < currentNode.value) {62 parentNode = currentNode;63 currentNode = currentNode.left;64 } else if (value > currentNode.value) {65 parentNode = currentNode;66 currentNode = currentNode.right;67 } else if (currentNode.value === value) {68 // Option 1: no right child69 if (currentNode.right === null) {70 if (parentNode === null) {71 this.root = currentNode.left;72 } else {73 // if parent > current value, make current left child a child of parent74 if (currentNode.value < parentNode.value) {75 parentNode.left = currentNode.left;76 // if parent < current value, make left child a right child of parent77 } else if (currentNode.value > parentNode.value) {78 parentNode.right = currentNode.left;79 }80 }81 //Option 2: right child which doesnt have a left child82 } else if (currentNode.right.left === null) {83 if (parentNode === null) {84 this.root = currentNode.left;85 } else {86 currentNode.right.left = currentNode.left;87 // if parent > current, make right child of the left the parent88 if (currentNode.value < parentNode.value) {89 parentNode.left = currentNode.right;90 // if parent < current, make right child a right child of the parent91 } else if (currentNode.value > parentNode.value) {92 parentNode.right = currentNode.right;93 }94 }95 //Option 3: right child that has a left child96 } else {97 // find the right child's leftmost child98 let leftmost = currentNode.right.left;99 let leftmostParent = currentNode.right;100 while (leftmost.left !== null) {101 leftmostParent = leftmost;102 leftmost = leftmost.left;103 }104 // parent's left subtree is now leftmost's right subtree105 leftmostParent.left = leftmost.right;106 leftmost.left = currentNode.left;107 leftmost.right = currentNode.right;108 if (parentNode === null) {109 this.root = leftmost;110 } else {111 if (currentNode.value < parentNode.value) {112 parentNode.left = leftmost;113 } else if (currentNode.value > parentNode.value) {114 parentNode.right = leftmost;115 }116 }117 }118 return true;119 }120 }121 }122 // Breadth-first search123 bfs() {124 let currentNode = this.root;125 let list = [];126 let queue = [];127 queue.push(currentNode);128 while (queue.length > 0) {129 currentNode = queue.shift();130 console.log(currentNode.value);131 list.push(currentNode.value);132 if (currentNode.left) {133 queue.push(currentNode.left);134 }135 if (currentNode.right) {136 queue.push(currentNode.right);137 }138 }139 return list;140 }141 // BFS recursive142 bfsRecursive(queue, list) {143 if (!queue.length) {144 return list;145 }146 let currentNode = queue.shift();147 list.push(currentNode.value);148 if (currentNode.left) {149 queue.push(currentNode.left);150 }151 if (currentNode.right) {152 queue.push(currentNode.right);153 }154 return this.bfsRecursive(queue, list);155 }156}157function traverse(node) {158 const tree = { value: node.value };159 tree.left = node.left === null ? null :160 traverse(node.left);161 tree.right = node.right === null ? null :162 traverse(node.right);163 return tree;164}165const bst = new BinarySearchTree();166bst.insert(9);167bst.insert(4);168bst.insert(6);169bst.insert(20);170bst.insert(170);171bst.insert(15);172bst.insert(1);173// console.log(bst.bfs());174console.log(bst.bfsRecursive([bst.root], []));175// 9176// 4 20...
链表基础.js
Source:链表基础.js
1/**2 * 1) åé¾è¡¨çæå
¥ãå é¤ãæ¥æ¾æä½ï¼3 * 2ï¼åé¾è¡¨å转4 */5class Node {6 constructor(value) {7 this.value = value;8 this.next = null;9 }10}11class SinglyLinkedList {12 constructor() {13 this.head = new Node('head');14 }15 // æ ¹æ®valueæ¥æ¾èç¹16 findByValue(value) {17 let currentNode = this.head.next;18 while (currentNode !== null && currentNode.value !== value) {19 currentNode = currentNode.next;20 }21 console.log(currentNode);22 return currentNode === null ? -1 : currentNode;23 }24 // æ ¹æ®indexæ¥æ¾èç¹ï¼ä¸æ ä»0å¼å§25 findByIndex(index) {26 let currentNode = this.head.next;27 let currentIndex = 0;28 while (currentNode !== null && currentNode < index) {29 currentNode = currentNode.next;30 currentIndex++;31 }32 console.log(currentNode);33 return currentNode === null ? -1 : currentNode;34 }35 // åé¾è¡¨æ«å°¾è¿½å èç¹36 append(newValue) {37 const newNode = new Node(newValue);38 let currentNode = this.head;39 while(currentNode.next) {40 currentNode = currentNode.next;41 }42 currentNode.next = newNode;43 }44 // æå®å
ç´ ååæå
¥45 insert(newValue, value) {46 const currentNode = this.findByValue(value);47 if (currentNode === -1) {48 console.log('æªæ¾å°æå
¥ä½ç½®');49 return50 }51 const newNode = new Node(newValue);52 newNode.next = currentNode.next;53 currentNode.next = newNode;54 }55 // æ ¹æ®å¼å é¤56 remove(value) {57 let currentNode = this.head;58 while (currentNode.next !== null && currentNode.next.value !== value) {59 currentNode = currentNode.next;60 }61 if (currentNode.next === null) {62 console.log('æªæ¾å°å
ç´ ');63 return64 }65 const prevNode = currentNode;66 prevNode.next = prevNode.next.next;67 }68 // éåæ¾ç¤ºææèç¹69 display() {70 let currentNode = this.head.next; // 忽ç¥å¤´æéçå¼71 while (currentNode !== null) {72 console.log(currentNode.value);73 currentNode = currentNode.next;74 }75 }76 // å转åé¾è¡¨77 reverse() {78 // headèç¹å³å¨å
µï¼ä½ç¨å°±æ¯ä½¿ææé¾è¡¨ï¼å
æ¬ç©ºé¾è¡¨ç头èç¹ä¸ä¸ºnullï¼å¹¶ä½¿å¯¹åé¾è¡¨çæå
¥ãå é¤æä½ä¸éè¦åºåæ¯å¦ä¸ºç©ºè¡¨ææ¯å¦å¨ç¬¬ä¸ä¸ªä½ç½®è¿è¡ï¼79 // ä»èä¸å
¶ä»ä½ç½®çæå
¥ãå é¤æä½ä¸è´ï¼éä½ç¼ç å¤æ度80 // æ以å转é¾è¡¨çæ¶åä¸éè¦å¸¦ä¸å¤´èç¹81 let currentNode = this.head.next;82 // 第ä¸ä¸ªèç¹çå驱ç»ç¹è®©å
¶æå null, å 为头èç¹ä¸åä¸é¾è¡¨å转83 let prevNode = null;84 while (currentNode !== null) {85 // æ¯ä¸ªå¾ªç¯ä¸ä¸æè¿è¡ currentNode, nextNode å prevNode ä¸ä¸ªæéçèµå¼ï¼86 // å
¶ä¸currentNode å prevNode å¨èµå¼å就被å¼ç¨äºï¼å æ¤è¿ä¸¤ä¸ªåééè¦å¨å¾ªç¯ä½å¤èµåå§å¼87 // å
ä¿çä¸ä¸ç»ç¹çæéï¼é¿å
æé丢失88 let nextNode = currentNode.next;89 // è¿ä¸æ¥çæ£å®ç°æéå转ï¼æåå驱èç¹90 currentNode.next = prevNode;91 prevNode = currentNode;92 currentNode = nextNode;93 }94 // æåç»å转åçé¾è¡¨æ´ä¸å¤´ç»ç¹ï¼è¿éæåä¸ä¸ªcurrentNode为nullï¼æåä¸ä¸ªprevNodeææå第ä¸ä¸ªç»ç¹ï¼å æ¤é prevNode æ¥èµå¼95 this.head.next = prevNode;96 }97 // ç¯çæ£æµï¼å¿«æ
¢æéæ³98 checkCircle() {99 let fast = this.head.next;100 let slow = this.head;101 while (fast != null && fast.next != null) {102 fast = fast.next.next;103 slow = slow.next;104 if (show === fast) return true;105 } 106 return false;107 } 108 // å é¤åæ°ç¬¬k个èç¹109 removeByIndexFromEnd(index) {110 // å
å¤ææ¯å¦æ¯ç¯é¾è¡¨111 if (this.checkCircle) return false;112 let pos = 1;113 this.reverse()114 let currentNode = this.head.next;115 while (currentNode !== null && pos < index) {116 currentNode = currentNode++117 pos++;118 }119 if (currentNode === null) {120 console.log('该ç»ç¹ä¸åå¨');121 return false;122 }123 this.remove(currentNode.value);124 this.reverse();125 }126 // æ±é¾è¡¨çä¸é´ç»ç¹127 findMiddleNode() {128 // 忽ç¥å¤´ç»ç¹129 let fast = this.head.next;130 let slow = this.head.next;131 while (fast.next != null && fast.next.next != null) {132 fast = fast.next.next;133 slow = slow.next;134 }135 console.log(slow);136 return slow;137 }138}139const mergeSortedLists = (listA, listB) => {140 if (!listA) {141 return listB142 }143 if (!listB) {144 return listA145 }146 let resultList = new Node('head');147 while (a !== null && b !== null) {148 if (a.value < b.value) {149 resultList.next = a;150 a = a.next;151 } else {152 resultList.next = b;153 b = b.next;154 }155 resultList = resultList.next;156 }157 // å¦æé¾è¡¨aæé¾è¡¨bè¿æå©ä½ç»ç¹ï¼åæ¥å°å并åçé¾è¡¨çåé¢158 if (a !== null) {159 resultList.next = a;160 } else {161 resultList.next = b;162 }163 return resultList;...
rbtree.js
Source:rbtree.js
1import { cloneTree, getUncle, getGrandFather, getSibling, rotate, buildTree } from './functions';2import { RED, BLACK, LEFT, RIGHT } from './constants';34function add(tree, value) {5 const newNode = {6 value,7 left: null,8 right: null,9 parent: null,10 color: RED,11 };1213 if (!tree) {14 newNode.color = BLACK;15 return newNode;16 }1718 const resultTree = cloneTree(tree);1920 let parentNode;21 let currentNode = resultTree;2223 while (currentNode) {24 parentNode = currentNode;25 if (currentNode.value === newNode.value) return resultTree;26 currentNode = newNode.value > currentNode.value ? currentNode.right : currentNode.left;27 }2829 currentNode = newNode;30 currentNode.parent = parentNode;31 if (currentNode.value > parentNode.value) {32 parentNode.right = currentNode;33 } else {34 parentNode.left = currentNode;35 }3637 while (currentNode.color === RED && currentNode.parent && currentNode.parent.color === RED) {38 const uncle = getUncle(currentNode);39 const grandFather = getGrandFather(currentNode);40 const { parent } = currentNode;4142 if (uncle && uncle.color === RED) {43 parent.color = BLACK;44 uncle.color = BLACK;45 grandFather.color = RED;46 currentNode = grandFather;47 if (!currentNode.parent) currentNode.color = BLACK;48 continue;49 }5051 if (!uncle || uncle.color === BLACK) {52 const sideOfTree = parent === grandFather.left ? LEFT : RIGHT;53 const anotherDirection = parent === grandFather.right ? LEFT : RIGHT;5455 if (parent[sideOfTree] === currentNode) {56 currentNode = parent;57 rotate(currentNode, anotherDirection);58 currentNode.color = BLACK;59 currentNode[anotherDirection].color = RED;60 } else {61 rotate(currentNode, sideOfTree);62 rotate(currentNode, anotherDirection);63 currentNode.color = BLACK;64 currentNode[anotherDirection].color = RED;65 }66 }67 }68 return buildTree(currentNode);69}7071function remove(tree, value) {72 const resultTree = cloneTree(tree);73 let deletedNode = resultTree;7475 while (value !== deletedNode.value) {76 if (value > deletedNode.value) deletedNode = deletedNode.right;77 else deletedNode = deletedNode.left;78 if (!deletedNode) return resultTree;79 }8081 let maxInLeft = deletedNode;82 if (deletedNode.left) {83 maxInLeft = deletedNode.left;84 while (maxInLeft.right) {85 maxInLeft = maxInLeft.right;86 }87 }8889 deletedNode.value = maxInLeft.value;9091 const currentNode = maxInLeft;92 const { parent } = currentNode;93 const direction = parent.left === currentNode ? LEFT : RIGHT;94 const anotherDirection = parent.left === currentNode ? RIGHT : LEFT;9596 parent[direction] = null;9798 while (currentNode.color === 0 && currentNode.parent) {99 const { parent: currentParent } = currentNode;100 const sibling = getSibling(currentNode);101 const nephewDirection = sibling[direction];102 const nephewAnother = sibling[anotherDirection];103 if (!sibling) break;104105 if (sibling.color === RED) {106 sibling.color = BLACK;107 currentParent.color = RED;108 rotate(sibling, direction);109 continue;110 }111112 if ((!nephewDirection || nephewDirection.color === BLACK) && (!nephewAnother || nephewAnother.color === BLACK)) {113 sibling.color = RED;114 currentParent.color = BLACK;115 break;116 }117118 if ((nephewDirection && nephewDirection.color === RED) && (!nephewAnother || nephewAnother.color === BLACK)) {119 sibling.color = RED;120 nephewDirection.color = BLACK;121 rotate(nephewDirection, anotherDirection);122 continue;123 }124125 if (nephewAnother && nephewAnother.color === RED) {126 sibling.color = currentParent.color;127 currentParent.color = BLACK;128 nephewAnother.color = BLACK;129 rotate(sibling, direction);130 break;131 }132 }133 return buildTree(currentNode);134}135
...
singleLinkedList.js
Source:singleLinkedList.js
1// 带头ç»ç¹çåé¾è¡¨ æå
¥ãå é¤ãæ¥æ¾2class Node {3 constructor(element) {4 this.element = element5 this.next = null6 }7}8class LinkList {9 // 带头ç»ç¹10 constructor() {11 this.head = new Node('head')12 }13 // åé¾è¡¨å°¾é¨æ·»å 14 append(element) {15 // å建æ°çç»ç¹16 let newNode = new Node(element)17 // 头ç»ç¹18 let currentNode = this.head19 while (currentNode.next !== null) {20 currentNode = currentNode.next21 }22 currentNode.next = newNode23 }24 findByValue (item) {25 let currentNode = this.head.next26 while (currentNode !== null && currentNode.element !== item) {27 currentNode = currentNode.next 28 }29 console.log(currentNode)30 return currentNode === null ? -1 : currentNode31 }32 // å¨æå®å
ç´ åæå
¥33 insert (element, newElement) {34 // æ¥æ¾æå®å
ç´ çç»ç¹35 let currentNode = this.findByValue(element)36 // è¥æ¾ä¸å°æå®å
ç´ çç»ç¹37 if (currentNode === -1) {38 console.log('æ¾ä¸å°è¦æå
¥çæå®å
ç´ çä½ç½®')39 return40 }41 // æ¾å°æå®å
ç´ çç»ç¹42 // å建æ°çç»ç¹43 let newNode = new Node(newElement)44 newNode.next = currentNode.next45 currentNode.next = newNode46 }47 // æ ¹æ®ä¸æ indexæ¥æ¾ç»ç¹ï¼ä»0å¼å§48 findByIndex (index) {49 let currentNode = this.head.next50 let pos = 051 while (currentNode !== null && pos !== index) {52 currentNode = currentNode.next 53 pos++54 }55 console.log(currentNode)56 return currentNode === null ? -1 : currentNode57 }58 // æ¥æ¾æå®å
ç´ çåä¸ä¸ªå
ç´ ç»ç¹59 findPrev (item) {60 let currentNode = this.head61 while (currentNode.next !== null && currentNode.next.element !== item) {62 currentNode = currentNode.next63 }64 if (currentNode.next === null) {65 return -166 }67 return currentNode68 }69 // å é¤æå®å
ç´ 70 remove (item) {71 // æ¥æ¾æå®å
ç´ çåä¸ä¸ªå
ç´ ç»ç¹72 let prevNode = this.findPrev(item)73 // console.log(currentNode)74 if (prevNode === -1) {75 console.log('ä¸åå¨æå®å
ç´ ')76 return77 }78 // å é¤æå®å
ç´ 79 prevNode.next = prevNode.next.next80 }81 // éåæ¾ç¤ºé¾è¡¨ç»ç¹82 display() {83 let currentNode = this.head.next // 忽ç¥å¤´ç»ç¹84 while (currentNode !== null) {85 console.log(currentNode.element)86 currentNode = currentNode.next87 }88 }89}90let linkList = new LinkList()91console.log(linkList)92// LinkList { head: Node { element: 'head', next: null } }93// åé¾è¡¨å°¾é¨æ·»å 94linkList.append('computer')95linkList.append('pencil')96linkList.append('water')97linkList.append('milk')98linkList.display()99// computer100// pencil101// water102// milk103// å¨æå®å
ç´ åæå
¥104linkList.insert('pencil', 'mobilephone') 105linkList.display()106// æ ¹æ®ä¸æ indexæ¥æ¾ç»ç¹ï¼ä»0å¼å§107linkList.findByIndex(2) 108linkList.findByIndex(6) 109// å é¤æå®å
ç´ 110linkList.remove('sugar')111linkList.display()112linkList.remove('water')...
index.js
Source:index.js
1// åé¾è¡¨çæå
¥ãå é¤ãæ¥æ¾2// 带æ头ç»ç¹çåé¾è¡¨3// ç»ç¹4class Node {5 constructor(element) {6 this.element = element // æ°æ®7 this.next = null // æé8 }9}10class LinkList {11 constructor() {12 this.head = new Node('head')13 }14 // åé¾è¡¨æ«å°¾æ·»å ç»ç¹15 append(newElement) {16 const newNode = new Node(newElement)17 let currentNode = this.head18 while (currentNode.next) {19 currentNode = currentNode.next20 }21 currentNode.next = newNode22 }23 // æ ¹æ®å
ç´ å¼valueæ¥æ¾ç»ç¹24 findByValue(item) {25 let currentNode = this.head.next26 while (currentNode !== null && currentNode.element !== item) {27 currentNode = currentNode.next28 }29 console.log(currentNode)30 return currentNode === null ? -1 : currentNode31 }32 // æ ¹æ®indexæ¥æ¾ç»ç¹ï¼ä¸æ ä»0å¼å§33 findByIndex (index) {34 let currentNode = this.head.next35 let pos = 036 while (currentNode !== null && pos !== index) {37 currentNode = currentNode.next38 pos++39 }40 console.log(currentNode)41 return currentNode === null ? -1 : currentNode42 }43 // å¨æå®å
ç´ åé¢æå
¥44 insert(element, newElement) {45 const currentNode = this.findByValue(element)46 if (currentNode === -1) {47 console.log('æªæ¾å°æå
¥çä½ç½®')48 return49 }50 const newNode = new Node(newElement)51 newNode.next = currentNode.next52 currentNode.next = newNode53 }54 // æ¥æ¾åä¸ä¸ªç»ç¹55 findPrev (item) {56 let currentNode = this.head 57 while (currentNode.next !== null && currentNode.next.element !== item) {58 currentNode = currentNode.next59 }60 if (currentNode.next === null) 61 return -162 return currentNode63 }64 // æ ¹æ®å¼å é¤65 remove (item) {66 const prevNode = this.findPrev(item)67 if (prevNode === -1) {68 console.log('æªæ¾å°è¯¥å
ç´ ')69 return 70 }71 prevNode.next = prevNode.next.next72 }73 // éåæ¾ç¤ºææçç»ç¹74 display () {75 let currentNode = this.head.next // 忽ç¥å¤´æéçå¼76 while (currentNode !== null) {77 console.log(currentNode.element)78 currentNode = currentNode.next79 }80 }81}82const linkList = new LinkList()83console.log(linkList)84// LinkList { head: Node { element: 'head', next: null } }85linkList.append('apple')86linkList.append('banana')87linkList.append('pear')88linkList.append('potato')89linkList.display()90// apple91// banana92// pear93// potato94linkList.findByValue('banana')95// Node {96// element: 'banana',97// next: Node {98// element: 'pear',99// next: Node { element: 'potato', next: null }100// }101// }102linkList.insert('banana', 'tomato')103linkList.display()104// apple105// banana106// tomato107// pear108// potato109linkList.findByIndex(2)110// Node {111// element: 'tomato',112// next: Node {113// element: 'pear',114// next: Node { element: 'potato', next: null }115// }116// }117linkList.remove('pear')118linkList.display()119// apple120// banana121// tomato122// potato123linkList.remove('egg')124linkList.display()125// æªæ¾å°è¯¥å
ç´ 126// apple127// banana128// tomato...
Using AI Code Generation
1var Cucumber = require('cucumber');2var fs = require('fs');3var gherkin = Cucumber.getGherkin();4var parser = new gherkin.Parser();5var lexer = new gherkin.Lexer('en');6var featureSource = fs.readFileSync('feature1.feature', 'utf8');7var feature = parser.parse(lexer.lex(featureSource));8var featureNode = feature.getFeature();9var scenarioNodes = featureNode.getScenarios();10var scenarioNode = scenarioNodes[0];11var stepNodes = scenarioNode.getSteps();12var stepNode = stepNodes[0];13console.log(stepNode.getName());14var express = require('express');15var app = express();16var bodyParser = require('body-parser');17var jsonParser = bodyParser.json();18var urlencodedParser = bodyParser.urlencoded({ extended: false });19var fs = require('fs');20var path = require('path');21var multer = require('multer');22var upload = multer({ dest: 'uploads/' });23var http = require('http');24var server = http.createServer(app);25var io = require('socket.io').listen(server);26app.use(express.static(path.join(__dirname, 'public')));27app.get('/', function(req, res) {28 res.sendFile(__dirname + '/index.html');29});30app.post('/upload', upload.single('file'), function(req, res) {31 var file = req.file;32 var fileName = file.originalname;33 var filePath = file.path;34 var fileSize = file.size;35 var fileType = file.mimetype;36 var fileExtension = fileName.split('.').pop();37 var fileBaseName = fileName.split('.').shift();38 var fileNewName = fileBaseName + '_' + Date.now() + '.' + fileExtension;39 fs.rename(filePath, 'uploads/' + fileNewName, function(err) {40 if (err) {41 throw err;42 }43 });
Using AI Code Generation
1var Cucumber = require('cucumber');2var parser = new Cucumber.Parser();3var fs = require('fs');4var gherkinSource = fs.readFileSync('test.feature', 'utf8');5var feature = parser.parse(gherkinSource);6var featureNode = feature.getFeature();7var scenarioNode = featureNode.getScenarioDefinitions()[0];8console.log(scenarioNode.getTitle());
Using AI Code Generation
1module.exports = function() {2 this.When(/^I search for "([^"]*)"$/, function (arg1, callback) {3 callback(null, 'pending');4 });5};61 scenario (1 undefined)73 steps (1 undefined, 2 skipped)8var {defineSupportCode} = require('cucumber');9defineSupportCode(function({Given, When, Then}) {10 Given(/^I am on the home page$/, function (callback) {11 callback(null, 'pending');12 });13 When(/^I search for "([^"]*)"$/, function (arg1, callback) {14 callback(null, 'pending');15 });16 Then(/^I should see the search results$/, function (callback) {17 callback(null, 'pending');18 });19});
Using AI Code Generation
1var gherkin = require('gherkin');2var parser = new gherkin.Parser();3var lexer = new gherkin.Lexer('en');4var fs = require('fs');5var data = fs.readFileSync('./features/feature1.feature');6var tokens = lexer.lex(data.toString());7var feature = parser.parse(tokens);8var currentNode = feature;9while (currentNode) {10 console.log(currentNode.type);11 currentNode = currentNode.next();12}
Using AI Code Generation
1var Cucumber = require('cucumber');2var gherkin = Cucumber.Gherkin.Parser();3var fs = require('fs');4fs.readFile('test.feature', 'utf8', function(err, data) {5 if (err) throw err;6 var feature = gherkin.parse(data);7 console.log(feature);8});
Using AI Code Generation
1var Cucumber = require('cucumber');2var fs = require('fs');3var featureSource = fs.readFileSync('./test.feature', 'utf8');4var parser = new Cucumber.Parser(featureSource);5var feature = parser.parse();6var astNode = feature.getAstNode();7var children = astNode.getChildren();8var scenarios = children.filter(function(child) {9 return child instanceof Cucumber.Ast.Scenario;10});11console.log(scenarios[0].getScenario().getAstNode().getChildren().getChildren().getChildren().get
LambdaTest offers a detailed Cucumber testing tutorial, explaining its features, importance, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed Cucumber testing chapters to help you get started:
Get 100 minutes of automation test minutes FREE!!