Balanced binary search tree from doubly linkedlist - data-structures

I am trying to write this function that takes in a doublyLinkedList and constructs a balanced binary search tree in place. The TreeNode.left is equivalent to the previous pointer and TreeNode.right is like the next pointer. I am taking inspiration from the program here but that doesnt work:
http://www.geeksforgeeks.org/in-place-conversion-of-sorted-dll-to-balanced-bst/
private static TreeNode constructBST2(TreeNode head, int m, int n) {
TreeNode temp = null;
if (m < n) {
int mid = m + (n - m)/ 2;
TreeNode left = constructBST2(head, m, mid);
temp = head;
temp.left = left;
head = head.right;
temp.right = constructBST2(head, mid + 1, n);
}
return temp;
}

Let me try:
private static TreeNode constructBST2(TreeNode root, int r, int m, int n) {
if (m < n) {
int leftTreeMid = m + (int)Math.ceil((r - m) / 2);
int delta = r - leftTreeMid;
TreeNode left = root;
for (int i = 0; i < delta; i++)
left = left.left;
root.left = left;
constructBST2(left, leftTreeMid, m, r - 1);
int rightTreeMid = r + (int)Math.ceil((n - r) / 2);
delta = rightTreeMid - r;
TreeNode right = root;
for(int i = 0; i < delta; i++)
right = right.right;
root.right = right;
constuctBST2(right, rightTreeMid, r + 1, n);
}
return root;
}
I haven't tried it at all, maybe you can try it and tell me if it works.

Related

Finding number of pairs, product of whose indices is divisible by another number X

