2

10 Things You Must Know About Data Structures

 1 year ago
source link: https://blog.bitsrc.io/10-things-you-must-know-about-data-structures-871e939b4db2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

10 Things You Must Know About Data Structures

Understand the basics of the 10 most commonly used data structures.

1*EOk_5BNGNkdh1X1j6xVHGA.png

Introduction

A data structure is a way of arranging data in a computer so that it may be efficiently utilized. Data structures aid in the learning and implementation of numerous techniques for solving various coding problems. We always have to choose the most efficient and relevant data structure depending on the requirement of the project/problem.

For example, we use the Array data structure if we want to store data sequentially.

There are two types of Data Structures:

  1. Linear Data Structure: The elements in linear data structures are organized one after the other in sequential order. They are simple to construct since the elements are placed in a specific order. However, due to operational complications, linear data structures may not be the ideal solution when the program’s complexity grows. For example, Array, Stack, Queue, Linked List.
  2. Non-Linear Data Structure: In non-linear data structures, elements are not arranged in sequential order. It can consist of multiple levels and one element can be connected to one or more elements. For example, Tree and Graph.

We will now be looking into the 10 most common data structures.

1. Arrays

An array is the most widely used and simplest data structure. It is a collection of elements of the same data type that are stored sequentially. We cannot change the size of an array once memory is allocated. Indexing of elements in an array starts with 0. An element can be easily accessed if we know the index of that element.

1*fe7-NWjdbHgQx6fPQgdaXg.png

In C++, an array can be initialized as: int arr [4], int arr[4]={}, int arr [4] = {2,5,7,8}

Types of Arrays:

  • One-Dimensional Array
  • Multi-Dimensional Array

2. Linked Lists

A linked list is a collection of elements connected to each other in a linear order. This means that data must be accessed in a specific order; random data access is not possible.

Each element in a linked list is termed as “node”, and each node contains a key and a “next” pointer. The “next” pointer points to the next node. The sequence starts with a “head” node, the origin of the linked list. The last element of the linked list is called “tail.”

1*AqWL7UNcMXamWX4JGkRPkw.png

Types of Linked List:

  • Single Linked List: We can traverse only in the forward direction.
  • Doubly Linked List: We can traverse both in the forward and backward direction.
  • Circular Linked List: In the circular linked list, the tail is connected to the head of the linked list. Hence, all nodes are connected and form a circle.

3. Stacks

The function of a stack is identical to what it sounds like. It’s similar to stacking items in a container. LIFO (Last In First Out) structures are referred to as stacks. This indicates that the element that was inserted last is the first to be accessible.

You may either “push” a new element to the top of the stack or “pop,” which deletes the element that was put previously and is now at the top of the stack. Stacks are often used in recursion programming to parse and evaluate mathematical statements as well as to implement function calls.

0*ajdIp_VaeAamC5n4

4. Queues

A queue is similar to a stack, however it is a FIFO (First In First Out) structure rather than a LIFO (Last In First Out) structure. A line of people in a queue waiting to get tickets is the easiest way to visualize a queue. The person at the front of the line will be the first to get a ticket, while the one at the back will be the last.

In this structure, you may enqueue an element, which means adding it to the end of the queue. You may also dequeue an element, which means removing it from the queue’s beginning.

5. Hash Tables

Each value is associated with a key in a hash table structure. This makes it simple to quickly lookup values using a key. It’s a quick and straightforward way to insert and search for data.

When putting data in a table, Direct Addressing employs a one-to-one mapping between values and keys. When there are a high number of key-value pairs, however, this strategy has difficulty.

Given the memory available on a standard computer, the table will be enormous with so many records and may be difficult or even impossible to store. Hash tables are used to prevent this problem.

1*GZyPcGe-R3KXZQP965j8Ng.png

6. Trees

A tree data structure is made up of nodes that are connected together. A tree’s structure is hierarchical, forming a connection similar to that of a parent and a child. The tree’s structure is designed so that each parent-child node relationship has just one link.

There should only be one path from the root to each node in the tree. There are different types of trees with unique properties such as AVL tree, binary tree, binary search tree, etc.

1*DpirFY_Tx5h-vwHXNsWHmw.png

7. Graphs

In its most basic form, a tree is basically a graph. The distinction between a graph and a tree is that with a graph, no rules govern how nodes are connected. A graph can be used to represent a network. There are vertices that connect two nodes in a graph.

Despite the fact that nodes appear as points on a graph, they are commonly connected to one another, generating a web-like structure.

There are two types of graphs:

  • Undirected graphs
  • Directed graphs

Graphs can be represented using an adjacency matrix or an adjacency list.

1*hkAwyNHxh6UfN3fEczEokw.png

8. Tries

Trie, often known as “Prefix Trees,” is a tree-like data structure that has shown to be highly effective in solving string-related issues. Each node has the same number of pointers as to the number of characters in the alphabet. It may use the prefix of a word to look up that word in the dictionary.

If we suppose that all strings are made up of the letters a through z from the English alphabet, each trie node can contain a maximum of 26 points.

It provides fast retrieval and is mostly used for searching words in a dictionary, providing auto suggestions in a search engine, and even for IP routing.

9. Binary Search Trees

A binary Search Tree is a node-based binary tree data structure.

The properties of a Binary Search Tree are:

  • The left subtree of a node contains nodes with values lesser than the node’s value.
  • The right subtree of a node only contains nodes with values greater than the node’s value.

10. Heaps

A heap is a binary tree in which the parent nodes and their children are compared. As a result, the values within the nodes can be organized in any way that is needed.

Binary arrays, as well as trees, can be used to represent heaps.

There are two different types of Heaps:

  • Min-Heap: The value of the root node must be less than or equal among all values present at all of its children and the same should apply recursively for every subtree present in the tree.
  • Max Heap: The value of the root node must be more than or equal among all values present at all of its children and the same should apply recursively for every subtree present in the tree.
1*YcsstT58KjSjkNV9JxwK6g.png

Understand more about Data Structures from here.

Happy Learning!

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favourite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

0*YpHFTWA6pG6xcuhs.gif

Learn more


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK