DEV Community

Cover image for Data Structures and Algorithms
Sharon Chang'ach
Sharon Chang'ach

Posted on

Data Structures and Algorithms

Introduction

A data structure is a data organization, management, and storage format that enables efficient access and modification. It shows the relationships among the data, and the functions or operations that can be applied to the data. Algorithms on the other hand are a set of instructions that solve a problem.

Data Structures

  • sets

  • lists

  • stacks

  • linked lists

  • queues

  • dictionaries

  • Trees

Sets

A set is an unordered collection of unique elements. This means that it does not allow duplication.

basket={'apple', 'eggs', 'banana', 'orange'}
same =set(['apple', 'eggs', 'banana', 'orange'])
print(basket==same)
Enter fullscreen mode Exit fullscreen mode

Lists

A list is a container data type that stores a sequence of elements. This means that it allows different types of elements in the list.

List = [1, 2, 3, "GFG", 2.3]
print(List)
Enter fullscreen mode Exit fullscreen mode

output:

[1, 2, 3, 'GFG', 2.3]
Enter fullscreen mode Exit fullscreen mode

Stacks

Think of a loading books into a box. Books are loaded one by one and the first one in becomes the last one when the loading is done. This is basically the working of a stack as well.
The functions in a stack are:

  • empty() – prints out whether the stack is empty.
  • size() – prints out the number of elements in the stack.
  • top() – prints out the topmost element.
  • append() – Inserts the element at the top of a stack.
  • pop() – Deletes the topmost element.
stack=[3]
stack.append(42)
stack.append(12)
stack.pop()
Enter fullscreen mode Exit fullscreen mode

output:

12,42,3
12
Enter fullscreen mode Exit fullscreen mode

Linked list

It is a linear data structure, in which the elements are not stored at contiguous memory locations.

Queues

Consider a line in a bank. The first person is at the front of the line and as people join, they are added from the rear. This is basically what a queue is. The structure reduces from the front.
Commands:

  • Enqueue: Adds an item to the queue.
  • Dequeue: Removes an item from the queue.
  • Front: prints out the front item from queue
  • Rear: prints out the last item from queue

Dictionaries

It is a useful data structure for storing (key, values) pairs.

# Creating a Dictionary
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("Creating Dictionary: ")
print(Dict)

# accessing a element using key
print("Accessing a element using key:")
print(Dict['Name'])

# accessing a element using get()
# method
print("Accessing a element using get:")
print(Dict.get(1))

# creation using Dictionary comprehension
myDict = {x: x**2 for x in [1,2,3,4,5]}
print(myDict)
Enter fullscreen mode Exit fullscreen mode

output:

Creating Dictionary: 
{'Name': 'Geeks', 1: [1, 2, 3, 4]}
Accessing a element using key:
Geeks
Accessing a element using get:
[1, 2, 3, 4]
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Enter fullscreen mode Exit fullscreen mode

Trees

A tree is a hierarchical data structure.
There are two types of trees:

  • Binary Search Tree

  • Red Black Trees
    It has several functions which include:
    Traversal, search, insertion, deletion and update.

Functions in data structures

  1. Insertion
    It is the addition of elements in the data structures.

  2. Deletion
    It is the removal of elements from the data structures

  3. Update
    It is when an element is changed in the data structures.

  4. Search
    It is where an element is searched in the data structure.

Top comments (0)