Given an array and some value X, find the number of pairs such that i < j , a[i] = a[j] and (i * j) % X == 0
Array size <= 10^5
I am thinking of this problem for a while but only could come up with the brute force solution(by checking all pairs) which will obviously time-out [O(N^2) time complexity]
Any better approach?
First of all, store separate search structures for each distinct A[i] as we iterate.
i * j = k * X
i = k * X / j
Let X / j be some fraction. Since i is an integer, k would be of the form m * least_common_multiple(X, j) / X, where m is natural.
Example 1: j = 20, X = 60:
lcm(60, 20) = 60
matching `i`s would be of the form:
(m * 60 / 60) * 60 / 20
=> m * q, where q = 3
Example 2: j = 6, X = 2:
lcm(2, 6) = 6
matching `i`s would be of the form:
(m * 6 / 2) * 2 / 6
=> m * q, where q = 1
Next, I would consider how to efficiently query the number of multiples of a number in a sorted list of arbitrary naturals. One way is to hash the frequency of divisors of each i we add to the search structure of A[i]. But first consider i as j and add to the result the count of divisors q that already exist in the hash map.
JavaScript code with brute force testing at the end:
function gcd(a, b){
return b ? gcd(b, a % b) : a;
}
function getQ(X, j){
return X / gcd(X, j);
}
function addDivisors(n, map){
let m = 1;
while (m*m <= n){
if (n % m == 0){
map[m] = -~map[m];
const l = n / m;
if (l != m)
map[l] = -~map[l];
}
m += 1;
}
}
function f(A, X){
const Ais = {};
let result = 0;
for (let j=1; j<A.length; j++){
if (A[j] == A[0])
result += 1;
// Search
if (Ais.hasOwnProperty(A[j])){
const q = getQ(X, j);
result += Ais[A[j]][q] || 0;
// Initialise this value's
// search structure
} else {
Ais[A[j]] = {};
}
// Add divisors for j
addDivisors(j, Ais[A[j]]);
}
return result;
}
function bruteForce(A, X){
let result = 0;
for (let j=1; j<A.length; j++){
for (let i=0; i<j; i++){
if (A[i] == A[j] && (i*j % X) == 0)
result += 1;
}
}
return result;
}
var numTests = 1000;
var n = 100;
var m = 50;
var x = 100;
for (let i=0; i<numTests; i++){
const A = [];
for (let j=0; j<n; j++)
A.push(Math.ceil(Math.random() * m));
const X = Math.ceil(Math.random() * x);
const _brute = bruteForce(A, X);
const _f = f(A, X);
if (_brute != _f){
console.log("Mismatch!");
console.log(X, JSON.stringify(A));
console.log(_brute, _f);
break;
}
}
console.log("Done testing.")
Just in case If someone needed the java version of this answer - https://stackoverflow.com/a/69690416/19325755 explanation has been provided in that answer.
I spent lot of time in understanding the javascript code so I thought the people who are comfortable with java can refer this for better understanding.
import java.util.HashMap;
public class ThisProblem {
public static void main(String[] args) {
int t = 1000;
int n = 100;
int m = 50;
int x = 100;
for(int i = 0; i<t; i++) {
int[] A = new int[n];
for(int j = 0; j<n; j++) {
A[j] = ((int)Math.random()*m)+1;
}
int X = ((int)Math.random()*x)+1;
int optR = createMaps(A, X);
int brute = bruteForce(A, X);
if(optR != brute) {
System.out.println("Wrong Answer");
break;
}
}
System.out.println("Test Completed");
}
public static int bruteForce(int[] A, int X) {
int result = 0;
int n = A.length;
for(int i = 1; i<n; i++) {
for(int j = 0; j<i; j++) {
if(A[i] == A[j] && (i*j)%X == 0)
result++;
}
}
return result;
}
public static int gcd(int a, int b) {
return b==0 ? a : gcd(b, a%b);
}
public static int getQ(int X, int j) {
return X/gcd(X, j);
}
public static void addDivisors(int n, HashMap<Integer, Integer> map) {
int m = 1;
while(m*m <= n) {
if(n%m == 0) {
map.put(m, map.getOrDefault(m, 0)+1);
int l = n/m;
if(l != m) {
map.put(l, map.getOrDefault(l, 0)+1);
}
}
m++;
}
}
public static int createMaps(int[] A, int X) {
int result = 0;
HashMap<Integer, HashMap<Integer, Integer>> contentsOfA = new HashMap<>();
int n = A.length;
for(int i = 1; i<n; i++) {
if(A[i] == A[0])
result++;
if(contentsOfA.containsKey(A[i])) {
int q = getQ(X, i);
result += contentsOfA.get(A[i]).getOrDefault(q, 0);
} else {
contentsOfA.put(A[i], new HashMap<>());
}
addDivisors(i, contentsOfA.get(A[i]));
}
return result;
}
}

segment tree correct but query output is not

