codeparrot/xlcost-text-to-code huggingface dataset:
Find k | A simple inorder traversal based Java program to find k - th smallest element in a BST . ; A BST node ; Recursive function to insert an key into BST ; Function to find k 'th largest element in BST  Here count denotes the number  of nodes processed so far ; base case ; search in left subtree ; if k 'th smallest is found in left subtree, return it ; if current element is k 'th smallest, return it ; else search in right subtree ; Function to find k 'th largest element in BST ; maintain an index to count number of nodes processed so far ; Driver code
import java . io . * ; class Node { int data ; Node left , right ; Node ( int x ) { data = x ; left = right = null ; } } class GFG { static int count = 0 ; public static Node insert ( Node root , int x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) root . left = insert ( root . left , x ) ; else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } public static Node kthSmallest ( Node root , int k ) { if ( root == null ) return null ; Node left = kthSmallest ( root . left , k ) ; if ( left != null ) return left ; count ++ ; if ( count == k ) return root ; return kthSmallest ( root . right , k ) ; } public static void printKthSmallest ( Node root , int k ) { count = 0 ; Node res = kthSmallest ( root , k ) ; if ( res == null ) System . out . println ( " There ▁ are ▁ less ▁ " + " than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) ; else System . out . println ( " K - th ▁ Smallest " + " ▁ Element ▁ is ▁ " + res . data ) ; } public static void main ( String [ ] args ) { Node root = null ; int keys [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; for ( int x : keys ) root = insert ( root , x ) ; int k = 3 ; printKthSmallest ( root , k ) ; } }
Find k | A simple inorder traversal based Java program to find k - th smallest element in a BST . ; A BST node ; Recursive function to insert an key into BST ; If a node is inserted in left subtree , then lCount of this node is increased . For simplicity , we are assuming that all keys ( tried to be inserted ) are distinct . ; Function to find k 'th largest element in BST  Here count denotes the number of  nodes processed so far ; base case ; else search in right subtree ; main function
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; int lCount ; Node ( int x ) { data = x ; left = right = null ; lCount = 0 ; } } class Gfg { public static Node insert ( Node root , int x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) { root . left = insert ( root . left , x ) ; root . lCount ++ ; } else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } public static Node kthSmallest ( Node root , int k ) { if ( root == null ) return null ; int count = root . lCount + 1 ; if ( count == k ) return root ; if ( count > k ) return kthSmallest ( root . left , k ) ; return kthSmallest ( root . right , k - count ) ; } public static void main ( String args [ ] ) { Node root = null ; int keys [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; for ( int x : keys ) root = insert ( root , x ) ; int k = 4 ; Node res = kthSmallest ( root , k ) ; if ( res == null ) System . out . println ( " There ▁ are ▁ less ▁ " + " than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) ; else System . out . println ( " K - th ▁ Smallest " + " ▁ Element ▁ is ▁ " + res . data ) ; } }
Construct Tree from given Inorder and Preorder traversals | Java program to construct tree using inorder and preorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Store indexes of all items so that we we can quickly find later ; 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 ; This function mainly creates an unordered_map , then calls buildTree ( ) ; This funtcion is here just to test buildTree ( ) ; Driver code ; Let us test the built tree by printing Insorder traversal
import java . io . * ; import java . util . * ; class Node { char data ; Node left , right ; Node ( char item ) { data = item ; left = right = null ; } } class Tree { public static Node root ; static HashMap < Character , Integer > mp = new HashMap < > ( ) ; static int preIndex = 0 ; public static Node buildTree ( char [ ] in , char [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) { return null ; } char curr = pre [ preIndex ++ ] ; Node tNode ; tNode = new Node ( curr ) ; if ( inStrt == inEnd ) { return tNode ; } int inIndex = mp . get ( curr ) ; tNode . left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } public static Node buldTreeWrap ( char [ ] in , char [ ] pre , int len ) { for ( int i = 0 ; i < len ; i ++ ) { mp . put ( in [ i ] , i ) ; } return buildTree ( in , pre , 0 , len - 1 ) ; } static 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 ) { char [ ] in = { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char [ ] pre = { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = in . length ; Tree . root = buldTreeWrap ( in , pre , len ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is " ) ; printInorder ( root ) ; } }
Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; Driver code
class BinaryTree { boolean hasOnlyOneChild ( int pre [ ] , int size ) { int nextDiff , lastDiff ; for ( int i = 0 ; i < size - 1 ; i ++ ) { nextDiff = pre [ i ] - pre [ i + 1 ] ; lastDiff = pre [ i ] - pre [ size - 1 ] ; if ( nextDiff * lastDiff < 0 ) { return false ; } ; } return true ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int pre [ ] = new int [ ] { 8 , 3 , 5 , 7 , 6 } ; int size = pre . length ; if ( tree . hasOnlyOneChild ( pre , size ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; Initialize min and max using last two elements ; Every element must be either smaller than min or greater than max ; Driver program to test above function
class BinaryTree { boolean hasOnlyOneChild ( int pre [ ] , int size ) { int min , max ; if ( pre [ size - 1 ] > pre [ size - 2 ] ) { max = pre [ size - 1 ] ; min = pre [ size - 2 ] ; } else { max = pre [ size - 2 ] ; min = pre [ size - 1 ] ; } for ( int i = size - 3 ; i >= 0 ; i -- ) { if ( pre [ i ] < min ) { min = pre [ i ] ; } else if ( pre [ i ] > max ) { max = pre [ i ] ; } else { return false ; } } return true ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int pre [ ] = new int [ ] { 8 , 3 , 5 , 7 , 6 } ; int size = pre . length ; if ( tree . hasOnlyOneChild ( pre , size ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
K 'th smallest element in BST using O(1) Extra Space | Java program to find k 'th largest element in BST ; A BST node ; A function to find ; Count to iterate over elements till we get the kth smallest number ; store the Kth smallest ; to store the current node ; Like Morris traversal if current does not have left child rather than printing as we did in inorder , we will just increment the count as the number will be in an increasing order ; if count is equal to K then we found the kth smallest , so store it in ksmall ; go to current 's right child ; we create links to Inorder Successor and count using these links ; building links ; link made to Inorder Successor ; While breaking the links in so made temporary threaded tree we will check for the K smallest condition ; Revert the changes made in if part ( break link from the Inorder Successor ) ; If count is equal to K then we found the kth smallest and so store it in ksmall ; return the found value ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Driver Program to test above functions ; 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 int KSmallestUsingMorris ( Node root , int k ) { int count = 0 ; int ksmall = Integer . MIN_VALUE ; Node curr = root ; while ( curr != null ) { if ( curr . left == null ) { count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } else { Node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } } } return ksmall ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; 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 if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } public static void main ( String [ ] args ) { 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 ) ; for ( int k = 1 ; k <= 7 ; k ++ ) System . out . print ( KSmallestUsingMorris ( root , k ) + " ▁ " ) ; } }
Check if given sorted sub | Java program to find if given array exists as a subsequece in BST ; A binary Tree node ; structure of int class ; A utility function to create a new BST node with key as given num ; A utility function to insert a given key to BST ; function to check if given sorted sub - sequence exist in BST index - . iterator for given sorted sub - sequence seq [ ] - . given sorted sub - sequence ; We traverse left subtree first in Inorder ; If current node matches with se [ index ] then move forward in sub - sequence ; We traverse left subtree in the end in Inorder ; A wrapper over seqExistUtil . It returns true if seq [ 0. . n - 1 ] exists in tree . ; Initialize index in seq [ ] ; Do an inorder traversal and find if all elements of seq [ ] were present ; index would become n if all elements of seq [ ] were present ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; } 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 seqExistUtil ( Node ptr , int seq [ ] , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static boolean seqExist ( Node root , int seq [ ] , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int seq [ ] = { 4 , 6 , 8 , 14 } ; int n = seq . length ; if ( seqExist ( root , seq , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if an array represents Inorder of Binary Search tree or not | Java program to check if a given array is sorted or not . ; Function that returns true if array is Inorder traversal of any Binary Search Tree or not . ; Array has one or no element ; Unsorted pair found ; No unsorted pair found ; Drivers code
class GFG { static boolean isInorder ( int [ ] arr , int n ) { if ( n == 0 n == 1 ) { return true ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] > arr [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 19 , 23 , 25 , 30 , 45 } ; int n = arr . length ; if ( isInorder ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " Non " ) ; } } }
Construct Tree from given Inorder and Preorder traversals | Java program to construct a tree using inorder and preorder traversal ; Function to build tree using given traversal ; Function to print tree in Inorder ; first recur on left child ; then print the data of node ; now recur on right child ; driver program to test above functions
import java . util . * ; public class TreeNode { int val ; TreeNode left ; TreeNode right ; TreeNode ( int x ) { val = x ; } } class BinaryTree { static Set < TreeNode > set = new HashSet < > ( ) ; static Stack < TreeNode > stack = new Stack < > ( ) ; public TreeNode buildTree ( int [ ] preorder , int [ ] inorder ) { TreeNode root = null ; for ( int pre = 0 , in = 0 ; pre < preorder . length ; ) { TreeNode node = null ; do { node = new TreeNode ( preorder [ pre ] ) ; if ( root == null ) { root = node ; } if ( ! stack . isEmpty ( ) ) { if ( set . contains ( stack . peek ( ) ) ) { set . remove ( stack . peek ( ) ) ; stack . pop ( ) . right = node ; } else { stack . peek ( ) . left = node ; } } stack . push ( node ) ; } while ( preorder [ pre ++ ] != inorder [ in ] && pre < preorder . length ) ; node = null ; while ( ! stack . isEmpty ( ) && in < inorder . length && stack . peek ( ) . val == inorder [ in ] ) { node = stack . pop ( ) ; in ++ ; } if ( node != null ) { set . add ( node ) ; stack . push ( node ) ; } } return root ; } void printInorder ( TreeNode node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . val + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int in [ ] = new int [ ] { 9 , 8 , 4 , 2 , 10 , 5 , 10 , 1 , 6 , 3 , 13 , 12 , 7 } ; int pre [ ] = new int [ ] { 1 , 2 , 4 , 8 , 9 , 5 , 10 , 10 , 3 , 6 , 7 , 12 , 13 } ; int len = in . length ; TreeNode root = tree . buildTree ( pre , in ) ; tree . printInorder ( root ) ; } }
Check if two BSTs contain same set of elements | JAVA program to check if two BSTs contains same set of elements ; BST Node ; Utility function to create new Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two hash sets and store elements both BSTs in them . ; Return true if both hash sets contain same elements . ; Driver program to check above functions ; First BST ; Second BST ; check if two BSTs have same set of elements
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void insertToHash ( Node root , HashSet < Integer > s ) { if ( root == null ) return ; insertToHash ( root . left , s ) ; s . add ( root . data ) ; insertToHash ( root . right , s ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; HashSet < Integer > s1 = new HashSet < Integer > ( ) ; HashSet < Integer > s2 = new HashSet < Integer > ( ) ; insertToHash ( root1 , s1 ) ; insertToHash ( root2 , s2 ) ; return ( s1 . equals ( s2 ) ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Check if two BSTs contain same set of elements | Java program to check if two BSTs contain same set of elements ; BST Node ; Utility function to create new Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two vectors and store inorder traversals of both BSTs in them . ; Return true if both vectors are identical ; Driver Code ; First BST ; Second BST ; check if two BSTs have same set of elements
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void storeInorder ( Node root , Vector < Integer > v ) { if ( root == null ) return ; storeInorder ( root . left , v ) ; v . add ( root . data ) ; storeInorder ( root . right , v ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; storeInorder ( root1 , v1 ) ; storeInorder ( root2 , v2 ) ; return ( v1 == v2 ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Shortest distance between two nodes in BST | Java program to find distance between two nodes in BST ; Standard BST insert function ; This function returns distance of x from root . This function assumes that x exists in BST and BST is not NULL . ; Returns minimum distance beween a and b . This function assumes that a and b exist in BST . ; Both keys lie in left ; Both keys lie in right same path ; Lie in opposite directions ( Root is LCA of two nodes ) ; This function make sure that a is smaller than b before making a call to findDistWrapper ( ) ; Driver code
class GfG { static class Node { Node left , right ; int key ; } static Node newNode ( int key ) { Node ptr = new Node ( ) ; ptr . key = key ; ptr . left = null ; ptr . right = null ; return ptr ; } static Node insert ( Node root , int key ) { if ( root == null ) root = newNode ( key ) ; else if ( root . key > key ) root . left = insert ( root . left , key ) ; else if ( root . key < key ) root . right = insert ( root . right , key ) ; return root ; } static int distanceFromRoot ( Node root , int x ) { if ( root . key == x ) return 0 ; else if ( root . key > x ) return 1 + distanceFromRoot ( root . left , x ) ; return 1 + distanceFromRoot ( root . right , x ) ; } static int distanceBetween2 ( Node root , int a , int b ) { if ( root == null ) return 0 ; if ( root . key > a && root . key > b ) return distanceBetween2 ( root . left , a , b ) ; if ( root . key < a && root . key < b ) return distanceBetween2 ( root . right , a , b ) ; if ( root . key >= a && root . key <= b ) return distanceFromRoot ( root , a ) + distanceFromRoot ( root , b ) ; return 0 ; } static int findDistWrapper ( Node root , int a , int b ) { int temp = 0 ; if ( a > b ) { temp = a ; a = b ; b = temp ; } return distanceBetween2 ( root , a , b ) ; } 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 ( findDistWrapper ( root , 5 , 35 ) ) ; } }
Print BST keys in given Range | O ( 1 ) Space | Java code to print BST keys in given Range in constant space using Morris traversal . ; Function to print the keys in range ; check if current node lies between n1 and n2 ; finding the inorder predecessor - inorder predecessor is the right most in left subtree or the left child , i . e in BST it is the maximum ( right most ) in left subtree . ; check if current node lies between n1 and n2 ; Helper function to create a new node ; Driver Code ; Constructed binary tree is 4 / \ 2 7 / \ / \ 1 3 6 10
class GfG { static class node { int data ; node left , right ; } static void RangeTraversal ( node root , int n1 , int n2 ) { if ( root == null ) return ; node curr = root ; while ( curr != null ) { if ( curr . left == null ) { if ( curr . data <= n2 && curr . data >= n1 ) { System . out . print ( curr . data + " ▁ " ) ; } curr = curr . right ; } else { node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; if ( curr . data <= n2 && curr . data >= n1 ) { System . out . print ( curr . data + " ▁ " ) ; } curr = curr . right ; } } } } static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . right = null ; temp . left = null ; return temp ; } public static void main ( String [ ] args ) { node root = newNode ( 4 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 10 ) ; RangeTraversal ( root , 4 , 12 ) ; } }