Sign in
Log inSign up

Data Structure and Algorithms For Beginners

Akuagwu Philemon's photo
Akuagwu Philemon
·May 26, 2020

chris-liverani-dBI_My696Rk-unsplash.jpg

Hello guys, this is a brief introduction to Data Structure and Algorithm. Please note that this is not an In-dept article because Data Structure & Algorithm is a broad course of its own. I will be referencing illustrations to common concepts in Programming Languages (especially javaScript ).

Algorithms are like verbs and Data Structures are like nouns. An Algorithm is just a method of doing something on a computer, while a Data Structure is a layout for memory that represents some sort of data. - Om Singh

Data Structure

A data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. - Wikipedia

campaign-creators-pypeCEaJeZY-unsplash.jpg

Algorithmns

An algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation. Algorithms are unambiguous specifications for performing calculation, data processing, automated reasoning, and other tasks. - Wikipedia

markus-spiske-iar-afB0QQw-unsplash.jpg

Prerequisites

  • fundamentals in Programming
  • fundamentals in OOP

Why Should you Learn Data Structures and Algorithms

Data Structures and Algorithms play a great role in programming but only if you know actually how to write a program. ... It's important to study these structures because in complex computing problems such as search, sort, hashing, etc

what you will Learn in this in this ARTICLE I am going to talk briefly on the Following topics.

  • Linked List
  • Stack
  • Queue
  • Binary Tree
  • Hash Tables

Linked List

what is a node (explain )

A node is the most basic building block for many common Data Structures(You can think of this as an Element in an Array). The Node has two functions. The first is that it provides a mechanism to contain a piece of data. For example, the integer value 5. The second, is that it provides a means of connecting itself to other nodes via an object reference pointer. This object reference is known as the Next pointer.

what is a nodechain

A node chain is simply a collection of Nodes with a form of connection where one node has a reference object pointer to its successor. think of it Like an Array (note that a Node chain is different from an array i only use it to make illustrations easier)

[1,2,3,4]

  • in this situation the nodes are 1, 2, 3 and 4
  • in a node chat 1 is pointing to 2 and 2 is pointing to 3

what is a Linked list

Linked List is a single chain of nodes in a linear Pattern. Linked Lists have a well defined starting point known as the List Head. The List will provide a Pointer to the first node in the list. Linked Lists also expose a Pointer to the last node in the list, the Tail, and Linked Lists provide operations that allow the list items to be managed, searched, and enumerated. It's these operations that make the List truly useful as a collection.

what is a pointer

A pointer is Simply a reference to a nodes

References

Stack

what is a Stack

A Stack is a collection in which data is added and removed in a Last In First Out order. This means that the item most recently added to the Stack will be the one that is next handed back from the collection. I think that the easiest way to explain how a Stack works is by seeing a visual example, and I'd like to use one of the most common examples, the restaurant plate stacker. If you aren't familiar with a restaurant plate stacker, this is a device where clean plates are put into a spring-loaded cylinder. As customers need a clean plate they walk up to the stacker and remove the top plate. So here we have an empty plate stacker. Since it's empty, the dishwasher is notified that more plates are needed, and as plates are cleaned, they're added to the stack. In computer science terms, this is known as "pushing" an item onto the stack, and notice that the plate that was just added is at the bottom of the cylinder or the bottom of the stack. But, this is also the top of the stack because it's the only plate in the stack. In a few moments, another plate is cleaned and added to the stack, and each time a plate is added, it becomes the new top plate, the one that the customer would pick up next. As a customer, I can approach the stacker and peak into it. This allows me to see the top plate. I can do this whether or not I want to take the plate off the stack. Now eventually, more plates are added, each one becoming the new top of the stack. Now, at this point, we have a plate stacker with four plates in it. A customer could walk up and could see that there is at least one plate in the stacker. She doesn't necessarily know or even care how many plates are beneath that plate; all she really cares about is that there's one available for her. So, she can take the top plate from the stack. In computer science terms, this action is known as "popping" the plate from the stack. And it should be now clear what Last In First Out means; the last plate added to the stack was the first plate removed by the customer. As more and more customers come, the plates are popped from the stack, each time reducing the depth or the number of items in the stack. Eventually, the stack is empty, and at this point, no more items can be popped from the stack. implementation can be done with a Linked List or an Array