I tried to implement segment tree algorithm for finding range minimum query in Java. Here is my complete java code. It builds the segment tree from an array. and then prints the minimum element in every range. the problem is, tree is correct (as far as I know), but the output is always the least element of whole array. :-| kindly point out where I am going wrong.
public class Solution {
static int arr[] = {-1, 2, 4, 0};
static int st[];
public static void main(String[] args) {
st = new int [ 2 * arr.length-1];
buildTree(0, 0, arr.length-1);
System.out.print("original array: ");
showArray(arr);
System.out.print("segment tree: ");
// shows segment tree
showArray(st);
System.out.println("\nqueries: \n");
// prints minimum in every range
for(int i=0; i<arr.length; i++) {
for(int j=i+1; j<arr.length; j++)
System.out.println(i+":"+j+" -> "+search(i, j));
}
}
// builds segment tree
static void buildTree(int pos, int left, int right) {
if(left == right) {
st[pos] = arr[left];
return;
}
int mid = (left + right) / 2;
buildTree(left(pos), left, mid);
buildTree(right(pos), mid+1, right);
int p1 = left(pos);
int p2 = right(pos);
st[pos] = (st[p1] > st[p2]) ? st[p2] : st[p1];
}
// left child
static int left(int pos) {
return pos * 2 + 1;
}
// right child
static int right(int pos) {
return pos * 2 + 2;
}
static int search(int left, int right) {
return rmq(0, 0, st.length, left, right);
}
// range minimum query function
static int rmq(int pos, int left, int right, int qleft, int qright) {
if((qleft > right) && (qright < left))
return Integer.MAX_VALUE;
if((qleft <= left) || (qright >= right))
return st[pos];
int mid = (left + right) / 2;
int l = rmq(left(pos), left, mid, qleft, qright);
int r = rmq(right(pos), mid + 1, right, qleft, qright);
return (l > r) ? r : l;
}
// show segment tree
static void showArray(int arr[]) {
for(Integer x : arr)
System.out.print(x+" ");
System.out.println();
}
}
You have to make following changes in your code.
First of all size of array used for storing data in segmentree datastructure should be 4 times of size of given array. Here is why?
st = new int [ 4 * arr.length - 1];
Next in search function, you third parameter for rmq must be arr.length - 1.
static int search(int left, int right) {
return rmq(0, 0, arr.length - 1, left, right);
}
And finally, you have to correct base cases and arguments in child calls in rmq function as follows:
static int rmq(int pos, int left, int right, int qleft, int qright) {
if((qleft > right) || (qright < left))
return Integer.MAX_VALUE;
if((qleft <= left) && (qright >= right))
return st[pos];
int mid = (left + right) / 2;
int l = rmq(left(pos), left, mid, qleft, Math.min(qright, mid) );
int r = rmq(right(pos), mid + 1, right, Math.max(mid + 1, qleft), qright);
return (l > r) ? r : l;
}
Hope this helps.

Iterative/ Non-Recursive Merge Sort

