Skip to content

Data Structures Introduction

Arghun-dev edited this page Aug 10, 2021 · 3 revisions

What Do they do?

Data Structures are collections of values, the relationships among them, and functions or operations that can applied to the data

think of an array, an array holds many values, It also contains the relationships between those values. the order in the case of an array, and arrays come with all sorts of built in methods and functionality, ways of interacting with them, adding to them, removing, push, pop, that sort of thing but also sorting, reversing, whatever other functionality there is. => So, we'll define our own structures that fit this exact same mold, when we define a singly linked list, it will not only hold data but it also puts them in obviously in a particular way where there's a relationship between each piece of data, it's not just randomly deciding how to store it, there's a particular pattern or relationship, so it holds a collection of data and we'll be writing a bunch of methods, we'll add our own push, our own pop, our own reverse what else, there's probably seven different methods we add to a singly linked list and they all help us interact with the singly linked list, same thing when we talk about graphs and trees and heaps and everything else

examples: Binary Search Trees, Queuse, Singly Linked Lists, Undirected Unweighted Graphs, Binary Heaps, Directed Graphs, Hash Tables, Doubly Linked Lists, Stacks, ...

We will implement each one of these as their own class, so we'll define it from scratch. Sort of like if we were defining our own array or our own object although it's a little higher level than that.

Why are there so many data structures?

Different Data Structures excel at different things. Some of them are pretty specialized but some of them are pretty general commonly use things like an array or object in javascript, which is why those come for free. we don't have to implement those on our own, but if we're trying to work with a red and black tree or undirected graph or somethinf like that you usually don't get that for free in a programming language. they are useful in special cases and you have to know when to use them. you have to know about them, in order to write one.

all of these do the same thing at the end of the day.

The Syntax

class Student {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

The method to create new objects must be called constructor.

The class keyword creates a constant, so you can not redefine it. Watch out for the syntax as well!

Creating Objects from Classes

We use the new keyword.

class Student {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

this.firstStudent = new Student('Colt', 'Steele');
this.secondStudent = new Student('Arghun', 'Mousanezhad');

firstStudent = {
   firstName: 'Colt',
   lastName: 'Steele'
}

Instance Methods

class Student {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  fullName() {
    return `Your full name is: ${this.firstName} ${this.lastName}`
  }
}

Clone this wiki locally