Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
54 views

Assignment

This document contains the output and source code for various algorithms including binary search trees (BST), sorting algorithms, and graph search algorithms. It includes examples of BST output showing trees of different heights and a tree being emptied by removing nodes. It also includes output showing insertion sort and bubble sort being applied to random arrays and the sorted results. Source code is provided for BST, sorting algorithms, and graph search algorithms.

Uploaded by

MEHUL RATHOD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Assignment

This document contains the output and source code for various algorithms including binary search trees (BST), sorting algorithms, and graph search algorithms. It includes examples of BST output showing trees of different heights and a tree being emptied by removing nodes. It also includes output showing insertion sort and bubble sort being applied to random arrays and the sorted results. Source code is provided for BST, sorting algorithms, and graph search algorithms.

Uploaded by

MEHUL RATHOD
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Krishika Gupta

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

Printout of output for Sorting 4


Output for Insertion sort 4
Output for Bubble sort 5
Example 5
Output for Selection sort 6
Example 6
Output for Quick sort 7
Example 7

Printout of output for BFS/DFS 8


Example 8

Source Code of BST 9


BSTDriver0 - BST containing seven nodes where height of tree is maximum 9
BSTDriver1 - BST containing seven nodes where height of tree is minimum 12
BSTDriver2 - BST containing seven nodes where deleting the root node until the tree is
empty 14

Source code of Sorting Algorithms 16


Solver.java 16
Sort.java 17

Source code of Graph search 22


SolverGraph.java 22
Searchmethods.java 23

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

After randomization, the array becomes:


94​ ​65​ ​89​ ​52​ ​87​ ​32​ ​71​ ​81​ ​88​ ​36
60​ ​97​ ​86​ ​31​ ​58​ ​28​ ​57​ ​85​ ​3​ ​9
39​ ​47​ ​29​ ​6​ ​56​ ​77​ ​63​ ​59​ ​79​ ​35
43​ ​1​ ​82​ ​24​ ​99​ ​49​ ​80​ ​30​ ​72​ ​92
73​ ​5​ ​46​ ​16​ ​37​ ​14​ ​15​ ​74​ ​41​ ​83
61​ ​62​ ​8​ ​91​ ​23​ ​33​ ​42​ ​2​ ​0​ ​69
26​ ​50​ ​44​ ​90​ ​70​ ​76​ ​53​ ​54​ ​64​ ​11
48​ ​93​ ​27​ ​17​ ​96​ ​78​ ​12​ ​4​ ​45​ ​7
38​ ​13​ ​55​ ​19​ ​40​ ​25​ ​84​ ​98​ ​20​ ​10
34​ ​67​ ​21​ ​51​ ​22​ ​75​ ​18​ ​68​ ​66​ ​95

The array is now sorted using InsertionSort:


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

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

After randomization, the array becomes:


53​ ​32​ ​76​ ​20​ ​70​ ​4​ ​40​ ​48​ ​50​ ​3
27​ ​15​ ​96​ ​54​ ​89​ ​80​ ​68​ ​87​ ​91​ ​10
75​ ​49​ ​24​ ​21​ ​33​ ​95​ ​67​ ​18​ ​79​ ​2
37​ ​88​ ​86​ ​31​ ​26​ ​12​ ​61​ ​13​ ​41​ ​25
98​ ​11​ ​97​ ​39​ ​83​ ​74​ ​99​ ​38​ ​65​ ​55
7​ ​64​ ​77​ ​92​ ​44​ ​5​ ​81​ ​34​ ​17​ ​14
36​ ​23​ ​6​ ​72​ ​30​ ​60​ ​52​ ​57​ ​29​ ​35
42​ ​78​ ​69​ ​46​ ​62​ ​73​ ​9​ ​85​ ​56​ ​43
63​ ​1​ ​16​ ​47​ ​66​ ​19​ ​93​ ​28​ ​90​ ​94
22​ ​0​ ​58​ ​51​ ​84​ ​59​ ​71​ ​8​ ​45​ ​82

The array is now sorted using Bubblesort:


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

6
Output for Selection sort

Example

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

After randomization, the array becomes:


17​ ​61​ ​37​ ​4​ ​56​ ​16​ ​67​ ​99​ ​92​ ​39
84​ ​76​ ​78​ ​51​ ​55​ ​46​ ​50​ ​80​ ​70​ ​33
41​ ​90​ ​98​ ​27​ ​91​ ​72​ ​18​ ​58​ ​31​ ​66
87​ ​42​ ​64​ ​47​ ​43​ ​28​ ​9​ ​54​ ​93​ ​29
26​ ​34​ ​48​ ​71​ ​74​ ​60​ ​13​ ​14​ ​57​ ​30
19​ ​0​ ​24​ ​12​ ​3​ ​1​ ​63​ ​94​ ​85​ ​53
25​ ​15​ ​73​ ​44​ ​5​ ​6​ ​36​ ​21​ ​35​ ​88
77​ ​52​ ​62​ ​38​ ​10​ ​96​ ​83​ ​89​ ​7​ ​69
59​ ​20​ ​82​ ​65​ ​81​ ​45​ ​32​ ​22​ ​49​ ​79
86​ ​75​ ​8​ ​23​ ​97​ ​40​ ​95​ ​68​ ​11​ ​2

