Sunday, April 17, 2016

Trie Data Structure

A trie (or a prefix tree) is a data structure similar to n-ary trees, where each node can have n children. Each child holds a reference to the next n children.

Here an example trie is discussed where some words are stored in the trie.

The TrieNode consists of the following elements:

class TrieNode {
    boolean isWord = false;
    TrieNode[] children = new TrieNode[26];
}

Here each node can have 26 children. Some children may point to null and others may point to the next character in the word.

For example, when the words "home" and "hot" are added to the trie, the state of the trie can be described as follows:
1. The first level will hold 26 children with only the 8th ("h") child holding a non-null reference. Rest of the 25 children will be null.
2. The second level which is the children of "h" will hold 25 null children with only the 15th ("o") child holding a non-null reference.
3. The third level will which is the children of "o" will hold 2 non-null references ("m" and "t") and rest 24 children will be null.
4. The fourth level will have one non-null reference ("e"), which is one of the children of "m", and "t" will hold 26 null references.

Java Code:

package com.sourabh.third;

import java.util.LinkedList;
import java.util.Queue;

class TrieNode {
 boolean isWord = false;
 TrieNode[] children = new TrieNode[26];
 public TrieNode() {
  
 }
 
 public TrieNode(String key) {
  this.add(key);
 }
 
 public boolean contains(String in) {
  TrieNode node = this;
  for(int i=0; i<in.length();i++) {
   TrieNode child = node.children[getAscii(in.charAt(i))];
   if(child == null) {
    // This particular character is not present in the current branch.
    return false;
   }
   else {
    if(in.length() - 1 == i) {
     // We have reached the end of the input string.
     return true;
    }
    else {
     node = child;
    }
   }
  }
  return false;
 }
 
 public void add(String in) {
  if(in.length() == 0) {
   this.isWord = true;
   return;
  }
  else {
   char first = in.charAt(0);
   TrieNode child = children[getAscii(first)];
   if(child == null) {
    child = new TrieNode();
    children[getAscii(first)] = child;
   }
   child.add(in.substring(1));
  }
 }
 
 public int getAscii(char c) {
  char A[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  int i=0;
  for(i=0;i<A.length;i++) {
   if(A[i] == c) {
    break;
   }
  }
  return i;
 }
}

public class TriePrograms {
 public TrieNode createNode(String word) {
  if(word == null) {
   return new TrieNode();
  }
  else {
   return new TrieNode(word);
  }
 }
 
 public void levelOrderTraversal(TrieNode node) {
  Queue<TrieNode[]> queue = new LinkedList<>();
  if(node == null) {
   return;
  }
  queue.add(node.children);
  int thisLevel = 1;
  int nextLevel = 0;
  while(!queue.isEmpty()) {
   for(int ele = 0; ele < thisLevel; ele++) {
    TrieNode[] childrenAtThisLevel = queue.remove();
    for(int i=0; i<childrenAtThisLevel.length; i++) {
     if(childrenAtThisLevel[i] != null) {
      System.out.print(getChar(i) + ".");
      queue.add(childrenAtThisLevel[i].children);
      nextLevel++;
     }
     else {
      System.out.print(".");
     }
    }
   }
   thisLevel = nextLevel;
   System.out.println();
   for(int i=0;i<26;i++) {
    System.out.print("-");
   }
   System.out.println();
   for(int i=0;i<26;i++) {
    System.out.print("|");
   }
   System.out.println();
   nextLevel = 0;
  }
  thisLevel = nextLevel;
 }
 
 public char getChar(int ascii) {
  char A[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  return A[ascii];
 }
}

Unit Tests:

package com.sourabh.third;

import org.junit.Assert;
import org.junit.Test;

public class TrieProgramTests {

 TriePrograms instance = new TriePrograms();
 
 @Test
 public void testContains() {
  TrieNode node = instance.createNode(null);
  node.add("home");
  Assert.assertTrue(node.contains("home"));
  node.add("house");
  Assert.assertTrue(node.contains("house"));
  node.add("hot");
  Assert.assertTrue(node.contains("hot"));
  Assert.assertFalse(node.contains("hole"));
 }
 
