Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7db1f5b

Browse files
String palindrome
1 parent 2f270c9 commit 7db1f5b

File tree

6 files changed

+338
-1
lines changed

6 files changed

+338
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.binarysearch.problem;
2+
3+
import java.util.*;
4+
5+
public class PreorderTraversal {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("hjgdshjgfdj");
10+
11+
12+
}
13+
}
14+
15+
class Node{
16+
int data;
17+
Node left,right;
18+
public Node(int d){
19+
data=d;
20+
left=null;
21+
right=null;
22+
}
23+
}

‎src/com/learn/datastructure/BinarySearchIterative.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public static void main(String[] args) {
1313
System.out.println("Element found at index:"+ found_index);
1414
}
1515

16-
1716
}
1817

1918
public static int recursiveBinarySearch(int[] array, int start_index, int end_index, int element) {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.learn.datastructure;
2+
3+
4+
import java.util.*;
5+
6+
public class MaxHeaps {
7+
8+
static class MaxHeap {
9+
private int[] Heap;
10+
private int size;
11+
12+
public MaxHeap(int size) {
13+
this.size = 0;
14+
Heap = new int[size + 1];
15+
Heap[0] = Integer.MAX_VALUE;
16+
}
17+
18+
private int parent(int pos) {
19+
return pos / 2;
20+
}
21+
22+
private int leftChild(int pos) {
23+
return (2 * pos) ;
24+
}
25+
26+
private int rightChild(int pos) {
27+
return (2 * pos) + 1;
28+
}
29+
30+
31+
private void swap(int fpos, int spos) {
32+
int tmp;
33+
tmp = Heap[fpos];
34+
Heap[fpos] = Heap[spos];
35+
Heap[spos] = tmp;
36+
}
37+
38+
private void downHeapify(int pos) {
39+
if (pos >= (size / 2) && pos <= size)
40+
return;
41+
42+
if (Heap[pos] < Heap[leftChild(pos)] ||
43+
Heap[pos] < Heap[rightChild(pos)]) {
44+
45+
if (Heap[leftChild(pos)] > Heap[rightChild(pos)]) {
46+
swap(pos, leftChild(pos));
47+
downHeapify(leftChild(pos));
48+
} else {
49+
swap(pos, rightChild(pos));
50+
downHeapify(rightChild(pos));
51+
}
52+
}
53+
}
54+
private void heapifyUp(int pos) {
55+
int temp = Heap[pos];
56+
while(pos>0 && temp > Heap[parent(pos)]){
57+
Heap[pos] = Heap[parent(pos)];
58+
pos = parent(pos);
59+
}
60+
Heap[pos] = temp;
61+
}
62+
63+
64+
public void insert(int element) {
65+
Heap[++size] = element;
66+
67+
68+
int current = size;
69+
heapifyUp(current);
70+
71+
}
72+
73+
public void print() {
74+
for (int i = 1; i <= size / 2; i++) {
75+
System.out.print(+ Heap[i] + ": L- " +
76+
Heap[2 * i] + " R- " + Heap[2 * i + 1]);
77+
System.out.println();
78+
}
79+
}
80+
81+
public int extractMax() {
82+
int max = Heap[1];
83+
Heap[1] = Heap[size--];
84+
downHeapify(1);
85+
return max;
86+
}
87+
}
88+
public static void main(String[] arg)
89+
{
90+
91+
MaxHeap maxHeap = new MaxHeap(15);
92+
maxHeap.insert(1);
93+
maxHeap.insert(4);
94+
maxHeap.insert(2);
95+
maxHeap.insert(5);
96+
maxHeap.insert(13);
97+
maxHeap.insert(6);
98+
maxHeap.insert(17);
99+
100+
maxHeap.print();
101+
System.out.println("The max is " + maxHeap.extractMax());
102+
}
103+
104+
}
105+
106+
107+
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.learn.datastructure;
2+
3+
import java.util.*;
4+
5+
public class MinHeap {
6+
int[] Heap;
7+
int size;
8+
int maxSize;
9+
10+
// Constructor
11+
public MinHeap(int maxSize) {
12+
this.maxSize = maxSize;
13+
this.size = 0;
14+
Heap = new int[this.maxSize + 1];
15+
Heap[0] = Integer.MIN_VALUE;
16+
}
17+
18+
public int parentNode(int index) {
19+
return index/2;
20+
}
21+
22+
public int lefTNode(int index) {
23+
return index * 2;
24+
}
25+
26+
public int rightNode(int index) {
27+
return index * 2 + 1;
28+
}
29+
30+
public void swap(int index1, int index2) {
31+
int temp = Heap[index1];
32+
Heap[index1] = Heap[index2];
33+
Heap[index2] = temp;
34+
}
35+
36+
public void insert(int data) {
37+
// if heap is not full
38+
if(size >= maxSize) {
39+
System.out.println("HEAP IS FULL");
40+
return;
41+
}
42+
// Put the data at the next empty position of the Heap
43+
Heap[++size] = data;
44+
45+
int index = size;
46+
while(Heap[index] < Heap[parentNode(index)]) {
47+
swap(index, parentNode(index));
48+
index = parentNode(index);
49+
}
50+
}
51+
52+
public boolean isLeafNode(int index) {
53+
if(index >= (size/2) && index <= size)
54+
return true;
55+
return false;
56+
}
57+
58+
public void minHeapify(int index) {
59+
// if the node is not a leaf node, then proceed
60+
if(!isLeafNode(index)) {
61+
62+
if(Heap[index] > Heap[lefTNode(index)] || Heap[index] > Heap[rightNode(index)]) {
63+
if(Heap[lefTNode(index)] < Heap[rightNode(index)]) {
64+
swap(index, lefTNode(index));
65+
minHeapify(lefTNode(index));
66+
}
67+
else {
68+
swap(index, rightNode(index));
69+
minHeapify(rightNode(index));
70+
}
71+
}
72+
}
73+
}
74+
75+
public int remove() {
76+
// remove the smallest element
77+
int del = Heap[1];
78+
79+
// Replace the Heap[1] with the last-most data
80+
Heap[1] = Heap[size--];
81+
minHeapify(1);
82+
83+
return del;
84+
}
85+
86+
public void display() {
87+
for(int i=1; i<= (size/2); i++) {
88+
System.out.print("Parent: " + Heap[i] +
89+
" Left Child: " + Heap[i * 2] +
90+
" Right Child: " + Heap[i * 2 + 1]);
91+
System.out.println();
92+
}
93+
}
94+
95+
public void minHeap() {
96+
for(int i=(size/2); i >= 1; i--)
97+
minHeapify(i);
98+
}
99+
100+
public static void main(String[] args) {
101+
MinHeap minHeap = new MinHeap(10);
102+
103+
minHeap.insert(5);
104+
minHeap.insert(20);
105+
minHeap.insert(7);
106+
minHeap.insert(9);
107+
minHeap.insert(100);
108+
minHeap.insert(1);
109+
minHeap.minHeap();
110+
111+
minHeap.display();
112+
113+
System.out.println();
114+
minHeap.remove();
115+
minHeap.display();
116+
}
117+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.learn.datastructure;
2+
3+
4+
import java.util.*;
5+
6+
public class StringPalindrome {
7+
8+
public static void main(String[] args) {
9+
10+
11+
12+
Scanner sc = new Scanner(System.in);
13+
String A = sc.next();
14+
sc.close();
15+
boolean found = true;
16+
for (int i = 0; i < A.length() / 2; i++) {
17+
if (A.charAt(i) != A.charAt(A.length() - 1 - i)) {
18+
found = false;
19+
break;
20+
}
21+
}
22+
23+
if (found)
24+
System.out.println("Yes");
25+
26+
else
27+
System.out.println("no");;
28+
29+
//alternate method
30+
31+
// System.out.println(found ? "Yes" : "No");
32+
33+
34+
35+
36+
37+
38+
39+
}
40+
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.patternprint.starprint;
2+
3+
public class Starprint {
4+
5+
public static void main(String[] args) {
6+
7+
8+
int letter = 65;
9+
10+
for (int i = 4; i >= 0; i--) {
11+
for (int j = 0; j <= i; j++) {
12+
13+
14+
System.out.print((char) (letter + j) + " ");
15+
16+
}
17+
System.out.println();
18+
}
19+
20+
21+
for (int i=0;i<=4;i++)
22+
{
23+
for (int j=0;j<=i;j++)
24+
{
25+
System.out.print((char)(letter+j)+ " ");
26+
}
27+
System.out.println();
28+
}
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
}
50+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /