Skip to content

Commit 39ce6ac

Browse files
committed
added head and BST
1 parent 25f81fc commit 39ce6ac

File tree

3 files changed

+254
-0
lines changed

3 files changed

+254
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public class BinarySearchTree<T extends Comparable<T>> {
2+
T value;
3+
BinarySearchTree<T> left;
4+
BinarySearchTree<T> right;
5+
public BinarySearchTree(T v) {
6+
value = v;
7+
left = null;
8+
right = null;
9+
}
10+
11+
/*
12+
* insert into binary search Tree
13+
* @params: datatype T which has to be inserted
14+
* @returns: void
15+
*/
16+
public void insert(T data) {
17+
if(value == data) {
18+
return;
19+
}
20+
if(data.compareTo(value) < 0) {
21+
// left side tree
22+
if(left == null) {
23+
//create a new Tree
24+
left = new BinarySearchTree<T>(data);
25+
} else {
26+
left.insert(data);
27+
}
28+
} else {
29+
if(right == null) {
30+
///create a new Tree
31+
right = new BinarySearchTree<T>(data);
32+
} else {
33+
right.insert(data);
34+
}
35+
}
36+
}
37+
38+
/*
39+
* Delete the data
40+
* @params: data T which needs to be deleted
41+
* @return: Boolean if element is found and deleted
42+
*/
43+
public Boolean delete(T data) {
44+
45+
return null;
46+
47+
}
48+
49+
public static void main(String[] args) {
50+
BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(5);
51+
bst.insert(3);
52+
bst.insert(7);
53+
assert(bst.left.value == 3);
54+
assert(bst.right.value == 7);
55+
System.out.println("Success..");
56+
assert(true);
57+
}
58+
59+
}
60+
61+

Heap/Java/MaxHeap.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package heap;
2+
3+
public class MaxHeap {
4+
int[] arr;
5+
int N;
6+
7+
public MaxHeap(int capacity) {
8+
arr = new int[capacity + 1];
9+
N = 0;
10+
}
11+
12+
public void insert(int value) {
13+
arr[++N] = value;
14+
swim(N);
15+
}
16+
17+
public int deleteMax() {
18+
swap(N, 1, arr);
19+
20+
int max = arr[N];
21+
arr[N] = 0;
22+
N--;
23+
sink(1);
24+
return max;
25+
}
26+
27+
public boolean isEmpty() {
28+
return N == 0;
29+
}
30+
31+
private int max(int a, int b) {
32+
return (a>b) ?a : b;
33+
}
34+
35+
/*
36+
* swim max to top
37+
*/
38+
private void swim(int index) {
39+
while(arr[index] > arr[index/2] && index != 0 && index/2 != 0) {
40+
swap(index, index/2, arr);
41+
index = index/2;
42+
}
43+
}
44+
45+
/*
46+
* Sink the element from top to bottom
47+
*/
48+
private void sink(int index) {
49+
while(index < N) {
50+
int childIndex = 2 * index;
51+
int maxChildIndex = (arr[childIndex] > arr[childIndex + 1])?childIndex :(childIndex + 1);
52+
if(arr[index] > arr[maxChildIndex]) {
53+
break;
54+
}
55+
//System.out.println("max child index :" + maxChildIndex);
56+
swap(index, maxChildIndex, arr);
57+
index = maxChildIndex;
58+
}
59+
}
60+
61+
/*
62+
* swaps entries between two index in arr
63+
*/
64+
private void swap(int index1, int index2, int[] arr) {
65+
int temp = arr[index1];
66+
arr[index1] = arr[index2];
67+
arr[index2] = temp;
68+
}
69+
70+
/*
71+
* diplays elements of queue
72+
*/
73+
public void display() {
74+
for(int i = 1; i <= N; i++) {
75+
System.out.print(arr[i] + " ");
76+
}
77+
System.out.println();
78+
}
79+
80+
public static void main(String[] args) {
81+
MaxHeap h = new MaxHeap(20);
82+
int[] arr = {1, 23, 12, 9, 30, 2, 50};
83+
for(int i = 0; i < arr.length; i++) {
84+
h.insert(arr[i]);
85+
}
86+
System.out.println("first highest element is " + h.deleteMax());
87+
System.out.println("second highest element is " + h.deleteMax());
88+
System.out.println("third highest element is " + h.deleteMax());
89+
}
90+
91+
}

Heap/Java/MinHeap.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package heap;
2+
3+
public class MinHeap {
4+
int[] arr;
5+
int N;
6+
7+
public MinHeap(int capacity) {
8+
arr = new int[capacity + 1];
9+
N = 0;
10+
}
11+
12+
public void insert(int value) {
13+
arr[++N] = value;
14+
swim(N);
15+
}
16+
17+
public int deleteMin() {
18+
swap(N, 1, arr);
19+
int max = arr[N];
20+
arr[N] = Integer.MAX_VALUE;
21+
N--;
22+
sink(1);
23+
return max;
24+
}
25+
26+
public boolean isEmpty() {
27+
return N == 0;
28+
}
29+
30+
/*
31+
* swim max to top
32+
*/
33+
private void swim(int index) {
34+
while(arr[index] < arr[index/2] && index != 0 && index/2 != 0) {
35+
swap(index, index/2, arr);
36+
index = index/2;
37+
}
38+
}
39+
40+
/*
41+
* Sink the element from top to bottom
42+
*/
43+
private void sink(int index) {
44+
while(index < N) {
45+
int childIndex = 2 * index;
46+
int minChildIndex = (arr[childIndex] < arr[childIndex + 1])?childIndex :(childIndex + 1);
47+
if(arr[index] < arr[minChildIndex]) {
48+
break;
49+
}
50+
//System.out.println("max child index :" + maxChildIndex);
51+
swap(index, minChildIndex, arr);
52+
index = minChildIndex;
53+
}
54+
}
55+
56+
/*
57+
* swaps entries between two index in arr
58+
*/
59+
private void swap(int index1, int index2, int[] arr) {
60+
int temp = arr[index1];
61+
arr[index1] = arr[index2];
62+
arr[index2] = temp;
63+
}
64+
65+
/*
66+
* diplays elements of queue
67+
*/
68+
public void display() {
69+
for(int i = 1; i <= N; i++) {
70+
System.out.print(arr[i] + " ");
71+
}
72+
System.out.println();
73+
}
74+
75+
public static void main(String[] args) {
76+
MinHeap h = new MinHeap(20);
77+
h.insert(5);
78+
h.display();
79+
h.insert(3);
80+
h.display();
81+
h.insert(2);
82+
h.display();
83+
h.insert(1);
84+
h.display();
85+
h.insert(10);
86+
h.display();
87+
System.out.println("first smallest element is " + h.deleteMin());
88+
h.display();
89+
System.out.println("second smallest element is " + h.deleteMin());
90+
h.display();
91+
System.out.println("third smallest element is " + h.deleteMin());
92+
93+
// int[] arr = {1, 23, 12, 9, 30, 2, 50};
94+
// for(int i = 0; i < arr.length; i++) {
95+
// h.insert(arr[i]);
96+
// }
97+
// System.out.println("first smallest element is " + h.deleteMin());
98+
// System.out.println("second smallest element is " + h.deleteMin());
99+
// System.out.println("third smallest element is " + h.deleteMin());
100+
}
101+
102+
}

0 commit comments

Comments
 (0)