Data Structures

What are Data Structures?

  • Ways of organising and storing data in a computer so it can be accessed and modified efficiently.
  • Different data structures are used for different types of tasks, each with its own strengths and weaknesses.

Components:

Stacks

 
  • Definition: A stack is a collection of elements with two main operations: push (adding an element) and pop (removing the most recently added element).
  • Characteristics:
    • LIFO (Last In, First Out): The last element added is the first one to be removed.
    • Operations:
      • Push: Adds an element to the top of the stack.
      • Pop: Removes the top element from the stack.
      • Peek/Top: Returns the top element without removing it.


Queues

 
  • Definition: A queue is a collection of elements with two main operations: enqueue (adding an element) and dequeue (removing the earliest added element).
  • Characteristics:
    • FIFO (First In, First Out): The first element added is the first one to be removed.
    • Operations:
      • Enqueue: Adds an element to the end of the queue.
      • Dequeue: Removes the front element from the queue.
      • Front/Peek: Returns the front element without removing it.

Example:

  • Usage: Managing tasks in a printer queue where the first task submitted is the first one to be printed.

 

Trees

 
  • Definition: A tree is a hierarchical data structure consisting of nodes, with each node having zero or more child nodes.
  • Characteristics:
    • Root: The top node in a tree.
    • Child: A node directly connected to another node when moving away from the root.
    • Leaf: A node with no children.
    • Subtree: A tree consisting of a node and its descendants.

Example:

  • Usage: Representing hierarchical data such as file systems or organisational structures.

Types of Trees:

  • Binary Tree: Each node has at most two children.
  • Binary Search Tree (BST): A binary tree where the left child contains values less than the parent node and the right child contains values greater than the parent node.
  • Balanced Trees: Trees like AVL trees or Red-Black trees that maintain a balanced height to ensure efficient operations.

Binary Tree:

    Understanding stacks, queues, trees and other data structures is essential for effective problem-solving and efficient programming.

    These fundamental concepts are widely applicable in software development and various computational tasks.