Data Structures & Algorithms To Know in Javascript

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How to invert a binary search tree

"90% of our engineers use the software you wrote, but you can't invert a binary search tree so **** off" 1. set a function called invertTree which will take in the head or root of the tree. 2. set a temporary varaible to the left or right side of the tree. 3. set head.left to head.right 4. set head.right to temp 5. recursively call the function on the head.left and head.right 6. return the head

How do you create a queue?

1. A queue can also be created using an array, and using various array methods such as shift and unshift to get the FIFO part of the queue. 2. The other way is similar to creating a Node class similar to creating a linked list.

How do you create a Stack?

1. A stack can be created with an array, and using various array methods such as the pop method. 2. The other way involves creating a Node class similar to creating a linked list, then creating a stack class.

Inserting inside a Binary search tree

1. First we need a new node to take in the data. 2. if there is no root, we set it null, and the new node becomes the root 3. else it this.insertnode to the new node. we also need a helper function 1.create a helper function which takes in the node and the new node 2. if the new node is less than the node and the left, then insert to the left 3. if it is greater insert to the right.

get the index in a LL

1. Get takes in an index value 2. We check if the index is less than 0 or the index is greater than the length, which we return null 3.have a counter which represents the number index 4. let current equal the head 5. while the counter does not equal index, let the cur equal current.next and increase the counter 6. return the current value

Inserting a value in a LL

1. Insert takes in a value and its index. (that being said you need the index method written out) 2. if the index is less than 0 or greater than the length you return false 3. if the index equals thelength then you push in the value 4. if the index equals 0 you need to unshift the value 5. otherwise set the prev value to this.get(index-1) 6. set this.get to the new node 7. increase the length 8. return true

Removing a Node from LL

1. Remove takes in an index. 2. check if the index is less than 0 and if the index is greater than the length, if it is return undefined 3. if it is the length - 1 pop it 4. if the index is zero then shift 5. set a previous variable with the get method of the index - 1 6. set a removed variable to the prev.next (which just means the current variable) 7. set prev.next to removed.next 8. decrement the length 9. return the removed value

Set a value in a LL

1. Set takes in a value and an index 2. we need a get function written out in order to do this. so we also declare a variable found to equal this.get(index) 3. if not found we return false 4. otherwise, the found value equals value 5. return true

How to create a push method in a Linked List

1. The push value will take in a value. 2. create a new node which takes in the value. 3. check if there is a head, if not, the head will become the node, and same with the tail (if there is only one node) 4. otherwise set the tail.next to node, and set the tail to node 5/ increase the length.

Unshifting in a Linked List

1. This will be taking in a value with the creation of a new Node. 2.If there is no head, the head becomes a new node and the tail is also the head 3.otherwise, the newnode next value becomes the head 4. the head becomes the new node 5. the length increases 6. return this.

DFS Preorder

1. set a visited array 2. set the current value to the root 3. create a helper function which takes in the node 4. push the value of the node into the visited array 5. if the left node traverse to that node 6. if there is a right node traverse to the right node 7. call the traverse function on current value 8 return the visited array

Find a node in a Binary Search Tree

1. the find method will take in a value 2. it will return our helper function with data and the root *create a helper function* 1. the helper function will take in the data and the root 2. check if there is no root, if there isn't return false 3. check if the data is less than the root, which will search the left 4. check if the data is greater than which will go to the right 5. return true

Singly Linked Lists

A Singly Linked list is a linear data structure, which consists of nodes. Each node contains a data and a pointer which points to another node.

Merge Sort

