codeparrot/xlcost-text-to-code huggingface dataset:
K 'th smallest element in BST using O(1) Extra Space | C # 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
using System ; class GfG { public class Node { public int key ; public Node left , right ; } static int KSmallestUsingMorris ( Node root , int k ) { int count = 0 ; int ksmall = int . MinValue ; 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 ++ ) Console . Write ( KSmallestUsingMorris ( root , k ) + " ▁ " ) ; } }
Check if given sorted sub | C # 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
using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; public class INT { public 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 bool 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 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if an array represents Inorder of Binary Search tree or not | C # 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 ; Driver code
using System ; class GFG { static bool 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 ( ) { int [ ] arr = { 19 , 23 , 25 , 30 , 45 } ; int n = arr . Length ; if ( isInorder ( arr , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " Non " ) ; } } }
Construct Tree from given Inorder and Preorder traversals | C # 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 Code
using System ; using System . Collections . Generic ; public class TreeNode { public int val ; public TreeNode left ; public TreeNode right ; public TreeNode ( int x ) { val = x ; } } class GFG { static HashSet < TreeNode > set = new HashSet < TreeNode > ( ) ; static Stack < TreeNode > stack = new Stack < TreeNode > ( ) ; 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 . Count != 0 ) { 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 . Count != 0 && 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 ) ; Console . Write ( node . val + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; 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 | C # 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 Code ; First BST ; Second BST ; check if two BSTs have same set of elements
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public 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 < int > s ) { if ( root == null ) return ; insertToHash ( root . left , s ) ; s . Add ( root . data ) ; insertToHash ( root . right , s ) ; } static bool checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; HashSet < int > s1 = new HashSet < int > ( ) ; HashSet < int > s2 = new HashSet < int > ( ) ; 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 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Check if two BSTs contain same set of elements | C # 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
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left ; public 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 , List < int > v ) { if ( root == null ) return ; storeInorder ( root . left , v ) ; v . Add ( root . data ) ; storeInorder ( root . right , v ) ; } static bool checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; 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 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Shortest distance between two nodes in BST | C # 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
using System ; class GfG { public class Node { public Node left , right ; public 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 ) ; Console . WriteLine ( findDistWrapper ( root , 5 , 35 ) ) ; } }
Print BST keys in given Range | O ( 1 ) Space | C # 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
using System ; public class GfG { public class node { public int data ; public 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 ) { Console . Write ( 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 ) { Console . Write ( 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 ) ; } }
Count BST subtrees that lie in given range | C # 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
using System ; class GfG { public class node { public int data ; public node left , right ; } ; public class INT { public int a ; } static bool inRange ( node root , int low , int high ) { return root . data >= low && root . data <= high ; } static bool getCountUtil ( node root , int low , int high , INT count ) { if ( root == null ) return true ; bool l = getCountUtil ( root . left , low , high , count ) ; bool 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 ; Console . WriteLine ( " Count ▁ of ▁ subtrees ▁ in ▁ [ " + l + " , ▁ " + h + " ] ▁ is ▁ " + getCount ( root , l , h ) . a ) ; } }
Remove all leaf nodes from the binary search tree | C # 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
using System ; class GfG { class Node { public int data ; public Node left ; public 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 ) ; Console . Write ( 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 ) ; Console . WriteLine ( " Inorder ▁ before ▁ Deleting " + " the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; Console . WriteLine ( ) ; leafDelete ( root ) ; Console . WriteLine ( " INorder ▁ after ▁ Deleting " + " the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; } }
Sum of k smallest elements in BST | C # 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
using System ; public class GFG { public class Node { public int data ; public 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 ) ; Console . WriteLine ( count ) ; } }
Sum of k smallest elements in BST | C # 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
using System ; public class GFG { public class Node { public int data ; public int lCount ; public int Sum ; public Node left ; public 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 ) ; Console . WriteLine ( temp_sum ) ; } }