The array is now sorted using Selectionsort:


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

7
Output for Quick sort

Example

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

After randomization, the array becomes:


62​ ​99​ ​40​ ​14​ ​55​ ​26​ ​35​ ​5​ ​50​ ​76
51​ ​67​ ​56​ ​61​ ​66​ ​25​ ​21​ ​91​ ​97​ ​72
28​ ​17​ ​70​ ​53​ ​36​ ​0​ ​2​ ​86​ ​58​ ​16
12​ ​1​ ​22​ ​98​ ​95​ ​32​ ​4​ ​78​ ​83​ ​15
90​ ​92​ ​96​ ​79​ ​10​ ​7​ ​3​ ​9​ ​44​ ​85
20​ ​13​ ​54​ ​52​ ​47​ ​23​ ​8​ ​29​ ​89​ ​6
57​ ​87​ ​11​ ​34​ ​71​ ​41​ ​69​ ​93​ ​38​ ​94
39​ ​30​ ​18​ ​48​ ​84​ ​49​ ​68​ ​31​ ​75​ ​77
33​ ​27​ ​46​ ​81​ ​60​ ​88​ ​43​ ​80​ ​65​ ​74
37​ ​45​ ​24​ ​59​ ​19​ ​64​ ​42​ ​63​ ​73​ ​82

The array is now sorted using Quicksort:


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

8
Printout of output for BFS/DFS
Example

The input graph G in adjacent Matrix is:

​0​ ​1​ ​1​ ​1​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0


​0​ ​0​ ​0​ ​0​ ​1​ ​0​ ​0​ ​0​ ​0​ ​ 0
​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​ 0
​0​ ​0​ ​0​ ​0​ ​1​ ​0​ ​0​ ​0​ ​0​ ​ 0
​0​ ​0​ ​1​ ​0​ ​0​ ​0​ ​1​ ​1​ ​0​ ​ 0
​0​ ​0​ ​0​ ​1​ ​1​ ​0​ ​0​ ​0​ ​0​ ​ 0
​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​1​ ​1​ ​ 0
​0​ ​0​ ​0​ ​0​ ​0​ ​1​ ​0​ ​0​ ​1​ ​ 1
​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​0​ ​ 0
​0​ ​0​ ​0​ ​0​ ​0​ ​1​ ​0​ ​0​ ​1​ ​ 0

Performing the BFS on G with start vertex A:

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

Performing the DFS on G with start vertex A:

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​ {

​// A utility function to insert a new


​// Node with given key in BST
​static​ BSTNode ​insert​(BSTNode root, ​int​ key) {
​// Create a new Node containing
​// the new element
BSTNode newnode = ​new​ BSTNode(key);

​// Pointer to start traversing from root and


​// traverses downward path to search
​// where the new node to be inserted
BSTNode x = root;

​// Pointer y maintains the trailing


​// pointer of x
BSTNode y = ​null​;

​while​ (x != ​null​) {
y = x;
​if​ (key < x.data)
x = x.left;
​else
x = x.right;
}

​// If the root is null i.e the tree is empty


​// The new node is the root node
​if​ (y == ​null​) {
newnode.mesasgeTo = ​"(root)"​;
y = newnode;
}

​// 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;
}

​// else assign the new node its right child


​else​ {
newnode.mesasgeTo = ​"(Right of "​ + y.data + ​")"​;
y.right = newnode;
}

​// Returns the pointer where the


​// new node is inserted
​return​ y;
}

​// A utility function to do inorder


​// traversal of BST
​static​ ​void​ ​Inorder​(BSTNode root) {
​if​ (root == ​null​)
​return​;
​else​ {
Inorder(root.left);
System.out.print(root.data + ​" "​);
Inorder(root.right);
}
}

​// Driver code


