Data Structures and Algorithms
Data Structures and Algorithms
AND ALGORITHMS
by
Jenny Lyn D. Masgong
DATA STRUCTURES IN JAVA
DATA STRUCTURE
Types of Data
Structure
Non-
Primitiv
primitiv
e
e
Charact Linked
Integer Float Boolean Array Queue Trees
er List
ARRAY
Definition Features Operation
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
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;
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