codeparrot/xlcost-text-to-code huggingface dataset:
Convert a given Binary tree to a tree that holds Logical AND property | Java code to convert a given binary tree to a tree that holds logical AND property . ; Structure of binary tree ; function to create a new node ; Convert the given tree to a tree where each node is logical AND of its children The main idea is to do Postorder traversal ; first recur on left child ; then recur on right child ; first recur on left child ; then print the data of node ; now recur on right child ; main function ; Create following Binary Tree 1 / \ 1 0 / \ / \ 0 1 1 1
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . data = key ; node . left = null ; node . right = null ; return node ; } static void convertTree ( Node root ) { if ( root == null ) return ; convertTree ( root . left ) ; convertTree ( root . right ) ; if ( root . left != null && root . right != null ) root . data = ( root . left . data ) & ( root . right . data ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ " ) ; printInorder ( root ) ; convertTree ( root ) ; System . out . println ( ) ; System . out . print ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ " ) ; printInorder ( root ) ; } }
Find if an expression has duplicate parenthesis or not | Java program to find duplicate parenthesis in a balanced expression ; Function to find duplicate parenthesis in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; pop character from the stack ; stores the number of characters between a closing and opening parenthesis if this count is less than or equal to 1 then the brackets are redundant else not ; push open parenthesis ' ( ' , operators and operands to stack ; No duplicates found ; Driver code ; input balanced expression
import java . util . Stack ; public class GFG { static boolean findDuplicateparenthesis ( String s ) { Stack < Character > Stack = new Stack < > ( ) ; char [ ] str = s . toCharArray ( ) ; for ( char ch : str ) { if ( ch == ' ) ' ) { char top = Stack . peek ( ) ; Stack . pop ( ) ; int elementsInside = 0 ; while ( top != ' ( ' ) { elementsInside ++ ; top = Stack . peek ( ) ; Stack . pop ( ) ; } if ( elementsInside < 1 ) { return true ; } } else { Stack . push ( ch ) ; } } return false ; } public static void main ( String [ ] args ) { String str = " ( ( ( a + ( b ) ) + ( c + d ) ) ) " ; if ( findDuplicateparenthesis ( str ) ) { System . out . println ( " Duplicate ▁ Found ▁ " ) ; } else { System . out . println ( " No ▁ Duplicates ▁ Found ▁ " ) ; } } }
Find next Smaller of next Greater in an array | Java Program to find Right smaller element of next greater element ; function find Next greater element ; create empty stack ; Traverse all array elements in reverse order order == ' G ' we compute next greater elements of every element order == ' S ' we compute right smaller element of every element ; Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S ) ; store the next greater element of current element ; If all elements in S were smaller than arr [ i ] ; Push this element ; Function to find Right smaller element of next greater element ; stores indexes of next greater elements ; stores indexes of right smaller elements ; Find next greater element Here G indicate next greater element ; Find right smaller element using same function nextGreater ( ) Here S indicate right smaller elements ; If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ] ; Driver code
import java . util . Stack ; public class Main { public static void nextGreater ( int arr [ ] , int next [ ] , char order ) { Stack < Integer > stack = new Stack < > ( ) ; for ( int i = arr . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && ( ( order == ' G ' ) ? arr [ stack . peek ( ) ] <= arr [ i ] : arr [ stack . peek ( ) ] >= arr [ i ] ) ) stack . pop ( ) ; if ( ! stack . isEmpty ( ) ) next [ i ] = stack . peek ( ) ; else next [ i ] = - 1 ; stack . push ( i ) ; } } public static void nextSmallerOfNextGreater ( int arr [ ] ) { int NG [ ] = new int [ arr . length ] ; int RS [ ] = new int [ arr . length ] ; nextGreater ( arr , NG , ' G ' ) ; nextGreater ( arr , RS , ' S ' ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( NG [ i ] != - 1 && RS [ NG [ i ] ] != - 1 ) System . out . print ( arr [ RS [ NG [ i ] ] ] + " ▁ " ) ; else System . out . print ( " - 1 ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 9 , 2 , 5 , 1 , 7 } ; nextSmallerOfNextGreater ( arr ) ; } }
Convert Ternary Expression to a Binary Tree | Java program to convert a ternary expreesion to a tree . ; Class to represent Tree node ; Class to convert a ternary expression to a Tree ; Function to convert Ternary Expression to a Binary Tree . It return the root of tree ; Base case ; store current character of expression_string [ ' a ' to ' z ' ] ; Move ahead in str ; if current character of ternary expression is ' ? ' then we add next character as a left child of current node ; else we have to add it as a right child of current node expression . at ( 0 ) == ' : ' ; function print tree ; Driver program to test above function
import java . util . Queue ; import java . util . LinkedList ; class Node { char data ; Node left , right ; public Node ( char item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node convertExpression ( char [ ] expression , int i ) { if ( i >= expression . length ) return null ; Node root = new Node ( expression [ i ] ) ; ++ i ; if ( i < expression . length && expression [ i ] == ' ? ' ) root . left = convertExpression ( expression , i + 1 ) ; else if ( i < expression . length ) root . right = convertExpression ( expression , i + 1 ) ; return root ; } public void printTree ( Node root ) { if ( root == null ) return ; System . out . print ( root . data + " ▁ " ) ; printTree ( root . left ) ; printTree ( root . right ) ; } public static void main ( String args [ ] ) { String exp = " a ? b ? c : d : e " ; BinaryTree tree = new BinaryTree ( ) ; char [ ] expression = exp . toCharArray ( ) ; Node root = tree . convertExpression ( expression , 0 ) ; tree . printTree ( root ) ; } }
Count natural numbers whose all permutation are greater than that number | Java program to count the number less than N , whose all permutation is greater than or equal to the number . ; Return the count of the number having all permutation greater than or equal to the number . ; Pushing 1 to 9 because all number from 1 to 9 have this property . ; take a number from stack and add a digit smaller than last digit of it . ; Driven Code
import java . util . Stack ; class GFG { static int countNumber ( int n ) { int result = 0 ; Stack < Integer > s = new Stack < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . peek ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { int x = tp * 10 + j ; if ( x <= n ) { s . push ( x ) ; result ++ ; } } } } return result ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( countNumber ( n ) ) ; } }
Delete consecutive same words in a sequence | Java program to remove consecutive same words ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Compare the current string with next one Erase both if equal ; Erase function delete the element and also shifts other element that 's why  i is not updated ; Update i , as to check from previous element again ; Reduce sequence size ; Increment i , if not equal ; Return modified size ; Driver method
import java . util . Vector ; class Test { static int removeConsecutiveSame ( Vector < String > v ) { int n = v . size ( ) ; for ( int i = 0 ; i < n - 1 ; ) { if ( v . get ( i ) . equals ( v . get ( i + 1 ) ) ) { v . remove ( i ) ; v . remove ( i ) ; if ( i > 0 ) i -- ; n = n - 2 ; } else i ++ ; } return v . size ( ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . addElement ( " tom " ) ; v . addElement ( " jerry " ) ; v . addElement ( " jerry " ) ; v . addElement ( " tom " ) ; System . out . println ( removeConsecutiveSame ( v ) ) ; } }
Delete consecutive same words in a sequence | Java implementation of above method ; Method to find the size of manipulated sequence ; Start traversing the sequence ; Push the current string if the stack is empty ; compare the current string with stack top if equal , pop the top ; Otherwise push the current string ; Return stack size ; Driver method
import java . util . Stack ; import java . util . Vector ; class Test { static int removeConsecutiveSame ( Vector < String > v ) { Stack < String > st = new Stack < > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( st . empty ( ) ) st . push ( v . get ( i ) ) ; else { String str = st . peek ( ) ; if ( str . equals ( v . get ( i ) ) ) st . pop ( ) ; else st . push ( v . get ( i ) ) ; } } return st . size ( ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . addElement ( " ab " ) ; v . addElement ( " aa " ) ; v . addElement ( " aa " ) ; v . addElement ( " bcd " ) ; v . addElement ( " ab " ) ; System . out . println ( removeConsecutiveSame ( v ) ) ; } }
Decode a string recursively encoded as count followed by substring | Java program to decode a string recursively encoded as count followed substring ; Returns decoded string for ' str ' ; Traversing the string ; If number , convert it into number and push it into integerstack . ; If closing bracket ' ] ' , pop elemment until ' [ ' opening bracket is not found in the character stack . ; Repeating the popped string ' temo ' count number of times . ; Push it in the character stack . ; If ' [ ' opening bracket , push it into character stack . ; Pop all the elmenet , make a string and return . ; Driver method
import java . util . Stack ; class Test { static String decode ( String str ) { Stack < Integer > integerstack = new Stack < > ( ) ; Stack < Character > stringstack = new Stack < > ( ) ; String temp = " " , result = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( Character . isDigit ( str . charAt ( i ) ) ) { while ( Character . isDigit ( str . charAt ( i ) ) ) { count = count * 10 + str . charAt ( i ) - '0' ; i ++ ; } i -- ; integerstack . push ( count ) ; } else if ( str . charAt ( i ) == ' ] ' ) { temp = " " ; count = 0 ; if ( ! integerstack . isEmpty ( ) ) { count = integerstack . peek ( ) ; integerstack . pop ( ) ; } while ( ! stringstack . isEmpty ( ) && stringstack . peek ( ) != ' [ ' ) { temp = stringstack . peek ( ) + temp ; stringstack . pop ( ) ; } if ( ! stringstack . empty ( ) && stringstack . peek ( ) == ' [ ' ) stringstack . pop ( ) ; for ( int j = 0 ; j < count ; j ++ ) result = result + temp ; for ( int j = 0 ; j < result . length ( ) ; j ++ ) stringstack . push ( result . charAt ( j ) ) ; result = " " ; } else if ( str . charAt ( i ) == ' [ ' ) { if ( Character . isDigit ( str . charAt ( i - 1 ) ) ) stringstack . push ( str . charAt ( i ) ) ; else { stringstack . push ( str . charAt ( i ) ) ; integerstack . push ( 1 ) ; } } else stringstack . push ( str . charAt ( i ) ) ; } while ( ! stringstack . isEmpty ( ) ) { result = stringstack . peek ( ) + result ; stringstack . pop ( ) ; } return result ; } public static void main ( String args [ ] ) { String str = "3 [ b2 [ ca ] ] " ; System . out . println ( decode ( str ) ) ; } }
Iterative method to find ancestors of a given binary tree | Java program to print all ancestors of a given key ; Structure for a tree node ; A utility function to create a new tree node ; Iterative Function to print all ancestors of a given key ; Create a stack to hold ancestors ; Traverse the complete tree in postorder way till we find the key ; Traverse the left side . While traversing , push the nodes into the stack so that their right subtrees can be traversed later ; push current node ; move to next node ; If the node whose ancestors are to be printed is found , then break the while loop . ; Check if right sub - tree exists for the node at top If not then pop that node because we don 't   need this node any more.  ; If the popped node is right child of top , then remove the top as well . Left child of the top must have processed before . ; if stack is not empty then simply set the root as right child of top and start traversing right sub - tree . ; If stack is not empty , print contents of stack Here assumption is that the key is there in tree ; Driver code ; Let us construct a binary tree
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void printAncestors ( Node root , int key ) { if ( root == null ) return ; Stack < Node > st = new Stack < Node > ( ) ; while ( 1 == 1 ) { while ( root != null && root . data != key ) { st . push ( root ) ; root = root . left ; } if ( root != null && root . data == key ) break ; if ( st . peek ( ) . right == null ) { root = st . peek ( ) ; st . pop ( ) ; while ( ! st . isEmpty ( ) && st . peek ( ) . right == root ) { root = st . peek ( ) ; st . pop ( ) ; } } root = st . isEmpty ( ) ? null : st . peek ( ) . right ; } while ( ! st . isEmpty ( ) ) { System . out . print ( st . peek ( ) . data + " ▁ " ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 9 ) ; root . left . left . left = newNode ( 4 ) ; root . left . right . right = newNode ( 6 ) ; root . right . right . left = newNode ( 10 ) ; int key = 6 ; printAncestors ( root , key ) ; } }
Tracking current Maximum Element in a Stack | Java program to keep track of maximum element in a stack ; main stack ; tack to keep track of max element ; If current element is greater than the top element of track stack , push the current element to track stack otherwise push the element at top of track stack again into it . ; Driver program to test above functions
import java . util . * ; class GfG { static class StackWithMax { static Stack < Integer > mainStack = new Stack < Integer > ( ) ; static Stack < Integer > trackStack = new Stack < Integer > ( ) ; static void push ( int x ) { mainStack . push ( x ) ; if ( mainStack . size ( ) == 1 ) { trackStack . push ( x ) ; return ; } if ( x > trackStack . peek ( ) ) trackStack . push ( x ) ; else trackStack . push ( trackStack . peek ( ) ) ; } static int getMax ( ) { return trackStack . peek ( ) ; } static void pop ( ) { mainStack . pop ( ) ; trackStack . pop ( ) ; } } ; public static void main ( String [ ] args ) { StackWithMax s = new StackWithMax ( ) ; s . push ( 20 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 10 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 50 ) ; System . out . println ( s . getMax ( ) ) ; } }
Reverse a number using stack | Java program to reverse the number using a stack ; Stack to maintain order of digits ; Function to push digits into stack ; Function to reverse the number ; Function call to push number 's  digits to stack ; Popping the digits and forming the reversed number ; Return the reversed number formed ; Driver program to test above function ; Function call to reverse number
import java . util . Stack ; public class GFG { static Stack < Integer > st = new Stack < > ( ) ; static void push_digits ( int number ) { while ( number != 0 ) { st . push ( number % 10 ) ; number = number / 10 ; } } static int reverse_number ( int number ) { push_digits ( number ) ; int reverse = 0 ; int i = 1 ; while ( ! st . isEmpty ( ) ) { reverse = reverse + ( st . peek ( ) * i ) ; st . pop ( ) ; i = i * 10 ; } return reverse ; } public static void main ( String [ ] args ) { int number = 39997 ; System . out . println ( reverse_number ( number ) ) ; } }
Flip Binary Tree | Java program to flip a binary tree ; A binary tree node structure ; method to flip the binary tree ; recursively call the same method ; rearranging main root Node after returning from recursive call ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current lelvel . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
import java . util . Queue ; import java . util . LinkedList ; public class FlipTree { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static Node flipBinaryTree ( Node root ) { if ( root == null ) return root ; if ( root . left == null && root . right == null ) return root ; Node flippedRoot = flipBinaryTree ( root . left ) ; root . left . left = root . right ; root . left . right = root ; root . left = root . right = null ; return flippedRoot ; } public static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( true ) { int nodeCount = q . size ( ) ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . remove ( ) ; System . out . print ( node . data + " ▁ " ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; nodeCount -- ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 1 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ given ▁ tree " ) ; printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ flipped ▁ tree " ) ; printLevelOrder ( root ) ; } }
Check if stack elements are pairwise consecutive | Java program to check if successive pair of numbers in the stack are consecutive or not ; Function to check if elements are pairwise consecutive in stack ; Transfer elements of s to aux . ; Traverse aux and see if elements are pairwise consecutive or not . We also need to make sure that original content is retained . ; Fetch current top two elements of aux and check if they are consecutive . ; Push the elements to original stack . ; Driver program
import java . util . * ; class GfG { static boolean pairWiseConsecutive ( Stack < Integer > s ) { Stack < Integer > aux = new Stack < Integer > ( ) ; while ( ! s . isEmpty ( ) ) { aux . push ( s . peek ( ) ) ; s . pop ( ) ; } boolean result = true ; while ( aux . size ( ) > 1 ) { int x = aux . peek ( ) ; aux . pop ( ) ; int y = aux . peek ( ) ; aux . pop ( ) ; if ( Math . abs ( x - y ) != 1 ) result = false ; s . push ( x ) ; s . push ( y ) ; } if ( aux . size ( ) == 1 ) s . push ( aux . peek ( ) ) ; return result ; } public static void main ( String [ ] args ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 4 ) ; s . push ( 5 ) ; s . push ( - 2 ) ; s . push ( - 3 ) ; s . push ( 11 ) ; s . push ( 10 ) ; s . push ( 5 ) ; s . push ( 6 ) ; s . push ( 20 ) ; if ( pairWiseConsecutive ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; System . out . println ( " Stack ▁ content ▁ ( from ▁ top ) ▁ after ▁ function ▁ call " ) ; while ( s . isEmpty ( ) == false ) { System . out . print ( s . peek ( ) + " ▁ " ) ; s . pop ( ) ; } } }
Remove brackets from an algebraic string containing + and | Java program to simplify algebraic string ; Function to simplify the string ; resultant string of max length equal to length of input string ; create empty stack ; If top is 1 , flip the operator ; If top is 0 , append the same operator ; x is opposite to the top of stack ; push value equal to top of the stack ; If closing parentheses pop the stack once ; copy the character to the result ; Driver program
import java . util . * ; class GfG { static String simplify ( String str ) { int len = str . length ( ) ; char res [ ] = new char [ len ] ; int index = 0 , i = 0 ; Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; while ( i < len ) { if ( str . charAt ( i ) == ' + ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' - ' ; if ( s . peek ( ) == 0 ) res [ index ++ ] = ' + ' ; } else if ( str . charAt ( i ) == ' - ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' + ' ; else if ( s . peek ( ) == 0 ) res [ index ++ ] = ' - ' ; } else if ( str . charAt ( i ) == ' ( ' && i > 0 ) { if ( str . charAt ( i - 1 ) == ' - ' ) { int x = ( s . peek ( ) == 1 ) ? 0 : 1 ; s . push ( x ) ; } else if ( str . charAt ( i - 1 ) == ' + ' ) s . push ( s . peek ( ) ) ; } else if ( str . charAt ( i ) == ' ) ' ) s . pop ( ) ; else res [ index ++ ] = str . charAt ( i ) ; i ++ ; } return new String ( res ) ; } public static void main ( String [ ] args ) { String s1 = " a - ( b + c ) " ; String s2 = " a - ( b - c - ( d + e ) ) - f " ; System . out . println ( simplify ( s1 ) ) ; System . out . println ( simplify ( s2 ) ) ; } }
Growable array based stack | Java Program to implement growable array based stack using tight strategy ; constant amount at which stack is increased ; top of the stack ; length of stack ; function to create new stack ; allocate memory for new stack ; copying the content of old stack ; re - sizing the length ; function to push new element ; if stack is full , create new one ; insert element at top of the stack ; function to pop an element ; function to display ; if top is - 1 , that means stack is empty ; Driver Code ; creating initial stack ; pushing element to top of stack ; pushing more element when stack is full ; pushing more element so that stack can grow
class GFG { static final int BOUND = 4 ; static int top = - 1 ; static int length = 0 ; static int [ ] create_new ( int [ ] a ) { int [ ] new_a = new int [ length + BOUND ] ; for ( int i = 0 ; i < length ; i ++ ) new_a [ i ] = a [ i ] ; length += BOUND ; return new_a ; } static int [ ] push ( int [ ] a , int element ) { if ( top == length - 1 ) a = create_new ( a ) ; a [ ++ top ] = element ; return a ; } static void pop ( int [ ] a ) { top -- ; } static void display ( int [ ] a ) { if ( top == - 1 ) System . out . println ( " Stack ▁ is ▁ Empty " ) ; else { System . out . print ( " Stack : ▁ " ) ; for ( int i = 0 ; i <= top ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int [ ] a = create_new ( new int [ length + BOUND ] ) ; a = push ( a , 1 ) ; a = push ( a , 2 ) ; a = push ( a , 3 ) ; a = push ( a , 4 ) ; display ( a ) ; a = push ( a , 5 ) ; a = push ( a , 6 ) ; display ( a ) ; a = push ( a , 7 ) ; a = push ( a , 8 ) ; display ( a ) ; a = push ( a , 9 ) ; display ( a ) ; } }