Skip to content

Commit 029b2a6

Browse files
authored
Merge pull request #426 from rpjayasekara/heapSort-java
Heap sort java
2 parents ddcea65 + b2c0f89 commit 029b2a6

File tree

6 files changed

+248
-0
lines changed

6 files changed

+248
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def isSubsetSum(set,n, sum) :
2+
3+
# Base Cases
4+
if (sum == 0) :
5+
return True
6+
if (n == 0 and sum != 0) :
7+
return False
8+
9+
# If last element is greater than
10+
# sum, then ignore it
11+
if (set[n - 1] > sum) :
12+
return isSubsetSum(set, n - 1, sum)
13+
14+
# else, check if sum can be obtained
15+
# by any of the following
16+
# (a) including the last element
17+
# (b) excluding the last element
18+
return isSubsetSum(set, n-1, sum) or isSubsetSum(set, n-1, sum-set[n-1])
19+
20+
21+
# Test case
22+
set = [3, 34, 4, 12, 5, 2]
23+
sum = 100
24+
n = len(set)
25+
if (isSubsetSum(set, n, sum) == True) :
26+
print("Found a subset with given sum")
27+
else :
28+
print("No subset with given sum")
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class BinarySearchTree {
2+
3+
/* Class containing left and right child of current node and key value*/
4+
class Node {
5+
int key;
6+
Node left, right;
7+
8+
public Node(int item) {
9+
key = item;
10+
left = right = null;
11+
}
12+
}
13+
14+
// Root of BST
15+
Node root;
16+
17+
// Constructor
18+
BinarySearchTree() {
19+
root = null;
20+
}
21+
22+
// This method mainly calls insertRec()
23+
void insert(int key) {
24+
root = insertRec(root, key);
25+
}
26+
27+
/* A recursive function to insert a new key in BST */
28+
Node insertRec(Node root, int key) {
29+
30+
/* If the tree is empty, return a new node */
31+
if (root == null) {
32+
root = new Node(key);
33+
return root;
34+
}
35+
36+
/* Otherwise, recur down the tree */
37+
if (key < root.key)
38+
root.left = insertRec(root.left, key);
39+
else if (key > root.key)
40+
root.right = insertRec(root.right, key);
41+
42+
/* return the (unchanged) node pointer */
43+
return root;
44+
}
45+
46+
// This method mainly calls InorderRec()
47+
void inorder() {
48+
inorderRec(root);
49+
}
50+
51+
// A utility function to do inorder traversal of BST
52+
void inorderRec(Node root) {
53+
if (root != null) {
54+
inorderRec(root.left);
55+
System.out.println(root.key);
56+
inorderRec(root.right);
57+
}
58+
}
59+
60+
// Driver Program to test above functions
61+
public static void main(String[] args) {
62+
BinarySearchTree tree = new BinarySearchTree();
63+
64+
/* Let us create following BST
65+
50
66+
/ \
67+
30 70
68+
/ \ / \
69+
20 40 60 80 */
70+
tree.insert(50);
71+
tree.insert(30);
72+
tree.insert(20);
73+
tree.insert(40);
74+
tree.insert(70);
75+
tree.insert(60);
76+
tree.insert(80);
77+
78+
// print inorder traversal of the BST
79+
tree.inorder();
80+
}
81+
}
82+
// This code is contributed by Ankur Narain Verma
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#Class which represents a node of binary tree
2+
class Node:
3+
def __init__(self,key):
4+
self.left = None
5+
self.right = None
6+
self.val = key
7+
8+
#Function which add a key to binary tree
9+
def insert(root,node):
10+
if root is None:
11+
root = node
12+
else:
13+
if root.val < node.val:
14+
if root.right is None:
15+
root.right = node
16+
else:
17+
insert(root.right, node)
18+
else:
19+
if root.left is None:
20+
root.left = node
21+
else:
22+
insert(root.left, node)
23+
#Function to do inorder tree traversal
24+
def inorder(root):
25+
if root:
26+
inorder(root.left)
27+
print(root.val)
28+
inorder(root.right)
29+
30+
#Function for searching a key in the tree
31+
def search(root,key):
32+
33+
# Base Cases: root is null or key is present at root
34+
if root is None:
35+
return False
36+
elif root.val == key:
37+
return True
38+
39+
# Key is greater than root's key
40+
if root.val < key:
41+
return search(root.right,key)
42+
43+
# Key is smaller than root's key
44+
return search(root.left,key)
45+
46+
47+
#Test case
48+
49+
r = Node(50)
50+
insert(r,Node(30))
51+
insert(r,Node(20))
52+
insert(r,Node(40))
53+
insert(r,Node(70))
54+
insert(r,Node(60))
55+
insert(r,Node(80))
56+
57+
print "Result of traversing"
58+
inorder(r)
59+
60+
61+
print "Is 25 in the tree?", search(r,25)
62+
print "Is 30 in the tree?", search(r,30)
63+
print "Is 2 in the tree?", search(r,2)
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class LinearSearch
2+
{
3+
// This function returns index of element x in arr[]
4+
static int search(int arr[], int n, int x)
5+
{
6+
for (int i = 0; i < n; i++)
7+
{
8+
// Return the index of the element if the element
9+
// is found
10+
if (arr[i] == x)
11+
return i;
12+
}
13+
14+
// return -1 if the element is not found
15+
return -1;
16+
}
17+
}

Sorting/Heapsort/java/heapSort.java

Whitespace-only changes.

data structures/stack/java/stack.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
class Test
5+
{
6+
// Pushing element on the top of the stack
7+
static void stack_push(Stack<Integer> stack)
8+
{
9+
for(int i = 0; i < 5; i++)
10+
{
11+
stack.push(i);
12+
}
13+
}
14+
15+
// Popping element from the top of the stack
16+
static void stack_pop(Stack<Integer> stack)
17+
{
18+
System.out.println("Pop :");
19+
20+
for(int i = 0; i < 5; i++)
21+
{
22+
Integer y = (Integer) stack.pop();
23+
System.out.println(y);
24+
}
25+
}
26+
27+
// Displaying element on the top of the stack
28+
static void stack_peek(Stack<Integer> stack)
29+
{
30+
Integer element = (Integer) stack.peek();
31+
System.out.println("Element on stack top : " + element);
32+
}
33+
34+
// Searching element in the stack
35+
static void stack_search(Stack<Integer> stack, int element)
36+
{
37+
Integer pos = (Integer) stack.search(element);
38+
39+
if(pos == -1)
40+
System.out.println("Element not found");
41+
else
42+
System.out.println("Element is found at position " + pos);
43+
}
44+
45+
46+
public static void main (String[] args)
47+
{
48+
Stack<Integer> stack = new Stack<Integer>();
49+
50+
stack_push(stack);
51+
stack_pop(stack);
52+
stack_push(stack);
53+
stack_peek(stack);
54+
stack_search(stack, 2);
55+
stack_search(stack, 6);
56+
}
57+
}

0 commit comments

Comments
 (0)