I was trying iterative merge sort , but am stuck at at conditions when input length is not 2^x.
like int[] A ={4,5,1,254,66,75,12,8,65,4,87,63,53,8,99,54,12,34};
public class MergeSort {
public static void sort(int[] A) {
System.out.println("Log(A.len):"+log(A.length, 2));
for (int i = 0; i < log(A.length, 2); i++) { //log A.len
int r = 2 << i; //2^i
int mid = r >>> 1;
for (int j = 0; j+r < A.length; j = j + r) {
System.out.print("offset:" + j + " mid:" + (j + mid) + " r:" + (j + r));
merge(A, j, (j + mid), (j + r));
}
}
}
public static void merge(int[] A, int offset, int mid, int n) {
mid = mid - offset;
n = n - offset;
int[] L = new int[mid];
int[] R = new int[n - mid];
for (int i = 0; i < mid; i++) {
L[i] = A[i + offset];
R[i] = A[mid + i + offset];
}
System.out.print("\nL:");
print_array(L);
System.out.print("\nR:");
print_array(R);
int l = 0;
int r = 0; //left right pointer
int k = offset;
while (l < mid && r < mid) {
if (L[l] < R[r]) {
// System.out.println("in left");
A[k] = L[l];
l++;
} else {
// System.out.println("in right");
A[k] = R[r];
r++;
}
k++;
}
while (l < mid) {
A[k] = L[l];
l++;
k++;
}
while (r < mid) {
A[k] = R[r];
r++;
k++;
}
System.out.print("\nA:");
print_array(A);
System.out.print("\n\n");
}
public static void main(String[] args) {
int[] A ={4,5,1,254,66,75,12,8,65,4,87,63,53,8,99,54,12,34};
sort(A);
}
public static void print_array(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
}
It works fine when input length is 2^x.
Also is there any better way to implement iterative version , this looks a lot messy.
C++ example of bottom up merge sort. a[] is array to sort, b[] is temp array. It includes a check for number of merge passes and swaps in place if the number of passes would be odd, in order to end up with the sorted data in a[].
void BottomUpMerge(int a[], int b[], size_t ll, size_t rr, size_t ee);
void BottomUpCopy(int a[], int b[], size_t ll, size_t rr);
size_t GetPassCount(size_t n);
void BottomUpMergeSort(int a[], int b[], size_t n)
{
size_t s = 1; // run size
if(GetPassCount(n) & 1){ // if odd number of passes
for(s = 1; s < n; s += 2) // swap in place for 1st pass
if(a[s] < a[s-1])
std::swap(a[s], a[s-1]);
s = 2;
}
while(s < n){ // while not done
size_t ee = 0; // reset end index
while(ee < n){ // merge pairs of runs
size_t ll = ee; // ll = start of left run
size_t rr = ll+s; // rr = start of right run
if(rr >= n){ // if only left run
rr = n;
BottomUpCopy(a, b, ll, rr); // copy left run
break; // end of pass
}
ee = rr+s; // ee = end of right run
if(ee > n)
ee = n;
BottomUpMerge(a, b, ll, rr, ee);
}
std::swap(a, b); // swap a and b
s <<= 1; // double the run size
}
}
void BottomUpMerge(int a[], int b[], size_t ll, size_t rr, size_t ee)
{
size_t o = ll; // b[] index
size_t l = ll; // a[] left index
size_t r = rr; // a[] right index
while(1){ // merge data
if(a[l] <= a[r]){ // if a[l] <= a[r]
b[o++] = a[l++]; // copy a[l]
if(l < rr) // if not end of left run
continue; // continue (back to while)
do // else copy rest of right run
b[o++] = a[r++];
while(r < ee);
break; // and return
} else { // else a[l] > a[r]
b[o++] = a[r++]; // copy a[r]
if(r < ee) // if not end of right run
continue; // continue (back to while)
do // else copy rest of left run
b[o++] = a[l++];
while(l < rr);
break; // and return
}
}
}
void BottomUpCopy(int a[], int b[], size_t ll, size_t rr)
{
do // copy left run
b[ll] = a[ll];
while(++ll < rr);
}
size_t GetPassCount(size_t n) // return # passes
{
size_t i = 0;
for(size_t s = 1; s < n; s <<= 1)
i += 1;
return(i);
}

Optimal Binary Search Trees

I have an assignment on optimal binary search trees and some questions came up while doing it. I have found many of the links online helpful (just from a Google search) but I was wondering...
Why must the keys must be initially sorted?
If I get a lower cost (for an optimal BST) when the keys are not sorted, does that mean there must be an error in my code?
Must an optimal BST be complete/perfect? (using the Wikipedia definitions of complete and perfect)
A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. [1] (This is ambiguously also called a complete binary tree.)
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. [2]
For the last question, I would assume that an optimal tree must be complete/perfect, but some of the applets online lead me to believe otherwise. I cannot reason why though...
Why must the keys be initially sorted?
They don't. In fact, unless you're using a self-balancing tree, it's better if you add the keys to the tree in random order, because the tree will end up more balanced.
If I get a lower cost (for an optimal BST) when the keys are not sorted, does that mean there must be an error in my code?
Not unless you're coding up a self-balancing tree (your self-balancing algorithm is not working).
must an optimal BST be complete/perfect?
Yes. In order to get the fastest possible search for a given tree, all of the tree's nodes must be equally distributed; i.e. the tree must be as short as possible.
void OptimalBinsearchtree_output(float R[21][20],int i, int j, int r1, char *dir)
{
int t;
if (i <= j)
{
t =(int)R[i][j];
fprintf(wp,"%s is %s child of %s\n", name[t], dir, name[r1]);
OptimalBinsearchtree_output(R,i, t - 1, t, "left");
OptimalBinsearchtree_output(R,t + 1, j, t, "right");
}
}
void OptimalBinarySearchTree(int n, const float p[],float *minavg)
{
int i, j, k, diagonal,l,pos;
float R[21][20];
float min = 0;
float A[21][20],sum=0;
printf("\n");
for (i = 1; i <=n; i++)
{
A[i][i - 1] = 0;
R[i][i - 1] = 0;
A[i][i] = p[i];
R[i][i] = i;
fprintf(wp,"A[%d][%d]=%4f\tA[%d][%d]=%4f\t",i,i-1,A[i][i-1],i,i,A[i][i]);
fprintf(wp,"R[%d][%d]=%4f\tR[%d][%d]=%4f\n", i, i - 1, R[i][i - 1], i, i, R[i][i]);
}
A[n+1][n] = 0;
R[n+1][n] = 0;
for (diagonal = 1; diagonal <= n - 1; diagonal++)
{
for (i = 1; i <= n - diagonal; i++)
{
min = 0;
sum = 0;
j = i + diagonal;
for (l = i; l <=j; l++)
{
sum = sum + p[l];
}
A[i][j] = sum;
for (k = i; k <= j; k++)
{
sum = A[i][k - 1] + A[k + 1][j];
if (min == 0)
{
min = sum;
pos = k;
}
else if (sum<min)
{
min = sum;
pos = k;
}
}
A[i][j] += min;
R[i][j] = pos;
}
}
*minavg = A[1][n];
printf("\n");
for (i = 1; i <= n; i++)
{
for (j = 0; j <= n; j++)
{
printf("%0.3f ", R[i][j]);
}
printf("\n");
}
for (i = 1; i <= n; i++)
{
for (j = 0; j <= n; j++)
{
printf("%0.3f ", A[i][j]);
}
printf("\n");
}
fprintf(wp,"\n\n");
fprintf(wp,"%s is the root of the tree\n",name[(int)R[1][n]]);
int r1 = (int)R[1][n];
OptimalBinsearchtree_output(R,1, r1 - 1, r1, "left");
OptimalBinsearchtree_output(R,r1 + 1, n, r1, "right");
}
void removeall()
{
nodeptr node,temp;
node = head;
while (node->next != NULL)
{
temp = node;
node = node->next;
}
if (node == node->next)
{
node->next = NULL;
temp->next = NULL;
free(node);
return;
}
node->next = NULL;
temp->next = NULL;
free(node);
}
void print()
{
nodeptr curr = NULL, temp = NULL;
curr = head;
gl_index = 1;
while (curr != NULL)
{
curr->index = gl_index;
gl_p[gl_index] = curr->val;
strcpy(name[gl_index], curr->str);
gl_index++;
wp=fopen("Output.txt","w+");
fprintf(wp,"%s\t%f\t%d\n", curr->str, curr->val, curr->index);
curr = curr->next;
}
}
void generatenode()
{
int i, j;
nodeptr temp = NULL;
char a[20];
while (!feof(fp))
{
nodeptr curr = NULL, prev = NULL;
temp = (struct node*)malloc(sizeof(struct node));
fscanf(fp, "%s", &temp->str);
fgets(a, 20, fp);
temp->index = gl_index;
b = atof(a);
int flag = 0;
temp->val = b;
gl_p[gl_index] = temp->val;
gl_index++;
temp->next = NULL;
if (head == NULL)
{
head = temp;
curr = head;
}
else
{
curr = head;
while (!(strcmp(temp->str, curr->str) < 0))
{
if(curr->next==NULL)
{
curr->next = temp;
curr = curr->next;
temp->next = NULL;
flag = 0;
break;
}
else
{
flag = 1;
prev = curr;
curr = curr->next;
}
}
if (curr == head)
{
temp->next = curr;
head = temp;
}
else
{
if (flag == 1)
{
prev->next = temp;
temp->next = curr;
}
}
flag = 0;
}
}
}

Knapsack 0-1 path reconstruction (which items to take) [duplicate]

This question already has answers here:
How to find which elements are in the bag, using Knapsack Algorithm [and not only the bag's value]?
(4 answers)
Closed 2 years ago.
I know how to solve knapsack 0-1 problem with dynamic programming approach, but I am having troubles figuring out which items to take without compromising the complexity of O(N * C) (N items, C capacity).
Any ideas (I would prefer a bottom-up approach)?
Suppose, right now you're storing results in array bool[] a, where a[i] is true when sum i can be achieved.
You'll need another array int[] b, where b[i] is a last element you've placed into knapsack to achieve sum i.
So, where you had
a[i] = true;
you'll need
a[i] = true;
b[i] = current_item;
Then, finding which items can be taken to achieve sum i is a simple loop.
PS I use two arrays for simplicity, but obviously array a can be removed.
Here is a modification to reconstruct path in O(n) times
int knapsack(int weight[], int profit[], int no_of_items, int capacity) {
for (int var = 0; var <= capacity; ++var) {
dp[0][var] = 0;
}
for (int var = 0; var <= no_of_items; ++var) {
path[var] = false;
}
int using_item_i, without_using_item_i;
for (int i = 1; i <= no_of_items; ++i) {
for (int j = 1; j <= capacity; ++j) {
without_using_item_i = dp[i - 1][j];
using_item_i = 0;
if ((weight[i]) <= j) {
using_item_i = dp[i - 1][j - weight[i]] + profit[i];
}
if (using_item_i >= without_using_item_i) {
taken[i][j] = true;
dp[i][j] = using_item_i;
} else {
taken[i][j] = false;
dp[i][j] = without_using_item_i;
}
}
}
//Reconstructing back the path
int j = capacity;
for (int i = no_of_items; i >= 0; --i) {
if (taken[i][j]) {
path[i] = true;
cnt++;
}
j = j - weight[i];
}
return dp[no_of_items][capacity];
}
boolean[] solution = new boolean[nItems];
for (int i = nItems, c = maxCapacity; i > 0 && c > 0; i--) {
int iThItemAddedValue = value[i - 1][c - weights[i - 1]] + values[i - 1];
int iThItemInheritedValue = value[i - 1][c];
if (iThItemAddedValue > iThItemInheritedValue) {
solution[i - 1] = true;
c = c - weights[i - 1];
} else {
solution[i - 1] = false;
}
}
Check the sol in the attached image
public class Knapsackproblem {
private static int[][] cache;
public static void main(String[] args) {
int val[] = new int[]{60, 100, 120};
int wt[] = new int[]{10, 20, 30};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
printValues(wt,val);
}
/**
* This method will find the result with
* more value with weight less than or equal
* to given weight
* #param w given weight
* #param wt arrays of weights
* #param val array of values
* #param n length of the array
* #return max value we can obtain
*/
private static int knapSack(int w, int[] wt, int[] val, int n) {
cache = new int[n+1][w+1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= w; j++) {
if(j < wt[i-1]){
cache[i][j] = cache[i-1][j];
}else {
cache[i][j] = Math.max(cache[i-1][j],(cache[i-1][j-wt[i-1]])+val[i-1]);
}
}
}
for (int[] aCache : cache) {
System.out.println(Arrays.toString(aCache));
}
return cache[n][w];
}
private static void printValues(int[] wt, int[] val) {
int m = cache.length-1;
int n = cache[0].length-1;
util(wt,val,m,n);
}
private static void util(int[] wt, int[] val, int m, int n) {
if(m <=0 || n<=0) return;
if((cache[m][n] != cache[m-1][n]) && (cache[m][n] != cache[m][n-1])){
System.out.println(val[m-1]+"-->"+wt[m-1]);
util(wt, val, m-1, (n - wt[m - 1] + 1));
}else
if(cache[m][n] == cache[m-1][n]){
util(wt,val,m-1,n);
}
else if(cache[m][n] == cache[m][n-1])
util(wt,val,m,n-1);
else
util(wt,val,m,(n-val[m-1]+1));
}
}
https://www.dropbox.com/s/ish7t5vgy91fovt/Screenshot%202017-01-01%2015.16.31.png?dl=0
Print the tmpList in the caller and you will get the answer

Resources