Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Data Structures and Algorithms

The document provides an overview of various data structures including arrays, linked lists, stacks, queues, hash maps, trees, and graphs, detailing their definitions, features, operations, and examples in Java. Each data structure is analyzed for its pros and cons, highlighting their efficiency and use cases. An assignment is included to explore graphs further, focusing on their definitions, types, and basic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures and Algorithms

The document provides an overview of various data structures including arrays, linked lists, stacks, queues, hash maps, trees, and graphs, detailing their definitions, features, operations, and examples in Java. Each data structure is analyzed for its pros and cons, highlighting their efficiency and use cases. An assignment is included to explore graphs further, focusing on their definitions, types, and basic operations.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA STRUCTURES

AND ALGORITHMS

by
Jenny Lyn D. Masgong
DATA STRUCTURES IN JAVA
DATA STRUCTURE

• a way to organize and store data so that it can be accessed


and worked with efficiently

Types of Data
Structure

Non-
Primitiv
primitiv
e
e

Charact Linked
Integer Float Boolean Array Queue Trees
er List
ARRAY
Definition Features Operation

A collection of Random access via Access: O(1) time


elements identified by indices. complexity.
index or key. Fixed size (determined Insert/Remove: O(n)
 Fixed in size and at the time of for worst-case
stores elements of creation). scenarios
the same type.  Can store primitive (inserting/removing in
data types or objects. the middle or
beginning).
 Search: O(n) for
unsorted arrays;
O(log n) for sorted
arrays using binary
search
EXAMPLE OF ARRAY DECLARATION
JAVA

int[] arr = {1, 2, 3, 4};


MORE ON ARRAY
PROS CONS
• Fast access time using • Fixed size (cannot resize
indices. dynamically).
• Simple and efficient when • Insertion and deletion can
the number of elements is be slow if not at the end.
fixed.
LINKED LISTS
Definition Features Operation
 a linear data structure Dynamic size (can grow or Insert at beginning: O(1)
where elements (nodes) shrink). Insert at end: O(n) for
are stored in objects, and  Efficient insertions and singly linked lists (O(1) for
each node points to the deletions. doubly linked lists with tail
next node in the list
 types: pointer).
 Singly Linked List: Each Delete: O(1) if node
reference is known,
node points to the next.
 Doubly Linked List: otherwise O(n) to find the
Each node points to both node.
 Search: O(n)
the next and previous
nodes.
 Circular Linked List:
The last node points to
the first node.
EXAMPLE OF SINGLY LINKED LIST IN
JAVA
class Node { // Print the linked list
int data; public void printList() {
Node next; Node temp = head;
while (temp != null) {
Node(int data) { System.out.print(temp.data + "
this.data = data; ");
next = null; temp = temp.next;
} }
} }
}
class LinkedList {
Node head; public class Main {
public static void main(String[] args)
// Add element at the front {
public void push(int new_data) { LinkedList llist = new
Node new_node = new Node(new_data); LinkedList();
new_node.next = head; llist.push(10);
head = new_node; llist.push(20);
} llist.push(30);
llist.printList(); // Output: 30 20
10
}
MORE ON LINKED LIST
PROS CONS
• Dynamic size (can grow or • Access time is slower
shrink as needed). (sequential search).
• Efficient insertions and • Extra space required for
deletions from the beginning pointers.
(or end if doubly linked).
STACKS
Definition Feature Operation
 linear data Only the top Push: Add an element
structure that element is to the top of the stack
follows the Last accessible. (O(1)).
In, First Out (LIFO)  Used for problems Pop: Remove the top
principle. element (O(1)).
like recursion,
 the last element Peek: View the top
undo operations,
inserted is the element without
and parsing
first one to be removing it (O(1)).
expressions.  isEmpty: Check if
removed the stack is empty
(O(1)).
EXAMPLE OF STACK IN JAVA
import java.util.Stack;

public class StackExample {


public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);

System.out.println(stack.peek()); // Output: 30
System.out.println(stack.pop()); // Output: 30
System.out.println(stack.peek()); // Output: 20
}
}
MORE ON STACKS
PROS CONS
• Efficient for problems • Limited in functionality to
requiring reverse order (e.g., LIFO operations.
undo operations). • Only allows access to the
• Fast access to the top top element (no direct
element. access to other elements).
QUEUE
Definition Types Operation

 a collection that Simple Queue: Basic Enqueue: Add an


