From the course: Data Structures in JavaScript: BSTs, Queues, and Stacks

Three data structures in JavaScript

- [Instructor] All right, welcome to Chapter 1. In this module, you'll review three different data structures in JavaScript: binary search trees, also known as BSTs, stacks, and queues. Each of these data structures are distinct and serve as solutions to particular problems. Let's review each one briefly. First, binary search trees, which, for the rest of this course, you'll hear referred to as BSTs, are a collection of nodes. Data is stored within these nodes. Shown here is a tree data structure. At the top of the tree is the root node from which branches out the other nodes. Nodes with no children nodes are referenced as leaf nodes. Each child node are the parents of their own branch and are children of their own nodes. A binary tree can only have two branches for every node. They are ordered. To the right is a node that contains data with a greater value, and to the left is data that is less in value. Operations on this type of tree are optimized by using binary search, which is O log event for time efficiency. You'll learn more about this later in the chapter on BSTs, but just for now, know that O log n is better than O of n, as it only searches half of the data, because of how binary search works and how a binary search tree is organized with the left and right values. You may be wondering and asking yourself, so when would BSTs be used in the real world? So glad you asked. BSTs are great for storing data. For instance, if you had an online clothing store and you want to keep track of how many items sold below a given price, a BST would sort the order of prices for you. This, again, is just a brief overview of this data structure and in the next chapter, you'll take a deep dive into the code and construct a BST while also carrying out CRUD functionality on the BST. The secant theta structure is the queue. A queue is a linear data structure that uses a first-in, first-out, FIFO, principle. When creating or updating the queue, there are two very foundational operations that will add elements at the end of the queue, known as enqueuing and will remove and delete elements from the beginning of the queue, known as dequeuing, hence, FIFO, first element in is the first element out. Here's an example of the creation of a queue using an array. Enqueue is the addition of elements or data to your queue, and dequeue is the removal or deletion of the data from the queue. First, you can see here that there is a value of five. Then there's the enqueue of the value 19, and as you see, it is going to the end of the queue. Same with the numbers 350. One. And 90. Now, to dequeue, you see that the deletion of the data starts at the beginning of the queue. We will delve more into queues and how to create them and carry out CRUD functionality on queues in Chapter 3. Let's take a look at one more data structure that will be covered in this course. Stacks. Did you think of a stack of pancakes? No, not speaking of a stack of pancakes, although it is similar. Stacks are also a linear data structure. However, it utilizes a LIFO principle of last in, first out. To add an element of data, you would push the element to the back, and to delete or remove an element, you would pop it from the end of the stack. Hence, here again, you see the principle, but this time, it is LIFO. Here is a visualization tool. Here you see the pushing of the first value, 101, followed by the push of the next three elements, 101, 23, 45, and nine. Which element do you think will be removed if you use the Pop function? If you guessed nine, the last element added, you guessed correctly. Good work. Just like a stack of pancakes, the last pancake added is placed on the top of the previous one and eaten first, LIFO for stacks. So that's all for your overview of BSTs, queues, and stacks. In the coming chapters, you'll learn more about how to create these data structures using nodes and arrays.

Contents