Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views18 pages

Data Structure

Data

Uploaded by

majumdarabir7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Data Structure

Data

Uploaded by

majumdarabir7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Q1.

Create a class BSTNode that contains a member 'info' to store a number,


with 'left' referring to the left child and 'right' referring to the right child.
Provide the necessary constructor. Additionally, create a method to insert a node
into a binary search tree.
class BSTNode {
int info;
BSTNode left, right;
public BSTNode(int info) {
this.info = info;
this.left = this.right = null;
}}
class BinarySearchTree {
BSTNode root;
public BinarySearchTree() {
this.root = null;
}
public void insert(int info) {
root = insertRec(root, info);
}
private BSTNode insertRec(BSTNode root, int info) {
if (root == null) {
root = new BSTNode(info);
return root;
}
if (info < root.info)
root.left = insertRec(root.left, info);
else if (info > root.info)
root.right = insertRec(root.right, info);
return root;
}
public void inorder() {
inorderRec(root);
}
private void inorderRec(BSTNode root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.info + " ");
inorderRec(root.right);
}}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inorder();
}}
Q2. Write a program to add methods to the binary search tree created in Q1 for
traversing the tree in pre-order, in-order, and post-order. Invoke the above
methods for execution.
Full same as 1 after that the following code
public void preorder() {
preorderRec(root);
System.out.println();
}
private void preorderRec(BSTNode root) {
if (root != null) {
System.out.print(root.info + " ");
preorderRec(root.left);
preorderRec(root.right);
}}
public void postorder() {
postorderRec(root);
System.out.println();
}
private void postorderRec(BSTNode root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.info + " ");
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.println("Inorder traversal:");
tree.inorder();
System.out.println("Preorder traversal:");
tree.preorder();
System.out.println("Postorder traversal:");
tree.postorder();
}}
Q3. Create a class Country containing members for name and population, along
with a constructor and necessary methods. Additionally, create a class BNode
with a member 'info' to store a country object, 'left' to refer to the left child, and
'right' to refer to the right child. Provide the required constructor. Finally,
create another class BST with a member 'root', along with the necessary
constructor and a method to insert a node into the binary search tree.

class Country {
String name;
int population;
public Country(String name, int population) {
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public String toString() {
return "Country{name='" + name + "', population=" + population + "}";}
}
class BNode {
Country info;
BNode left, right;
public BNode(Country info) {
this.info = info;
this.left = this.right = null;
}
}
class BST {
BNode root;
public BST() {
this.root = null;
}

public void insert(Country info) {


root = insertRec(root, info);
}
private BNode insertRec(BNode root, Country info) {
if (root == null) {
root = new BNode(info);
return root;
}
if (info.getPopulation() < root.info.getPopulation())
root.left = insertRec(root.left, info);
else if (info.getPopulation() > root.info.getPopulation())
root.right = insertRec(root.right, info);
return root;
}
public void inorder() {
inorderRec(root);
System.out.println();
}
private void inorderRec(BNode root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.info + " ");
inorderRec(root.right);
}
}
public static void main(String[] args) {
BST tree = new BST();
Country usa = new Country("USA", 331002651);
Country canada = new Country("Canada", 37742154);
Country mexico = new Country("Mexico", 128932753);
Country japan = new Country("Japan", 126476461);
Country india = new Country("India", 1380004385);
tree.insert(usa);
tree.insert(canada);
tree.insert(mexico);
tree.insert(japan);
tree.insert(india);
System.out.println("Inorder traversal of the BST:");
tree.inorder();
}}
Q4. Extend the BST created in Q3 by adding methods to traverse the tree in level order,
find the node with the maximum population (find-max), and find the node with the
minimum population (find-min). Invoke these methods for execution
import java.util.LinkedList;
import java.util.Queue;
Then same as Q3
public void levelOrder() {
if (root == null) return;
Queue<BNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
BNode currentNode = queue.poll();
System.out.print(currentNode.info + " ");
if (currentNode.left != null) {
queue.add(currentNode.left);
}
if (currentNode.right != null) {
queue.add(currentNode.right);
}}
System.out.println();
}
public Country findMax() {
return findMaxRec(root);
}
private Country findMaxRec(BNode root) {
if (root == null) return null;
while (root.right != null) {
root = root.right;
}
return root.info;
}
public Country findMin() {
return findMinRec(root);
}
private Country findMinRec(BNode root) {
if (root == null) return null;
while (root.left != null) {
root = root.left;
}
return root.info;
}
public static void main(String[] args) {
BST tree = new BST();
Country usa = new Country("USA", 331002651);
Country canada = new Country("Canada", 37742154);
Country mexico = new Country("Mexico", 128932753);
Country japan = new Country("Japan", 126476461);
Country india = new Country("India", 1380004385);
tree.insert(usa);
tree.insert(canada);
tree.insert(mexico);
tree.insert(japan);
tree.insert(india);
System.out.println("Inorder traversal of the BST:");
tree.inorder();
System.out.println("Level order traversal of the BST:");
tree.levelOrder();
Country maxCountry = tree.findMax();
System.out.println("Country with maximum population: " + maxCountry);
Country minCountry = tree.findMin();
System.out.println("Country with minimum population: " + minCountry);
}}

