Deep copy

A deep copy is a complete copy of an object from the heap, creating a new area of the heap to store the new object, and modifying the new object does not affect the original object.

Deep copy code:

Let arr1 = [1, 2, 3, {the username: 'ywt}]. let arr2=JSON.parse(JSON.stringify(arr1)); arr2[3].username='zgy'; console.log(arr1,arr2); / / arr1 arr2 / / (4) [1, 2, 3, {...}] (4) [1, 2, 3, {...}] / / 0:0:1 1 / / 1:2:1/2/2, 3, 2, 3 / / 3: {username: "ywt"} 3: {username: "zgy"} // length: 4 length: 4 // __proto__: Array(0) __proto__: Array(0)Copy the code

Shallow copy

A shallow copy creates a new object with an exact copy of the original object’s property values. If the property is a primitive type, it copies the value of the primitive type, and if the property is a reference type, it copies the memory address, so if one object changes the address, it affects the other object.

Shallow copy code:

let obj1={ person:{ name:'YY', age:21 }, sports:'basketball' }; let obj2=Object.assign({},obj1); obj2.person.name='ZZ'; obj2.sports='football'; console.log(obj1); //person: {name: "ZZ", age: 21}sports: "basketball"Copy the code

In short, a deep copy creates an identical object. The new object does not share memory with the original object, and changes to the new object do not change to the original object. Shallow copy copies only Pointers to an object, not the object itself, and the old and new objects still share the same memory.

Depth-first traversal (DFS)

Depth-first search after accessing a vertex during the search, recursively accesses all of the unvisited adjacent vertices of that vertex.

  • If the adjacent node is not visited, mark it and enter the recursion to find its unmarked adjacent nodes; If the node is marked for access, it goes back to the parent node, looks for its unmarked adjacent nodes, and then recurses until all vertices connected to the starting point are marked for access.
  • If all nodes are marked for access, end; Conversely, if there is still a node that has not been accessed, the next recursive lookup needs to take that node as the vertex until all points have been marked for access.

Breadth-first traversal (BFS)

Breadth-first traversal, also known as “width first search” or “horizontal first search”, or BFS for short. Its realization idea is: starting from a point, find out its adjacent node into the queue and mark, and then pop out the first node from the queue, find its adjacent node has not been visited into the queue, until all the nodes have been visited the adjacent point; If there are still unaccessed points in the figure, select another unaccessed point and perform the same operation until all nodes in the figure are accessed.