codeparrot/xlcost-text-to-code huggingface dataset:
Inorder Successor in Binary Search Tree | C # program to find minimum value node 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
using System ; public class Node { public int data ; public Node left , right , parent ; public Node ( int d ) { data = d ; left = right = parent = null ; } } public 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 ) { Console . WriteLine ( " Inorder ▁ successor ▁ of ▁ " + temp . data + " ▁ is ▁ " + suc . data ) ; } else { Console . WriteLine ( " Inorder ▁ successor ▁ does ▁ not ▁ exist " ) ; } } }
Inorder predecessor and successor for a given key in BST |
public class Node { public int key ; public Node left , right ; } ;
Inorder predecessor and successor for a given key in BST | Iterative Approach | C # 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 predecessor . ; the maximum value in left subtree is successor . ; 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
using System ; class GFG { class Node { public int key ; public 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 ) Console . WriteLine ( " Predecessor ▁ is ▁ " + pre . key ) ; else Console . Write ( " - 1" ) ; if ( suc != null ) Console . Write ( " Successor ▁ is ▁ " + suc . key ) ; else Console . Write ( " - 1" ) ; } }
Find a pair with given sum in a Balanced BST | C # 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 code ; 15 / \ 10 20 / \ / \ 8 12 16 25
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } public class BinarySearchTree { Node root ; BinarySearchTree ( ) { root = null ; } void inorder ( ) { inorderUtil ( this . root ) ; } void inorderUtil ( Node node ) { if ( node == null ) return ; inorderUtil ( node . left ) ; Console . Write ( 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 ; } List < int > treeToList ( Node node , List < int > list ) { if ( node == null ) return list ; treeToList ( node . left , list ) ; list . Add ( node . data ) ; treeToList ( node . right , list ) ; return list ; } bool isPairPresent ( Node node , int target ) { List < int > a1 = new List < int > ( ) ; List < int > a2 = treeToList ( node , a1 ) ; int start = 0 ; int end = a2 . Count - 1 ; while ( start < end ) { if ( a2 [ start ] + a2 [ end ] == target ) { Console . WriteLine ( " Pair ▁ Found : ▁ " + a2 [ start ] + " ▁ + ▁ " + a2 [ end ] + " ▁ " + " = ▁ " + target ) ; return true ; } if ( a2 [ start ] + a2 [ end ] > target ) { end -- ; } if ( a2 [ start ] + a2 [ end ] < target ) { start ++ ; } } Console . WriteLine ( " 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 | C # program to find a pair with given sum using hashing ; Driver code
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public 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 bool findpairUtil ( Node root , int sum , HashSet < int > set ) { if ( root == null ) return false ; if ( findpairUtil ( root . left , sum , set ) ) return true ; if ( set . Contains ( sum - root . data ) ) { Console . WriteLine ( " 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 < int > set = new HashSet < int > ( ) ; if ( ! findpairUtil ( root , sum , set ) ) Console . Write ( " Pairs ▁ do ▁ not ▁ exit " + " STRNEWLINE " ) ; } 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 | C # 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
using System ; using System . Collections . Generic ; public class solution { public class Node { public int data ; public Node left , right ; } public static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } public 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 ; } public static void storeInorder ( Node ptr , List < int > vect ) { if ( ptr == null ) { return ; } storeInorder ( ptr . left , vect ) ; vect . Add ( ptr . data ) ; storeInorder ( ptr . right , vect ) ; } public static void pairSumUtil ( List < int > vect1 , List < int > vect2 , int sum ) { int left = 0 ; int right = vect2 . Count - 1 ; while ( left < vect1 . Count && right >= 0 ) { if ( vect1 [ left ] + vect2 [ right ] == sum ) { Console . Write ( " ( " + vect1 [ left ] + " , ▁ " + vect2 [ right ] + " ) , ▁ " ) ; left ++ ; right -- ; } else if ( vect1 [ left ] + vect2 [ right ] < sum ) { left ++ ; } else { right -- ; } } } public static void pairSum ( Node root1 , Node root2 , int sum ) { List < int > vect1 = new List < int > ( ) , vect2 = new List < int > ( ) ; 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 C # 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
using System ; public class solution { public static int min_diff , min_diff_key ; public class Node { public int key ; public Node left , right ; } public static Node newnode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } public 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 ) ; } } public 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 ; Console . WriteLine ( maxDiff ( root , k ) ) ; } }
Construct a complete binary tree from given array in level order fashion | C # 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 code
using System ; public class Tree { Node root ; public class Node { public int data ; public Node left , right ; public 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 ) ; Console . Write ( 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 |
public class Node { public int data ; public Node left , right ; public bool 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 ) { Console . Write ( " { 0 } ▁ " , cur . data ) ; if ( cur . rightThread ) cur = cur . right ; else cur = leftmost ( cur . right ) ; } }
Construct Full Binary Tree from given preorder and postorder traversals | C # program for construction of full binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A recursive function to construct Full from pre [ ] and post [ ] . preIndex is used to keep track of index in pre [ ] . l is low index and h is high index for the current subarray in post [ ] ; 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 or preIndex > size after incrementing ; Search the next element of pre [ ] in post [ ] ; Use the index of element found in postorder to divide postorder array in two parts . Left subtree and right subtree ; The main function to construct Full Binary Tree from given preorder and postorder traversals . This function mainly uses constructTreeUtil ( ) ; A utility function to print inorder traversal of a Binary Tree ; Driver Code
using System ; class GFG { public static int preindex ; public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } public static node constructTreeUtil ( int [ ] pre , int [ ] post , int l , int h , int size ) { if ( preindex >= size l > h ) { return null ; } node root = new node ( pre [ preindex ] ) ; preindex ++ ; if ( l == h preindex >= size ) { return root ; } int i ; for ( i = l ; i <= h ; i ++ ) { if ( post [ i ] == pre [ preindex ] ) { break ; } } if ( i <= h ) { root . left = constructTreeUtil ( pre , post , l , i , size ) ; root . right = constructTreeUtil ( pre , post , i + 1 , h , size ) ; } return root ; } public static node constructTree ( int [ ] pre , int [ ] post , int size ) { preindex = 0 ; return constructTreeUtil ( pre , post , 0 , size - 1 , size ) ; } public static void printInorder ( node root ) { if ( root == null ) { return ; } printInorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void Main ( string [ ] args ) { int [ ] pre = new int [ ] { 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 } ; int [ ] post = new int [ ] { 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int size = pre . Length ; node root = constructTree ( pre , post , size ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ " + " the ▁ constructed ▁ tree : " ) ; printInorder ( root ) ; } }
Convert a Binary Tree to Threaded binary tree | Set 2 ( Efficient ) | C # program to convert a Binary Tree to Threaded Tree ; structure of a node in threaded binary tree ; Used to indicate whether the right pointer is a normal right pointer or a pointer to inorder successor . ; Converts tree with given root to threaded binary tree . This function returns rightmost child of root . ; Base cases : Tree is empty or has single node ; Find predecessor if it exists ; Find predecessor of root ( Rightmost child in left subtree ) ; Link a thread from predecessor to root . ; If current node is rightmost child ; Recur for right subtree . ; A utility function to find leftmost node in a binary tree rooted with ' root ' . This function is used in inOrder ( ) ; Function to do inorder traversal of a threadded binary tree ; Find the leftmost node in Binary Tree ; If this Node is a thread Node , then go to inorder successor ; Else go to the leftmost child in right subtree ; A utility function to create a new node ; Driver program to test above functions ; 1 / \ 2 3 / \ / \ 4 5 6 7
using System ; public class solution { public class Node { public int key ; public Node left , right ; public bool isThreaded ; } public static Node createThreaded ( Node root ) { if ( root == null ) { return null ; } if ( root . left == null && root . right == null ) { return root ; } if ( root . left != null ) { Node l = createThreaded ( root . left ) ; l . right = root ; l . isThreaded = true ; } if ( root . right == null ) { return root ; } return createThreaded ( root . right ) ; } public static Node leftMost ( Node root ) { while ( root != null && root . left != null ) { root = root . left ; } return root ; } public static void inOrder ( Node root ) { if ( root == null ) { return ; } Node cur = leftMost ( root ) ; while ( cur != null ) { Console . Write ( cur . key + " ▁ " ) ; if ( cur . isThreaded ) { cur = cur . right ; } else { cur = leftMost ( cur . right ) ; } } } public static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = key ; return temp ; } public static void Main ( string [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createThreaded ( root ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ created ▁ " + " threaded ▁ tree ▁ is STRNEWLINE " ) ; inOrder ( root ) ; } }
Inorder Non | C # program to print inorder traversal of a Binary Search Tree without recursion and stack ; 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 ; Function to print inorder traversal using parent pointer ; Start traversal from root ; If left child is not traversed , find the leftmost child ; Print root 's data ; Mark left as done ; If right child exists ; If right child doesn 't exist,  move to parent ; If this node is right child of its parent , visit parent 's  parent first ; Driver Code
using System ; class Node { public int key ; public Node left , right , parent ; public Node ( int key ) { this . key = key ; left = right = parent = null ; } } class BinaryTree { Node root ; Node insert ( Node node , int key ) { if ( node == null ) return new Node ( key ) ; if ( key < node . key ) { node . left = insert ( node . left , key ) ; node . left . parent = node ; } else if ( key > node . key ) { node . right = insert ( node . right , key ) ; node . right . parent = node ; } return node ; } void inorder ( Node root ) { Boolean leftdone = false ; while ( root != null ) { if ( ! leftdone ) { while ( root . left != null ) { root = root . left ; } } Console . Write ( root . key + " ▁ " ) ; leftdone = true ; if ( root . right != null ) { leftdone = false ; root = root . right ; } else if ( root . parent != null ) { while ( root . parent != null && root == root . parent . right ) root = root . parent ; if ( root . parent == null ) break ; root = root . parent ; } else break ; } } static public void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = tree . insert ( tree . root , 24 ) ; tree . root = tree . insert ( tree . root , 27 ) ; tree . root = tree . insert ( tree . root , 29 ) ; tree . root = tree . insert ( tree . root , 34 ) ; tree . root = tree . insert ( tree . root , 14 ) ; tree . root = tree . insert ( tree . root , 4 ) ; tree . root = tree . insert ( tree . root , 10 ) ; tree . root = tree . insert ( tree . root , 22 ) ; tree . root = tree . insert ( tree . root , 13 ) ; tree . root = tree . insert ( tree . root , 3 ) ; tree . root = tree . insert ( tree . root , 2 ) ; tree . root = tree . insert ( tree . root , 6 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ is ▁ " ) ; tree . inorder ( tree . root ) ; } }
Sorted order printing of a given array that represents a BST | C # Code for Sorted order printing of a given array that represents a BST ; print left subtree ; print root ; print right subtree ; Driver Code
using System ; class GFG { static private void printSorted ( int [ ] arr , int start , int end ) { if ( start > end ) return ; printSorted ( arr , start * 2 + 1 , end ) ; Console . Write ( arr [ start ] + " ▁ " ) ; printSorted ( arr , start * 2 + 2 , end ) ; } static public void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 5 , 1 , 3 } ; printSorted ( arr , 0 , arr . Length - 1 ) ; } }