Mohit Ee
Mohit Ee
EXPERIMENT FILE
ACADEMIC SESSION 2023-24
.
4
Write Java programs that use recursive and non-recursive
functions to traverse the given binary tree in a) Preorder b)
Inorder c) Postorder.
public class LinearSearchNonRecursive { public static int linearSearch (int[] arr, int target) { for (int i = 0; i
< arr.length; i+
+) {
target);
public class Linear SearchRecursive { public static int linearSearch(int[] arr, int target, int index) { if
(index> arr.length) { return -1; // Element not found
return linear Search (arr, target, index + 1); // Recursively search the next
index }
int[] arr (1, 2, 3, 4, 5); int target 3; int result = linear Search (arr,
public class Linear SearchRecursive { public static int linearSearch(int[] arr, int target, int index) { if
(index> arr.length) { return -1; // Element not found
return linear Search (arr, target, index + 1); // Recursively search the next
index }
int[] arr (1, 2, 3, 4, 5); int target 3; int result = linear Search (arr,
public class BinarySearchRecursive { public static int binarySearch(int[] arr, int target, int left, int right) {
if (left <= right) { int mid left + (right)
left) / 2;
right half
} else { return binarySearch(arr, target, left, mid 1); // Search the left
half
int target = 5;
8, 9};
}
}
EXPERIMENT-02
Aim- Write a Java program to implement all the functions of a dictionary (ADT) using
Hashing.
Source Code:
java
import java.util.LinkedList;
class Dictionary {
private int capacity; // The capacity of the hash table
private LinkedList<Entry>[] table; // Array of linked lists for collision handling
// Retrieve values
System.out.println("Meaning of 'apple': " + dictionary.get("apple"));
System.out.println("Meaning of 'banana': " + dictionary.get("banana"));
// Update value
dictionary.insert("apple", "a tech company");
System.out.println("Updated meaning of 'apple': " + dictionary.get("apple"));
// Remove a key-value pair
dictionary.remove("banana");
System.out.println("Meaning of 'banana' after removal: " + dictionary.get("banana"));
}
}