follows the First In, FIFO behavior. element to the queue
First Out (FIFO) Circular Queue: The (O(1)).
principle end of the queue Dequeue: Remove
 the first element connects back to the the front element
inserted is the first beginning. (O(1)).
 Priority Queue:
one to be removed Elements are
Peek: View the front
dequeued based on element without
priority rather than removing it (O(1)).
the order they were  isEmpty: Check if
added. the queue is empty
(O(1)).
EXAMPLE OF SIMPLE QUEUE IN JAVA
import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {


public static void main(String[] args) {
Queue<Integer> queue = new
LinkedList<>();
queue.offer(10);
queue.offer(20);
queue.offer(30);

System.out.println(queue.poll()); // Output:
10
System.out.println(queue.peek()); // Output:
20
}
}
MORE ON QUEUES
PROS CONS
• Ideal for scheduling • Limited to FIFO operations.
problems and handling • Not efficient for random
requests in order. access or deletions from
• Efficient enqueue and arbitrary positions.
dequeue operations.
HASH MAP
Definition Feature Operation
• collection of key-value • allows fast retrieval of Put: Add or update a
pairs elements using a key key-value pair (O(1) on
• implemented using a average).
hash table. Get: Retrieve the value
for a given key (O(1) on
average).
Remove: Remove the
key-value pair (O(1) on
average).
• ContainsKey: Check
if a key exists (O(1) on
average).
EXAMPLE OF HASH MAP IN JAVA
import java.util.HashMap;

public class HashMapExample {


public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 1);
map.put("banana", 2);
map.put("cherry", 3);

System.out.println(map.get("apple")); // Output: 1
System.out.println(map.containsKey("banana")); //
Output: true
}
}
MORE ON HASH MAPS
PROS CONS
• Efficient for key-value • Hash collisions can degrade
mapping. performance (though this is
• Provides constant time rare with good hashing).
access for lookups, • Not ordered (if you need
insertions, and deletions. ordered traversal, consider
TreeMap).
TREES
Definition Types Operation
• A hierarchical Binary Tree (Each node Insert: Add a node
structure made up of has at most two (O(log n) in a balanced
nodes connected by children) tree).
edges. Binary Search Tree Search: Find a node
(BST) (Left child < (O(log n) in a balanced
parent < right child) tree).
AVL Tree (Self- • Delete: Remove a
balancing BST) node (O(log n) in a
• Heap (Complete balanced tree).
binary tree, used for
priority queues)
EXAMPLE OF BST IN JAVA
MORE ON TREES
PROS CONS
• Fast searching and insertion • Can become unbalanced
(O(log n) for balanced trees). (leading to O(n) complexity
• Hierarchical structure that for skewed trees). Use self-
can represent hierarchical balancing trees (like AVL or
data. Red-Black Trees) to mitigate
this.
ASSIGNMENT
To fully understand graphs as a data structure, you
need to cover the following topics systematically:
• Definition of a Graph
• Types of Graphs
• Pros and Cons of Graphs
• Write a Java implementation for basic graph operations
such as graph creation, graph traversal (DFS, BFS), and
graph representation.
GRAPHS
Definition Types Operation
• a non-linear data Directed Graph Add Vertex
structure that consists (Digraph): edges have a Add Edge
of a collection of direction, i.e., they go Remove Vertex
nodes (vertices) and a from one vertex to Remove Edge
collection of edges
connecting pairs of another. BFS (Breadth-First
• Undirected Graph: Search
nodes
• used to represent edges have no DFS (Depth-First Search
direction; they simply Shortest Path
various real-world connect two vertices.
problems like social Cycle Detection
networks, • Graph Traversal
transportation
networks, and web
pages

You might also like