Assignment
Assignment
216088403
ITEC 2620
Prof. Xin Tan
Table of Content
Printout of output for BST 2
Output for BST containing seven nodes where height of tree is maximum 2
Example 1 2
Example 2 2
Output for BST containing seven nodes where height of tree is minimum 3
Example 1 3
Example 2 3
Output for BST containing seven nodes where deleting the root node until the tree is
empty 3
1
Printout of output for BST
Output for BST containing seven nodes where height of tree is maximum
Example 1
Tree Display :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
Level 0: 0(root)
Level 1: 1(Right of 0)
Level 2: 2(Right of 1)
Level 3: 3(Right of 2)
Level 4: 4(Right of 3)
Level 5: 5(Right of 4)
Level 6: 6(Right of 5)
Example 2
Tree Display :
130
130 140
130 140 180
130 140 180 200
130 140 180 200 240
130 140 180 200 240 540
130 140 180 200 240 540 632
Level 0: 130(root)
Level 1: 140(Right of 130)
Level 2: 180(Right of 140)
Level 3: 200(Right of 180)
Level 4: 240(Right of 200)
Level 5: 540(Right of 240)
Level 6: 632(Right of 540)
2
Output for BST containing seven nodes where height of tree is minimum
Example 1
Tree Display :
Level 0: 3
Level 1: 1(Left of 3) 5 (Right of 3)
Level 2: 0(Left of 1 ) 2 (Right of 1
) 4(Left of 5) 6(Right of 5)
Example 2
Tree Display :
Level 0: 15
Level 1: 13(Left of 15) 1 7(Right of 15)
Level 2: 12(Left of 1 3) 1 4(Right of 1
3) 16(Left of 17) 1
8(Right of 1
7)
3
Output for BST containing seven nodes where deleting the root node until
the tree is empty
Tree Display :
Level 0: 3
Level 1: 1(Left of 3) 5 (Right of 3)
Level 2: 0(Left of 1 ) 2 (Right of 1
) 4(Left of 5) 6(Right of 5)
Removing node : 3
Level 0: 4
Level 1: 1 5
Level 2: 0 2 6
Removing node : 4
Level 0: 5
Level 1: 1 6
Level 2: 0 2
Removing node : 5
Level 0: 6
Level 1: 1
Level 2: 0 2
Removing node : 6
Level 0: 1
Level 1: 0 2
Removing node : 1
Level 0: 2
Level 1: 0
Removing node : 2
Level 0: 0
Removing node : 0
Empty
4
Printout of output for Sorting
Output for Insertion sort
Initially, the array is:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
5
Output for Bubble sort
Example
6
Output for Selection sort
Example
7
Output for Quick sort
Example
8
Printout of output for BFS/DFS
Example
T T F F F F F F F F
T T T F F F F F F F
T T T T F F F F F F
T T T T T F F F F F
T T T T T F T F F F
T T T T T F T T F F
T T T T T F T T T F
T T T T T T T T T F
T T T T T T T T T T
T T F F F F F F F F
T T F F T F F F F F
T T T F T F F F F F
T T T F T F T F F F
T T T F T F T T F F
T T T F T T T T F F
T T T T T T T T F F
T T T T T T T T T F
T T T T T T T T T T
9
Source Code of BST
BSTDriver0 - BST containing seven nodes where height of tree is
maximum
class BSTDriver0 {
while (x != null) {
y = x;
if (key < x.data)
x = x.left;
else
x = x.right;
}
// If the new key is less then the leaf node key
10
// Assign the new node to be its left child
else if (key < y.data) {
newnode.mesasgeTo = "(Left of " + y.data + ")";
y.left = newnode;
}
11
Inorder(root);
System.out.println();
}
BSTDriver1.printLevelOrder(root, true);
12
BSTDriver1 - BST containing seven nodes where height of tree is minimum
import java.util.LinkedList;
import java.util.Queue;
if (isMsg) {
System.out.print(" " + node.data + node.mesasgeTo);
} else {
System.out.print(" " + node.data);
}
q.remove();
if (node.left != null)
q.add(node.left);
if (node.right != null)
13
q.add(node.right);
nodeCount--;
}
i++;
System.out.println();
}
}
public BSTNode convert(int[] arrA, int start, int end, String msg) {
if (start > end) {
return null;
}
int mid = (start + end) / 2;
BSTNode root = new BSTNode(arrA[mid]);
root.left = convert(arrA, start, mid - 1, "(Left of " + root.data +
")");
root.right = convert(arrA, mid + 1, end, "(Right of " + root.data +
")");
root.mesasgeTo = msg;
return root;
}
}
}
class BSTNode {
int data;
BSTNode left;
BSTNode right;
String mesasgeTo;
14
mesasgeTo = null;
}
}
15
BSTDriver2 - BST containing seven nodes where deleting the root node
until the tree is empty
import java.util.LinkedList;
import java.util.Queue;
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
BSTNode temp = root;
// Finding minimum element from right
BSTNode minNodeForRight = minimumElement(temp.right);
// Replacing current node with minimum node from right
subtree
root.data = minNodeForRight.data;
// Deleting minimum node from right now
root.right = deleteNode(root.right, minNodeForRight.data);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
16
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
q.add(root);
while (true) {
int nodeCount = q.size();
if (nodeCount == 0)
break;
q.remove();
System.out.println("Removing node : " + node.data);
BSTNode tempnode = deleteNode(root, node.data);
if (tempnode != null) {
root = tempnode;
BSTDriver1.printLevelOrder(root, false);
q.add(root);
} else {
System.out.println("Empty");
}
nodeCount--;
}
17
System.out.println();
}
}
18
Source code of Sorting Algorithms
Solver.java
public class Solver {
public static void main(String[] args) {
final int SIZE = 100;
int[] array = new int[SIZE];
s.initializeArray(array, SIZE);
System.out.print("Initially, the array is:");
s.displayArray(array, SIZE);
System.out.println();
System.out.print("After randomization, the array becomes:");
s.randomizeArray(array, SIZE);
s.displayArray(array, SIZE);
System.out.println();
s.displayArray(array, SIZE);
}
}
19
Sort.java
import java.util.Random;
20
}
// insertionSort.
int j;
a[j] = tmp;
// bubbleSort.
public void bubbleSort(int a[], int size) {
int n = a.length;
21
}
// selectionSort
int index = i;
index = j;
a[index] = a[i];
a[i] = min;
int i = (p - 1);
22
if (num[j] <= piv) {
i++;
num[i] = num[j];
num[j] = temp;
num[q] = temp;
return i + 1;
if (l < h) {
qsort(arr, l, pi - 1);
// quickSort
23
// your code here. for this method, you are suggested to write
24
Source code of Graph search
SolverGraph.java
adjMatrix = {
// A, B, C, D, E, F, G, H, I, J,
{ 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }, // A
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, // B
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // C
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, // D
{ 0, 0, 1, 0, 0, 0, 1, 1, 0, 0 }, // E
{ 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // F
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, // G
{ 0, 0, 0, 0, 0, 1, 0, 0, 1, 1 }, // H
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // I
{ 0, 0, 0, 0, 0, 1, 0, 0, 1, 0 } // J
};
SearchMethods sm = new SearchMethods();
25
}
26
Searchmethods.java
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
// this method should be called each time the array reached is updated.
private void displayReached(boolean[] r) {
for (int i = 0; i < r.length; i++) {
if (r[i] == false) {
System.out.print(" F");
} else {
System.out.print(" T");
}
}
System.out.println();
}
27
public void breadthFirstSearch(Integer[][] M, int startVertex) {
// your implementation here.
boolean[] visited = new boolean[M.length];
visited[startVertex - 1] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(startVertex);
// System.out.println("The breadth first order is");
while (!queue.isEmpty()) {
// System.out.println(queue.peek());
int x = queue.poll();
int i;
for (i = 0; i < M.length; i++) {
if (M[x - 1][i] == 1 && visited[i] == false) {
queue.add(i + 1);
visited[i] = true;
displayReached(visited);
}
}
}
}
28
}
29