Basic operations performed on a Stack are:

  • push adding data to a Stack
  • pop simply removing a data from a Stack

References msdn.microsoft.com/en-us/library/3278tedw.… msdn.microsoft.com/en-us/library/56fa1zk5(…

Queue

what is a Queue

A Queue is a collection that returns items in the same order that they were added. You can think of this as a checkout line at a grocery store. The people in the line are checked out in the order that they get into the line; first in the line, first checked out, last in the line, last checked out; it's a pretty simple concept.

  • a Queue is a First In, First Out collection.

operations on a Queue are :

  • Add (enqueue)- Enqueue an item. This is like a shopper getting into the grocery line. For javaScript developers think of enqueue as the push() method
  • remove (Dequeue) -Removing from the Head Dequeue is Simply the shift() method
  • Peek - A simple method to lets you know the head(Node with the Head pointers) of Queue

Binary Tree

what is a Tree

Trees are similar to Linked Lists in that they chain together nodes of data, but they do it in a hierarchical rather than in a linear manner. for javaScript developers Think of a list as an object ( They are not objects though, Objects are more similar to Hash tables )

  const obj = {
             node1 :{
                 data: 1
            },
            node2 : 2,
            node3:{
                 data: 3
            },
         }
  • Obj is the root or top node (also called Head node)
    • node2 and data are called Leaf nodes or Teminal nodes
    • node1 , node3 are Chilren of obj

What is a Binary Tree

Binary Tree is a Tree (as defined Previous) that has at most two child nodes, thus the name Binary, and those children are known as the Left and the Right Children.

  • Binary Search Tree doesn't change the structural rules of the Binary Tree, but it imposes an additional data rule, and that is that all the values in the Tree are stored in Sort Order; the smallest values are on the left, and the largest values are on the right.

Hash Tables

what is a Hash Table

Hash Tables are a type of Data Structure that implements an Associative Array. Associative Arrays provide the Storage of Key/Value pairs into an Array or an Array-like collection, but unlike an Array, the index can be any comparable type, not just an integer. So, besides being comparable, the only other restriction is that an Associative Array generally only contains unique keys. Multiple keys may refer to the same value, but the keys themselves should be unique.

What is Hashing

Hashing is a process that derives a fixed size result from an arbitrary input. And what I mean by that is that any string of any length when Hashed would return a fixed size, or in this case, a 32-bit integer Hash value. Fixed size simply means that every input returns a Hash code of the same size or type. The Hash codes we'll be looking at are 32-bit integer values, but some Hashing algorithms return smaller values, and some return much larger values. For example, there are Hashing algorithms that can return Hash codes that are thousands of bits long. But, for the sake of time and simplicity, we'll be looking at Hashing algorithms that return integers. Now, Hashing algorithms have several properties. One is an invariant, and some are ideals. The first is stability, and a stable algorithm returns the same output given the same input always. So, if you pass in the same string a million times, you should get that same Hash code value back a million times. It's an invariant; every Hashing algorithm has to be stable; if it's not stable it's not useful.

Conclusion

Thank you guys for reading my First Article. This Article was inspired By Robert Horvick's PluralSight course on DataStructure and Algorithm I will advice you checkout the Course for a Deeper understanding.
Please Look out for more and Follow me on Twitter @Ace1Philz and HashNode so that you can vote and make request for the next topic you will like me to write on (Polls will be raised on Sundays) you can decide if You want to see a more on Data Structures Sorting Algorithm, AVL tree, collection Concurrency and So much more ) or Another Topic

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct