awesome-interview/assets/js/667a1a38.a4c2fee2.js

1 line
16 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[3253],{3905:function(e,n,t){t.d(n,{Zo:function(){return p},kt:function(){return s}});var r=t(7294);function i(e,n,t){return n in e?Object.defineProperty(e,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[n]=t,e}function a(e,n){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);n&&(r=r.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),t.push.apply(t,r)}return t}function o(e){for(var n=1;n<arguments.length;n++){var t=null!=arguments[n]?arguments[n]:{};n%2?a(Object(t),!0).forEach((function(n){i(e,n,t[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):a(Object(t)).forEach((function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}))}return e}function l(e,n){if(null==e)return{};var t,r,i=function(e,n){if(null==e)return{};var t,r,i={},a=Object.keys(e);for(r=0;r<a.length;r++)t=a[r],n.indexOf(t)>=0||(i[t]=e[t]);return i}(e,n);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)t=a[r],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(i[t]=e[t])}return i}var d=r.createContext({}),c=function(e){var n=r.useContext(d),t=n;return e&&(t="function"==typeof e?e(n):o(o({},n),e)),t},p=function(e){var n=c(e.components);return r.createElement(d.Provider,{value:n},e.children)},u={inlineCode:"code",wrapper:function(e){var n=e.children;return r.createElement(r.Fragment,{},n)}},m=r.forwardRef((function(e,n){var t=e.components,i=e.mdxType,a=e.originalType,d=e.parentName,p=l(e,["components","mdxType","originalType","parentName"]),m=c(t),s=i,k=m["".concat(d,".").concat(s)]||m[s]||u[s]||a;return t?r.createElement(k,o(o({ref:n},p),{},{components:t})):r.createElement(k,o({ref:n},p))}));function s(e,n){var t=arguments,i=n&&n.mdxType;if("string"==typeof e||i){var a=t.length,o=new Array(a);o[0]=m;var l={};for(var d in n)hasOwnProperty.call(n,d)&&(l[d]=n[d]);l.originalType=e,l.mdxType="string"==typeof e?e:i,o[1]=l;for(var c=2;c<a;c++)o[c]=t[c];return r.createElement.apply(null,o)}return r.createElement.apply(null,t)}m.displayName="MDXCreateElement"},5838:function(e,n,t){t.r(n),t.d(n,{frontMatter:function(){return l},contentTitle:function(){return d},metadata:function(){return c},toc:function(){return p},default:function(){return m}});var r=t(7462),i=t(3366),a=(t(7294),t(3905)),o=["components"],l={sidebar_label:"\u7f16\u7801\uff1a\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784",sidebar_position:10},d="\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784",c={unversionedId:"book3/coding-arr-to-tree",id:"book3/coding-arr-to-tree",isDocsHomePage:!1,title:"\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784",description:"\u9700\u6c42\u6765\u6e90\u5206\u6790\uff1a",source:"@site/docs/book3/coding-arr-to-tree.md",sourceDirName:"book3",slug:"/book3/coding-arr-to-tree",permalink:"/awesome-interview/book3/coding-arr-to-tree",tags:[],version:"current",sidebarPosition:10,frontMatter:{sidebar_label:"\u7f16\u7801\uff1a\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784",sidebar_position:10},sidebar:"tutorialSidebar",previous:{title:"\u7f51\u7edc\uff1aHTTP2 \u548c HTTP1.1 \u7684\u5bf9\u6bd4",permalink:"/awesome-interview/book3/network-http-1-2"},next:{title:"\u7b97\u6cd5\uff1a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u7b2c k \u4e2a\u7ed3\u70b9",permalink:"/awesome-interview/book3/algorithm-binary-tree-k"}},p=[{value:"\u5178\u578b\u9898\u76ee",id:"\u5178\u578b\u9898\u76ee",children:[]},{value:"\u89e3\u6cd5",id:"\u89e3\u6cd5",children:[{value:"\u89e3\u6cd5\u4e00",id:"\u89e3\u6cd5\u4e00",children:[]},{value:"\u89e3\u6cd5\u4e8c",id:"\u89e3\u6cd5\u4e8c",children:[]},{value:"\u89e3\u6cd5\u4e8c\u53d8\u4f53",id:"\u89e3\u6cd5\u4e8c\u53d8\u4f53",children:[]},{value:"\u4ee3\u7801\u6f14\u793a\u53ca\u603b\u7ed3",id:"\u4ee3\u7801\u6f14\u793a\u53ca\u603b\u7ed3",children:[]}]}],u={toc:p};function m(e){var n=e.components,t=(0,i.Z)(e,o);return(0,a.kt)("wrapper",(0,r.Z)({},u,t,{components:n,mdxType:"MDXLayout"}),(0,a.kt)("h1",{id:"\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784"},"\u5c06\u5217\u8868\u8fd8\u539f\u4e3a\u6811\u72b6\u7ed3\u6784"),(0,a.kt)("p",null,"\u9700\u6c42\u6765\u6e90\u5206\u6790\uff1a"),(0,a.kt)("p",null,"\u5728\u9700\u8981\u5b58\u50a8\u6811\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u822c\u7531\u4e8e\u4f7f\u7528\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982 MySQL\uff09\uff0c\u662f\u4ee5\u7c7b\u4f3c\u8868\u683c\u7684\u6241\u5e73\u5316\u65b9\u5f0f\u5b58\u50a8\u6570\u636e\u3002\u56e0\u6b64\u4e0d\u4f1a\u76f4\u63a5\u5c06\u6811\u7ed3\u6784\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u901a\u5e38\u662f\u901a\u8fc7\u90bb\u63a5\u8868\u3001\u8def\u5f84\u679a\u4e3e\u3001\u5d4c\u5957\u96c6\u6216\u95ed\u5305\u8868\u6765\u5b58\u50a8\u3002"),(0,a.kt)("p",null,"\u5176\u4e2d\uff0c\u90bb\u63a5\u8868\u662f\u6700\u5e38\u7528\u7684\u65b9\u6848\u4e4b\u4e00\uff0c\u5176\u5b58\u50a8\u6a21\u578b\u5982\u4e0b\uff1a"),(0,a.kt)("table",null,(0,a.kt)("thead",{parentName:"table"},(0,a.kt)("tr",{parentName:"thead"},(0,a.kt)("th",{parentName:"tr",align:null},"id"),(0,a.kt)("th",{parentName:"tr",align:null},"pid"),(0,a.kt)("th",{parentName:"tr",align:null},"data"))),(0,a.kt)("tbody",{parentName:"table"},(0,a.kt)("tr",{parentName:"tbody"},(0,a.kt)("td",{parentName:"tr",align:null},"1"),(0,a.kt)("td",{parentName:"tr",align:null},"0"),(0,a.kt)("td",{parentName:"tr",align:null},"a")),(0,a.kt)("tr",{parentName:"tbody"},(0,a.kt)("td",{parentName:"tr",align:null},"2"),(0,a.kt)("td",{parentName:"tr",align:null},"1"),(0,a.kt)("td",{parentName:"tr",align:null},"b")),(0,a.kt)("tr",{parentName:"tbody"},(0,a.kt)("td",{parentName:"tr",align:null},"3"),(0,a.kt)("td",{parentName:"tr",align:null},"1"),(0,a.kt)("td",{parentName:"tr",align:null},"c")))),(0,a.kt)("p",null,"\u8be5\u6a21\u578b\u4ee3\u8868\u4e86\u5982\u4e0b\u7684\u6811\u72b6\u7ed3\u6784\uff1a"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},"{\n id: 1,\n pid: 0,\n data: 'a',\n children: [\n {id: 2, pid: 1, data: 'b'},\n {id: 3, pid: 1, data: 'c'},\n ]\n}\n")),(0,a.kt)("p",null,"\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u4f1a\u4ea4\u7ed9\u5e94\u7528\u7a0b\u5e8f\u6765\u6784\u9020\u6811\u7ed3\u6784\u3002"),(0,a.kt)("h2",{id:"\u5178\u578b\u9898\u76ee"},"\u5178\u578b\u9898\u76ee"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'const list = [\n { pid: null, id: 1, data: "1" },\n { pid: 1, id: 2, data: "2-1" },\n { pid: 1, id: 3, data: "2-2" },\n { pid: 2, id: 4, data: "3-1" },\n { pid: 3, id: 5, data: "3-2" },\n { pid: 4, id: 6, data: "4-1" },\n];\n')),(0,a.kt)("h2",{id:"\u89e3\u6cd5"},"\u89e3\u6cd5"),(0,a.kt)("h3",{id:"\u89e3\u6cd5\u4e00"},"\u89e3\u6cd5\u4e00"),(0,a.kt)("p",null,"\u9012\u5f52\u89e3\u6cd5\uff1a\u8be5\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u4ece\u6839\u8282\u70b9\u51fa\u53d1\uff0c\u6bcf\u4e00\u8f6e\u8fed\u4ee3\u627e\u5230 pid \u4e3a\u5f53\u524d\u8282\u70b9 id \u7684\u8282\u70b9\uff0c\u4f5c\u4e3a\u5f53\u524d\u8282\u70b9\u7684 children\uff0c\u9012\u5f52\u8fdb\u884c\u3002"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'function listToTree(\n list,\n pid = null,\n { idName = "id", pidName = "pid", childName = "children" } = {}\n) {\n return list.reduce((root, item) => {\n // \u904d\u5386\u6bcf\u4e00\u9879\uff0c\u5982\u679c\u8be5\u9879\u4e0e\u5f53\u524d pid \u5339\u914d\uff0c\u5219\u9012\u5f52\u6784\u5efa\u8be5\u9879\u7684\u5b50\u6811\n if (item[pidName] === pid) {\n const children = listToTree(list, item[idName]);\n if (children.length) {\n item[childName] = children;\n }\n return [...root, item];\n }\n return root;\n }, []);\n}\n')),(0,a.kt)("p",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\uff1a\u6700\u574f\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u68f5\u6811\u9000\u5316\u4e3a\u94fe\u8868\uff0c\u4e14\u5012\u5e8f\u6392\u5217\u3002\u6bcf\u4e00\u8f6e\u8fed\u4ee3\u9700\u8981\u5728\u6700\u540e\u9762\u624d\u627e\u5230\u76ee\u6807\u8282\u70b9\u3002\u5047\u8bbe\u6709 n \u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u603b\u8fed\u4ee3\u6b21\u6570\u4e3a ",(0,a.kt)("inlineCode",{parentName:"p"},"n+(n-1) + (n-2) + ... + 1"),"\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n^2)\u3002"),(0,a.kt)("h3",{id:"\u89e3\u6cd5\u4e8c"},"\u89e3\u6cd5\u4e8c"),(0,a.kt)("p",null,"\u8fed\u4ee3\u6cd5\uff1a\u5229\u7528\u5bf9\u8c61\u5728 js \u4e2d\u662f\u5f15\u7528\u7c7b\u578b\u7684\u539f\u7406\u3002\u7b2c\u4e00\u8f6e\u904d\u5386\u5c06\u6240\u6709\u7684\u9879\uff0c\u5c06\u9879\u7684 id \u4e0e\u9879\u81ea\u8eab\u5728\u5b57\u5178\u4e2d\u5efa\u7acb\u6620\u5c04\uff0c\u4e3a\u540e\u9762\u7684\u7acb\u5373\u8bbf\u95ee\u505a\u597d\u51c6\u5907\u3002\n\u7531\u4e8e\u64cd\u4f5c\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u5bf9\u8c61\uff0c\u7ed3\u679c\u96c6 root \u4e2d\u7684\u6bcf\u4e00\u9879\u548c\u5b57\u5178\u4e2d\u76f8\u540c id \u5bf9\u5e94\u7684\u9879\u5b9e\u9645\u4e0a\u6307\u5411\u7684\u662f\u540c\u4e00\u5757\u6570\u636e\u3002\u540e\u7eed\u7684\u904d\u5386\u4e2d\uff0c\u76f4\u63a5\u5bf9\u5b57\u5178\u8fdb\u884c\u64cd\u4f5c\uff0c\u64cd\u4f5c\u540c\u65f6\u4f1a\u53cd\u5e94\u5230 root \u4e2d\u3002"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'function listToTree(\n list,\n rootId = null,\n { idName = "id", pidName = "pid", childName = "children" } = {}\n) {\n const record = {}; // \u7528\u7a7a\u95f4\u6362\u65f6\u95f4\uff0c\u7528\u4e8e\u5c06\u6240\u6709\u9879\u7684 id \u53ca\u81ea\u8eab\u8bb0\u5f55\u5230\u5b57\u5178\u4e2d\n const root = [];\n\n list.forEach((item) => {\n record[item[idName]] = item; // \u8bb0\u5f55 id \u4e0e\u9879\u7684\u6620\u5c04\n item[childName] = [];\n });\n\n list.forEach((item) => {\n if (item[pidName] === rootId) {\n root.push(item);\n } else {\n // \u7531\u4e8e\u6301\u6709\u7684\u662f\u5f15\u7528\uff0crecord \u4e2d\u76f8\u5173\u5143\u7d20\u7684\u4fee\u6539\uff0c\u4f1a\u5728\u53cd\u6620\u5728 root \u4e2d\u3002\n record[item[pidName]][childName].push(item);\n }\n });\n\n return root;\n}\n')),(0,a.kt)("p",null,"record \u5b57\u5178 \u4e0e root \u7ed3\u679c\u96c6\u7684\u53c2\u8003\u5185\u5b58\u5f15\u7528\u5173\u7cfb\u5982\u56fe\u6240\u793a\uff1a"),(0,a.kt)("p",null,(0,a.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/4338052/129750250-f447f56b-4342-4130-b82f-af017bd7c3c7.png",alt:"image"})),(0,a.kt)("p",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\uff1a\u7ecf\u5386\u4e86\u4e24\u8f6e\u8fed\u4ee3\uff0c\u5047\u8bbe\u6709 n \u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u603b\u8fed\u4ee3\u6b21\u6570\u4e3a ",(0,a.kt)("inlineCode",{parentName:"p"},"n + n"),"\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002"),(0,a.kt)("h3",{id:"\u89e3\u6cd5\u4e8c\u53d8\u4f53"},"\u89e3\u6cd5\u4e8c\u53d8\u4f53"),(0,a.kt)("h4",{id:"\u53d8\u4f53\u4e00"},"\u53d8\u4f53\u4e00"),(0,a.kt)("p",null,"\u5728\u89e3\u6cd5\u4e8c\u7684\u57fa\u7840\u4e0a\uff0c\u5c06\u4e24\u8f6e\u8fed\u4ee3\u5408\u5e76\u6210\u4e00\u8f6e\u8fed\u4ee3\u3002\u91c7\u7528\u8fb9\u8fed\u4ee3\u8fb9\u6784\u5efa\u7684\u65b9\u5f0f\uff1a"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'function listToTree(\n list,\n rootId = null,\n { idName = "id", pidName = "pid", childName = "children" } = {}\n) {\n const record = {}; // \u7528\u7a7a\u95f4\u6362\u65f6\u95f4\uff0c\u7528\u4e8e\u5c06\u6240\u6709\u9879\u7684 id \u53ca\u81ea\u8eab\u8bb0\u5f55\u5230\u5b57\u5178\u4e2d\n const root = [];\n\n list.forEach((item) => {\n const id = item[idName];\n const parentId = item[pidName];\n\n // \u5982\u679c\u8be5\u9879\u4e0d\u5728 record \u4e2d\uff0c\u5219\u653e\u5165 record\u3002\u5982\u679c\u8be5\u9879\u5df2\u5b58\u5728 \uff08\u53ef\u80fd\u7531\u522b\u7684\u9879\u6784\u5efa pid \u52a0\u5165\uff09\uff0c\u5219\u5408\u5e76\u8be5\u9879\u548c\u5df2\u5b58\u5728\u7684\u6570\u636e\n record[id] = !record[id] ? item : { ...item, ...record[id] };\n\n const treeItem = record[id];\n\n if (parentId === rootId) {\n // \u5982\u679c\u662f\u6839\u5143\u7d20\uff0c\u5219\u52a0\u5165\u7ed3\u679c\u96c6\n root.push(treeItem);\n } else {\n // \u5982\u679c\u7236\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u521d\u59cb\u5316\u7236\u5143\u7d20\n if (!record[parentId]) {\n record[parentId] = {};\n }\n // \u5982\u679c\u7236\u5143\u7d20\u6ca1\u6709 children, \u5219\u521d\u59cb\u5316\n if (!record[parentId][childName]) {\n record[parentId][childName] = [];\n }\n\n record[parentId][childName].push(treeItem);\n }\n });\n\n return root;\n}\n')),(0,a.kt)("p",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\uff1a\u7ecf\u5386\u4e86\u4e00\u8f6e\u8fed\u4ee3\uff0c\u5047\u8bbe\u6709 n \u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002"),(0,a.kt)("h4",{id:"\u53d8\u4f53\u4e8c"},"\u53d8\u4f53\u4e8c"),(0,a.kt)("p",null,"record \u5b57\u5178\u4ec5\u8bb0\u5f55 id \u4e0e children \u7684\u6620\u5c04\u5173\u7cfb\uff0c\u4ee3\u7801\u66f4\u7cbe\u7b80\uff1a"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-js"},'function listToTree(\n list,\n rootId = null,\n { idName = "id", pidName = "pid", childName = "children" } = {}\n) {\n const record = {}; // \u7528\u7a7a\u95f4\u6362\u65f6\u95f4\uff0c\u4ec5\u7528\u4e8e\u8bb0\u5f55 children\n const root = [];\n\n list.forEach((item) => {\n const newItem = Object.assign({}, item); // \u5982\u6709\u9700\u8981\uff0c\u53ef\u4ee5\u590d\u5236 item \uff0c\u53ef\u4ee5\u4e0d\u5f71\u54cd list \u4e2d\u539f\u6709\u7684\u5143\u7d20\u3002\n const id = newItem[idName];\n const parentId = newItem[pidName];\n\n // \u5982\u679c\u5f53\u524d id \u7684 children \u5df2\u5b58\u5728\uff0c\u5219\u52a0\u5165 children \u5b57\u6bb5\u4e2d\uff0c\u5426\u5219\uff0c\u521d\u59cb\u5316 children\n // item \u4e0e record[id] \u5f15\u7528\u540c\u4e00\u4efd children\uff0c\u540e\u7eed\u8fed\u4ee3\u4e2d\u66f4\u65b0 record[parendId] \u5c31\u4f1a\u53cd\u6620\u5230 item \u4e2d\n newItem[childName] = record[id] ? record[id] : (record[id] = []);\n\n if (parentId === rootId) {\n root.push(newItem);\n } else {\n if (!record[parentId]) {\n record[parentId] = [];\n }\n record[parentId].push(newItem);\n }\n });\n\n return root;\n}\n')),(0,a.kt)("p",null,"\u65f6\u95f4\u590d\u6742\u5ea6\u5206\u6790\uff1a\u7ecf\u5386\u4e86\u4e00\u8f6e\u8fed\u4ee3\uff0c\u5047\u8bbe\u6709 n \u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(n)\u3002"),(0,a.kt)("h3",{id:"\u4ee3\u7801\u6f14\u793a\u53ca\u603b\u7ed3"},"\u4ee3\u7801\u6f14\u793a\u53ca\u603b\u7ed3"),(0,a.kt)("p",null,(0,a.kt)("a",{parentName:"p",href:"https://codesandbox.io/s/practical-sun-vo4yi?file=/src/App.js"},"Code Sandbox - List to Tree")),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},"\u9012\u5f52\u6cd5\uff1a\u5728\u6570\u636e\u91cf\u589e\u5927\u7684\u65f6\u5019\uff0c\u6027\u80fd\u4f1a\u6025\u5267\u4e0b\u964d\u3002\u597d\u5904\u662f\u53ef\u4ee5\u5728\u6784\u5efa\u6811\u7684\u8fc7\u7a0b\u4e2d\uff0c\u7ed9\u8282\u70b9\u6dfb\u52a0\u5c42\u7ea7\u4fe1\u606f\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u8fed\u4ee3\u6cd5\uff1a\u901f\u5ea6\u5feb\u3002\u4f46\u5982\u679c\u60f3\u8981\u4e0d\u5f71\u54cd\u6e90\u6570\u636e\uff0c\u9700\u8981\u5728 record \u4e2d\u5b58\u50a8\u4e00\u4efd\u590d\u5236\u7684\u6570\u636e\uff0c\u4e14\u65e0\u6cd5\u5728\u6784\u5efa\u7684\u8fc7\u7a0b\u4e2d\u5f97\u77e5\u8282\u70b9\u7684\u5c42\u7ea7\u4fe1\u606f\uff0c\u9700\u8981\u6784\u5efa\u5b8c\u540e\u518d\u6b21\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u83b7\u53d6\u3002"),(0,a.kt)("li",{parentName:"ul"},"\u8fed\u4ee3\u6cd5\u53d8\u4f53\u4e00\uff1a\u6309\u9700\u521b\u5efa children\uff0c\u53ef\u4ee5\u907f\u514d\u7a7a\u7684 children \u5217\u8868\u3002")))}m.isMDXComponent=!0}}]);