Professional Documents
Culture Documents
Bah Abdulai Ibrahim D.S. & A
Bah Abdulai Ibrahim D.S. & A
Bah Abdulai Ibrahim D.S. & A
TASK: ASSIGNMENT
ID NO: 2023126
1B
1C
1D
So, the order in which the node values are reached in each traversal type for the BST of part (c) is:
1E
The height of a tree is the length of the longest path from the root to a leaf.
import java.util.Random;
class AVLTreeNode {
int key;
int height;
AVLTreeNode left;
AVLTreeNode right;
AVLTreeNode(int key) {
this.key = key;
this.height = 1;
class AVLTree {
AVLTreeNode x = y.left;
AVLTreeNode T2 = x.right;
x.right = y;
y.left = T2;
return x;
}
// Function to left rotate subtree rooted with x
AVLTreeNode y = x.right;
AVLTreeNode T2 = y.left;
y.left = x;
x.right = T2;
return y;
if (node == null) {
} else {
return rightRotate(node);
return leftRotate(node);
node.left = leftRotate(node.left);
return rightRotate(node);
node.right = rightRotate(node.right);
return leftRotate(node);
return node;
if (root == null) {
return root;
} else {
if (temp == root.left) {
temp = root.right;
} else {
temp = root.left;
if (temp == null) {
root = null;
} else {
root = temp;
} else {
root.key = temp.key;
if (root == null) {
return root;
return rightRotate(root);
}
root.left = leftRotate(root.left);
return rightRotate(root);
return leftRotate(root);
root.right = rightRotate(root.right);
return leftRotate(root);
return root;
current = current.left;
return current;
return root;
preOrderTraversal(root);
System.out.println();
if (node != null) {
preOrderTraversal(node.left);
preOrderTraversal(node.right);
System.out.println();
tree.preOrder();
keysToDelete[i] = data[rand.nextInt(data.length)];
tree.delete(key);
System.out.println();
tree.preOrder();
if (searchResult) {
} else {
}
Analysis of Time Complexity
Insertion: The time complexity of inserting a node in an AVL tree is O(logn) because the tree is balanced and the
height is kept logarithmic.
Deletion: The time complexity of deleting a node is also O(logn) for the same reason as insertion.
Search: The time complexity of searching for a node is O(logn) since the tree height is O(logn).
2B
GRAPH IMPLEMENTATION IN JAVA
import java.util.*;
class Graph {
public Graph() {
adjacencyList.remove(vertex);
adjacencyList.get(source).add(destination);
if (edges != null) {
edges.remove(Integer.valueOf(destination));
dfsRecursive(startVertex, visited);
if (visited.contains(vertex)) {
return;
visited.add(vertex);
dfsRecursive(neighbor, visited);
queue.add(startVertex);
visited.add(startVertex);
while (!queue.isEmpty()) {
if (!visited.contains(neighbor)) {
visited.add(neighbor);
queue.add(neighbor);
// Check if the graph contains a cycle and return the cycle path if found
return path;
return null;
private boolean findCycleUtil(int vertex, Set<Integer> visited, Set<Integer> stack, List<Integer> path) {
if (stack.contains(vertex)) {
path.add(vertex);
return true;
if (visited.contains(vertex)) {
return false;
visited.add(vertex);
stack.add(vertex);
path.add(vertex);
return true;
path.remove(path.size() - 1);
stack.remove(vertex);
return false;
graph.addVertex(0);
graph.addVertex(1);
graph.addVertex(2);
graph.addVertex(3);
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(2, 3);
graph.dfs(0);
System.out.println();
graph.bfs(0);
System.out.println();
if (cycle != null) {
} else {
o Visited Set: Tracks all the nodes that have been visited.
o Recursion Stack Set: Tracks nodes in the current path of the recursion stack.
o Path List: Stores the sequence of nodes in the current path. If a cycle is detected, this list will be used to
return the cycle sequence.
Testing
o Graph Construction: A graph is constructed with vertices and edges, including an edge that creates a cycle.
o DFS and BFS: Perform DFS and BFS traversals starting from vertex 0.
o Cycle Detection: The findCycle method is used to detect a cycle and return the sequence of tasks that form
the cycle.
Remove Vertex: O(V+E) (where V is the number of vertices and E is the number of edges)
import java.util.LinkedList;
import java.util.List;
public HashTable() {
size = 0;
K key;
V value;
this.key = key;
this.value = value;
if (table[index] == null) {
}
for (Entry<K, V> entry : table[index]) {
if (entry.key.equals(key)) {
return;
size++;
resize();
if (table[index] != null) {
if (entry.key.equals(key)) {
return entry.value;
return null;
if (table[index] != null) {
if (entry.key.equals(key)) {
table[index].remove(entry);
size--;
return;
size = 0;
if (bucket != null) {
insert(entry.key, entry.value);
import java.io.*;
import java.util.*;
class SpellChecker {
public SpellChecker() {
String word;
while ((word = br.readLine()) != null) {
dictionary.insert(word.toLowerCase(), word);
br.close();
String line;
misspelledWords.add(word);
br.close();
return misspelledWords;
chars[i] = c;
if (dictionary.search(newWord) != null) {
suggestions.add(newWord);
}
}
return suggestions;
try {
spellChecker.loadDictionary("dictionary.txt");
} catch (IOException e) {
e.printStackTrace();
Testing
a. Create a dictionary.txt file with a list of correctly spelled words, one per line.
b. Create a document.txt file with the text to check for spelling errors.
c. Run the main method in the SpellChecker class.
Potential Optimizations
Better Hash Functions: Implement a more sophisticated hash function to reduce collisions.
Dynamic Resizing: Resize the table dynamically based on more complex heuristics to balance memory
usage and performance.
Cache Recent Searches: Implement a caching mechanism for recently searched words to speed up repeated
lookups.
Phonetic Algorithms: Use phonetic algorithms (e.g., Soundex) for generating better suggestions for
misspelled words.