0% found this document useful (0 votes)
6 views

STS

The document contains multiple Java programs that implement various data structures and algorithms, including a binary tree for printing boundary nodes, a sliding window maximum algorithm, cycle detection in linked lists, segregation of even and odd nodes in linked lists, and a celebrity problem solution. Each program includes methods for inserting nodes, detecting cycles, and processing user input. The document demonstrates practical applications of data structures such as trees and linked lists in solving computational problems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

STS

The document contains multiple Java programs that implement various data structures and algorithms, including a binary tree for printing boundary nodes, a sliding window maximum algorithm, cycle detection in linked lists, segregation of even and odd nodes in linked lists, and a celebrity problem solution. Each program includes methods for inserting nodes, detecting cycles, and processing user input. The document demonstrates practical applications of data structures such as trees and linked lists in solving computational problems.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

//Print Boundary

import java.util.Scanner;

class Node {
int data;
Node left, right;

Node(int item) {
data = item;
left = right = null;
}
}

public class PrintBoundary {


static Node root;

// Function to insert a node into the binary tree


static Node insert(Node root, int data) {
if (root == null)
return new Node(data);
if (root.data < data)
root.right = insert(root.right, data);
else
root.left = insert(root.left, data);
return root;
}

// Function to print the boundary nodes of a binary tree


static void printBoundary(Node node) {
if (node != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
printLeaves(node.left);
printLeaves(node.right);
printBoundaryRight(node.right);
}
}

// Function to print the left boundary nodes of a binary tree


static void printBoundaryLeft(Node node) {
if (node != null) {
if (node.left != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.left);
} else if (node.right != null) {
System.out.print(node.data + " ");
printBoundaryLeft(node.right);
}
}
}

// Function to print the right boundary nodes of a binary tree


static void printBoundaryRight(Node node) {
if (node != null) {
if (node.right != null) {
printBoundaryRight(node.right);
System.out.print(node.data + " ");
} else if (node.left != null) {
printBoundaryRight(node.left);
System.out.print(node.data + " ");
}
}
}

// Function to print the leaves of a binary tree


static void printLeaves(Node node) {
if (node != null) {
printLeaves(node.left);
if (node.left == null && node.right == null)
System.out.print(node.data + " ");
printLeaves(node.right);
}
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
int input = sc.nextInt();

while (input >= 0) {


root = insert(root, input);
input = sc.nextInt();
}

printBoundary(root);
sc.close();
}
}