Q9. Create a class Graph with a linked list member to represent N number of
vertices. Implement the required constructor. Add a method to the Graph class
for reading a graph and storing it in an adjacency list representation.
Include a depth-first search (DFS) method in the Graph class to traverse the
vertices of the graph, and a main method to invoke all the methods.

import java.util.*;
class Graph {
private LinkedList<Integer>[] adjList;
private int numVertices;
public Graph(int numVertices) {
this.numVertices = numVertices;
adjList = new LinkedList[numVertices];
for (int i = 0; i < numVertices; i++) {
adjList[i] = new LinkedList<>();
}}
public void addEdge(int src, int dest) {
adjList[src].add(dest);
adjList[dest].add(src);
}
public void DFS(int startVertex) {
boolean[] visited = new boolean[numVertices];
DFSUtil(startVertex, visited);
}
private void DFSUtil(int vertex, boolean[] visited) {
visited[vertex] = true;
System.out.print(vertex + " ");
for (int adj : adjList[vertex]) {
if (!visited[adj]) {
DFSUtil(adj, visited);
}}}
public void displayList() {
System.out.println("Adjacency List:");
for (int i = 0; i < numVertices; i++) {
System.out.print(i + ": ");
for (int j : adjList[i]) {
System.out.print(j + " ");
}
System.out.println();
}}
public static void main(String[] args) {
Graph graph = new Graph(5);
graph.addEdge(0, 1);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
graph.displayList();
System.out.println("DFS traversal starting from vertex 0:");
graph.DFS(0); }}
Q5. Construct a binary search tree (BST) from the given array of elements:
{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}. Include a method called 'CreateTree' to
construct the binary search tree from a sorted array. This method takes an array
of integers as input and constructs the tree recursively using a binary search
algorithm.

class Country {
String name;
int population;
public Country(String name, int population) {
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public String toString() {
return "Country{name='" + name + "', population=" + population + "}";
}}
class BNode {
Country info;
BNode left, right;
public BNode(Country info) {
this.info = info;
this.left = this.right = null;
}}
class BST {
BNode root;
public BST() {
this.root = null;
}
public void insert(Country info) {
root = insertRec(root, info);
}
private BNode insertRec(BNode root, Country info) {
if (root == null) {
root = new BNode(info);
return root;
}
if (info.getPopulation() < root.info.getPopulation())
root.left = insertRec(root.left, info);
else if (info.getPopulation() > root.info.getPopulation())
root.right = insertRec(root.right, info);
return root;
}
public void inorder() {
inorderRec(root);
System.out.println();
}
private void inorderRec(BNode root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.info + " ");
inorderRec(root.right);
}}
public void createTree(int[] arr) {
root = createTreeRec(arr, 0, arr.length - 1);
}
private BNode createTreeRec(int[] arr, int start, int end) {
if (start > end) {
return null;
}
int mid = (start + end) / 2;
BNode node = new BNode(new Country("Country" + arr[mid], arr[mid]));
node.left = createTreeRec(arr, start, mid - 1);
node.right = createTreeRec(arr, mid + 1, end);
return node;
}
public static void main(String[] args) {
BST tree = new BST();
int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
tree.createTree(arr);
System.out.println("Inorder traversal of the BST:");
tree.inorder();
}}

Q6. Determine if a given binary tree is a binary search tree. You will use an
'isBST()' method, which takes the maximum and minimum range of the values of
the nodes.

class Country {
String name;
int population;
public Country(String name, int population) {
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public int getPopulation() {
return population;
}
public String toString() {
return "Country{name='" + name + "', population=" + population + "}";
}}
class BNode {
Country info;
BNode left, right;
public BNode(Country info) {
this.info = info;
this.left = this.right = null;
}}
class BST {
BNode root;
public BST() {
this.root = null;
}
public void insert(Country info) {
root = insertRec(root, info);
}
private BNode insertRec(BNode root, Country info) {
if (root == null) {
root = new BNode(info);
return root;
}
if (info.getPopulation() < root.info.getPopulation())
root.left = insertRec(root.left, info);
else if (info.getPopulation() > root.info.getPopulation())
root.right = insertRec(root.right, info);
return root;
}
public void inorder() {
inorderRec(root);
System.out.println();
}
private void inorderRec(BNode root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.info + " ");
inorderRec(root.right);
}}
public boolean isBST() {
return isBSTRec(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isBSTRec(BNode node, int min, int max) {
if (node == null) {
return true;
}
if (node.info.getPopulation() < min || node.info.getPopulation() > max) {
return false;
}
return isBSTRec(node.left, min, node.info.getPopulation() - 1) &&
isBSTRec(node.right, node.info.getPopulation() + 1, max);
}
public static void main(String[] args) {
BST tree = new BST();
Country usa = new Country("USA", 331002651);
Country canada = new Country("Canada", 37742154);
Country mexico = new Country("Mexico", 128932753);
Country japan = new Country("Japan", 126476461);
Country india = new Country("India", 1380004385);
tree.insert(usa);
tree.insert(canada);
tree.insert(mexico);
tree.insert(japan);
tree.insert(india);
System.out.println("Inorder traversal of the BST:");
tree.inorder();
System.out.println("Is the tree a BST? " + tree.isBST());
}
}

Q7. Remove node x from the binary search tree and reorganize the nodes to
maintain its necessary properties.
There are three cases in the deletion process. Let us denote the node that needs to
be deleted as x:
Case 1: Node x has no children.
Case 2: Node x has one child.
Case 3: Node x has two children.

Upto answer 6 inorderRec(root.right);}}


