Lab#10 Binary Search Tree-merged
Lab#10 Binary Search Tree-merged
LAB # 11
A binary search tree is a tree where each node contains a value, and for each node, the
left sub-tree only contains values less than the value of the node and the right sub-tree
only contains greater values. This allows finding the node for any value in O (logN)
average time.
This procedure deletes the node N at location LOC, where N does not have two
children the pointer PAR gives the location of the parent of N, or else PAR=NULL
indicates that N is the ROOT node. The pointer CHILD gives the location of the only
child of N, or else CHILD=NULL indicates N has no children.
else
setrightchild(locp,NULL)
[Deleting node with one child]
Else If getleftchild(loc) != NULL and getrightchild(loc)=NULL, then:
setleftchild(locp,getleftchild(loc))
else
setrightchild(locp, getleftchild(loc))
Else If getleftchild(loc) = NULL and getrightchild(loc) !=NULL, then:
(End of if structure)
This procedure will delete the node N at location LOC where N has two children. The
pointer PAR gives the location of the parent N or else PAR=NULL indicates that N is
the ROOT node. The pointer SUC gives the location of the inorder successor of N,
and PARSUC gives the location of the parent the inorder successor.
Else:
Set setrightchild(locp,SUC)
(End of if structure)
Else:
Set ROOT: =SUC
(End of if structure)
Set setleftchild(suc, getleftchild(loc)) and Setrightchild(suc, getrightchild(loc))
Return Root and item
Lab Task
1. Write a program that takes an item input to search if there is a item present, deletes
it otherwise insert it in a tree.
2. Write a program to create binary search tree of the following list of elements;
- 45, 15, 79, 90, 10, 55, 12, 20, 50
Home Task
1. Write a function called MergeBST that give a combine Binary search tree of two
different binary search trees according to rules of binary tree then print all nodes.
2. Write a program to insert new element ‘60’ in the Binary tree created in above
ques.2.
Q1. Write a program that takes an item input to search if there is a item present, deletes it
otherwise insert it in a tree.
Code:
import java.util.*;
class Node {
String data;
Node left, right;
Node(String data) {
this.data = data;
this.left = this.right = null;
}}
class BinaryTree {
Node root;
while (true) {
System.out.println("Enter an item (or 'exit' to quit):");
String item = scanner.nextLine();
if (item.equals("exit")) break;
if (tree.search(tree.root, item)) {
System.out.println("Item found, deleting...");
tree.root = tree.delete(tree.root, item);
} else {
System.out.println("Item not found, inserting...");
tree.root = tree.insert(tree.root, item);
}
System.out.println("Current tree (in-order):");
tree.inorder(tree.root);
System.out.println();
}}}
Output:
Item not found, inserting...
Current tree (in-order):
apple
Item not found, inserting...
Current tree (in-order):
apple banana
Item found, deleting...
Current tree (in-order):
banana
Q2. Write a program to create binary search tree of the following list of elements;
- 45, 15, 79, 90, 10, 55, 12, 20, 5
Code:
import java.util.*;
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
this.left = this.right = null;
}}
class BinaryTree {
Node root;
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
this.left = this.right = null;
}
}
class BinaryTree {
Node root;
root = null;
for (int element : elements) {
root = insert(root, element);
}}
void storeInOrder(Node root, List<Integer> elements) {
if (root != null) {
storeInOrder(root.left, elements);
elements.add(root.data);
storeInOrder(root.right, elements);
}}}
public class TreeItemManager {
public static void main(String[] args) {
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();
Q4. Write a program to insert new element ‘60’ in the Binary tree created in above ques.2.
Code:
import java.util.*;
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
this.left = this.right = null;
}}
class BinaryTree {
Node root;
Node insert(Node root, int key) {
if (root == null) return new Node(key);
if (key < root.data) root.left = insert(root.left, key);
else if (key > root.data) root.right = insert(root.right, key);
return root;
}
void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}}
void mergeTrees(Node root1, Node root2) {
List<Integer> elements = new ArrayList<>();
storeInOrder(root1, elements);
storeInOrder(root2, elements);
Collections.sort(elements);
root = null;
for (int element : elements) {
root = insert(root, element);
}}
void storeInOrder(Node root, List<Integer> elements) {
if (root != null) {
storeInOrder(root.left, elements);
elements.add(root.data);
storeInOrder(root.right, elements);
}}}
public class TreeItemManager {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
int[] elements = {45, 15, 79, 90, 10, 55, 12, 20, 5};
LAB # 12
The graph is one non-linear data structure which consists of some nodes and their
connected edges. The edges may be directed or undirected.
The Breadth First Search (BFS) traversal is an algorithm, which is used to visit all of
the nodes of a given graph. In this traversal algorithm one node is selected and then
all of the adjacent nodes are visited one by one. After completing all of the adjacent
vertices, it moves further to check another vertices and checks its adjacent vertices
again.
Sample Program#1
// Java program to print BFS traversal from a given source vertex.
// BFS(int s) traverses vertices reachable from s.
import java.io.*;
import java.util.*;
Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v,int w) {
adj[v].add(w);
}
// prints BFS traversal from a given source s
void BFS(int s) {
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];
while (queue.size() != 0) {
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Driver method to
public static void main(String args[]) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
The Depth First Search (DFS) is a graph traversal algorithm. In this algorithm one
starting vertex is given, and when an adjacent vertex is found, it moves to that
adjacent vertex first and try to traverse in the same manner.
Sample program #2
// Java program to print DFS traversal from a given graph
import java.io.*;
import java.util.*;
// Constructor
@SuppressWarnings("unchecked") Graph(int v) {
V = v;
adj = new LinkedList[v];
for (int i = 0; i < v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v, int w) {
adj[v].add(w); // Add w to v's list.
}
// A function used by DFS
System.out.println(
"Following is Depth First Traversal "
+ "(starting from vertex 2)");
// Function call
g.DFS(2);
}
}
Lab Task
Home Task
2. Write a JAVA program that creates a recursive function that takes the index of the
node and a visited array. Mark the current node as visited and print the node and
traverse all the adjacent and unmarked nodes and call the recursive function with the
index of the adjacent node.
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
System.out.println("BFS Traversal:");
graph.bfs(0);
System.out.println("DFS Traversal:");
graph.dfs(0);
}}
Output:
BFS Traversal:
01324
DFS Traversal:
01324
Code:
import java.util.*;
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
queue.add(neighbor);
visited[neighbor] = true;
}}}
System.out.println();}
public static void main(String[] args) {
GraphTraversal graph = new GraphTraversal(5);
graph.addEdge(0, 1);
graph.addEdge(0, 3);
graph.addEdge(0, 2);
graph.addEdge(2, 4);
graph.bfs(0);
}}
Output:
01324
while (!queue.isEmpty()) {
int node = queue.poll();
System.out.print(node + " ");
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
queue.add(neighbor);
visited[neighbor] = true;
}}}
System.out.println();
}
public void dfsUtil(int node, boolean[] visited) {
visited[node] = true;
System.out.print(node + " ");
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
dfsUtil(neighbor, visited);
}}}
public void dfs(int start) {
boolean[] visited = new boolean[vertices];
dfsUtil(start, visited);
System.out.println();
}
public static void main(String[] args) {
GraphTraversal graph = new GraphTraversal(8); // Number of nodes: 0 to 7
graph.addEdge(0, 1); // 40 -> 20
graph.addEdge(0, 2); // 40 -> 10
graph.addEdge(1, 2); // 20 -> 10
graph.addEdge(1, 3); // 20 -> 30
graph.addEdge(1, 5); // 20 -> 60
graph.addEdge(1, 4); // 20 -> 50
graph.addEdge(2, 3); // 10 -> 30
graph.addEdge(3, 5); // 30 -> 60
graph.addEdge(5, 6); // 60 -> 70
graph.addEdge(4, 6); // 50 -> 70
System.out.println("BFS Traversal:");
graph.bfs(0); // Start from 40 (node 0)
System.out.println("DFS Traversal:");
graph.dfs(0); // Start from 40 (node 0)
}}
Output:
BFS Traversal:
0123546
DFS Traversal:
0123564
Q2. Write a JAVA program that creates a recursive function that takes the index of the
node and a visited array. Mark the current node as visited and print the node and traverse
all the adjacent and unmarked nodes and call the recursive function with the index of the
adjacent node.
Code:
import java.util.*;