Best JavaScript code snippet using wpt
viteraParser.js
Source:viteraParser.js
1"use strict";2var t = require("./templatePath");3var titleMap = {4 "Allergies": "Allergies, adverse reactions, alerts",5 "Medications": "History of medication use",6 "Procedures and Surgical/Medical History": "History of Procedures",7 "Insurance": "Payers",8 "Problems": "Problem List",9 "SOCIAL HISTORY": "Social History",10 "Lab Results": "Relevant diagnostic tests and/or laboratory data"11};12var normalizedCodeSystemNames = {13 "RxNorm": "RXNORM",14 "CPT-4": "CPT",15 "RoleClassRelationshipFormal": "HL7 RoleCode",16 "RoleCode": "HL7 Role",17 "ICD9CM": "ICD-9-CM",18 "AdministrativeGender": "HL7 AdministrativeGender",19 "MaritalStatus": "HL7 Marital Status",20 "CDC Race and Ethnicity": "Race and Ethnicity - CDC"21};22var normalizedDisplayNames = {23 "HISTORY OF MEDICATION USE": "History of medication use",24 "HISTORY OF IMMUNIZATIONS": "Immunizations",25 "HISTORY OF PROCEDURES": "History of Procedures",26 "history of prior surgery [For Hx of Tx, use H prefix]": "history of prior surgery [For Hx of Tx, use H prefix]",27 "History of encounters": "Encounters",28 "PAYMENT SOURCES": "Payment sources",29 "TREATMENT PLAN": "Plan of Care",30 "Problem list": "Problem List",31 "VITAL SIGNS": "Vital Signs",32 "RESULTS": "Relevant diagnostic tests and/or laboratory data"33};34module.exports = [{35 xpath: "//h:name[not(h:family)][not(text())]",36 action: "removeNode",37 comment: "bunch of empty names to be investigated"38}, {39 xpath: "//h:effectiveTime[not(*)][not(@*)]",40 action: "removeNode",41 comment: "all childless and attributeless times (maybe previously removed nullFlavor)"42}, {43 xpath: "//h:assignedPerson[not(*)]",44 action: "removeNode",45 comment: "all childless and attributeless assignedPerson (maybe previously removed nullFlavor)"46}, {47 xpath: t.allergiesSection + '/.//h:effectiveTime[not(@value | h:low | h:high)]',48 action: "removeNode",49}, {50 xpath: t.allergiesSection + '/h:id',51 action: "removeNode",52 comment: "error in file: id does not exist in spec"53}, {54 xpath: t.allergyObs + '/..',55 action: "addAttribute",56 params: {57 "inversionInd": "true"58 },59 comment: "parser expects a value",60}, {61 xpath: t.allergyObs + '/h:informant',62 action: "removeNode",63 comment: "error in file: informant does not exist in spec",64}, {65 xpath: t.allergyObs + '/h:participant/h:participantRole/h:playingEntity/h:name',66 action: "removeNode",67 comment: "needs to be researched"68}, {69 xpath: t.allergyReaction + '/h:code',70 action: "removeNode",71 comment: "can be anything according to spec and parser does not read it"72}, {73 xpath: t.allergyCommentAct,74 action: "removeNode",75 comment: "error in file: Ignoring Comment Activity"76}, {77 xpath: t.medSection + '/h:id',78 action: "removeNode",79 comment: "error in file: id does not exist in spec"80}, {81 xpath: t.medActivity + '/h:effectiveTime[@operator="A"]',82 action: "removeNode",83 comment: "error in file: unexpected interval"84}, {85 xpath: t.medActivity + '/h:informant',86 action: "removeNode",87 comment: "error in file: no informant node in spec"88}, {89 xpath: t.medActivity + '/h:entryRelationship/h:observation[not(h:templateId)]/..',90 action: "removeNode",91 comment: "error in file: template without templateId"92}, {93 xpath: t.medStatus,94 action: "removeNode",95 comment: "error in file: C32 template not valid in CCDA"96}, {97 xpath: t.medProbAct,98 action: "removeNode",99 comment: "error in file: there is no Problem Act in meidcations"100}, {101 xpath: t.medDispenseInfo,102 action: "removeNode",103 comment: "parser does not read"104}, {105 xpath: t.medSupplyInfo,106 action: "removeNode",107 comment: "parser does not read"108}, {109 xpath: t.medActivityInfo + '/h:manufacturedMaterial/h:name',110 action: "removeNode",111 comment: "parser does not read"112}, {113 xpath: t.medSupplyOrder + '/h:id',114 action: "removeNode",115 comment: "parser does not read"116}, {117 xpath: t.medSupplyOrder + '/h:effectiveTime',118 action: "removeNode",119 comment: "no value"120}, {121 xpath: t.medSupplyOrder + '/h:author/h:assignedAuthor/h:addr',122 action: "removeNode",123 comment: "parser does not read"124}, {125 xpath: t.medSupplyOrder + '/h:quantity[@unit]',126 action: "removeAttribute",127 params: "unit",128 comment: "parser does not read"129}, {130 xpath: t.immSection + '/h:id',131 action: "removeNode",132 comment: "error in file: id does not exist in spec"133}, {134 xpath: t.immActivity + '/h:code',135 action: "removeNode",136 comment: "parser does not read"137}, {138 xpath: t.immActivity + '/h:consumable/h:manufacturedProduct/h:manufacturedMaterial/h:name',139 action: "removeNode",140 comment: "to be researched"141}, {142 xpath: t.immActivity + '/h:consumable/h:manufacturedProduct/h:manufacturerOrganization/h:standardIndustryClassCode',143 action: "removeNode",144 comment: "to be researched"145}, {146 xpath: t.immActivity + '/h:informant',147 action: "removeNode",148 comment: "to be researched"149}, {150 xpath: t.immActUnknown1,151 action: "removeNode",152 comment: "unknown CCDA templateId"153}, {154 xpath: t.immActUnknown2,155 action: "removeNode",156 comment: "unknown CCDA templateId"157}, {158 xpath: t.immActComment + '/h:act',159 action: "addAttribute",160 params: {161 "moodCode": "INT"162 },163 comment: "just change ...22.4.64 is not good anyway"164}, {165 xpath: t.immActComment + '/h:act/h:templateId',166 action: "addAttribute",167 params: {168 "root": "2.16.840.1.113883.10.20.22.4.20"169 },170 comment: "2.16.840.1.113883.10.20.22.4.64 (comment) or 2.16.840.1.113883.10.20.22.4.20"171}, {172 xpath: t.procSection + '/h:id',173 action: "removeNode",174 comment: "error in file: id does not exist in spec"175}, {176 xpath: t.procActProc + '/h:informant',177 action: "removeNode",178 comment: "to be researched"179}, {180 xpath: t.procActProc + '/h:participant/h:templateId',181 action: "removeNode",182 comment: "error in file: this should be in participantRole"183}, {184 xpath: t.procActProc + '/h:participant/h:participantRole/h:id',185 action: "removeNode",186 comment: "to be researched"187}, {188 xpath: t.procActProcUnknown,189 action: "removeNode",190 comment: "to be researched"191}, {192 xpath: t.encSection + '/h:id',193 action: "removeNode",194 comment: "error in file: id does not exist in spec"195}, {196 xpath: t.encAct + '/h:informant',197 action: "removeNode",198 comment: "to be researched"199}, {200 xpath: t.encAct + '/h:participant/h:templateId',201 action: "removeNode",202 comment: "error in file: this should be in participantRole"203}, {204 xpath: t.encAct + '/h:participant/h:participantRole/h:id',205 action: "removeNode",206 comment: "to be researched"207}, {208 xpath: t.payersSection + '/h:id',209 action: "removeNode",210 comment: "error in file: id does not exist in spec"211}, {212 xpath: t.payersSection + '/h:code',213 action: "addAttribute",214 params: {215 "displayName": "Payers"216 }217}, {218 xpath: t.coverageAct + '/h:informant',219 action: "removeNode",220 comment: "to be researched"221}, {222 xpath: t.coverageAct + '/h:entryRelationship/h:sequenceNumber',223 action: "removeNode",224 comment: "to be researched"225}, {226 xpath: t.policyAct + '/h:entryRelationship/h:act[@moodCode="DEF"]',227 action: "addAttribute",228 params: {229 "moodCode": "EVN"230 },231 comment: "to be researched"232}, {233 xpath: t.policyAct + '/h:participant/h:participantRole/h:playingEntity/*[@value="19381212"]',234 action: "removeNode",235 comment: "to be researched"236}, {237 xpath: t.policyAct + '/h:performer/h:assignedEntity/h:representedOrganization[not(*)]',238 action: "removeNode",239 comment: "to be researched"240}, {241 xpath: t.pocSection + '/h:id',242 action: "removeNode",243 comment: "error in file: id does not exist in spec"244}, {245 xpath: t.pocActProc,246 action: "addAttribute",247 params: {248 "moodCode": "RQO"249 },250 comment: "parser does not support"251}, {252 xpath: t.pocActProcUnknown,253 action: "removeNode",254 comment: "not clear in specification, parser does not read"255}, {256 xpath: t.pocActProc + '/h:performer',257 action: "removeNode",258 comment: "not clear in specification, parser does not read"259}, {260 xpath: t.probSection + '/h:id',261 action: "removeNode",262 comment: "error in file: id does not exist in spec"263}, {264 xpath: t.probAct + '/h:statusCode',265 action: "addAttribute",266 params: {267 "code": "completed"268 },269 comment: "parser deficiency: not read"270}, {271 xpath: t.probAct + '/h:performer',272 action: "removeNode",273 comment: "invalid"274}, {275 xpath: t.probObservation + '/h:informant',276 action: "removeNode",277 comment: "invalid"278}, {279 xpath: t.probObservation + '/h:code',280 action: "removeNode",281}, {282 xpath: t.probObservation + '/..',283 action: "removeAttribute",284 params: "inversionInd",285}, {286 xpath: t.probStatus + '/..',287 action: "removeAttribute",288 params: "inversionInd",289}, {290 xpath: t.probStatus + '/h:value',291 action: "addAttribute",292 params: {293 "xsi:type": "CD"294 }295}, {296 xpath: t.probActComment,297 action: "removeNode",298 comment: "Comment Activity is not implemented by Parser"299}, {300 xpath: t.resultsSection + '/h:id',301 action: "removeNode",302 comment: "error in file: id does not exist in spec"303}, {304 xpath: t.resultsSection + '/h:entry',305 action: "addAttribute",306 params: {307 "typeCode": "DRIV"308 }309}, {310 xpath: t.resultOrg + '/h:participant',311 action: "removeNode"312}, {313 xpath: t.resultOrg + '/h:component/h:procedure',314 action: "removeNode"315}, {316 xpath: t.resultOrg + '/h:specimen',317 action: "removeNode"318}, {319 xpath: t.resultOrg + '/h:effectiveTime',320 action: "removeNode"321}, {322 xpath: t.resultObs + '/h:performer',323 action: "removeNode"324}, {325 xpath: t.resultObs + '/h:value[@value]',326 action: "removeZeros"327}, {328 xpath: t.resultsCommentAct,329 action: "removeNode",330 comment: "error in file: Ignoring Comment Activity"331}, {332 xpath: t.vitalsSection + '/h:id',333 action: "removeNode",334 comment: "error in file: id does not exist in spec"335}, {336 xpath: t.vitalsObs + '/h:informant',337 action: "removeNode"338}, {339 xpath: t.vitalsObs + '/h:methodCode',340 action: "removeNode"341}, {342 xpath: t.resultsSection + "/.//*[not(*)][not(@*)][not(text())]",343 action: "removeNode"344}, {345 xpath: "//h:title",346 action: "replaceText",347 params: titleMap,348 comment: "titles may differ"349}, {350 xpath: "//h:recordTarget/h:patientRole/h:patient/h:name",351 action: "addAttribute",352 params: {353 "use": "L"354 },355 comment: "parser does read @use and generator assumes it is always 'L'"356}, {357 xpath: "//*[@codeSystem][@codeSystemName]",358 action: "normalize",359 params: {360 attr: "codeSystemName",361 map: normalizedCodeSystemNames362 },363 comment: 'blue-button parser normalization'364}, {365 xpath: "//*[@codeSystem][@displayName][@code]",366 action: "normalize",367 params: {368 attr: "displayName",369 srcAttr: "code",370 map: normalizedDisplayNames371 },372 comment: 'blue-button parser normalization'...
linkedmap_test.js
Source:linkedmap_test.js
1// Copyright 2006 The Closure Library Authors. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS-IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14goog.provide('goog.structs.LinkedMapTest');15goog.setTestOnly('goog.structs.LinkedMapTest');16goog.require('goog.structs.LinkedMap');17goog.require('goog.testing.jsunit');18goog.require('goog.testing.recordFunction');19function fillLinkedMap(m) {20 m.set('a', 0);21 m.set('b', 1);22 m.set('c', 2);23 m.set('d', 3);24}25var someObj = {};26function testLinkedMap() {27 var m = new goog.structs.LinkedMap();28 fillLinkedMap(m);29 assertArrayEquals(['a', 'b', 'c', 'd'], m.getKeys());30 assertArrayEquals([0, 1, 2, 3], m.getValues());31}32function testMaxSizeLinkedMap() {33 var m = new goog.structs.LinkedMap(3);34 fillLinkedMap(m);35 assertArrayEquals(['b', 'c', 'd'], m.getKeys());36 assertArrayEquals([1, 2, 3], m.getValues());37}38function testLruLinkedMap() {39 var m = new goog.structs.LinkedMap(undefined, true);40 fillLinkedMap(m);41 assertArrayEquals(['d', 'c', 'b', 'a'], m.getKeys());42 assertArrayEquals([3, 2, 1, 0], m.getValues());43 m.get('a');44 assertArrayEquals(['a', 'd', 'c', 'b'], m.getKeys());45 assertArrayEquals([0, 3, 2, 1], m.getValues());46 m.set('b', 4);47 assertArrayEquals(['b', 'a', 'd', 'c'], m.getKeys());48 assertArrayEquals([4, 0, 3, 2], m.getValues());49}50function testMaxSizeLruLinkedMap() {51 var m = new goog.structs.LinkedMap(3, true);52 fillLinkedMap(m);53 assertArrayEquals(['d', 'c', 'b'], m.getKeys());54 assertArrayEquals([3, 2, 1], m.getValues());55 m.get('c');56 assertArrayEquals(['c', 'd', 'b'], m.getKeys());57 assertArrayEquals([2, 3, 1], m.getValues());58 m.set('d', 4);59 assertArrayEquals(['d', 'c', 'b'], m.getKeys());60 assertArrayEquals([4, 2, 1], m.getValues());61}62function testGetCount() {63 var m = new goog.structs.LinkedMap();64 assertEquals(0, m.getCount());65 m.set('a', 0);66 assertEquals(1, m.getCount());67 m.set('a', 1);68 assertEquals(1, m.getCount());69 m.set('b', 2);70 assertEquals(2, m.getCount());71 m.remove('a');72 assertEquals(1, m.getCount());73}74function testIsEmpty() {75 var m = new goog.structs.LinkedMap();76 assertTrue(m.isEmpty());77 m.set('a', 0);78 assertFalse(m.isEmpty());79 m.remove('a');80 assertTrue(m.isEmpty());81}82function testSetMaxCount() {83 var m = new goog.structs.LinkedMap(3);84 fillLinkedMap(m);85 assertEquals(3, m.getCount());86 m.setMaxCount(5);87 m.set('e', 5);88 m.set('f', 6);89 m.set('g', 7);90 assertEquals(5, m.getCount());91 m.setMaxCount(4);92 assertEquals(4, m.getCount());93 m.setMaxCount(0);94 m.set('h', 8);95 m.set('i', 9);96 m.set('j', 10);97 assertEquals(7, m.getCount());98}99function testClear() {100 var m = new goog.structs.LinkedMap();101 fillLinkedMap(m);102 m.clear();103 assertTrue(m.isEmpty());104}105function testForEach() {106 var m = new goog.structs.LinkedMap();107 fillLinkedMap(m);108 m.forEach(function(val, key, linkedMap) {109 linkedMap.set(key, val * 2);110 assertEquals('forEach should run in provided context.', someObj, this);111 }, someObj);112 assertArrayEquals(['a', 'b', 'c', 'd'], m.getKeys());113 assertArrayEquals([0, 2, 4, 6], m.getValues());114}115function testMap() {116 var m = new goog.structs.LinkedMap();117 fillLinkedMap(m);118 var result = m.map(function(val, key, linkedMap) {119 assertEquals('The LinkedMap object should get passed in', m, linkedMap);120 assertEquals('map should run in provided context', someObj, this);121 return key + val;122 }, someObj);123 assertArrayEquals(['a0', 'b1', 'c2', 'd3'], result);124}125function testSome() {126 var m = new goog.structs.LinkedMap();127 fillLinkedMap(m);128 var result = m.some(function(val, key, linkedMap) {129 assertEquals('The LinkedMap object should get passed in', m, linkedMap);130 assertEquals('map should run in provided context', someObj, this);131 return val > 2;132 }, someObj);133 assertTrue(result);134 assertFalse(m.some(function(val) {return val > 3}));135 assertTrue(m.some(function(val, key) {return key == 'c';}));136 assertFalse(m.some(function(val, key) {return key == 'e';}));137}138function testEvery() {139 var m = new goog.structs.LinkedMap();140 fillLinkedMap(m);141 var result = m.every(function(val, key, linkedMap) {142 assertEquals('The LinkedMap object should get passed in', m, linkedMap);143 assertEquals('map should run in provided context', someObj, this);144 return val < 5;145 }, someObj);146 assertTrue(result);147 assertFalse(m.every(function(val) {return val < 2}));148 assertTrue(m.every(function(val, key) {return key.length == 1;}));149 assertFalse(m.every(function(val, key) {return key == 'b';}));150}151function testPeek() {152 var m = new goog.structs.LinkedMap();153 assertEquals(undefined, m.peek());154 assertEquals(undefined, m.peekLast());155 fillLinkedMap(m);156 assertEquals(0, m.peek());157 m.remove('a');158 assertEquals(1, m.peek());159 assertEquals(3, m.peekLast());160 assertEquals(3, m.peekValue('d'));161 assertEquals(1, m.peek());162 m.remove('d');163 assertEquals(2, m.peekLast());164}165function testPop() {166 var m = new goog.structs.LinkedMap();167 assertEquals(undefined, m.shift());168 assertEquals(undefined, m.pop());169 fillLinkedMap(m);170 assertEquals(4, m.getCount());171 assertEquals(0, m.shift());172 assertEquals(1, m.peek());173 assertEquals(3, m.pop());174 assertEquals(2, m.peekLast());175 assertEquals(2, m.getCount());176}177function testContains() {178 var m = new goog.structs.LinkedMap();179 fillLinkedMap(m);180 assertTrue(m.contains(2));181 assertFalse(m.contains(4));182}183function testContainsKey() {184 var m = new goog.structs.LinkedMap();185 fillLinkedMap(m);186 assertTrue(m.containsKey('b'));187 assertFalse(m.containsKey('elephant'));188 assertFalse(m.containsKey('undefined'));189}190function testRemoveNodeCalls() {191 var m = new goog.structs.LinkedMap(1);192 m.removeNode = goog.testing.recordFunction(m.removeNode);193 m.set('1', 1);194 assertEquals('removeNode not called after adding an element', 0,195 m.removeNode.getCallCount());196 m.set('1', 2);197 assertEquals('removeNode not called after updating an element', 0,198 m.removeNode.getCallCount());199 m.set('2', 2);200 assertEquals('removeNode called after adding an overflowing element', 1,201 m.removeNode.getCallCount());202 m.remove('3');203 assertEquals('removeNode not called after removing a non-existing element', 1,204 m.removeNode.getCallCount());205 m.remove('2');206 assertEquals('removeNode called after removing an existing element', 2,207 m.removeNode.getCallCount());208 m.set('1', 1);209 m.clear();210 assertEquals('removeNode called after clearing the map', 3,211 m.removeNode.getCallCount());212 m.clear();213 assertEquals('removeNode not called after clearing an empty map', 3,214 m.removeNode.getCallCount());215 m.set('1', 1);216 m.pop();217 assertEquals('removeNode called after calling pop', 4,218 m.removeNode.getCallCount());219 m.pop();220 assertEquals('removeNode not called after calling pop on an empty map', 4,221 m.removeNode.getCallCount());222 m.set('1', 1);223 m.shift();224 assertEquals('removeNode called after calling shift', 5,225 m.removeNode.getCallCount());226 m.shift();227 assertEquals('removeNode not called after calling shift on an empty map', 5,228 m.removeNode.getCallCount());229 m.setMaxCount(2);230 m.set('1', 1);231 m.set('2', 2);232 assertEquals('removeNode not called after increasing the maximum map size', 5,233 m.removeNode.getCallCount());234 m.setMaxCount(1);235 assertEquals('removeNode called after decreasing the maximum map size', 6,236 m.removeNode.getCallCount());...
BinarySearchTree.js
Source:BinarySearchTree.js
1const BinaryTree = require('./BinaryTree');2class Node {3 constructor(value) {4 this.value = value;5 this.left = null;6 this.right = null;7 }8}9class BinarySearchTree extends BinaryTree {10 constructor() {11 super();12 this.size = 0;13 }14 // táºn dụng thứ tá»± có sẵn của BinarySearchTree Äá» tiếp cáºn node cần tìm nhanh chóng15 search(value) {16 if (!this.root) return;17 let curNode = this.root;18 while (curNode) {19 if (value < curNode.value) {20 curNode = curNode.left;21 } else if (value > curNode.value) {22 curNode = curNode.right;23 } else {24 return curNode;25 }26 }27 return null;28 } // O(logn)29 add(value) {30 const newNode = new Node(value);31 if (!this.root) {32 this.root = newNode;33 return;34 }35 let curNode = this.root;36 while (true) {37 if (value < curNode.value) {38 if (!curNode.left) {39 curNode.left = newNode;40 newNode.parent = curNode;41 this.size++;42 return;43 }44 curNode = curNode.left;45 } else if (value > curNode.value) {46 if (!curNode.right) {47 curNode.right = newNode;48 newNode.parent = curNode;49 this.size++;50 return;51 }52 curNode = curNode.right;53 } else {54 // no duplicate55 return;56 }57 }58 } // O(logn)59 /**60 * Xoá 1 node ra khá»i BinarySearchTree thá»±c chất là thay thế node bá» xoá bằng 1 node descendant của nó61 * LÆ°u ý: removeNode.left < [removeNode] < removeNode.right.left < removeNode.right62 * Gá»i node sẽ bá» xoá là removeNode. Ta chia ra 3 trÆ°á»ng hợp:63 * TH1: removeNode không có con bên phải64 *65 * Ex: removeNode = 5 không có con bên phải66 *67 * 1068 * /69 * [5]70 * /71 * 172 * / \73 * ... ...74 *75 * -> removeNode = 5 sẽ Äược thay thế bằng removeNode.left = 176 *77 * TH2: removeNode có con bên phải và con bên phải không có con bên trái78 *79 * Ex: removeNode = 5 có con bên phải là node9 và node nà y không có con bên trái80 *81 * 1082 * /83 * [5]84 * / \85 * 1 986 * / \ \87 * ... ... ...88 *89 * -> removeNode = 5 sẽ Äược thay thế bằng removeNode.right = 990 *91 * TH3: removeNode có con bên phải và con bên phải có con bên trái92 *93 * Ex: removeNode = 5 có con bên phải là node9 và node nà y có con bên trái là node894 *95 * 1096 * /97 * [5]98 * / \99 * 1 9100 * / \ / \101 * ... ... 8 ...102 * /103 * 6104 *105 * -> removeNode = 5 sẽ Äược thay thế bằng removeNode.right.mostLeft = 6106 * (mostLeft là node nằm bên trái nhất của cây con có root là node9)107 *108 * >> Sau khi xác Äá»nh Äược replaceNode, xoá các liên kết tá»i removeNode và thà nh láºp các liên kết cho replaceNode109 */110 remove(value) {111 let removeNode = this.search(value);112 if (!removeNode) return;113 this.size--;114 // parent of removeNode115 const parentNode = removeNode.parent;116 // check to see position of removeNode is right of parentNode or left of parentNode117 const rmNodePos = removeNode.value > parentNode.value ? 'right' : 'left';118 // find replaceNode119 let replaceNode;120 // TH1: removeNode không có con bên phải121 if (!removeNode.right) replaceNode = removeNode.left;122 // TH2: removeNode có con bên phải và con bên phải không có con bên trái123 else if (removeNode.right && !removeNode.right.left) {124 replaceNode = removeNode.right;125 const removeNode_left = removeNode.left;126 if (removeNode_left) {127 replaceNode.left = removeNode_left;128 removeNode_left.parent = replaceNode;129 }130 }131 // TH3: removeNode có con bên phải và con bên phải có con bên trái132 else if (removeNode.right && removeNode.right.left) {133 replaceNode = this._leftMost(removeNode);134 const removeNode_left = removeNode.left;135 if (removeNode_left) {136 replaceNode.left = removeNode_left;137 removeNode_left.parent = replaceNode;138 }139 const removeNode_right = removeNode.right;140 if (removeNode_right) {141 replaceNode.right = removeNode_right;142 removeNode_right.parent = replaceNode;143 }144 replaceNode.parent.left = null;145 }146 if (!parentNode) {147 this.root = replaceNode;148 } else {149 parentNode[rmNodePos] = replaceNode;150 replaceNode.parent = removeNode.parent;151 }152 removeNode = null;153 return replaceNode;154 } // O(logn)155 // node nằm bên trái nhất của cây156 _leftMost(root) {157 let curNode = root;158 while (curNode.left) {159 curNode = curNode.left;160 }161 return curNode;162 }163}164const bst = new BinarySearchTree();165bst.add(10);166bst.add(5);167bst.add(1);168bst.add(8);169bst.add(9);170bst.add(7);171bst.add(6);172bst.remove(5);173// console.log(bst.root.left);174// console.log(JSON.stringify(bst))175// bst.breadthFirstTraversal();176// bst.preorderTraversal(bst.root);177// bst.inorderTraversal(bst.root);178// bst.postorderTraversal(bst.root);179// console.log(bst.search(100))...
BST.js
Source:BST.js
1class BSTNode {2 constructor(value = null, parent = null) {3 this.value = value;4 this.parent = parent;5 this.left = null;6 this.right = null;7 }8}9class BST {10 constructor() {11 this.root = undefined;12 }13 insert(value) {14 if (this.root === undefined) {15 this.root = new BSTNode(value);16 } else {17 let currentNode = this.root;18 while (currentNode) {19 if(value < currentNode.value){20 if (currentNode.left === null) {21 currentNode.left = new BSTNode(value, currentNode);22 } else {23 currentNode = currentNode.left;24 }25 }26 if(value > currentNode.value){27 if (currentNode.right === null) {28 currentNode.right = new BSTNode(value, currentNode);29 } else {30 currentNode = currentNode.right;31 }32 }33 if(value === currentNode.value) {34 return "Value already exists in tree!";35 }36 }37 }38 }39 search(value) {40 if(value === this.root.value) {41 return this.root;42 } else {43 let currentNode = this.root;44 while(currentNode) {45 if(value < currentNode.value) {46 currentNode = currentNode.left;47 } else if (value > currentNode.value) {48 currentNode = currentNode.right;49 } 50 51 if (value == currentNode.value) {52 return currentNode;53 }54 }55 return "This value isn't in the tree!";56 }57 }58 minValue(node = this.root) {59 while(node.left) {60 node = node.left;61 }62 return node;63 }64 remove(value) {65 let removeNode = search(value);66 67 if(typeof removeNode === "string") {68 return removeNode;69 } else {70 if(removeNode.left === null && removeNode.right === null){71 removeNode = null;72 } else if (removeNode.right && removeNode.left === null) {73 removeNode = removeNode.right;74 } else if (removeNode.left && removeNode.right === null) {75 removeNode = removeNode.left;76 } else if (removeNode.left && removeNode.right) {77 removeNode = this.minValue(removeNode.right)78 }79 return `${value} was removed successfully!`80 }81 }...
Using AI Code Generation
1var tree = new WebFXTree("Root");2var node1 = new WebFXTreeItem("Node 1");3var node2 = new WebFXTreeItem("Node 2");4var node3 = new WebFXTreeItem("Node 3");5var node4 = new WebFXTreeItem("Node 4");6var node5 = new WebFXTreeItem("Node 5");7var node6 = new WebFXTreeItem("Node 6");8var node7 = new WebFXTreeItem("Node 7");9var node8 = new WebFXTreeItem("Node 8");10var node9 = new WebFXTreeItem("Node 9");11var node10 = new WebFXTreeItem("Node 10");12var node11 = new WebFXTreeItem("Node 11");13var node12 = new WebFXTreeItem("Node 12");14var node13 = new WebFXTreeItem("Node 13");15var node14 = new WebFXTreeItem("Node 14");16var node15 = new WebFXTreeItem("Node 15");17var node16 = new WebFXTreeItem("Node 16");18var node17 = new WebFXTreeItem("Node 17");19var node18 = new WebFXTreeItem("Node 18");20var node19 = new WebFXTreeItem("Node 19");21var node20 = new WebFXTreeItem("Node 20");22var node21 = new WebFXTreeItem("Node 21");23var node22 = new WebFXTreeItem("Node 22");24var node23 = new WebFXTreeItem("Node 23");25var node24 = new WebFXTreeItem("Node 24");26var node25 = new WebFXTreeItem("Node 25");27var node26 = new WebFXTreeItem("Node 26");28var node27 = new WebFXTreeItem("Node 27");29var node28 = new WebFXTreeItem("Node 28");30var node29 = new WebFXTreeItem("Node 29");31var node30 = new WebFXTreeItem("Node 30");32var node31 = new WebFXTreeItem("Node 31");33var node32 = new WebFXTreeItem("Node 32");34var node33 = new WebFXTreeItem("Node 33");35var node34 = new WebFXTreeItem("Node 34");36var node35 = new WebFXTreeItem("Node
Using AI Code Generation
1var wptree = require("wptree");2var tree = new wptree();3tree.addNode(1, 0);4tree.addNode(2, 0);5tree.addNode(3, 1);6tree.addNode(4, 1);7tree.addNode(5, 2);8tree.addNode(6, 2);9tree.addNode(7, 3);10tree.addNode(8, 3);11tree.addNode(9, 4);12tree.addNode(10, 4);13tree.addNode(11, 5);14tree.addNode(12, 5);15tree.addNode(13, 6);16tree.addNode(14, 6);17tree.addNode(15, 7);18tree.addNode(16, 7);19tree.addNode(17, 8);20tree.addNode(18, 8);21tree.addNode(19, 9);22tree.addNode(20, 9);23tree.addNode(21, 10);24tree.addNode(22, 10);25tree.addNode(23, 11);26tree.addNode(24, 11);27tree.addNode(25, 12);28tree.addNode(26, 12);29tree.addNode(27, 13);30tree.addNode(28, 13);31tree.addNode(29, 14);32tree.addNode(30, 14);33tree.addNode(31, 15);34tree.addNode(32, 15);35tree.addNode(33, 16);36tree.addNode(34, 16);37tree.addNode(35, 17);38tree.addNode(36, 17);39tree.addNode(37, 18);40tree.addNode(38, 18);41tree.addNode(39, 19);42tree.addNode(40, 19);43tree.addNode(41, 20);44tree.addNode(42, 20);45tree.addNode(43, 21);46tree.addNode(44, 21);47tree.addNode(45, 22);48tree.addNode(46, 22);49tree.addNode(47, 23);50tree.addNode(48, 23);51tree.addNode(49, 24);52tree.addNode(50, 24);53tree.addNode(51, 25);54tree.addNode(52, 25);55tree.addNode(53, 26);56tree.addNode(54, 26);57tree.addNode(55, 27);58tree.addNode(56, 27);59tree.addNode(57, 28);60tree.addNode(58, 28);61tree.addNode(59, 29);62tree.addNode(60,
Using AI Code Generation
1var tree = new WPTree();2var node = new WPNode();3tree.addNode(node);4tree.removeNode(node);5function WPTree() {6 this.root = null;7}8WPTree.prototype.addNode = function(node) {9}10WPTree.prototype.removeNode = function(node) {11}12function WPNode() {13 this.parent = null;14 this.children = [];15}16WPNode.prototype.addChild = function(child) {17}18WPNode.prototype.removeChild = function(child) {
Using AI Code Generation
1var tree = new WPTree('tree');2var node = tree.getNodeById('node1');3tree.removeNode(node);4tree.removeNode('node2');5tree.removeNode('node3');6tree.removeNode('node4');7tree.removeNode('node5');8tree.removeNode('node6');9tree.removeNode('node7');10tree.removeNode('node8');11tree.removeNode('node9');12tree.removeNode('node10');13tree.removeNode('node11');14tree.removeNode('node12');15tree.removeNode('node13');16tree.removeNode('node14');17tree.removeNode('node15');18tree.removeNode('node16');19tree.removeNode('node17');20tree.removeNode('node18');21tree.removeNode('node19');22tree.removeNode('node20');23tree.removeNode('node21');24tree.removeNode('node22');25tree.removeNode('node23');26tree.removeNode('node24');27tree.removeNode('node25');28tree.removeNode('node26');29tree.removeNode('node27');30tree.removeNode('node28');31tree.removeNode('node29');32tree.removeNode('node30');33tree.removeNode('node31');34tree.removeNode('node32');35tree.removeNode('node33');36tree.removeNode('node34');37tree.removeNode('node35');38tree.removeNode('node36');39tree.removeNode('node37');40tree.removeNode('node38');41tree.removeNode('node39');42tree.removeNode('node40
Using AI Code Generation
1var tree = new WPTree();2tree.createTree(0, 0, 0, 0, 0);3tree.addNode(1, 1, 1, 1, 1, 1);4tree.addNode(2, 2, 2, 2, 2, 2);5tree.removeNode(1);6var tree = new WPTree();7tree.createTree(0, 0, 0, 0, 0);8tree.addNode(1, 1, 1, 1, 1, 1);9tree.addNode(2, 2, 2, 2, 2, 2);10tree.removeNode(2);11var tree = new WPTree();12tree.createTree(0, 0, 0, 0, 0);13tree.addNode(1, 1, 1, 1, 1, 1);14tree.addNode(2, 2, 2, 2, 2, 2);15tree.addNode(3, 3, 3, 3, 3, 3);16tree.removeNode(2);17var tree = new WPTree();18tree.createTree(0, 0, 0, 0, 0);19tree.addNode(1, 1, 1, 1, 1, 1);20tree.addNode(2, 2, 2, 2, 2, 2);21tree.addNode(3, 3, 3, 3, 3, 3);22tree.removeNode(1);23var tree = new WPTree();24tree.createTree(0, 0, 0, 0, 0);25tree.addNode(1, 1, 1, 1, 1, 1);26tree.addNode(2, 2, 2, 2, 2, 2);27tree.addNode(3, 3, 3, 3, 3, 3);28tree.removeNode(3);
Using AI Code Generation
1var tree = new WPTree('tree1');2tree.removeNode('node1');3var tree = new WPTree('tree1');4tree.removeAllNodes();5var tree = new WPTree('tree1');6if(tree.isEmpty())7{8 alert('Tree is empty');9}10{11 alert('Tree is not empty');12}13var tree = new WPTree('tree1');14if(tree.nodeExists('node1'))15{16 alert('Node exists');17}18{19 alert('Node does not exist');20}21var tree = new WPTree('tree1');22alert('Number of nodes in tree: ' + tree.getNumberOfNodes());23var tree = new WPTree('tree1');24alert('Number of nodes in tree: ' + tree.getNumberOfNodes());25var tree = new WPTree('tree1');26alert('Value of node: ' + tree.getNodeValue('node1'));
Using AI Code Generation
1function removeNode() {2 var node = document.getElementById("node").value;3 var tree = document.getElementById("tree");4 var nodeToRemove = tree.getNodeById(node);5 tree.removeNode(nodeToRemove);6 tree.update();7}8function addNode() {9 var node = document.getElementById("node").value;10 var tree = document.getElementById("tree");11 var nodeToAdd = document.createElement("div");12 nodeToAdd.setAttribute("id", node);13 nodeToAdd.innerHTML = node;14 tree.addNode(nodeToAdd);15 tree.update();16}17function updateTree() {18 var tree = document.getElementById("tree");19 tree.update();20}21function collapseTree() {22 var tree = document.getElementById("tree");23 tree.collapse();24}25function expandTree() {26 var tree = document.getElementById("tree");27 tree.expand();28}29function expandTreeToLevel() {30 var tree = document.getElementById("tree");31 var level = document.getElementById("level").value;32 tree.expandToLevel(level);33}34function getNodeById() {35 var node = document.getElementById("node").value;36 var tree = document.getElementById("tree");37 var nodeToGet = tree.getNodeById(node);38 alert(nodeToGet.innerHTML);39}40function getNodeByIndex() {41 var node = document.getElementById("node").value;42 var tree = document.getElementById("tree");43 var nodeToGet = tree.getNodeByIndex(node);44 alert(nodeToGet.innerHTML);45}46function getNumberOfNodes() {47 var tree = document.getElementById("tree");48 var numberOfNodes = tree.getNumberOfNodes();49 alert(numberOfNodes);50}51function getNodeIndex() {52 var node = document.getElementById("node").value;53 var tree = document.getElementById("tree");54 var nodeToGet = tree.getNodeById(node);55 var nodeIndex = tree.getNodeIndex(nodeToGet);56 alert(nodeIndex);57}58function getNodeParent() {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!