codeparrot/xlcost-text-to-code huggingface dataset:
Modify contents of Linked List | Java implementation to modify the contents of the linked list ; Linked list node ; Function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list at the end of the new node ; move the head to point to the new node ; function to print the linked list ; Function to middle node of list . ; Advance ' fast ' two nodes , and advance ' slow ' one node ; If number of nodes are odd then update slow by slow . next ; ; function to modify the contents of the linked list . ; Create Stack . ; Traverse the list by using temp until stack is empty . ; Driver program to test above ; creating the linked list ; Call Function to Find the starting point of second half of list . ; Call function to modify the contents of the linked list . ; print the modified linked list
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) { return ; } while ( head . next != null ) { System . out . print ( head . data + " - > " ) ; head = head . next ; } System . out . print ( head . data + "NEW_LINE"); } static Node find_mid ( Node head ) { Node temp = head , slow = head , fast = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } if ( fast != null ) { slow = slow . next ; } return slow ; } static void modifyTheList ( Node head , Node slow ) { Stack < Integer > s = new Stack < Integer > ( ) ; Node temp = head ; while ( slow != null ) { s . add ( slow . data ) ; slow = slow . next ; } while ( ! s . empty ( ) ) { temp . data = temp . data - s . peek ( ) ; temp = temp . next ; s . pop ( ) ; } } public static void main ( String [ ] args ) { Node head = null , mid ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; mid = find_mid ( head ) ; modifyTheList ( head , mid ) ; System . out . print ( " Modified ▁ List : " + "NEW_LINE"); printList ( head ) ; } }
Construct BST from given preorder traversal | Set 1 | Construct a BST from given pre - order traversal for example if the given traversal is { 10 , 5 , 1 , 7 , 40 , 50 } , then the output should be the root of the following tree . 10 / \ 5 40 / \ \ 1 7 50 ; This will create the BST ; A wrapper function of createNode ; A function to print BST in inorder ; Driver Code
class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = this . right = null ; } } class CreateBSTFromPreorder { private static Node node ; public static Node createNode ( Node node , int data ) { if ( node == null ) node = new Node ( data ) ; if ( node . data > data ) node . left = createNode ( node . left , data ) ; if ( node . data < data ) node . right = createNode ( node . right , data ) ; return node ; } public static void create ( int data ) { node = createNode ( node , data ) ; } public static void inorderRec ( Node root ) { if ( root != null ) { inorderRec ( root . left ) ; System . out . println ( root . data ) ; inorderRec ( root . right ) ; } } public static void main ( String [ ] args ) { int [ ] nodeData = { 10 , 5 , 1 , 7 , 40 , 50 } ; for ( int i = 0 ; i < nodeData . length ; i ++ ) { create ( nodeData [ i ] ) ; } inorderRec ( node ) ; } }
Modify a binary tree to get preorder traversal using right pointers only | Java code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; function that allocates a new node with the given data and null left and right pointers . ; Function to modify tree ; if the left tree exists ; get the right - most of the original left subtree ; set root right to left subtree ; if the right subtree does not exists we are done ! ; set right pointer of right - most of the original left subtree ; modify the rightsubtree ; printing using right pointer only ; Driver cde ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static Node modifytree ( Node root ) { Node right = root . right ; Node rightMost = root ; if ( root . left != null ) { rightMost = modifytree ( root . left ) ; root . right = root . left ; root . left = null ; } if ( right == null ) return rightMost ; rightMost . right = right ; rightMost = modifytree ( right ) ; return rightMost ; } static void printpre ( Node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }
Construct BST from its given level order traversal | Java implementation to construct a BST from its level order traversal ; node of a BST ; function to get a new node ; Allocate memory ; put in the data ; function to construct a BST from its level order traversal ; function to print the inorder traversal ; Driver code
class GFG { static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static Node LevelOrder ( Node root , int data ) { if ( root == null ) { root = getNode ( data ) ; return root ; } if ( data <= root . data ) root . left = LevelOrder ( root . left , data ) ; else root . right = LevelOrder ( root . right , data ) ; return root ; } static Node constructBst ( int arr [ ] , int n ) { if ( n == 0 ) return null ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = LevelOrder ( root , arr [ i ] ) ; return root ; } static void inorderTraversal ( Node root ) { if ( root == null ) return ; inorderTraversal ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorderTraversal ( root . right ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . length ; Node root = constructBst ( arr , n ) ; System . out . print ( " Inorder ▁ Traversal : ▁ " ) ; inorderTraversal ( root ) ; } }
Modify a binary tree to get preorder traversal using right pointers only | Java code to modify binary tree for traversal using only right pointer ; A binary tree node has data , left child and right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; An iterative process to set the right pointer of Binary tree ; Base Case ; Create an empty stack and push root to it ; Pop all items one by one . Do following for every popped item a ) print it b ) push its right child c ) push its left child Note that right child is pushed first so that left is processed first ; Pop the top item from stack ; Push right and left children of the popped node to stack ; check if some previous node exists ; set the right pointer of previous node to currrent ; set previous node as current node ; printing using right pointer only ; Driver code ; Constructed binary tree is 10 / \ 8 2 / \ 3 5
import java . util . * ; class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void modifytree ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . push ( root ) ; Node pre = null ; while ( nodeStack . isEmpty ( ) == false ) { Node node = nodeStack . peek ( ) ; nodeStack . pop ( ) ; if ( node . right != null ) nodeStack . push ( node . right ) ; if ( node . left != null ) nodeStack . push ( node . left ) ; if ( pre != null ) { pre . right = node ; } pre = node ; } } static void printpre ( Node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }
Check given array of size n can represent BST of n levels or not | Java program to Check given array can represent BST or not ; structure for Binary Node ; To create a Tree with n levels . We always insert new node to left if it is less than previous value . ; Please refer below post for details of this function . www . geeksforgeeks . org / a - program - to - check - if - a - binary - tree - is - bst - or - not / https : ; Allow only distinct values ; Returns tree if given array of size n can represent a BST of n levels . ; Driver code
class GFG { static class Node { int key ; Node right , left ; } ; static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . key = num ; temp . left = null ; temp . right = null ; return temp ; } static Node createNLevelTree ( int arr [ ] , int n ) { Node root = newNode ( arr [ 0 ] ) ; Node temp = root ; for ( int i = 1 ; i < n ; i ++ ) { if ( temp . key > arr [ i ] ) { temp . left = newNode ( arr [ i ] ) ; temp = temp . left ; } else { temp . right = newNode ( arr [ i ] ) ; temp = temp . right ; } } return root ; } static boolean isBST ( Node root , int min , int max ) { if ( root == null ) { return true ; } if ( root . key < min root . key > max ) { return false ; } return ( isBST ( root . left , min , ( root . key ) - 1 ) && isBST ( root . right , ( root . key ) + 1 , max ) ) ; } static boolean canRepresentNLevelBST ( int arr [ ] , int n ) { Node root = createNLevelTree ( arr , n ) ; return isBST ( root , Integer . MIN_VALUE , Integer . MAX_VALUE ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 512 , 330 , 78 , 11 , 8 } ; int n = arr . length ; if ( canRepresentNLevelBST ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Check given array of size n can represent BST of n levels or not | Java program to Check given array can represent BST or not ; Driver code ; This element can be inserted to the right of the previous element , only if it is greater than the previous element and in the range . ; max remains same , update min ; This element can be inserted to the left of the previous element , only if it is lesser than the previous element and in the range . ; min remains same , update max ; if the loop completed successfully without encountering else condition
class Solution { public static void main ( String args [ ] ) { int arr [ ] = { 5123 , 3300 , 783 , 1111 , 890 } ; int n = arr . length ; int max = Integer . MAX_VALUE ; int min = Integer . MIN_VALUE ; boolean flag = true ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { min = arr [ i - 1 ] ; } else if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { max = arr [ i - 1 ] ; } else { flag = false ; break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Convert a normal BST to Balanced BST | Java program to convert a left unbalanced BST to a balanced BST ; A binary tree node has data , pointer to left child and a pointer to right child ; This function traverse the skewed binary tree and stores its nodes pointers in vector nodes [ ] ; Base case ; Store nodes in Inorder ( which is sorted order for BST ) ; Recursive function to construct binary tree ; base case ; Get the middle element and make it root ; Using index in Inorder traversal , construct left and right subtress ; This functions converts an unbalanced BST to a balanced BST ; Store nodes of given BST in sorted order ; Constucts BST from nodes [ ] ; Function to do preorder traversal of tree ; Driver program to test the above functions ; Constructed skewed binary tree is 10 / 8 / 7 / 6 / 5
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void storeBSTNodes ( Node root , Vector < Node > nodes ) { if ( root == null ) return ; storeBSTNodes ( root . left , nodes ) ; nodes . add ( root ) ; storeBSTNodes ( root . right , nodes ) ; } Node buildTreeUtil ( Vector < Node > nodes , int start , int end ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; Node node = nodes . get ( mid ) ; node . left = buildTreeUtil ( nodes , start , mid - 1 ) ; node . right = buildTreeUtil ( nodes , mid + 1 , end ) ; return node ; } Node buildTree ( Node root ) { Vector < Node > nodes = new Vector < Node > ( ) ; storeBSTNodes ( root , nodes ) ; int n = nodes . size ( ) ; return buildTreeUtil ( nodes , 0 , n - 1 ) ; } void preOrder ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 6 ) ; tree . root . left . left . left . left = new Node ( 5 ) ; tree . root = tree . buildTree ( tree . root ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ balanced ▁ BST ▁ is ▁ : " ) ; tree . preOrder ( tree . root ) ; } }
Find the node with minimum value in a Binary Search Tree | A binary tree node ; Given a binary search tree and a number , inserts a new node with the given number in the correct place in the tree . Returns the new root pointer which the caller should then use ( the standard trick to avoid using reference parameters ) . ; 1. If the tree is empty , return a new , single node ; 2. Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Given a non - empty binary search tree , return the minimum data value found in that tree . Note that the entire tree does not need to be searched . ; loop down to find the leftmost leaf ; Driver program to test above functions
class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { static Node head ; Node insert ( Node node , int data ) { if ( node == null ) { return ( new Node ( data ) ) ; } else { if ( data <= node . data ) { node . left = insert ( node . left , data ) ; } else { node . right = insert ( node . right , data ) ; } return node ; } } int minvalue ( Node node ) { Node current = node ; while ( current . left != null ) { current = current . left ; } return ( current . data ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 4 ) ; tree . insert ( root , 2 ) ; tree . insert ( root , 1 ) ; tree . insert ( root , 3 ) ; tree . insert ( root , 6 ) ; tree . insert ( root , 5 ) ; System . out . println ( " Minimum ▁ value ▁ of ▁ BST ▁ is ▁ " + tree . minvalue ( root ) ) ; } }
Check if the given array can represent Level Order Traversal of Binary Search Tree | Java implementation to check if the given array can represent Level Order Traversal of Binary Search Tree ; to store details of a node like node ' s ▁ data , ▁ ' min ' ▁ and ▁ ' max ' ▁ to ▁ obtain ▁ the ▁ range ▁ of ▁ values ▁ where ▁ node ' s left and right child 's should lie ; function to check if the given array can represent Level Order Traversal of Binary Search Tree ; if tree is empty ; queue to store NodeDetails ; index variable to access array elements ; node details for the root of the BST ; until there are no more elements in arr [ ] or queue is not empty ; extracting NodeDetails of a node from the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be left child of ' temp . data ' or not ; Create NodeDetails for newNode and add it to the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be right child of ' temp . data ' or not ; Create NodeDetails for newNode and add it to the queue ; given array represents level order traversal of BST ; given array do not represent level order traversal of BST ; Driver code
import java . util . * ; class Solution { static class NodeDetails { int data ; int min , max ; } ; static boolean levelOrderIsOfBST ( int arr [ ] , int n ) { if ( n == 0 ) return true ; Queue < NodeDetails > q = new LinkedList < NodeDetails > ( ) ; int i = 0 ; NodeDetails newNode = new NodeDetails ( ) ; newNode . data = arr [ i ++ ] ; newNode . min = Integer . MIN_VALUE ; newNode . max = Integer . MAX_VALUE ; q . add ( newNode ) ; while ( i != n && q . size ( ) > 0 ) { NodeDetails temp = q . peek ( ) ; q . remove ( ) ; newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] < ( int ) temp . data && arr [ i ] > ( int ) temp . min ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . min ; newNode . max = temp . data ; q . add ( newNode ) ; } newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] > ( int ) temp . data && arr [ i ] < ( int ) temp . max ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . data ; newNode . max = temp . max ; q . add ( newNode ) ; } } if ( i == n ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . length ; if ( levelOrderIsOfBST ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Construct Tree from given Inorder and Preorder traversals | A binary tree node has data , pointer to left child and a pointer to right child ; Recursive function to construct binary of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for cases where  inorder and preorder do not form a tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; This funtcion is here just to test buildTree ( ) ; first recur on left child ; then print the data of node ; now recur on right child ; driver program to test above functions ; building the tree by printing inorder traversal
class Node { char data ; Node left , right ; Node ( char item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static int preIndex = 0 ; Node buildTree ( char in [ ] , char pre [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; Node tNode = new Node ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( in , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } int search ( char arr [ ] , int strt , int end , char value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return i ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; char in [ ] = new char [ ] { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char pre [ ] = new char [ ] { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = in . length ; Node root = tree . buildTree ( in , pre , 0 , len - 1 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ constructed ▁ tree ▁ is ▁ : ▁ " ) ; tree . printInorder ( root ) ; } }
Lowest Common Ancestor in a Binary Search Tree . | Recursive Java program to print lca of two nodes A binary tree node ; Function to find LCA of n1 and n2 . The function assumes that both n1 and n2 are present in BST ; If both n1 and n2 are smaller than root , then LCA lies in left ; If both n1 and n2 are greater than root , then LCA lies in right ; Driver program to test lca ( ) ; Let us construct the BST shown in the above figure
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static Node lca ( Node root , int n1 , int n2 ) { while ( root != null ) { if ( root . data > n1 && root . data > n2 ) root = root . left ; else if ( root . data < n1 && root . data < n2 ) root = root . right ; else break ; } return root ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; int n1 = 10 , n2 = 14 ; Node t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; n1 = 14 ; n2 = 8 ; t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; n1 = 10 ; n2 = 22 ; t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; } }
A program to check if a binary tree is BST or not | Java program to check if a given tree is BST . ; A binary tree node has data , pointer to left child && a pointer to right child / ; Returns true if given tree is BST . ; Base condition ; if left node exist then check it has correct data or not i . e . left node ' s ▁ data ▁ ▁ should ▁ be ▁ less ▁ than ▁ root ' s data ; if right node exist then check it has correct data or not i . e . right node ' s ▁ data ▁ ▁ should ▁ be ▁ greater ▁ than ▁ root ' s data ; check recursively for every node . ; Helper function that allocates a new node with the given data && null left && right pointers . / ; Driver code
class Sol { static class Node { int data ; Node left , right ; } ; static boolean isBST ( Node root , Node l , Node r ) { if ( root == null ) return true ; if ( l != null && root . data <= l . data ) return false ; if ( r != null && root . data >= r . data ) return false ; return isBST ( root . left , l , root ) && isBST ( root . right , root , r ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; if ( isBST ( root , null , null ) ) System . out . print ( " Is ▁ BST " ) ; else System . out . print ( " Not ▁ a ▁ BST " ) ; } }