 @Test
 public void testConstructor() {
  TrieNode node = instance.createNode("home");
  Assert.assertTrue(node.contains("home"));
  node.add("house");
  Assert.assertTrue(node.contains("house"));
  node.add("hot");
  Assert.assertTrue(node.contains("hot"));
  Assert.assertFalse(node.contains("hole"));
 }
 
 @Test
 public void testLevelOrderTraversal() {
  TrieNode node = instance.createNode("home");
  node.add("house");
  node.add("hot");
  instance.levelOrderTraversal(node);
 }
}

Trie printed in level order:
.......h...................
--------------------------
||||||||||||||||||||||||||
..............o............
--------------------------
||||||||||||||||||||||||||
............m.......t.u......
--------------------------
||||||||||||||||||||||||||
....e..................................................................s........
--------------------------
||||||||||||||||||||||||||
..............................e......................
--------------------------
||||||||||||||||||||||||||
..........................
--------------------------
||||||||||||||||||||||||||

Sunday, April 3, 2016

Find the smallest window within an array that contains all the elements of another array

Java Program:

package com.sourabh.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class MinimumArrayContains {
 class Interval
 {
  private int start;
  private int end;
  public Interval()
  {
   super();
  }
  public Interval(int start, int end) {
   super();
   this.start = start;
   this.end = end;
  }
  public int getStart() {
   return start;
  }
  public void setStart(int start) {
   this.start = start;
  }
  public int getEnd() {
   return end;
  }
  public void setEnd(int end) {
   this.end = end;
  }
 }
 
 Map<Integer, Boolean> map = new HashMap<>();
 Queue<Integer> queue = new LinkedList<>();
 public void resetMap(int... A) {
  for(int i=0;i<A.length;i++) {
   map.put(A[i], false);
  }
 }
 
 public Interval resetQueue() {
  int start = queue.peek();
  int end = 0;
  while(!queue.isEmpty()) {
   end = queue.remove();
  }
  return new Interval(start, end);
 }
 
 public List<Interval> getIntervals(int[] A1, int[] A2) {
  int found = 0;
  int end = 0;
  List<Interval> intervalsContainingAllElementsOfA2 = new ArrayList<>();
  resetMap(A2);
  for(int i=0; i<A1.length; i++) {
   if(map.containsKey(A1[i])) {
    if(map.get(A1[i]) == false) {
     found++;
     map.put(A1[i], true);
    }
    if(queue.peek()!= null && A1[i] == A1[queue.peek()]) {
     queue.remove();
    }
    queue.add(i);
    end = i;
   }
   if(found == A2.length) {
    int start = queue.peek();
    intervalsContainingAllElementsOfA2.add(new Interval(start, end));
    int prevIndex = queue.remove();
    map.put(A1[prevIndex], false);
    found--;
    int currIndex = queue.remove();
    map.put(A1[currIndex], false);
    found--;
    while(currIndex - prevIndex ==1) {
     prevIndex = currIndex;
     currIndex = queue.remove();
     found--;
     map.put(A1[currIndex], false);
    }
    i = currIndex;
    queue.add(currIndex);
    map.put(A1[currIndex], true);
    found++;
   }
  }
  return intervalsContainingAllElementsOfA2;
 }
 
 public Interval getMinimumInterval(int[] A1, int[] A2) {
  List<Interval> intervalsList = getIntervals(A1, A2);
  Interval minInterval = intervalsList.get(0);
  for(Interval interval : intervalsList) {
   if(interval.getEnd() - interval.getStart() < minInterval.getEnd() - minInterval.getStart()) {
    minInterval = interval;
   }
  }
  return minInterval;
 }
}

Unit Tests:
package com.sourabh.test;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.junit.Test;
import org.junit.Assert;
import com.sourabh.test.MinimumArrayContains.Interval;

public class QueueTest {

 Queue<Integer> queue = new LinkedList<>();
 MinimumArrayContains instance = new MinimumArrayContains();
 
 @Test
 public void testQueueFunctions() {
  queue.add(1);
  queue.add(2);
  queue.add(3);
  queue.add(4);
  Assert.assertTrue(queue.peek() == 1);
  Assert.assertTrue(queue.remove() == 1);
  Assert.assertTrue(queue.peek() == 2);
  Assert.assertTrue(queue.remove() == 2);
 }
 
