codeparrot/xlcost-text-to-code huggingface dataset:
Count BST subtrees that lie in given range | Java program to count subtrees that lie in a given range ; A BST node ; int class ; A utility function to check if data of root is in range from low to high ; A recursive function to get count of nodes whose subtree is in range from low to hgih . This function returns true if nodes in subtree rooted under ' root ' are in range . ; Base case ; Recur for left and right subtrees ; If both left and right subtrees are in range and current node is also in range , then increment count and return true ; A wrapper over getCountUtil ( ) . This function initializes count as 0 and calls getCountUtil ( ) ; Utility function to create new node ; Driver code ; Let us con the BST shown in the above figure ; Let us construct BST shown in above example 10 / \ 5 50 / / \ 1 40 100
class GfG { static class node { int data ; node left , right ; } ; static class INT { int a ; } static boolean inRange ( node root , int low , int high ) { return root . data >= low && root . data <= high ; } static boolean getCountUtil ( node root , int low , int high , INT count ) { if ( root == null ) return true ; boolean l = getCountUtil ( root . left , low , high , count ) ; boolean r = getCountUtil ( root . right , low , high , count ) ; if ( l && r && inRange ( root , low , high ) ) { ++ count . a ; return true ; } return false ; } static INT getCount ( node root , int low , int high ) { INT count = new INT ( ) ; count . a = 0 ; getCountUtil ( root , low , high , count ) ; return count ; } static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = temp . right = null ; return ( temp ) ; } public static void main ( String args [ ] ) { node root = newNode ( 10 ) ; root . left = newNode ( 5 ) ; root . right = newNode ( 50 ) ; root . left . left = newNode ( 1 ) ; root . right . left = newNode ( 40 ) ; root . right . right = newNode ( 100 ) ; int l = 5 ; int h = 45 ; System . out . println ( " Count ▁ of ▁ subtrees ▁ in ▁ [ " + l + " , ▁ " + h + " ] ▁ is ▁ " + getCount ( root , l , h ) . a ) ; } }
Remove all leaf nodes from the binary search tree | Java program to delete leaf Node from binary search tree . ; Create a newNode in binary search tree . ; Insert a Node in binary search tree . ; Function for inorder traversal in a BST . ; Delete leaf nodes from binary search tree . ; Else recursively delete in left and right subtrees . ; Driver code
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int data ) { if ( root == null ) return newNode ( data ) ; if ( data < root . data ) root . left = insert ( root . left , data ) ; else if ( data > root . data ) root . right = insert ( root . right , data ) ; return root ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } static Node leafDelete ( Node root ) { if ( root == null ) { return null ; } if ( root . left == null && root . right == null ) { return null ; } root . left = leafDelete ( root . left ) ; root . right = leafDelete ( root . right ) ; return root ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; System . out . println ( " Inorder ▁ before ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; System . out . println ( ) ; leafDelete ( root ) ; System . out . println ( " INorder ▁ after ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; } }
Sum of k smallest elements in BST | Java program to find Sum Of All Elements smaller than or equal to Kth Smallest Element In BST ; Binary tree Node ; utility function new Node of BST ; A utility function to insert a new Node with given key in BST and also maintain lcount , Sum ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; return the ( unchanged ) Node pointer ; function return sum of all element smaller than and equal to Kth smallest element ; Base cases ; Compute sum of elements in left subtree ; Add root 's data ; Add current Node ; If count is less than k , return right subtree Nodes ; Wrapper over ksmallestElementSumRec ( ) ; Driver program to test above functions ; 20 / \ 8 22 / \ 4 12 / \ 10 14
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node createNode ( int data ) { Node new_Node = new Node ( ) ; new_Node . left = null ; new_Node . right = null ; new_Node . data = data ; return new_Node ; } static Node insert ( Node root , int key ) { if ( root == null ) return createNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else if ( root . data < key ) root . right = insert ( root . right , key ) ; return root ; } static int count = 0 ; static int ksmallestElementSumRec ( Node root , int k ) { if ( root == null ) return 0 ; if ( count > k ) return 0 ; int res = ksmallestElementSumRec ( root . left , k ) ; if ( count >= k ) return res ; res += root . data ; count ++ ; if ( count >= k ) return res ; return res + ksmallestElementSumRec ( root . right , k ) ; } static int ksmallestElementSum ( Node root , int k ) { int res = ksmallestElementSumRec ( root , k ) ; return res ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; int count = ksmallestElementSum ( root , k ) ; System . out . println ( count ) ; } }
Sum of k smallest elements in BST | Java program to find Sum Of All Elements smaller than or equal t Kth Smallest Element In BST ; Binary tree Node ; utility function new Node of BST ; A utility function to insert a new Node with given key in BST and also maintain lcount , Sum ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; increment lCount of current Node ; increment current Node sum by adding key into it ; return the ( unchanged ) Node pointer ; function return sum of all element smaller than and equal to Kth smallest element ; if we fount k smallest element then break the function ; store sum of all element smaller than current root ; ; decremented k and call right sub - tree ; call left sub - tree ; Wrapper over ksmallestElementSumRec ( ) ; Driver program to test above functions ; 20 / \ 8 22 / \ 4 12 / \ 10 14
import java . util . * ; class GFG { static class Node { int data ; int lCount ; int Sum ; Node left ; Node right ; } ; static Node createNode ( int data ) { Node new_Node = new Node ( ) ; new_Node . left = null ; new_Node . right = null ; new_Node . data = data ; new_Node . lCount = 0 ; new_Node . Sum = 0 ; return new_Node ; } static Node insert ( Node root , int key ) { if ( root == null ) return createNode ( key ) ; if ( root . data > key ) { root . lCount ++ ; root . Sum += key ; root . left = insert ( root . left , key ) ; } else if ( root . data < key ) root . right = insert ( root . right , key ) ; return root ; } static int temp_sum ; static void ksmallestElementSumRec ( Node root , int k ) { if ( root == null ) return ; if ( ( root . lCount + 1 ) == k ) { temp_sum += root . data + root . Sum ; return ; } else if ( k > root . lCount ) { temp_sum += root . data + root . Sum ; k = k - ( root . lCount + 1 ) ; ksmallestElementSumRec ( root . right , k ) ; } else ksmallestElementSumRec ( root . left , k ) ; } static void ksmallestElementSum ( Node root , int k ) { temp_sum = 0 ; ksmallestElementSumRec ( root , k ) ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; ksmallestElementSum ( root , k ) ; System . out . println ( temp_sum ) ; } }
Inorder Successor in 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 ; step 1 of the above algorithm ; step 2 of the above algorithm ; 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 , parent ; Node ( int d ) { data = d ; left = right = parent = null ; } } class BinaryTree { static Node head ; Node insert ( Node node , int data ) { if ( node == null ) { return ( new Node ( data ) ) ; } else { Node temp = null ; if ( data <= node . data ) { temp = insert ( node . left , data ) ; node . left = temp ; temp . parent = node ; } else { temp = insert ( node . right , data ) ; node . right = temp ; temp . parent = node ; } return node ; } } Node inOrderSuccessor ( Node root , Node n ) { if ( n . right != null ) { return minValue ( n . right ) ; } Node p = n . parent ; while ( p != null && n == p . right ) { n = p ; p = p . parent ; } return p ; } Node minValue ( Node node ) { Node current = node ; while ( current . left != null ) { current = current . left ; } return current ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null , temp = null , suc = null , min = null ; root = tree . insert ( root , 20 ) ; root = tree . insert ( root , 8 ) ; root = tree . insert ( root , 22 ) ; root = tree . insert ( root , 4 ) ; root = tree . insert ( root , 12 ) ; root = tree . insert ( root , 10 ) ; root = tree . insert ( root , 14 ) ; temp = root . left . right . right ; suc = tree . inOrderSuccessor ( root , temp ) ; if ( suc != null ) { System . out . println ( " Inorder ▁ successor ▁ of ▁ " + temp . data + " ▁ is ▁ " + suc . data ) ; } else { System . out . println ( " Inorder ▁ successor ▁ does ▁ not ▁ exist " ) ; } } }
Inorder predecessor and successor for a given key in BST |
static class Node { int key ; Node left , right ; } ;
Inorder predecessor and successor for a given key in BST | Iterative Approach | Java program to find predecessor and successor in a BST ; BST Node ; Function that finds predecessor and successor of key in BST . ; Search for given key in BST . ; If root is given key . ; the minimum value in right subtree is successor . ; the maximum value in left subtree is predecessor . ; If key is greater than root , then key lies in right subtree . Root could be predecessor if left subtree of key is null . ; If key is smaller than root , then key lies in left subtree . Root could be successor if right subtree of key is null . ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; Driver Code ; Key to be searched in BST ; Let us create following BST 50 / \ / \ 30 70 / \ / \ / \ / \ 20 40 60 80
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node pre ; static Node suc ; static void findPreSuc ( Node root , int key ) { if ( root == null ) return ; while ( root != null ) { if ( root . key == key ) { if ( root . right != null ) { suc = root . right ; while ( suc . left != null ) suc = suc . left ; } if ( root . left != null ) { pre = root . left ; while ( pre . right != null ) pre = pre . right ; } return ; } else if ( root . key < key ) { pre = root ; root = root . right ; } else { suc = root ; root = root . left ; } } } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else node . right = insert ( node . right , key ) ; return node ; } public static void main ( String [ ] args ) { int key = 65 ; Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; findPreSuc ( root , key ) ; if ( pre != null ) System . out . println ( " Predecessor ▁ is ▁ " + pre . key ) ; else System . out . print ( " - 1" ) ; if ( suc != null ) System . out . print ( " Successor ▁ is ▁ " + suc . key ) ; else System . out . print ( " - 1" ) ; } }
Find a pair with given sum in a Balanced BST | Java code to find a pair with given sum in a Balanced BST ; A binary tree node ; Root of BST ; Constructor ; Inorder traversal of the tree ; Utility function for inorder traversal of the tree ; This method mainly calls insertRec ( ) ; A recursive function to insert a new key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Method that adds values of given BST into ArrayList and hence returns the ArrayList ; Base Case ; method that checks if there is a pair present ; This list a1 is passed as an argument in treeToList method which is later on filled by the values of BST ; a2 list contains all the values of BST returned by treeToList method ; Starting index of a2 ; Ending index of a2 ; Target Found ! ; decrements end ; increments start ; Driver function ; 15 / \ 10 20 / \ / \ 8 12 16 25
import java . util . ArrayList ; class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinarySearchTree { Node root ; BinarySearchTree ( ) { root = null ; } void inorder ( ) { inorderUtil ( this . root ) ; } void inorderUtil ( Node node ) { if ( node == null ) return ; inorderUtil ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inorderUtil ( node . right ) ; } void insert ( int key ) { root = insertRec ( root , key ) ; } Node insertRec ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( data < root . data ) root . left = insertRec ( root . left , data ) ; else if ( data > root . data ) root . right = insertRec ( root . right , data ) ; return root ; } ArrayList < Integer > treeToList ( Node node , ArrayList < Integer > list ) { if ( node == null ) return list ; treeToList ( node . left , list ) ; list . add ( node . data ) ; treeToList ( node . right , list ) ; return list ; } boolean isPairPresent ( Node node , int target ) { ArrayList < Integer > a1 = new ArrayList < > ( ) ; ArrayList < Integer > a2 = treeToList ( node , a1 ) ; int start = 0 ; int end = a2 . size ( ) - 1 ; while ( start < end ) { if ( a2 . get ( start ) + a2 . get ( end ) == target ) { System . out . println ( " Pair ▁ Found : ▁ " + a2 . get ( start ) + " ▁ + ▁ " + a2 . get ( end ) + " ▁ " + " = ▁ " + target ) ; return true ; } if ( a2 . get ( start ) + a2 . get ( end ) > target ) { end -- ; } if ( a2 . get ( start ) + a2 . get ( end ) < target ) { start ++ ; } } System . out . println ( " No ▁ such ▁ values ▁ are ▁ found ! " ) ; return false ; } public static void main ( String [ ] args ) { BinarySearchTree tree = new BinarySearchTree ( ) ; tree . insert ( 15 ) ; tree . insert ( 10 ) ; tree . insert ( 20 ) ; tree . insert ( 8 ) ; tree . insert ( 12 ) ; tree . insert ( 16 ) ; tree . insert ( 25 ) ; tree . isPairPresent ( tree . root , 33 ) ; } }
Find a pair with given sum in BST | JAVA program to find a pair with given sum using hashing ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node NewNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return NewNode ( key ) ; if ( key < root . data ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static boolean findpairUtil ( Node root , int sum , HashSet < Integer > set ) { if ( root == null ) return false ; if ( findpairUtil ( root . left , sum , set ) ) return true ; if ( set . contains ( sum - root . data ) ) { System . out . println ( " Pair ▁ is ▁ found ▁ ( " + ( sum - root . data ) + " , ▁ " + root . data + " ) " ) ; return true ; } else set . add ( root . data ) ; return findpairUtil ( root . right , sum , set ) ; } static void findPair ( Node root , int sum ) { HashSet < Integer > set = new HashSet < Integer > ( ) ; if ( ! findpairUtil ( root , sum , set ) ) System . out . print ( " Pairs ▁ do ▁ not ▁ exit " + "NEW_LINE"); } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 15 ) ; root = insert ( root , 10 ) ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 12 ) ; root = insert ( root , 16 ) ; root = insert ( root , 25 ) ; root = insert ( root , 10 ) ; int sum = 33 ; findPair ( root , sum ) ; } }
Find pairs with given sum such that pair elements lie in different BSTs | Java program to find pairs with given sum such that one element of pair exists in one BST and other in other BST . ; A binary Tree node ; A utility function to create a new BST node with key as given num ; A utility function to insert a given key to BST ; store storeInorder traversal in auxiliary array ; Function to find pair for given sum in different bst vect1 . get ( ) - . stores storeInorder traversal of first bst vect2 . get ( ) - . stores storeInorder traversal of second bst ; Initialize two indexes to two different corners of two Vectors . ; find pair by moving two corners . ; If we found a pair ; If sum is more , move to higher value in first Vector . ; If sum is less , move to lower value in second Vector . ; Prints all pairs with given " sum " such that one element of pair is in tree with root1 and other node is in tree with root2 . ; Store inorder traversals of two BSTs in two Vectors . ; Now the problem reduces to finding a pair with given sum such that one element is in vect1 and other is in vect2 . ; Driver program to run the case ; first BST ; second BST
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void storeInorder ( Node ptr , Vector < Integer > vect ) { if ( ptr == null ) return ; storeInorder ( ptr . left , vect ) ; vect . add ( ptr . data ) ; storeInorder ( ptr . right , vect ) ; } static void pairSumUtil ( Vector < Integer > vect1 , Vector < Integer > vect2 , int sum ) { int left = 0 ; int right = vect2 . size ( ) - 1 ; while ( left < vect1 . size ( ) && right >= 0 ) { if ( vect1 . get ( left ) + vect2 . get ( right ) == sum ) { System . out . print ( " ( " + vect1 . get ( left ) + " , ▁ " + vect2 . get ( right ) + " ) , ▁ " ) ; left ++ ; right -- ; } else if ( vect1 . get ( left ) + vect2 . get ( right ) < sum ) left ++ ; else right -- ; } } static void pairSum ( Node root1 , Node root2 , int sum ) { Vector < Integer > vect1 = new Vector < Integer > ( ) , vect2 = new Vector < Integer > ( ) ; storeInorder ( root1 , vect1 ) ; storeInorder ( root2 , vect2 ) ; pairSumUtil ( vect1 , vect2 , sum ) ; } public static void main ( String args [ ] ) { Node root1 = null ; root1 = insert ( root1 , 8 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 6 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 14 ) ; root1 = insert ( root1 , 13 ) ; Node root2 = null ; root2 = insert ( root2 , 5 ) ; root2 = insert ( root2 , 18 ) ; root2 = insert ( root2 , 2 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 3 ) ; root2 = insert ( root2 , 4 ) ; int sum = 10 ; pairSum ( root1 , root2 , sum ) ; } }
Find the closest element in Binary Search Tree | Recursive Java program to find key closest to k in given Binary Search Tree . ; A binary tree node has key , pointer to left child and a pointer to right child ; Utility that allocates a new node with the given key and null left and right pointers . ; Function to find node with minimum absolute difference with given K min_diff - . minimum difference till now min_diff_key - . node having minimum absolute difference with K ; If k itself is present ; update min_diff and min_diff_key by checking current node value ; if k is less than ptr . key then move in left subtree else in right subtree ; Wrapper over maxDiffUtil ( ) ; Initialize minimum difference ; Find value of min_diff_key ( Closest key in tree with k ) ; Driver program to run the case
class solution { static int min_diff , min_diff_key ; static class Node { int key ; Node left , right ; } ; static Node newnode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } static void maxDiffUtil ( Node ptr , int k ) { if ( ptr == null ) return ; if ( ptr . key == k ) { min_diff_key = k ; return ; } if ( min_diff > Math . abs ( ptr . key - k ) ) { min_diff = Math . abs ( ptr . key - k ) ; min_diff_key = ptr . key ; } if ( k < ptr . key ) maxDiffUtil ( ptr . left , k ) ; else maxDiffUtil ( ptr . right , k ) ; } static int maxDiff ( Node root , int k ) { min_diff = 999999999 ; min_diff_key = - 1 ; maxDiffUtil ( root , k ) ; return min_diff_key ; } public static void main ( String args [ ] ) { Node root = newnode ( 9 ) ; root . left = newnode ( 4 ) ; root . right = newnode ( 17 ) ; root . left . left = newnode ( 3 ) ; root . left . right = newnode ( 6 ) ; root . left . right . left = newnode ( 5 ) ; root . left . right . right = newnode ( 7 ) ; root . right . right = newnode ( 22 ) ; root . right . right . left = newnode ( 20 ) ; int k = 18 ; System . out . println ( maxDiff ( root , k ) ) ; } }
Construct a complete binary tree from given array in level order fashion | Java program to construct binary tree from given array in level order fashion ; Tree Node ; Function to insert nodes in level order ; Base case for recursion ; insert left child ; insert right child ; Function to print tree nodes in InOrder fashion ; Driver program to test above function
public class Tree { Node root ; static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } public Node insertLevelOrder ( int [ ] arr , Node root , int i ) { if ( i < arr . length ) { Node temp = new Node ( arr [ i ] ) ; root = temp ; root . left = insertLevelOrder ( arr , root . left , 2 * i + 1 ) ; root . right = insertLevelOrder ( arr , root . right , 2 * i + 2 ) ; } return root ; } public void inOrder ( Node root ) { if ( root != null ) { inOrder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inOrder ( root . right ) ; } } public static void main ( String args [ ] ) { Tree t2 = new Tree ( ) ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 } ; t2 . root = t2 . insertLevelOrder ( arr , t2 . root , 0 ) ; t2 . inOrder ( t2 . root ) ; } }
Threaded Binary Tree |
static class Node { int data ; Node left , right ; boolean rightThread ; }
Threaded Binary Tree | Utility function to find leftmost node in a tree rooted with n ; C code to do inorder traversal in a threaded binary tree ; If this node is a thread node , then go to inorder successor ; Else go to the leftmost child in right ; subtree
Node leftMost ( Node n ) { if ( n == null ) return null ; while ( n . left != null ) n = n . left ; return n ; } static void inOrder ( Node root ) { Node cur = leftMost ( root ) ; while ( cur != null ) { System . out . printf ( " % d ▁ " , cur . data ) ; if ( cur . rightThread ) cur = cur . right ; else cur = leftmost ( cur . right ) ; } }