size is static and bounded, dynamic resizing not possible
insert requires shifting out subsequent elements to right
delete requires shifting out subsequent elements to left
A data structure that holds a pointer head to a chained link of nodes.
Each node contains 2 parts:
data : holds the data
next : holds link to the next node
Properties of linked list
No random access.
To reach a node, always start from the head and traverse.
Operations on linked list
Problems based on linked list properties
Given a sequence of operations, print the state after each operation
class Node {
int data;
Node next;
Node(int d);
int getData();
Node getNext();
}
class LinkedList {
Node head;
Node createHead(); // creates first node
Node insertFirst(int key); // returns new head
void insert(int pos, int key);
void insert(int key);
void deleteHead(); // delete last remaining node
boolean delete(int pos);
boolean search(int key); // returns position
int size();
}
LinkedList in Collections
Problems on linked list (use Collections' LinkedList)
In a single traversal find the middle element/position
Create a sorted list as you read elements (sort a linked list)
Merge two sorted lists into one
Rotate a linked list by k positions
Remove duplicates from a sorted list (shrink the list)
Partition list based on > and <= x (based on pivot, odd/even)
Union of two linked lists keep duplicates (remove duplicates)
Intersection of two linked lists
Reverse a linked list
Check if a linked list has a cycle
When to use LinkedList or ArrayList?
Last node links back to first node
Can reach the first node from the last node
insert
delete
search
Rotate right k times
Rotate left = rotate right size - k times
Implementation changes needed
insert and delete needs modification to set the next back to head
search needs modification to stop traversal as soon as next points to head
Contains pointer to first node head
Each node contains 3 parts:
data : holds the data
prev : holds pointer to previous node
next : holds pointer to next node
Can traverse in both left and right directions
Implementation changes needed
insert and delete needs modification to set both prev and next
A data structure that holds pointer (top ) to a chained link of nodes
A new node is pushed onto the stack, and it becomes the new top
Delete pops the top node from the stack, the below node becomes the top
It can be seen as restricted linked list that supports insert last and delete last
LIFO : Last-In-First-Out
Insert and delete at one end only
class Node {
int data;
Node next;
Node(int d);
int getData();
Node getNext();
}
class Stack {
Node top;
void push(int key);
int pop();
int peek();
int size();
}
Reverse
Check palindrome
Balanced parantheses
Infix to prefix
Infix to postfix
Min stack
Basic calculator*
Decode string
Backspace string compare
Score of parantheses
A data structure that holds pointers (front, rear) to the first and last nodes of chained link of nodes
A new node is enqueued to the rear
Node is removed from the front
It can be seen as restricted linked list that supports insert last and delete first
FIFO : First-In-First-Out
Insert at one end and remove from the other end
class Node {
int data;
Node next;
Node(int d);
int getData();
Node getNext();
}
class Queue {
Node front;
Node rear;
void enqueue(int key);
int dequeue();
int peek();
int size();
}
ArrayDeque in Collections
Task scheduler
Stack using queue
Queue using stack
A non-linear representation of nodes where each node links to 0, 1 or 2 child nodes
Child nodes are referred to as left and right
Topmost node is called root
Access to any node always starts with the root
Level-order: Level-by-level from top to bottom, at each level left to right
25 48 16 11 37 64 76 83 51 93
Preorder: visit(node), preorder(left), preorder(right)
25 48 11 76 83 37 51 16 64 93
Inorder: inorder(left), visit(node), inorder(right)
76 11 83 48 37 51 25 16 93 64
Postorder: postorder(left), postorder(right), visit(node)
76 83 11 51 37 48 93 64 16 25
insert
delete
search
preorder
inorder
postorder
class Node {
- int data;
- Node left;
- Node right;
.
Node(int d);
boolean search(int d);
boolean insert(int d);
boolean delete(int d);
void preorder(Node n);
void inorder(Node n);
void postorder(Node n);
}
class BinaryTree {
Node root;
boolean search(int d) { root.search(d); }
boolean insert(int d) { root.insert(d); } // Implementation depends on the type of binary tree
boolean delete(int d) { root.delete(d); } // Implementation depends on the type of binary tree
void preorder() { preorder(root); }
void inorder() { inorder(root); }
void postorder() { postorder(root); }
}
Expression tree
Huffman encoding
A binary tree in which each node's value is less than that of its left and right nodes
A binary tree in which each node's value is greater than that of its left and right nodes
Access starts from the root
Min Heap property: parent < left, right
Max Heap property: parent > left, right
No constraints between left and right
Can be implemented as array for fast access
Level-order (array implementation suits this)
parent(i) = (i-1)/2
left(i) = 2i + 1
right(i) = 2i + 2
insert
getMin (or getMax)
class MinHeap {
int[] arr;
int root;
int size;
MinHeap() { arr = int[100]; root = 0; size = 0; }
int getParent(int i) { return (i-1)/2; }
int getLeft(int i) { return 2*i + 1; }
int getRight(int i) { return 2*i + 2; }
void buildHeap();
void insert(int key);
void fixHeap();
int getMin();
}
Heap sort
Implement priority queue
PriorityQueue in Collections
Kth largest element in an array
Top K frequent elements
Split array into consecutive subsequences
Last stone weight
K closest points to origin
A binary tree in which a node's value is greater than its left and smaller than its right
To search faster
It takes O(n) time to search in a linked list
BST provides a way to reduce search time to O(logn)
Access starts from the root
left < data < right
Inorder traversal results in sorted sequence
Usually inorder to retrieve sorted sequence
Other traversals are also common
insert
delete
search
preorder, inorder, postorder
class Node {
int data;
Node left;
Node right;
Node(int d);
boolean search(int d);
boolean insert(int d);
boolean delete(int d);
void preorder(Node n);
void inorder(Node n);
void postorder(Node n);
}
class BST extend Binary Tree {
Node root;
boolean search(int d) { root.search(); }
boolean insert(int d) { root.insert(); }
boolean delete(int d) { root.delete(); }
void preorder() { preorder(root); }
void inorder() { inorder(root); }
void postorder() { postorder(root); }
}
search : traverse recursively taking appropriate path at each level until node containing search element is found or leaf node is reached
insert : traverse recursively taking appropriate path at each level until point of insertion identified
delete : search, remove the node, replace it with the node containing next higher element
preorder : starting from root, traverse in node, preorder(left), preorder(right) manner
inorder : starting from root traverse in inorder(left), node, inorder(right) manner
postorder : starting from root traverse in postorder(left), postorder(right), node manner
Compute height of the tree
Compute height of the root's left subtree and root's right subtree
Compute height of every node of the tree
Compute weight of the tree
Compute weight of the root's left and root's right
Compute weight of every node of the tree
Compute the diameter of the tree
Compute the lowest common ancestor of two nodes
Count the number of internal and leaf nodes of the tree
Check if the tree is complete, perfect
A binary search tree which is height-balanced
Height balanced: The height of the left and right subtrees differ by at most 1
Usually inorder
for retrieving sorted sequence
Other traversals are also common
insert, delete, search
preorder, inorder, postorder
llrotate
lrrotate
lrrotate
rlrotate
class Node {
int data;
Node left;
Node right;
Node(int d);
boolean search(int d);
boolean insert(int d);
boolean delete(int d);
void preorder(Node n);
void inorder(Node n);
void postorder(Node n);
}
class AVLTree extend BST {
Node root;
void llRotate(Node n);
void rrRotate(Node n);
void lrRotate(Node n);
void rlRotate(Node n);
boolean search(int d) { root.search(); }
boolean insert(int d) { root.insert(); }
boolean delete(int d) { root.delete(); }
void preorder() { preorder(root); }
void inorder() { inorder(root); }
void postorder() { postorder(root); }
}
Median of two sorted arrays