//Stack perm
import java.util.*;
public class Max_Sliding {
static void maximum(int arr[], int N, int K)
{
int j, max;
for (int i = 0; i <= N - K; i++) {
max = arr[i];
for (j = 1; j < K; j++) {
if (arr[i + j] > max)
max = arr[i + j];
}
System.out.print(max + " ");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int K = sc.nextInt();
maximum(arr,n, K);
}
}

//max sliding
import java.util.*;
public class Max_Sliding {
static void maximum(int arr[], int N, int K)
{
int j, max;
for (int i = 0; i <= N - K; i++) {
max = arr[i];
for (j = 1; j < K; j++) {
if (arr[i + j] > max)
max = arr[i + j];
}
System.out.print(max + " ");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
int K = sc.nextInt();
maximum(arr,n, K);
}
}

//loop
import java.util.Scanner;

class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

public class Loop {

static Node head;

// Function to insert a node at the beginning of the linked list


static void insert(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

// Function to detect a cycle in a linked list using Floyd's Cycle-


Finding Algorithm
static boolean detectCycle() {
Node slow = head, fast = head;

while (fast != null && fast.next != null) {


slow = slow.next;
fast = fast.next.next;

// If there is a cycle, the pointers will meet at some


point
if (slow == fast) {
return true;
}
}
return false;
}

// Function to create a cycle in the linked list


static void createCycle(int a, int b) {
if (head == null || head.next == null) {
System.out.println("Error: Insufficient nodes in the linked
list to create a cycle.");
return;
}

Node p1 = head;
Node p2 = head;

// Move p1 to the 'a' position


for (int i = 0; i < a; i++) {
p1 = p1.next;

// Check if 'a' is out of bounds


if (p1 == null) {
System.out.println("Error: Position 'a' is out of
bounds.");
return;
}
}

// Move p2 to the 'b' position


for (int i = 0; i < b; i++) {
p2 = p2.next;

// Check if 'b' is out of bounds


if (p2 == null) {
System.out.println("Error: Position 'b' is out of
bounds.");
return;
}
}

// Create a cycle
p2.next = p1;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of nodes in the linked


list:");
int n = scanner.nextInt();
System.out.println("Enter the elements of the linked list:");

// Creating the linked list


for (int i = 0; i < n; i++) {
int data = scanner.nextInt();
insert(data);
}

System.out.println("Enter the positions (a and b) to create a


cycle (0-based indexing):");
int a = scanner.nextInt();
int b = scanner.nextInt();

// Creating a cycle
createCycle(a, b);

// Detecting a cycle
if (detectCycle()) {
System.out.println("Cycle detected in the linked list.");
} else {
System.out.println("No cycle detected in the linked
list.");
}

scanner.close();
}
}

//PQDLL
import java.util.Scanner;

class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

public class EvenOdd {


static Node head;

static void insert(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}

static void segregateEvenOdd() {


if (head == null || head.next == null) {
return; // No need to segregate if the list has 0 or 1 node
}

Node evenStart = null, evenEnd = null;


Node oddStart = null, oddEnd = null;

Node current = head;

while (current != null) {


int data = current.data;

if (data % 2 == 0) {
if (evenStart == null) {
evenStart = current;
evenEnd = evenStart;
} else {
evenEnd.next = current;
evenEnd = evenEnd.next;
}
} else {
if (oddStart == null) {
oddStart = current;
oddEnd = oddStart;
} else {
oddEnd.next = current;
oddEnd = oddEnd.next;
}
}

current = current.next;
}

if (evenStart == null || oddStart == null) {


return; // No need to segregate if there are no even or odd
nodes
}
evenEnd.next = oddStart;
oddEnd.next = null;

head = evenStart;
}

static void display() {


Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the linked


list: ");
int n = scanner.nextInt();

System.out.println("Enter the elements of the linked list:");


for (int i = 0; i < n; i++) {
int data = scanner.nextInt();
insert(data);
}

System.out.println("Linked list before segregation:");


display();

segregateEvenOdd();

System.out.println("Linked list after segregating even and odd


nodes:");
display();

scanner.close();
}
}

//BiotonicDLL
import java.util.Scanner;

class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

public class Loop {

static Node head;

// Function to insert a node at the beginning of the linked list


static void insert(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

// Function to detect a cycle in a linked list using Floyd's Cycle-


Finding Algorithm
static boolean detectCycle() {
Node slow = head, fast = head;

while (fast != null && fast.next != null) {


slow = slow.next;
fast = fast.next.next;

// If there is a cycle, the pointers will meet at some


point
if (slow == fast) {
return true;
}
}
return false;
}

// Function to create a cycle in the linked list


static void createCycle(int a, int b) {
if (head == null || head.next == null) {
System.out.println("Error: Insufficient nodes in the linked
list to create a cycle.");
return;
}

Node p1 = head;
Node p2 = head;
// Move p1 to the 'a' position
for (int i = 0; i < a; i++) {
p1 = p1.next;

// Check if 'a' is out of bounds


if (p1 == null) {
System.out.println("Error: Position 'a' is out of
bounds.");
return;
}
}

// Move p2 to the 'b' position


for (int i = 0; i < b; i++) {
p2 = p2.next;

// Check if 'b' is out of bounds


if (p2 == null) {
System.out.println("Error: Position 'b' is out of
bounds.");
return;
}
}

// Create a cycle
p2.next = p1;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of nodes in the linked


list:");
int n = scanner.nextInt();

System.out.println("Enter the elements of the linked list:");

// Creating the linked list


for (int i = 0; i < n; i++) {
int data = scanner.nextInt();
insert(data);
}

System.out.println("Enter the positions (a and b) to create a


cycle (0-based indexing):");
int a = scanner.nextInt();
int b = scanner.nextInt();
// Creating a cycle
createCycle(a, b);

// Detecting a cycle
if (detectCycle()) {
System.out.println("Cycle detected in the linked list.");
} else {
System.out.println("No cycle detected in the linked
list.");
}

scanner.close();
}
}

//Even Odd
import java.util.Scanner;

class Node {
int data;
Node next;

Node(int data) {
this.data = data;
this.next = null;
}
}

public class EvenOdd {


static Node head;

static void insert(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}

static void segregateEvenOdd() {


if (head == null || head.next == null) {
return; // No need to segregate if the list has 0 or 1 node
}

Node evenStart = null, evenEnd = null;


Node oddStart = null, oddEnd = null;

Node current = head;

while (current != null) {


int data = current.data;

if (data % 2 == 0) {
if (evenStart == null) {
evenStart = current;
evenEnd = evenStart;
} else {
evenEnd.next = current;
evenEnd = evenEnd.next;
}
} else {
if (oddStart == null) {
oddStart = current;
oddEnd = oddStart;
} else {
oddEnd.next = current;
oddEnd = oddEnd.next;
}
}

current = current.next;
}

if (evenStart == null || oddStart == null) {


return; // No need to segregate if there are no even or odd
nodes
}

evenEnd.next = oddStart;
oddEnd.next = null;

head = evenStart;
}

static void display() {


Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the linked


list: ");
int n = scanner.nextInt();

System.out.println("Enter the elements of the linked list:");


for (int i = 0; i < n; i++) {
int data = scanner.nextInt();
insert(data);
}

System.out.println("Linked list before segregation:");


display();

segregateEvenOdd();

System.out.println("Linked list after segregating even and odd


nodes:");
display();

scanner.close();
}
}

//Celebrity Problem
import java.util.Scanner;

public class CelebrityProblem {

static int findCelebrity(int[][] matrix, int n) {


int candidate = 0;

// Find a potential candidate


for (int i = 1; i < n; i++) {
if (knows(candidate, i, matrix)) {
candidate = i;
}
}

// Verify if the candidate is a celebrity


for (int i = 0; i < n; i++) {
if (i != candidate && (knows(candidate, i, matrix) || !
knows(i, candidate, matrix))) {
return -1; // No celebrity found
}
}

return candidate;
}

static boolean knows(int a, int b, int[][] matrix) {


return matrix[a][b] == 1;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of people: ");


int n = scanner.nextInt();

// Create a matrix to represent who knows whom (1 means they


know, 0 means they don't)
int[][] matrix = new int[n][n];

System.out.println("Enter the matrix elements (1 for knowing, 0


for not knowing):");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}

int celebrity = findCelebrity(matrix, n);

if (celebrity != -1) {
System.out.println("Celebrity found at index: " +
celebrity);
} else {
System.out.println("No celebrity found.");
}

scanner.close();
}
}

//Min Stack
import java.util.Scanner;
import java.util.Stack;

class Mystack {
Stack<Integer> s;
Stack<Integer> a;

Mystack() {
s = new Stack<>();
a = new Stack<>();
}

void getMin() {
if (a.isEmpty())
System.out.println("Stack is Empty");
else
System.out.println("Minimum element: " + a.peek());
}

void peek() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
}
int t = s.peek();
System.out.println("Top most element: " + t);
}

void pop() {
if (s.isEmpty()) {
System.out.println("Stack is Empty");
return;
} else {
int t = s.pop();
System.out.println("Removed element: " + t);

if (t == a.peek())
a.pop();
}
}

public void push(int x) {


s.push(x);

if (a.isEmpty() || x <= a.peek()) {


a.push(x);
}
}
}

public class MinStack {


public static void main(String args[]) {
Mystack s = new Mystack();
Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of elements: ");


int n = sc.nextInt();

for (int i = 0; i < n; i++) {


System.out.print("Enter element " + (i + 1) + ": ");
int m = sc.nextInt();
s.push(m);
}

s.getMin();
s.pop();
s.getMin();
s.pop();
s.push(1);
s.peek();
s.getMin();

}
}

//stockspan
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

public class Stockspan {


static void calculateStockSpan(int[] arr, int n, int[] S) {
Stack<Integer> stack = new Stack<>();
stack.push(0);
S[0] = 1;

for (int i = 1; i < n; i++) {


while (!stack.isEmpty() && arr[stack.peek()] <= arr[i]) {
stack.pop();
}
S[i] = stack.isEmpty() ? (i + 1) : (i - stack.peek());
stack.push(i);
}
}
static void printArray(int[] arr) {
System.out.println(Arrays.toString(arr));
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of days:");


int n = scanner.nextInt();
int[] arr = new int[n];

System.out.println("Enter the stock prices for each day:");

for (int i = 0; i < n; i++) {


arr[i] = scanner.nextInt();
}

int[] stockSpans = new int[n];


calculateStockSpan(arr, n, stockSpans);

System.out.println("Stock Span for each day:");


printArray(stockSpans);

scanner.close();
}
}

You might also like