​public​ ​static​ ​void​ ​main​(String args[]) {

BSTNode root = ​null​;


​int​[] keys = { ​130​, ​140​, ​180​, ​200​, 2
​ 40​, ​540​, ​632​ };
System.out.println(​"Tree Display : "​);
​for​ (​int​ key : keys) {
​if​ (root == ​null​) {
root = insert(root, key);
} ​else​ {
insert(root, key);
}

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;

public​ ​class​ ​BSTDriver1​ {


​static​ ​void​ ​printLevelOrder​(BSTNode root, Boolean isMsg) {
​// Base Case
​if​ (root == ​null​)
​return​;

​// Create an empty queue for level order tarversal


Queue<BSTNode> q = ​new​ LinkedList<BSTNode>();

​// Enqueue Root and initialize height


q.add(root);
​int​ i = ​0​;
​while​ (​true​) {

​// nodeCount (queue size) indicates number of nodes


​// at current level.
​int​ nodeCount = q.size();
​if​ (nodeCount == ​0​)
​break​;

​// Dequeue all nodes of current level and Enqueue all


​// nodes of next level
System.out.print(​"Level "​ + i + ​":"​);
​while​ (nodeCount > ​0​) {
BSTNode node = q.peek();

​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;
}

​public​ ​static​ ​void​ ​main​(String args[]) {


​int​[] arrA = { ​12​, ​13​, ​14​, ​15​, ​16​, ​17​, ​18​ };
BSTDriver1 s = ​new​ BSTDriver1();
BSTNode x = s.convert(arrA, ​0​, arrA.length - ​1​, " ​ "​);
System.out.println(​"Tree Display : "​);
printLevelOrder(x, ​true​);

}
}

class​ ​BSTNode​ {
​int​ data;
BSTNode left;
BSTNode right;
String mesasgeTo;

​public​ ​BSTNode​(​int​ data) {


​this​.data = data;
left = ​null​;
right = ​null​;

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;

public​ ​class​ ​BSTDriver2​ {

​public​ ​static​ BSTNode ​minimumElement​(BSTNode root) {


​if​ (root.left == ​null​)
​return​ root;
​else​ {
​return​ minimumElement(root.left);
}
}

​public​ ​static​ BSTNode ​deleteNode​(BSTNode root, ​int​ value) {


​if​ (root == ​null​)
​return​ ​null​;
​if​ (root.data > value) {
root.left = deleteNode(root.left, value);
} ​else​ ​if​ (root.data < value) {
root.right = deleteNode(root.right, value);

} ​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;
}

​static​ v ​ oid​ ​findRootNode​(BSTNode root) {


​// Base Case
​if​ (root == ​null​)
​return​;

Queue<BSTNode> q = ​new​ LinkedList<BSTNode>();

q.add(root);

​while​ (​true​) {
​int​ nodeCount = q.size();
​if​ (nodeCount == ​0​)
​break​;

​while​ (nodeCount > ​0​) {


BSTNode node = q.peek();

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();
}
}

​public​ ​static​ ​void​ ​main​(String[] args) {

​int​[] arrA = { ​0​, ​1​, ​2​, ​3​, ​4​, ​5​, ​6​ };


BSTDriver1 s = ​new​ BSTDriver1();
BSTNode x = s.convert(arrA, ​0​, arrA.length - ​1​, "​ "​);
System.out.println(​"Tree Display : "​);
BSTDriver1.printLevelOrder(x, ​true​);
findRootNode(x);

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];

Sort s = ​new​ Sort();

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();

System.out.print(​"The array is now sorted using InsertionSort:"​);


s.insertionSort(array, SIZE);
System.out.print(​"The array is now sorted using Bubblesort:"​);
s.bubbleSort(array, SIZE);
System.out.print(​"The array is now sorted using Selectionsort:"​);
s.selectionSort(array, SIZE);
System.out.print(​"The array is now sorted using Quicksort:"​);
s.quickSort(array, SIZE);

s.displayArray(array, SIZE);

}
}

19
Sort.java
import​ java.util.Random;

public​ ​class​ ​Sort​ {


​// swap the ith element with the jth elements.
​private​ ​void​ ​swap​(​int​[] a, ​int​ i, ​int​ j) {
​int​ temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}

​// initialize the array a with elements from 0 to size-1.


​public​ ​void​ ​initializeArray​(​int​[] a, ​int​ size) {
​for​ (​int​ i = ​0​; i < size; i++) {
a[i] = i;
}
}

​// display the elements in the array a, 10 elements per row.


​public​ ​void​ ​displayArray​(​int​[] a, ​int​ size) {
​for​ (​int​ i = ​0​; i < size; i++) {
​if​ (i % ​10​ == ​0​) {
System.out.println();
}
System.out.print(a[i] + ​" "​);
}
System.out.println();
}

​// randomly swap two elements in array a for SWAPTIMES times.


​public​ ​void​ ​randomizeArray​(​int​[] a, ​int​ size) {
​final​ ​int​ SWAPTIMES = ​10000​;
Random r = ​new​ Random();
​int​ j, k;
​for​ (​int​ i = ​0​; i < SWAPTIMES; i++) {
j = r.nextInt(size);
k = r.nextInt(size);
​this​.swap(a, j, k);
}

20
}

​// insertionSort.

​public​ ​void​ ​insertionSort​(​int​[] a, ​int​ size) {

​// your code here.

​int​ j;

​for​ (​int​ p = ​1​; p < a.length; p++) {

​int​ tmp = a[p];

​for​ (j = p; j > ​0​ && tmp < a[j - ​1​]; j--)

a[j] = a[j - ​1​];

a[j] = tmp;

​// bubbleSort.
​public​ ​void​ ​bubbleSort​(​int​ a[], ​int​ size) {

​int​ n = a.length;

​for​ (​int​ i = ​0​; i < n - ​1​; i++)

​for​ (​int​ j = ​0​; j < n - i - ​1​; j++)

​if​ (a[j] > a[j + ​1​]) {

​// swap temp and arr[i]

​int​ temp = a[j];

a[j] = a[j + ​1​];

a[j + ​1​] = temp;

21
}

​// selectionSort

​public​ ​void​ ​selectionSort​(​int​ a[], ​int​ size) {

​// your code here.

​for​ (​int​ i = ​0​; i < a.length - ​1​; i++) {

​int​ index = i;

​for​ (​int​ j = i + ​1​; j < a.length; j++) {

​if​ (a[j] < a[index]) {

index = j;

​int​ min = a[index];

a[index] = a[i];

a[i] = min;

​private​ ​static​ ​int​ ​partition​(​int​ num[], ​int​ p, ​int​ q) {

​int​ piv = num[q];

​int​ i = (p - ​1​);

​for​ (​int​ j = p; j < q; j++) {

22
​if​ (num[j] <= piv) {

i++;

​int​ temp = num[i];

num[i] = num[j];

num[j] = temp;

​int​ temp = num[i + ​1​];

num[i + ​1​] = num[q];

num[q] = temp;

​return​ i + ​1​;

​private​ ​static​ ​void​ ​qsort​(​int​ arr[], ​int​ l, ​int​ h) {

​if​ (l < h) {

​int​ pi = partition(arr, l, h);

qsort(arr, l, pi - ​1​);

qsort(arr, pi + ​1​, h);

​// quickSort

​public​ ​void​ ​quickSort​(​int​[] a, ​int​ size) {

23
​// your code here. for this method, you are suggested to write

​// additional helper methods as appropriate on top of quickSort.

qsort(a, ​0​, size - ​1​);

24
Source code of Graph search
SolverGraph.java

public​ ​class​ ​SolverGraph​ {

​public​ ​static​ ​void​ ​main​(String[] args) {

Integer[][] ​// adjMatrix

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();

System.out.println(​"The input graph G in adjacent Matrix is:"​);


System.out.println();
sm.displayMatrix(adjMatrix);
System.out.println();

System.out.println(​"Performing the BFS on G with start vertex A: "​);


System.out.println();
sm.breadthFirstSearch(adjMatrix, ​1​);
System.out.println();

System.out.println(​"Performing the DFS on G with start vertex A: "​);


System.out.println();
sm.depthFirstSearch(adjMatrix, ​1​);

25
}

26
Searchmethods.java

import​ java.util.LinkedList;
import​ java.util.Queue;
import​ java.util.Stack;

public​ ​class​ ​SearchMethods​ {


​// initialize the adjacent matrix: Matrix to all zeros.

​public​ ​void​ ​initMatrix​(Integer[][] Matrix) {


​for​ (​int​ i = ​0​; i < Matrix.length; i++) {
​for​ (​int​ j = ​0​; j < Matrix[i].length; j++) {
Matrix[i][j] = ​0​;
}
}

​// disply the adjacent matrix: Matrix.


​public​ ​void​ ​displayMatrix​(Integer[][] Matrix) {
​for​ (​int​ i = ​0​; i < Matrix.length; i++) {
​for​ (​int​ j = ​0​; j < Matrix[i].length; j++) {
System.out.print(​" "​ + Matrix[i][j]);
}
System.out.println();
}

​// 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);
}
}
}
}

​public​ ​void​ ​depthFirstSearch​(Integer[][] M, ​int​ startVertex) {


​// your implementation here.
​boolean​[] visited = ​new​ ​boolean​[M.length];
visited[startVertex - ​1​] = ​true​;
Stack<Integer> stack = ​new​ Stack<>();
stack.push(startVertex);
​int​ i, x;
​// System.out.println("The depth first order is");
​// System.out.println(startVertex);
​while​ (!stack.isEmpty()) {
x = stack.pop();
​for​ (i = ​0​; i < M.length; i++) {
​if​ (M[x - ​1​][i] == ​1​ && visited[i] == ​false​) {
stack.push(x);
visited[i] = ​true​;
displayReached(visited);
​// System.out.println(i + 1);
x = i + ​1​;
i = -​1​;
}
}

28
}

29

You might also like