Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
478 changes: 239 additions & 239 deletions Data-Structures/Trees/AVLTree.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Data-Structures/Trees/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.util.*;

public class Main {
public class BinarySearchTree {
static class Node {
int data;
Node left, right;
Expand Down
118 changes: 59 additions & 59 deletions Data-Structures/Trees/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
class Node {
int data;
Node left;
Node right;
public Node(int key) {
data = key;
left = right = null;
}
}
class BinaryTree {
Node root = null;
void inorder_traversal(Node node) {
if (node != null) {
inorder_traversal(node.left);
System.out.print(node.data + " ");
inorder_traversal(node.right);
}
}
void pre_order_traversal(Node node) {
if (node != null) {
System.out.print(node.data + " ");
pre_order_traversal(node.left);
pre_order_traversal(node.right);
}
}
void post_order_traversal(Node node) {
if (node != null) {
post_order_traversal(node.left);
post_order_traversal(node.right);
System.out.print(node.data + " ");
}
}
}
public class Main {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(27);
tree.root.left = new Node(12);
tree.root.right = new Node(3);
tree.root.left.left = new Node(44);
tree.root.left.right = new Node(17);
tree.root.right.left = new Node(56);
System.out.println("Inorder traversal:");
tree.inorder_traversal(tree.root);
System.out.println("\nPreorder traversal:");
tree.pre_order_traversal(tree.root);
System.out.println("\nPostorder traversal:");
tree.post_order_traversal(tree.root);
}
}
class Node {
int data;
Node left;
Node right;

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

class BinaryTreeStructure {
Node root = null;

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

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

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

public class BinaryTree {
public static void main(String[] args) {
BinaryTreeStructure tree = new BinaryTreeStructure();
tree.root = new Node(27);
tree.root.left = new Node(12);
tree.root.right = new Node(3);
tree.root.left.left = new Node(44);
tree.root.left.right = new Node(17);
tree.root.right.left = new Node(56);

System.out.println("Inorder traversal:");
tree.inorder_traversal(tree.root);

System.out.println("\nPreorder traversal:");
tree.pre_order_traversal(tree.root);

System.out.println("\nPostorder traversal:");
tree.post_order_traversal(tree.root);
}
}
76 changes: 38 additions & 38 deletions Searching-algorithms/Binarysearch.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import java.util.Scanner;
import java.util.Arrays;
public class DS_Binarysearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int size = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = binarysearch(arr, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int binarysearch(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
}
import java.util.Scanner;
import java.util.Arrays;

public class Binarysearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int size = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = binarysearch(arr, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int binarysearch(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
}
56 changes: 28 additions & 28 deletions Searching-algorithms/Interpolationsearch.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import java.util.Scanner;
import java.util.Arrays;
public class DS_Interpolationsearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int n = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = interpolationSearch(arr, 0, arr.length - 1, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
import java.util.Scanner;
import java.util.Arrays;

public class Interpolationsearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int n = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = interpolationSearch(arr, 0, arr.length - 1, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int interpolationSearch(int arr[], int lo, int hi, int x) {
int pos;
Expand All @@ -34,9 +34,9 @@ public static int interpolationSearch(int arr[], int lo, int hi, int x) {
return pos;
if (arr[pos] < x)
return interpolationSearch(arr, pos + 1, hi, x);
if (arr[pos] > x)
return interpolationSearch(arr, lo, pos - 1, x);
}
return -1;
}
}
if (arr[pos] > x)
return interpolationSearch(arr, lo, pos - 1, x);
}
return -1;
}
}
64 changes: 32 additions & 32 deletions Searching-algorithms/Linearsearch.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import java.util.Scanner;
import java.util.Arrays;
public class DS_linearsearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int size = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = linearsearch(arr, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int linearsearch(int[] arr, int key){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
}
import java.util.Scanner;
import java.util.Arrays;

public class Linearsearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of the Array: ");
int size = sc.nextInt();
System.out.println("Enter Array elements: ");
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Entered Array: " + Arrays.toString(arr));
System.out.println("Enter the key element to be Found: ");
int key = sc.nextInt();
int result = linearsearch(arr, key);
if (result != -1) {
System.out.println("Element " + key + " Found at Index: " + result);
} else {
System.out.println("Element " + key + " was NOT Found in the entered Array.");
}
}
public static int linearsearch(int[] arr, int key){
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
}
Loading
Loading