const createTreeNode = (node) => { let res = new TreeNode(node); return res; };
// the compare of sort const compare = (properties) => { return(a, b) => { if (a[properties[0]] !== b[properties[0]]) { return a[properties[0]] - b[properties[0]]; } else { return a[properties[1]] - b[properties[1]]; } }; };
const arrayToTree = (arr) => { // judge the boundary conditions if (!arr || !arr.length) return; // sort the data according to rules, pid -> id arr.sort(compare(["pid", "id"]));
/** * root * curNodes: record which nodes have been traversed * curParNode: record who the current parent node is */ let root = createTreeNode(arr.shift()), curNodes = [root], curParNode = curNodes.shift();
for (let node of arr) { let curNode = createTreeNode(node); // Mark the curNode(current node) has been traversed, put it into the curNodes curNodes.push(curNode);
// Determine whether the parent node of curNode(current node) is the curParNode(current parent node) while (curParNode.id !== curNode.pid) { // if not, changed the curParNode curParNode = curNodes.shift(); } // put the children node into the curParNode's children array curParNode.children.push(curNode); } return root; };
console.log(arrayToTree(data));
Algorithm
Sort
1 2 3 4 5 6
const swap = (arr, a, b) => { let temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; return arr; };
BubbleSort
If a larger value is encountered, pass it backward.
1 2 3 4 5 6 7 8 9 10 11 12
const bubbleSort = (arr) => { let len = arr.length; while (len--) { for (let i = 0; i < len; i++) { // Tow adjacent elements compare and swap. if (arr[i] > arr[i + 1]) swap(arr, i, i + 1); // Current element and the last element compare and swap. // if(arr[i] > arr[len]) swap(arr, i, len) } } return arr; };
SelectSort
Select the maximum value in current scope and place it last.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
const selectSort = (arr) => { let len = arr.length;
while (len--) { let maxIndex = 0, maxVal = arr[0];
// because len--, here sholud is len+1 for (let i = 0; i < len + 1; i++) { if (arr[i] > maxVal) { maxVal = arr[i]; maxIndex = i; } swap(arr, maxIndex, len); } }
return arr; };
InsertSort
From the beginning of traversal, asuume that the n-1 has been sorted, and compare and insert from back to front.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const insertSort = (arr) => { let len = arr.length;
// Assume the first value is ordered. for (let i = 1; i < len; i++) { let j = i, temp = arr[j]; // Find the current correct position. while (j > 0 && arr[j] < temp) { arr[j] = arr[j - 1]; j--; } arr[j] = temp; }
const shellSort = (arr) => { let len = arr.length; let gap = Math.floor(len / 2);
while (gap) { // shellSort is start from the second part, so our i start from gap to the end of array. // This gap is rounded down, so there may be extra parts in lower half. for (let i = gap; i < len; i++) { let j = i, temp = arr[i];
// find the pivot let pivot = median(arr, left, right);
// double pointers traversal exchange value let i = left; let j = right - 1; while (true) { while (i < j && arr[i] <= pivot) i++; while (i < j && arr[j] >= pivot) j--; if (i >= j) break; swap(arr, i, j); } swap(arr, j, right - 1);