-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracketTreeRm.js
50 lines (47 loc) · 1.61 KB
/
bracketTreeRm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*let leftChildIndex = (i) => 2*i+1;
let rightChildIndex = (i) => 2 * i + 2;
function TreeConstructor(strArr) {
/*let leftChildIndex = (i) => 2 * i;
let rightChildIndex = (i) => 2 * i + 1;
let parentIndex = (i) => Math.floor(i / 2);*/
//1. Parents have at Most 2 Childern Parent {parent : [child1, child2]}
// 2. Child have at Most 1 Parents Child childern = {child : parent}
// replace bracket
let parentOfChild = {};
let childernOfParent = {};
console.log(strArr);
let replacer = (match, p1, p2) =>{
return p1 ? "[" : "]"
}
let pairs =strArr.replace(/([(])|([)])/g, replacer);
pairs = pairs.split(",")
// code goes here
console.log(pairs);
let isTree = true;
for(let i=0; i< pairs.length; i++){
let parent = pairs[i][1];
let child = pairs[i][0];
// if parent of child already exits return false
if(!parentOfChild[child]){ // parents does not exist
parentOfChild[child] = parent; // assign it
if(!childernOfParent[parent]){ //if there are no childern of the parent
childernOfParent[parent] = [child]; // assign the first child
} else if(!childernOfParent[parent]){
childernOfParent[parent] = child;
} else {
console.log("Too Many Childern");
isTrue = false;
}
} else {
console.log("Too many parents")
isTrue = false;
}
}
console.log('parentOfChild : ');
console.log(parentOfChild);
console.log('childernOfParent : ');
console.log(childernOfParent);
return isTree
}
// keep this function call here
console.log(TreeConstructor(readline()));