Ashish Ada Lab File
Ashish Ada Lab File
Submitted By
class Stack {
Node top;
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack.isEmpty()); // Output: false
System.out.println(stack.peek()); // Output: 30
System.out.println(stack.pop()); // Output: 30
System.out.println(stack.peek()); // Output: 20
public static void TowerOfHanoi(int n, char from, char to, char aux) {
if (n == 1) {
System.out.println("Move disk 1 from " + from + " to " + to);
return;
}
TowerOfHanoi(n - 1, from, aux, to);
System.out.println("Move disk " + n + " from " + from + " to " + to);
TowerOfHanoi(n - 1, aux, to, from);
}
}
Experiment : 3
AIM: write a java program to convert infix expression to postfix and evaluate it.
Program Code:
import java.util.Stack;
while (!stack.isEmpty()) {
postfix.append(stack.pop());
}
return postfix.toString();
}
return stack.pop();
}
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.dequeue();
class Node<T> {
T data;
Node<T> next;
class Queue<T> {
Node<T> front;
Node<T> rear;
public Queue() {
front = rear = null;
}
if (rear == null) {
front = rear = newNode;
return;
}
rear.next = newNode;
rear = newNode;
}
public T dequeue() {
if (isEmpty()) {
return null;
}
T item = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return item;
}
public T front() {
if (isEmpty()) {
return null;
}
return front.data;
}
}
Experiment : 5
AIM: Write a java program to implement Binary search Tree.
Program Code:
import java.util.LinkedList;
class Node {
int data;
Node left, right;
class BinaryTree {
Node root;
public BinaryTree() {
root = null;
}
return current;
}
if (data == current.data) {
if (current.left == null && current.right == null) {
return null;
}
if (current.right == null) {
return current.left;
}
if (current.left == null) {
return current.right;
}
return current;
}
public Node minValueNode(Node node) {
Node current = node;
return current;
}
}
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.delete(20);
System.out.println("\nInorder traversal of the modified binary search tree: ");
tree.inorderTraversal(tree.root);
}
}
Experiment : 6
AIM: Write a java program to construct binary tree using given tree traversal
Program Code:
import java.util.HashMap;
class Node {
int data;
Node left, right;
class BinaryTree {
int preIndex = 0;
if (n == 1) {
return tNode;
}
return tNode;
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int n = in.length;
tree.hMap.put(Integer.MIN_VALUE, -1);
Node(int d) {
key = d;
height = 1;
}
}
class AVLTree {
Node root;
int height(Node N) {
if (N == null)
return 0;
return N.height;
}
Node rightRotate(Node y) {
Node x = y.left;
Node T2 = x.right;
x.right = y;
y.left = T2;
y.height = max(height(y.left), height(y.right)) + 1;
x.height = max(height(x.left), height(x.right)) + 1;
return x;
}
Node leftRotate(Node x) {
Node y = x.right;
Node T2 = y.left;
y.left = x;
x.right = T2;
return y;
}
int getBalance(Node N) {
if (N == null)
return 0;
node.height = 1 + max(height(node.left),
height(node.right));
return node;
}
void displayTree() {
preOrder(root);
}
}
tree.insert(10);
tree.insert(20);
tree.insert(30);
tree.insert(40);
tree.insert(50);
tree.displayTree();
}
}
Experiment : 9
AIM: Write a java program to implement Quad Tree.
Program Code:
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class Node {
Point point;
Node topLeft, topRight, bottomLeft, bottomRight;
Node(Point point) {
this.point = point;
topLeft = topRight = bottomLeft = bottomRight = null;
}
}
class QuadTree {
Node root;
int capacity;
if (isLeaf(node)) {
if (node.point == null) {
node.point = point;
} else {
if (point.x < node.point.x) {
if (point.y < node.point.y) {
node.topLeft = new Node(point);
} else {
node.bottomLeft = new Node(point);
}
} else {
if (point.y < node.point.y) {
node.topRight = new Node(point);
} else {
node.bottomRight = new Node(point);
}
}
}
} else {
if (point.x < node.point.x) {
if (point.y < node.point.y) {
insert(node.topLeft, point);
} else {
insert(node.bottomLeft, point);
}
} else {
if (point.y < node.point.y) {
insert(node.topRight, point);
} else {
insert(node.bottomRight, point);
}
}
}
}
// Print tree
}
}
Experiment : 10
AIM : Write a java program to traverse a graph using breadth first search.
Program Code:
import java.util.*;
class Graph {
private int vertices;
private LinkedList<Integer>[] adjacencyList;
while (!queue.isEmpty()) {
int vertex = queue.poll();
System.out.print(vertex + " ");
class Graph {
private int vertices;
private LinkedList<Integer>[] adjacencyList;
class Graph {
private int vertices;
private LinkedList<Pair<Integer, Integer>>[] adjacencyList;
return dist;
}
}
class TrieNode {
public char value;
public HashMap<Character, TrieNode> children;
public boolean isEndOfWord;
class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode(' ');
}
if (!currentNode.children.containsKey(ch)) {
currentNode.children.put(ch, new TrieNode(ch));
}
currentNode = currentNode.children.get(ch);
}
currentNode.isEndOfWord = true;
}
if (!currentNode.children.containsKey(ch)) {
return false;
}
currentNode = currentNode.children.get(ch);
}
currentNode.isEndOfWord = false;
return currentNode.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = currentNode.children.get(ch);
if (node == null) {
return false;
}
if (shouldDeleteCurrentNode) {
currentNode.children.remove(ch);
return currentNode.children.size() == 0;
}
return false;
}
}
trie.delete("app");
System.out.println(trie.search("apple")); // Output: true
System.out.println(trie.search("app")); // Output: false
System.out.println(trie.search("apples")); // Output: true
}
}