A divide and conquer sort algorithm which requires a helper function to be written. Pseudocode: 1. First you need to write out the merge helper function where you take in two arrays (remember this is a divide and conquer algo). 2. Declare a results variable, and two pointers at 0. 3. Using a while loop check if the first pointer and the second pointer are less than the array length. 4. Check if arr2 is greater than arr1, if it is push into results 5. same above with 6.Checking for edge cases, using a while loop check if i is less than arr1.length, repeat with arr[j MergeSort actual code 1. Mergesort will take in an array 2. Similar to Binary search, declare a mid point using Math.floor and divide the array length by 2 3. set a left point calling mergesort recursively using arr.slice 4. set a right point calling mergesort recursively using arr.slice 5. return by calling the merge function Time - O(nlogn) Space - O(n)

What is a HashMap?

A hash map is a data structure that can map keys to values. It has a constant look up time of O(1)

When would you use a linked list over an array?

A linked list is better to be used when you need to be fast with constant time insert and deletions, and you don't know how many items will be in the list as well as random access. Arrays are better for when you know the number of elements ahead of time. Also, memory is a concern.

What is recursion?

A programming technique that causes a method to call itself in order to compute a result.

How do you know if a tree is a binary search tree?

A tree is a binary search tree when it is made up of nodes where in the root, the left value is less than the current root, and the right value is greater than. Same thing can be applied to its children nodes.

Quick Sort

An inplace sorting algorithm with a run time of O(nlogN) Similar to merge sort it is a divide and conquer algorithm using a pivot point. Pseudocode: 1. First check the length of the array, if it is equal to 0 return an empty array 2. next set a left list, right list, and pivot point. 3. Iterate through the array, setting the i + 1 of the pivot point. 4. For those numbers that are LESS than the pivot push to the left array 5. For those numbers that are GREATER than the pivot, push to the right array 6. Recrusively call quicksort on the left array while concating the pivot and recursively calling quicksort on the right array.

What is Binary Search and the time and space complexity?

Binary Search is a divide and conquer algorithm, which requires the list to be sorted Pseudocoded version: 1. set a start point at 0, and the end point being the length of the array - 1. 2. find the mid point of the array by adding the start and end point and dividing it by 2 3. check if the element you are looking for is at the middle, return true. 4. Set a while loop checking to make sure that the start is less than the end 5. else look in the right or left half. 6. if not found then return false Time and Space: O(logN) O(1)

How to do BFS

Breadth first search: Breadth first search goes from the top to the bottom. 1. create an array for those that were already visited. 2. create a queue 3. set a current value which starts at the root. 4. push the current value 5. use a while loop while there is a queue, and set the current value to queue.shift() 6. push the current value into the visited array 7. do an if statement for the left node and push it in 8. do an if statement to push the right value in 9. return the visited array

Bubble Sort Algorithm and the time and space complexity

Bubble sort is the slowest sorting algorithm with a O(n^2) run time. Pseudocoded version: 1. create a first loop, then create a second loop. 2. during the second loop you would check and see if the index of that, ex: arr[j] is less than the following index arr[j+1] 3. set a temp variable to arr[j] 4. next set arr[j] = arr[j+1] 5. then set the arr[j+1] to equal temp 6. return the array

How to create a Binary Search Tree

Create a Node class, with a constructor taking a value in, set a left and a right value. Create a Binary Search Tree Class which sets the root as null.

What are the two methods used to search through a tree?

DFS (Depth first Search) BFS (Breadth first Search)

DFS preorder

Depth first search starts from the bottom and goes up. 1. set a visited array to store values that we have visited 2. set the current value to the root 3. create a helper function called traverse which takes in the node 4. take in an if statement which takes in the left node and recursively called traverse then push the node into visited. 6. write an if statement which takes in the right node and recursively call the right node 7. call traverse on current 8 return visited.

Insertion Sort

Insertion sort works best for data that is already mostly sorted. The time space complxity of O(n^2). Pseudocode: 1. Set the length to a variable. 2. Set a for loop which loops through the array which will handle the sorting. 3.

Order these time complexities from worst to the best O(nlogN), O(1), O(logN), O(n!), O(n^2), O(N)

O(n!) < O(n^2) < O(nlogN) < O(n) < O(logN) < O(1)

How to pop in a Linked List

Pop is a method that deletes from the end of a list. 1. check if there is a head, if not return undefined 2. declare a current value to the head and declare a newtail variable and setit to the current value 3. while current.next is true set newtail to cur and current to current.next 4.set this.tail to newtail 5.set this.tail.next to null 6.decrement the length 7. if the length is 0 this.ehad = null this.tail = nul 8. return null

How to traverse a linked list?

Pseudocode version: 1. Inside the linked list class, set a current value to the head. 2. Create a while loop and have the condition be set to while the current value is not null. 3. set current to current.next (which is the next value)

How do you reverse a linked list?

Pseudocoded version: 1. Inside your linked list class create a reverse method which takes in the head. 2. create a previous variable and set it to null 3. create a while loop with head inside as the value. 4. create a variable called current and set it to the next value after the head. 5. set the next value after the head to the previous value 6. set the previous to the head 7. set the head to the current value 8. return the previous value

Push in Doubly Linked List

Push pushes a value into our DLL. 1. We need to create a node 2. Our edge case: if the length is 0 we set our new node to the head and the tail. 3. Otherwise. we set this.tail.next to null and the prev new node to tail 4. set the tail to new node 5. add +1 to the length 6. return this.

What are Queues?

Queues are another linear data structure which follows FIFO (first in first out)

Selection Sort

Selection sort is a simple yet inefficient algorithm, similar to bubble sort with an O(n^2) run time. It starts with swapping with the first smallest element. Pseudocode: 1. Get the length of the array. 2. Have a first for loop, then after declare the smallest variable as i. 3. Have a second loop which checks the following numbers in the array. IE let j = i + 1 4. Check the smallest variable in this case arr[small] if it is greater than arr[j], if it is small now becomes j. 5. Outside of that loop check if min is not equal to i. 6. If it is declare a temp variable of arr[i] 7. arr[i] becomes the min 8. arr[min] becomes temp 9 Return an array

Shifting in a Linked List

Shift does not take in a value 1. If there is no node, return undefined. 2. set a current variable to this.head 3. set this.head to current.next 4. decrement the length 5. return the current value

How to create a doubly linked list

Similar to a singly linked list we need to create a node class and then a Linked List class. 1. The node class will take in a value and set a next and prev value to null. 2. Create a doubly linked list class and set the head and tail to null and the length to 0

Doubly Linked List

Similar to a singly linked list, but there pointers pointing to the next node and the previous node

What is the time and space complexity of queues?

Since Queues are a linear data structure: Find -> O(n) Access -> O(n) Insert -> O(1) Delete -> O(1) Space complexity worst case is O(n)

What are the time/space complexity of Stacks?

Stacks are a linear data structure so: find -> O(n) access -> O(n) insert -> O(1) delete -> O(1) Space complexity worst case is O(n)

What are Stacks?

Stacks are another linear data structure, which follow LIFO (last in first out).

Levenstein's distance

The Levensteins distance algorithim is the distance between two given strings. 1. check the length of the two strings that are given 2. set an array 3. increment the first column of each row using a for loop 4. increment each column in the first row 5. fill in the rest of the matrix

How would you create a Linked List?

The psuedocoded version of Linked Lists: 1. Create a Node class. 2. inside the constructor, it takes in data. 3. Next this.data = data 4. this.next = null Then create a Linked list class which has the length, the head and the tail set to null

Time and Space complexity of Linked lists

Time Complexity of Linked Lists: Find -> O(n) Access -> O(n) Insertion -> O(1) Deletion-> O(1) The reason behind this is due to the fact that Linked lists are a linear data structure.


Set pelajaran terkait

PSYCH 111 Chapter 4 States of Consciousness

View Set

Medsurge II - Chapter 25: Nursing Management: Patients With Hepatic and Biliary Disorders

View Set

EMT Chapter 3 Lifting and Moving Patients

View Set

Chapter 9: Nursing Care During Normal Pregnancy and Care of the Developing Fetus

View Set

Google Ads Certification- Measurement

View Set

American Airlines - STAR Method Questions (Glassdoor)

View Set