 @Test
 public void testGetMinInterval1() {
  int[] A1 = {1,2,3,1,2,1,4,6,5,4,2,3,4,1};
  int[] A2 = {1,2,4,5};
  Interval interval = instance.getMinimumInterval(A1, A2);
  Assert.assertEquals(4, interval.getStart());
  Assert.assertEquals(8, interval.getEnd());
 }
 
 @Test
 public void testGetMinInterval2() {
  int[] A1 = {6, 7, 1, 3, 2, 4, 5, 2, 3, 1, 2, 5};
  int[] A2 = {1, 3, 5};
  Interval interval = instance.getMinimumInterval(A1, A2);
  Assert.assertEquals(3, interval.getEnd() - interval.getStart());
 }
}

Water in valley one dimensional


Problem:
Given a one dimensional array of integers representing the height of cliffs of a valley, find the total amount of water that can be filled in the valley.

Diagram:


Java Program:

package com.sourabh.test;

import java.util.ArrayList;
import java.util.List;

public class ValleyWater {
 class Valley
 {
  private int start;
  private int end;
  private int water;

  public Valley() {
   super();
  }
  public Valley(int start, int end) {
   super();
   this.start = start;
   this.end = end;
  }
  public int getStart() {
   return start;
  }
  public void setStart(int start) {
   this.start = start;
  }
  public int getEnd() {
   return end;
  }
  public void setEnd(int end) {
   this.end = end;
  }
  public int getWater() {
   return water;
  }
  public void setWater(int water) {
   this.water = water;
  }
 }
 
 public List<Valley> getValleys(int... A) {
  List<Integer> maxima = new ArrayList<>();
  List<Integer> minima = new ArrayList<>();
  List<Valley> valleysList = new ArrayList<>();
  int index = 0;
  for(int i=0;i<A.length - 1;i++) {
   if(A[i+1] > A[i]) {
    minima.add(i);
    index = i;
    break;
   }
   if(A[i+1] < A[i]) {
    maxima.add(i);
    index = i;
    break;
   }
  }
  for(int i=index+1;i+1<A.length;i++) {
   if(A[i+1] < A[i] && A[i] >= A[i-1]) {
    maxima.add(i);
   }
   if(A[i+1] > A[i] && A[i] <= A[i-1]) {
    minima.add(i);
   }
  }
  for(int i = 0; i < maxima.size() - 1; i++) {
   valleysList.add(new Valley(maxima.get(i), maxima.get(i+1)));
  }
  return valleysList;
 }
 
 public int addWater(List<Valley> valleysList, int... A) {
  int totalWater = 0;
  for(Valley valley : valleysList) {
   int min = A[valley.getStart()] < A[valley.getEnd()] ? A[valley.getStart()] : A[valley.getEnd()];
   int water = 0;
   for(int i=valley.getStart(); i<=valley.getEnd(); i++) {
    if(A[i] < min) {
     water += min - A[i];
    }
   }
   valley.setWater(water);
   totalWater += water;
  }
  for(Valley valley : valleysList) {
   System.out.println(valley.getWater());
  }
  return totalWater;
 }
 
 public int getWater(int... A) {
  return addWater(getValleys(A), A);
 }
}
Unit Tests:
package com.sourabh.test;

import static org.junit.Assert.*;

import java.util.List;
import com.sourabh.test.ValleyWater.Valley;
import org.junit.Assert;

import org.junit.Test;

public class ValleyWaterTests {

 ValleyWater instance = new ValleyWater();
 
 @Test
 public void getValleysTest() {
  int[] A = {1,2,3,1,2,1,4,6,5,4,2,3,4,1};
  List<Valley> valleysList = instance.getValleys(A);
  Assert.assertEquals(3, valleysList.size());
  Assert.assertEquals(2,valleysList.get(0).getStart());
  Assert.assertEquals(4,valleysList.get(0).getEnd());
  Assert.assertEquals(4,valleysList.get(1).getStart());
  Assert.assertEquals(7,valleysList.get(1).getEnd());
  Assert.assertEquals(7,valleysList.get(2).getStart());
  Assert.assertEquals(12,valleysList.get(2).getEnd());
 }
 
 @Test
 public void getWaterTest() {
  int[] A = {1,2,3,1,2,1,4,6,5,4,2,3,4,1};
  int totalWater = instance.getWater(A);
  Assert.assertEquals(5, totalWater);
 }
}