public void delete(int population) {
root = deleteRec(root, population);
}
private BNode deleteRec(BNode root, int population) {
if (root == null) return root;
if (population < root.info.getPopulation())
root.left = deleteRec(root.left, population);
else if (population > root.info.getPopulation())
root.right = deleteRec(root.right, population);
else {
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
root.info = minValue(root.right);
root.right = deleteRec(root.right, root.info.getPopulation());
}
return root;
}

private Country minValue(BNode root) {


Country minv = root.info;
while (root.left != null) {
root = root.left;
minv = root.info;
}
return minv;
}
public static void main(String[] args) {
BST tree = new BST();
Country usa = new Country("USA", 331002651);
Country canada = new Country("Canada", 37742154);
Country mexico = new Country("Mexico", 128932753);
Country japan = new Country("Japan", 126476461);
Country india = new Country("India", 1380004385);
tree.insert(usa);
tree.insert(canada);
tree.insert(mexico);
tree.insert(japan);
tree.insert(india);
System.out.println("Inorder traversal of the BST:");
tree.inorder();
System.out.println("Deleting node with population 128932753 (Mexico):");
tree.delete(128932753);
tree.inorder();
System.out.println("Deleting node with population 37742154 (Canada):");
tree.delete(37742154);
tree.inorder();
System.out.println("Deleting node with population 331002651 (USA):");
tree.delete(331002651);
tree.inorder();
}}
Q8. Write a program to implement the graph using adjacency matrix
representation and adjacency list representation. Create methods to display the
adjacency matrix and adjacency list of the graph.
import java.util.*;

class GraphMatrix {
private int[][] adjMatrix;
private int numVertices;
public GraphMatrix(int numVertices) {
this.numVertices = numVertices;
adjMatrix = new int[numVertices][numVertices];
}
public void addEdge(int i, int j) {
adjMatrix[i][j] = 1;
adjMatrix[j][i] = 1;
}
public void removeEdge(int i, int j) {
adjMatrix[i][j] = 0;
adjMatrix[j][i] = 0;
}
public void displayMatrix() {
System.out.println("Adjacency Matrix:");
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < numVertices; j++) {
System.out.print(adjMatrix[i][j] + " ");
}
System.out.println();
}}}
class GraphList {
private LinkedList<Integer>[] adjList;
private int numVertices;

public GraphList(int numVertices) {


this.numVertices = numVertices;
adjList = new LinkedList[numVertices];
for (int i = 0; i < numVertices; i++) {
adjList[i] = new LinkedList<>();
}
}
public void addEdge(int i, int j) {
adjList[i].add(j);
adjList[j].add(i);
}
public void removeEdge(int i, int j) {
adjList[i].remove((Integer) j);
adjList[j].remove((Integer) i);
}
public void displayList() {
System.out.println("Adjacency List:");
for (int i = 0; i < numVertices; i++) {
System.out.print(i + ": ");
for (int j : adjList[i]) {
System.out.print(j + " ");
}
System.out.println();
}}}
public class Q8 {
public static void main(String[] args) {
int numVertices = 5;

GraphMatrix graphMatrix = new GraphMatrix(numVertices);


graphMatrix.addEdge(0, 1);
graphMatrix.addEdge(0, 4);
graphMatrix.addEdge(1, 2);
graphMatrix.addEdge(1, 3);
graphMatrix.addEdge(1, 4);
graphMatrix.addEdge(2, 3);
graphMatrix.addEdge(3, 4);
graphMatrix.displayMatrix();

GraphList graphList = new GraphList(numVertices);


graphList.addEdge(0, 1);
graphList.addEdge(0, 4);
graphList.addEdge(1, 2);
graphList.addEdge(1, 3);
graphList.addEdge(1, 4);
graphList.addEdge(2, 3);
graphList.addEdge(3, 4);
graphList.displayList();
}
}

You might also like