Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:11388283
Last active August 23, 2020 06:12
inorder successor and predecessor in a Binary Search Tree
//http://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/
//With Parent node given
struct node * inOrderSuccessor(struct node *root, struct node *n)
{
// step 1 of the above algorithm
if( n->right != NULL )
return minValue(n->right);
// step 2 of the above algorithm
struct node *p = n->parent;
@gabhi
gabhi / gist:66c4a9c4d9f1db91132d
Created April 29, 2014 23:58
Find level of the node in binary tree
/* Helper function for getLevel(). It returns level of the data if data is
present in tree, otherwise returns 0.*/
int getLevelUtil(struct node *node, int data, int level)
{
if (node == NULL)
return 0;
if (node->data == data)
return level;
@gabhi
gabhi / gist:bb9056a2604a1208017e
Created April 29, 2014 23:59
Print ancestors of an element in tree
/* If target is present in tree, then prints the ancestors
and returns true, otherwise returns false. */
bool printAncestors(struct node *root, int target)
{
/* base cases */
if (root == NULL)
return false;
if (root->data == target)
return true;
@gabhi
gabhi / gist:b59aaf2da3a0ffa58b5b
Created May 13, 2014 19:43
AngularJS: Creating multiple factories for every endpoint?
services.factory('Api', ['$resource',
function($resource) {
return {
Recipe: $resource('/recipes/:id', {id: '@id'}),
Users: $resource('/users/:id', {id: '@id'}),
Group: $resource('/groups/:id', {id: '@id'})
};
}]);
function myCtrl($scope, Api){
@gabhi
gabhi / gist:e081516379caf4dc5f97
Created May 30, 2014 04:18
Finding three elements in an array whose sum is closest to an given number
for (i in 1..n-2) {
j = i+1 // Start right after i.
k = n // Start at the end of the array.
while (k >= j) {
// We got a match! All done.
if (A[i] + A[j] + A[k] == 0) return (A[i], A[j], A[k])
// We didn't match. Let's try to get a little closer:
// If the sum was too big, decrement k.
@gabhi
gabhi / gist:37a2a053030d574f07c0
Last active May 20, 2017 11:11
Spring RESTTEMPLATE example using post and GET
package sample;
import java.util.HashMap;
import org.springframework.web.client.RestTemplate;
public class HelloWorld {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
@gabhi
gabhi / gist:cc5d8ec2f67b1f7383bd
Created June 4, 2014 21:01
singleton + expressjs + javascript
var constants = require('../config/constants');
var url = constants.APP_URL;
var soap = require('soap');
var singleton = function singleton() {
var instance = null;
soap.createClient(url, function(err, client) {
if (err) {
console.log(err);
instance = null;
@gabhi
gabhi / gist:157a25554881b8efb2e1
Created June 5, 2014 21:50
Run Length Encoding - compress string by duplicate count
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunLengthEncoding {
public static String encode(String source) {
StringBuffer dest = new StringBuffer();
for (int i = 0; i < source.length(); i++) {
int runLength = 1;
while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
runLength++;
@gabhi
gabhi / gist:60473b491d346dedb2a7
Last active August 29, 2015 14:02
Detect cycle in linked list
private static boolean detectCycle(ListNode head) {
ListNode p1 = head;
ListNode p2 = head;
while (p2 != null && p2.next != null) {
p1 = p1.next;
p2 = p2.next.next;
if (p1 == p2)
return true;
}
// Use built in or binary modules
var crypto = require('crypto');
var hash = crypto.createHmac("sha1",key).update(signatureBase).digest("base64");