codeparrot/xlcost-text-to-code huggingface dataset:
Floor and Ceil from a BST | C # program to find ceil of a given value in BST ; A binary tree node has key , left child and right child ; Function to find ceil of a given input in BST . If input is more than the max key in BST , return - 1 ; Base case ; We found equal key ; If root 's key is smaller, ceil must be in right subtree ; Else , either left subtree or root has the ceil value ; Driver program to test the above functions
using System ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinaryTree { public static Node root ; public virtual int Ceil ( Node node , int input ) { if ( node == null ) { return - 1 ; } if ( node . data == input ) { return node . data ; } if ( node . data < input ) { return Ceil ( node . right , input ) ; } int ceil = Ceil ( node . left , input ) ; return ( ceil >= input ) ? ceil : node . data ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; BinaryTree . root = new Node ( 8 ) ; BinaryTree . root . left = new Node ( 4 ) ; BinaryTree . root . right = new Node ( 12 ) ; BinaryTree . root . left . left = new Node ( 2 ) ; BinaryTree . root . left . right = new Node ( 6 ) ; BinaryTree . root . right . left = new Node ( 10 ) ; BinaryTree . root . right . right = new Node ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) { Console . WriteLine ( i + " ▁ " + tree . Ceil ( root , i ) ) ; } } }
Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree | C # program to construct full binary tree using its preorder traversal and preorder traversal of its mirror tree ; A Binary Tree Node ; Utility function to create a new tree node ; A utility function to print inorder traversal of a Binary Tree ; A recursive function to con Full binary tree from pre [ ] and preM [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in preM [ ] ; Base case ; The first node in preorder traversal is root . So take the node at preIndex from preorder and make it root , and increment preIndex ; If the current subarry has only one element , no need to recur ; Search the next element of pre [ ] in preM [ ] ; con left and right subtrees recursively ; return root ; function to con full binary tree using its preorder traversal and preorder traversal of its mirror tree ; Driver code
using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; public class INT { public int a ; public INT ( int a ) { this . a = a ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; Console . Write ( " { 0 } ▁ " , node . data ) ; printInorder ( node . right ) ; } static Node conBinaryTreeUtil ( int [ ] pre , int [ ] preM , INT preIndex , int l , int h , int size ) { if ( preIndex . a >= size l > h ) return null ; Node root = newNode ( pre [ preIndex . a ] ) ; ++ ( preIndex . a ) ; if ( l == h ) return root ; int i ; for ( i = l ; i <= h ; ++ i ) if ( pre [ preIndex . a ] == preM [ i ] ) break ; if ( i <= h ) { root . left = conBinaryTreeUtil ( pre , preM , preIndex , i , h , size ) ; root . right = conBinaryTreeUtil ( pre , preM , preIndex , l + 1 , i - 1 , size ) ; } return root ; } static void conBinaryTree ( Node root , int [ ] pre , int [ ] preMirror , int size ) { INT preIndex = new INT ( 0 ) ; int preMIndex = 0 ; root = conBinaryTreeUtil ( pre , preMirror , preIndex , 0 , size - 1 , size ) ; printInorder ( root ) ; } public static void Main ( String [ ] args ) { int [ ] preOrder = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int [ ] preOrderMirror = { 1 , 3 , 7 , 6 , 2 , 5 , 4 } ; int size = preOrder . Length ; Node root = new Node ( ) ; conBinaryTree ( root , preOrder , preOrderMirror , size ) ; } }
Floor and Ceil from a BST | C # program to find floor and ceil of a given key in BST ; A binary tree node has key , left child and right child ; Helper function to find floor and ceil of a given key in BST ; Display the floor and ceil of a given key in BST . If key is less than the min key in BST , floor will be - 1 ; If key is more than the max key in BST , ceil will be - 1 ; ; Variables ' floor ' and ' ceil ' are passed by reference ; Driver code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinaryTree { public static Node root ; int floor ; int ceil ; public int floorCeilBSTHelper ( Node root , int key ) { while ( root != null ) { if ( root . data == key ) { ceil = root . data ; floor = root . data ; return 0 ; } if ( key > root . data ) { floor = root . data ; root = root . right ; } else { ceil = root . data ; root = root . left ; } } return 0 ; } public void floorCeilBST ( Node root , int key ) { floor = - 1 ; ceil = - 1 ; floorCeilBSTHelper ( root , key ) ; Console . WriteLine ( key + " ▁ " + floor + " ▁ " + ceil ) ; } static public void Main ( ) { BinaryTree tree = new BinaryTree ( ) ; BinaryTree . root = new Node ( 8 ) ; BinaryTree . root . left = new Node ( 4 ) ; BinaryTree . root . right = new Node ( 12 ) ; BinaryTree . root . left . left = new Node ( 2 ) ; BinaryTree . root . left . right = new Node ( 6 ) ; BinaryTree . root . right . left = new Node ( 10 ) ; BinaryTree . root . right . right = new Node ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) { tree . floorCeilBST ( BinaryTree . root , i ) ; } } }
Leaf nodes from Preorder of a Binary Search Tree | C # program to print leaf node from preorder of binary search tree . ; Binary Search ; Point to the index in preorder . ; Function to print Leaf Nodes by doing preorder traversal of tree using preorder and inorder arrays . ; If l == r , therefore no right or left subtree . So , it must be leaf Node , print it . ; If array is out of bound , return . ; Finding the index of preorder element in inorder array using binary search . ; Incrementing the index . ; Finding on the left subtree . ; Finding on the right subtree . ; Finds leaf nodes from given preorder traversal . ; To store inorder traversal ; Copy the preorder into another array . ; Finding the inorder by sorting the array . ; Print the Leaf Nodes . ; Driver Code
using System ; class GFG { static int binarySearch ( int [ ] inorder , int l , int r , int d ) { int mid = ( l + r ) >> 1 ; if ( inorder [ mid ] == d ) return mid ; else if ( inorder [ mid ] > d ) return binarySearch ( inorder , l , mid - 1 , d ) ; else return binarySearch ( inorder , mid + 1 , r , d ) ; } static int ind ; static void leafNodesRec ( int [ ] preorder , int [ ] inorder , int l , int r , int n ) { if ( l == r ) { Console . Write ( " { 0 } ▁ " , inorder [ l ] ) ; ind = ind + 1 ; return ; } if ( l < 0 l > r r >= n ) return ; int loc = binarySearch ( inorder , l , r , preorder [ ind ] ) ; ind = ind + 1 ; leafNodesRec ( preorder , inorder , l , loc - 1 , n ) ; leafNodesRec ( preorder , inorder , loc + 1 , r , n ) ; } static void leafNodes ( int [ ] preorder , int n ) { int [ ] inorder = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) inorder [ i ] = preorder [ i ] ; Array . Sort ( inorder ) ; leafNodesRec ( preorder , inorder , 0 , n - 1 , n ) ; } public static void Main ( String [ ] args ) { int [ ] preorder = { 890 , 325 , 290 , 530 , 965 } ; int n = preorder . Length ; leafNodes ( preorder , n ) ; } }
Leaf nodes from Preorder of a Binary Search Tree | Stack based C # program to print leaf nodes from preorder traversal . ; Print the leaf node from the given preorder of BST . ; Since rightmost element is always leaf node . ; Driver code
using System ; using System . Collections . Generic ; public class GfG { public static void leafNode ( int [ ] preorder , int n ) { Stack < int > s = new Stack < int > ( ) ; for ( int i = 0 , j = 1 ; j < n ; i ++ , j ++ ) { bool found = false ; if ( preorder [ i ] > preorder [ j ] ) { s . Push ( preorder [ i ] ) ; } else { while ( s . Count > 0 ) { if ( preorder [ j ] > s . Peek ( ) ) { s . Pop ( ) ; found = true ; } else { break ; } } } if ( found ) { Console . Write ( preorder [ i ] + " ▁ " ) ; } } Console . WriteLine ( preorder [ n - 1 ] ) ; } public static void Main ( string [ ] args ) { int [ ] preorder = new int [ ] { 890 , 325 , 290 , 530 , 965 } ; int n = preorder . Length ; leafNode ( preorder , n ) ; } }
Leaf nodes from Preorder of a Binary Search Tree ( Using Recursion ) | Recursive C # program to find leaf nodes from given preorder traversal ; Print the leaf node from the given preorder of BST . ; Driver code
using System ; class GFG { static int i = 0 ; static bool isLeaf ( int [ ] pre , int n , int min , int max ) { if ( i >= n ) { return false ; } if ( pre [ i ] > min && pre [ i ] < max ) { i ++ ; bool left = isLeaf ( pre , n , min , pre [ i - 1 ] ) ; bool right = isLeaf ( pre , n , pre [ i - 1 ] , max ) ; if ( ! left && ! right ) { Console . Write ( pre [ i - 1 ] + " ▁ " ) ; } return true ; } return false ; } static void printLeaves ( int [ ] preorder , int n ) { isLeaf ( preorder , n , int . MinValue , int . MaxValue ) ; } public static void Main ( String [ ] args ) { int [ ] preorder = { 890 , 325 , 290 , 530 , 965 } ; int n = preorder . Length ; printLeaves ( preorder , n ) ; } }
Binary Search Tree insert with Parent Pointer | C # program to demonstrate insert operation in binary search tree with parent pointer ; Node structure ; A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; 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 ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Driver code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; print iNoder traversal of the BST
using System ; class GfG { class Node { public int key ; public Node left , right , parent ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; temp . parent = null ; return temp ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; Console . Write ( " Node ▁ : ▁ " + root . key + " ▁ , ▁ " ) ; if ( root . parent == null ) Console . WriteLine ( " Parent ▁ : ▁ NULL " ) ; else Console . WriteLine ( " Parent ▁ : ▁ " + root . parent . key ) ; inorder ( root . right ) ; } } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) { Node lchild = insert ( node . left , key ) ; node . left = lchild ; lchild . parent = node ; } else if ( key > node . key ) { Node rchild = insert ( node . right , key ) ; node . right = rchild ; rchild . parent = node ; } 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 ) ; inorder ( root ) ; } }
Construct a special tree from given preorder traversal | C # program to construct a binary tree from preorder traversal ; A Binary Tree node ; A recursive function to create a Binary Tree from given pre [ ] preLN [ ] arrays . The function returns root of tree . index_ptr is used to update index values in recursive calls . index must be initially passed as 0 ; store the current value of index in pre [ ] ; Base Case : All nodes are constructed ; Allocate memory for this node and increment index for subsequent recursive calls ; If this is an internal node , construct left and right subtrees and link the subtrees ; A wrapper over constructTreeUtil ( ) ; Initialize index as 0. Value of index is used in recursion to maintain the current index in pre [ ] and preLN [ ] arrays . ; This function is used only for testing ; first recur on left child ; then print the data of node ; now recur on right child ; Driver Code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Index { public int index = 0 ; } class GFG { public Node root ; public Index myindex = new Index ( ) ; public virtual Node constructTreeUtil ( int [ ] pre , char [ ] preLN , Index index_ptr , int n , Node temp ) { int index = index_ptr . index ; if ( index == n ) { return null ; } temp = new Node ( pre [ index ] ) ; ( index_ptr . index ) ++ ; if ( preLN [ index ] == ' N ' ) { temp . left = constructTreeUtil ( pre , preLN , index_ptr , n , temp . left ) ; temp . right = constructTreeUtil ( pre , preLN , index_ptr , n , temp . right ) ; } return temp ; } public virtual Node constructTree ( int [ ] pre , char [ ] preLN , int n , Node node ) { int index = 0 ; return constructTreeUtil ( pre , preLN , myindex , n , node ) ; } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; int [ ] pre = new int [ ] { 10 , 30 , 20 , 5 , 15 } ; char [ ] preLN = new char [ ] { ' N ' , ' N ' , ' L ' , ' L ' , ' L ' } ; int n = pre . Length ; Node mynode = tree . constructTree ( pre , preLN , n , tree . root ) ; Console . WriteLine ( " Following ▁ is ▁ Inorder ▁ Traversal ▁ of ▁ the " + " Constructed ▁ Binary ▁ Tree : ▁ " ) ; tree . printInorder ( mynode ) ; } }
Minimum Possible value of | ai + aj | C # program to find number of pairs and minimal possible value ; function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; Driver Code
using System ; class GFG { static void pairs ( int [ ] arr , int n , int k ) { int smallest = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . Abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } Console . WriteLine ( " Minimal ▁ Value ▁ = ▁ " + smallest ) ; Console . WriteLine ( " Total ▁ Pairs ▁ = ▁ " + count ) ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 7 , 5 , 1 , 9 , 9 } ; int k = 12 ; int n = arr . Length ; pairs ( arr , n , k ) ; } }
Rank of an element in a stream | C # program to find rank of an element in a stream . ; Inserting a new Node . ; Updating size of left subtree . ; Function to get Rank of a Node x . ; Step 1. ; Step 2. ; Step 3. ; Driver code
using System ; class GFG { public class Node { public int data ; public Node left , right ; public int leftSize ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; temp . leftSize = 0 ; 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 ) ; root . leftSize ++ ; } else root . right = insert ( root . right , data ) ; return root ; } static int getRank ( Node root , int x ) { if ( root . data == x ) return root . leftSize ; if ( x < root . data ) { if ( root . left == null ) return - 1 ; else return getRank ( root . left , x ) ; } else { if ( root . right == null ) return - 1 ; else { int rightSize = getRank ( root . right , x ) ; if ( rightSize == - 1 ) return - 1 ; return root . leftSize + 1 + rightSize ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 4 , 4 , 5 , 9 , 7 , 13 , 3 } ; int n = arr . Length ; int x = 4 ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = insert ( root , arr [ i ] ) ; Console . WriteLine ( " Rank ▁ of ▁ " + x + " ▁ in ▁ stream ▁ is ▁ : ▁ " + getRank ( root , x ) ) ; x = 13 ; Console . WriteLine ( " Rank ▁ of ▁ " + x + " ▁ in ▁ stream ▁ is ▁ : ▁ " + getRank ( root , x ) ) ; } }
Rank of an element in a stream | C # program to find rank of an element in a stream . ; Driver code
using System ; class GFG { public static void Main ( ) { int [ ] a = { 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 } ; int key = 20 ; int arraySize = a . Length ; int count = 0 ; for ( int i = 0 ; i < arraySize ; i ++ ) { if ( a [ i ] <= key ) { count += 1 ; } } Console . WriteLine ( " Rank ▁ of ▁ " + key + " ▁ in ▁ stream ▁ is : ▁ " + ( count - 1 ) ) ; } }
Special two digit numbers in a Binary Search Tree | A binary tree node ; Function to create a new node ; If the tree is empty , return a new , single node ; Otherwise , recur down the tree ; return the ( unchanged ) node pointer ; Function to find if number is special or not ; Check if number is two digit or not ; Function to count number of special two digit number ; Driver code ; Function call
using System ; public class Node { public int info ; public Node left , right ; public Node ( int d ) { info = d ; left = right = null ; } } public class BinaryTree { public static Node head ; public static int count ; public Node insert ( Node node , int info ) { if ( node == null ) { return ( new Node ( info ) ) ; } else { if ( info <= node . info ) { node . left = insert ( node . left , info ) ; } else { node . right = insert ( node . right , info ) ; } } return node ; } static int check ( int num ) { int sum = 0 , i = num , sum_of_digits , prod_of_digits ; if ( num < 10 num > 99 ) { return 0 ; } else { sum_of_digits = ( i % 10 ) + ( i / 10 ) ; prod_of_digits = ( i % 10 ) * ( i / 10 ) ; sum = sum_of_digits + prod_of_digits ; } if ( sum == num ) { return 1 ; } else { return 0 ; } } static void countSpecialDigit ( Node rt ) { int x ; if ( rt == null ) { return ; } else { x = check ( rt . info ) ; if ( x == 1 ) { count = count + 1 ; } countSpecialDigit ( rt . left ) ; countSpecialDigit ( rt . right ) ; } } static public void Main ( ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 50 ) ; tree . insert ( root , 29 ) ; tree . insert ( root , 59 ) ; tree . insert ( root , 19 ) ; tree . insert ( root , 53 ) ; tree . insert ( root , 556 ) ; tree . insert ( root , 56 ) ; tree . insert ( root , 94 ) ; tree . insert ( root , 13 ) ; countSpecialDigit ( root ) ; Console . WriteLine ( count ) ; } }
Construct Special Binary Tree from given Inorder traversal | C # program to construct tree from inorder traversal ; 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 inorder [ ] . Initial values of start and end should be 0 and len - 1. ; Find index of the maximum element from Binary Tree ; Pick the maximum value and make it root ; If this is the only element in inorder [ start . . end ] , then return it ; Using index in Inorder traversal , construct left and right subtress ; Function to find index of the maximum value in arr [ start ... end ] ; 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 Code ; Assume that inorder traversal of following tree is given 40 / \ 10 30 / \ 5 28 ; Let us test the built tree by printing Inorder traversal
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual Node buildTree ( int [ ] inorder , int start , int end , Node node ) { if ( start > end ) { return null ; } int i = max ( inorder , start , end ) ; node = new Node ( inorder [ i ] ) ; if ( start == end ) { return node ; } node . left = buildTree ( inorder , start , i - 1 , node . left ) ; node . right = buildTree ( inorder , i + 1 , end , node . right ) ; return node ; } public virtual int max ( int [ ] arr , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } public virtual void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; int [ ] inorder = new int [ ] { 5 , 10 , 40 , 30 , 28 } ; int len = inorder . Length ; Node mynode = tree . buildTree ( inorder , 0 , len - 1 , tree . root ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " the ▁ constructed ▁ tree ▁ is ▁ " ) ; tree . printInorder ( mynode ) ; } }
Continuous Tree | C # program to check if a tree is continuous or not ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Function to check tree is continuous or not ; if next node is empty then return true ; if current node is leaf node then return true because it is end of root to leaf path ; If left subtree is empty , then only check right ; If right subtree is empty , then only check left ; If both left and right subtrees are not empty , check everything ; Driver program to test mirror ( )
using System ; class solution { class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static Boolean treeContinuous ( Node ptr ) { if ( ptr == null ) return true ; if ( ptr . left == null && ptr . right == null ) return true ; if ( ptr . left == null ) return ( Math . Abs ( ptr . data - ptr . right . data ) == 1 ) && treeContinuous ( ptr . right ) ; if ( ptr . right == null ) return ( Math . Abs ( ptr . data - ptr . left . data ) == 1 ) && treeContinuous ( ptr . left ) ; return Math . Abs ( ptr . data - ptr . left . data ) == 1 && Math . Abs ( ptr . data - ptr . right . data ) == 1 && treeContinuous ( ptr . left ) && treeContinuous ( ptr . right ) ; } static public void Main ( String [ ] args ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 5 ) ; if ( treeContinuous ( root ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sum of middle row and column in Matrix | C # program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function
using System ; class GFG { static int MAX = 100 ; static void middlesum ( int [ , ] mat , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 , i ] ; Console . WriteLine ( " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " + row_sum ) ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i , n / 2 ] ; Console . WriteLine ( " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " + col_sum ) ; } public static void Main ( ) { int [ , ] mat = { { 2 , 5 , 7 } , { 3 , 7 , 2 } , { 5 , 6 , 9 } } ; middlesum ( mat , 3 ) ; } }