codeparrot/xlcost-text-to-code huggingface dataset:
Postorder traversal of Binary Tree without recursion and without stack | CPP program or postorder traversal ; 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 . ; Visited left subtree ; Visited right subtree ; Print node ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; void postorder ( struct Node * head ) { struct Node * temp = head ; unordered_set < Node * > visited ; while ( temp && visited . find ( temp ) == visited . end ( ) ) { if ( temp -> left && visited . find ( temp -> left ) == visited . end ( ) ) temp = temp -> left ; else if ( temp -> right && visited . find ( temp -> right ) == visited . end ( ) ) temp = temp -> right ; else { printf ( " % d ▁ " , temp -> data ) ; visited . insert ( temp ) ; temp = head ; } } } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 10 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; postorder ( root ) ; return 0 ; }
Postorder traversal of Binary Tree without recursion and without stack | CPP program or postorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Visited left subtree ; Visited right subtree ; Print node ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; bool visited ; } ; void postorder ( struct Node * head ) { struct Node * temp = head ; while ( temp && temp -> visited == false ) { if ( temp -> left && temp -> left -> visited == false ) temp = temp -> left ; else if ( temp -> right && temp -> right -> visited == false ) temp = temp -> right ; else { printf ( " % d ▁ " , temp -> data ) ; temp -> visited = true ; temp = head ; } } } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; node -> visited = false ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 10 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; postorder ( root ) ; return 0 ; }
Check if a linked list is Circular Linked List | C ++ program to check if linked list is circular ; Link list Node ; This function returns true if given linked list is circular , else false . ; An empty linked list is circular ; Next of head ; This loop would stop in both cases ( 1 ) If Circular ( 2 ) Not circular ; If loop stopped because of circular condition ; Utility function to create a new node . ; Driver program to test above function ; Start with the empty list ; Making linked list circular
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; bool isCircular ( struct Node * head ) { if ( head == NULL ) return true ; struct Node * node = head -> next ; while ( node != NULL && node != head ) node = node -> next ; return ( node == head ) ; } Node * newNode ( int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> next = NULL ; return temp ; } int main ( ) { struct Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 3 ) ; head -> next -> next -> next = newNode ( 4 ) ; isCircular ( head ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; head -> next -> next -> next -> next = head ; isCircular ( head ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Circular Singly Linked List | Insertion |  ; This function is only for empty list ; Creating a node dynamically . ; Assigning the data . ; Note : list was empty . We link single node to itself .
struct Node * addToEmpty ( struct Node * last , int data ) { if ( last != NULL ) return last ; struct Node * temp = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; temp -> data = data ; last = temp ; temp -> next = last ; return last ; }
Circular Singly Linked List | Insertion |  ; Creating a node dynamically . ; Assigning the data . ; Adjusting the links .
struct Node * addBegin ( struct Node * last , int data ) { if ( last == NULL ) return addToEmpty ( last , data ) ; struct Node * temp = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; temp -> data = data ; temp -> next = last -> next ; last -> next = temp ; return last ; }
Diagonal Traversal of Binary Tree | C ++ program for diagnoal traversal of Binary Tree ; Tree node ; root - root of the binary tree d - distance of current line from rightmost - topmost slope . diagonalPrint - multimap to store Diagonal elements ( Passed by Reference ) ; Base case ; Store all nodes of same line together as a vector ; Increase the vertical distance if left child ; Vertical distance remains same for right child ; Print diagonal traversal of given binary tree ; create a map of vectors to store Diagonal elements ; Utility method to create a new node ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; void diagonalPrintUtil ( Node * root , int d , map < int , vector < int > > & diagonalPrint ) { if ( ! root ) return ; diagonalPrint [ d ] . push_back ( root -> data ) ; diagonalPrintUtil ( root -> left , d + 1 , diagonalPrint ) ; diagonalPrintUtil ( root -> right , d , diagonalPrint ) ; } void diagonalPrint ( Node * root ) { map < int , vector < int > > diagonalPrint ; diagonalPrintUtil ( root , 0 , diagonalPrint ) ; cout << " Diagonal ▁ Traversal ▁ of ▁ binary ▁ tree ▁ : ▁ STRNEWLINE " ; for ( auto it : diagonalPrint ) { vector < int > v = it . second ; for ( auto it : v ) cout << it << " ▁ " ; cout << endl ; } } Node * newNode ( int data ) { Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } int main ( ) { Node * root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 10 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; diagonalPrint ( root ) ; return 0 ; }
Binary Tree ( Array implementation ) | C ++ implementation of tree using array numbering starting from 0 to n - 1. ; create root ; create left son of root ; create right son of root ; Print tree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char tree [ 10 ] ; int root ( char key ) { if ( tree [ 0 ] != ' \0' ) cout << " Tree ▁ already ▁ had ▁ root " ; else tree [ 0 ] = key ; return 0 ; } int set_left ( char key , int parent ) { if ( tree [ parent ] == ' \0' ) cout << " Can ' set child at " << ( parent * 2 ) + 1 << " ▁ , ▁ no ▁ parent ▁ found " ; else tree [ ( parent * 2 ) + 1 ] = key ; return 0 ; } int set_right ( char key , int parent ) { if ( tree [ parent ] == ' \0' ) cout << " Can ' set child at " << ( parent * 2 ) + 2 << " ▁ , ▁ no ▁ parent ▁ found " ; else tree [ ( parent * 2 ) + 2 ] = key ; return 0 ; } int print_tree ( ) { cout << " STRNEWLINE " ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( tree [ i ] != ' \0' ) cout << tree [ i ] ; else cout << " - " ; } return 0 ; } int main ( ) { root ( ' A ' ) ; set_right ( ' C ' , 0 ) ; set_left ( ' D ' , 1 ) ; set_right ( ' E ' , 1 ) ; set_right ( ' F ' , 2 ) ; print_tree ( ) ; return 0 ; }
Swap Kth node from beginning with Kth node from end in a Linked List | A C ++ program to swap Kth node from beginning with kth node from end ; A Linked List node ; Utility function to insert a node at the beginning ; Utility function for displaying linked list ; Utility function for calculating length of linked list ; Function for swapping kth nodes from both ends of linked list ; Count nodes in linked list ; Check if k is valid ; If x ( kth node from start ) and y ( kth node from end ) are same ; Find the kth node from the beginning of the linked list . We also find previous of kth node because we need to update next pointer of the previous . ; Similarly , find the kth node from end and its previous . kth node from end is ( n - k + 1 ) th node from beginning ; If x_prev exists , then new next of it will be y . Consider the case when y -> next is x , in this case , x_prev and y are same . So the statement " x _ prev - > next ▁ = ▁ y " creates a self loop . This self loop will be broken when we change y -> next . ; Same thing applies to y_prev ; Swap next pointers of x and y . These statements also break self loop if x -> next is y or y -> next is x ; Change head pointers when k is 1 or n ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void printList ( struct Node * node ) { while ( node != NULL ) { cout << node -> data << " ▁ " ; node = node -> next ; } cout << endl ; } int countNodes ( struct Node * s ) { int count = 0 ; while ( s != NULL ) { count ++ ; s = s -> next ; } return count ; } void swapKth ( struct Node * * head_ref , int k ) { int n = countNodes ( * head_ref ) ; if ( n < k ) return ; if ( 2 * k - 1 == n ) return ; Node * x = * head_ref ; Node * x_prev = NULL ; for ( int i = 1 ; i < k ; i ++ ) { x_prev = x ; x = x -> next ; } Node * y = * head_ref ; Node * y_prev = NULL ; for ( int i = 1 ; i < n - k + 1 ; i ++ ) { y_prev = y ; y = y -> next ; } if ( x_prev ) x_prev -> next = y ; if ( y_prev ) y_prev -> next = x ; Node * temp = x -> next ; x -> next = y -> next ; y -> next = temp ; if ( k == 1 ) * head_ref = y ; if ( k == n ) * head_ref = x ; } int main ( ) { struct Node * head = NULL ; for ( int i = 8 ; i >= 1 ; i -- ) push ( & head , i ) ; cout << " Original ▁ Linked ▁ List : ▁ " ; printList ( head ) ; for ( int k = 1 ; k < 9 ; k ++ ) { swapKth ( & head , k ) ; cout << " Modified List for k = " printList ( head ) ; } return 0 ; }
Find pairs with given sum in doubly linked list | C ++ program to find a pair with given sum x . ; structure of node of doubly linked list ; Function to find pair whose sum equal to given value x . ; Set two pointers , first to the beginning of DLL and second to the end of DLL . ; To track if we find a pair or not ; The loop terminates when two pointers cross each other ( second -> next == first ) , or they become same ( first == second ) ; pair found ; move first in forward direction ; move second in backward direction ; if pair is not present ; A utility function to insert a new node at the beginning of doubly linked list ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next , * prev ; } ; void pairSum ( struct Node * head , int x ) { struct Node * first = head ; struct Node * second = head ; while ( second -> next != NULL ) second = second -> next ; bool found = false ; while ( first != second && second -> next != first ) { if ( ( first -> data + second -> data ) == x ) { found = true ; cout << " ( " << first -> data << " , ▁ " << second -> data << " ) " << endl ; first = first -> next ; second = second -> prev ; } else { if ( ( first -> data + second -> data ) < x ) first = first -> next ; else second = second -> prev ; } } if ( found == false ) cout << " No ▁ pair ▁ found " ; } void insert ( struct Node * * head , int data ) { struct Node * temp = new Node ; temp -> data = data ; temp -> next = temp -> prev = NULL ; if ( ! ( * head ) ) ( * head ) = temp ; else { temp -> next = * head ; ( * head ) -> prev = temp ; ( * head ) = temp ; } } int main ( ) { struct Node * head = NULL ; insert ( & head , 9 ) ; insert ( & head , 8 ) ; insert ( & head , 6 ) ; insert ( & head , 5 ) ; insert ( & head , 4 ) ; insert ( & head , 2 ) ; insert ( & head , 1 ) ; int x = 7 ; pairSum ( head , x ) ; return 0 ; }
Iterative diagonal traversal of binary tree | C ++ program to construct string from binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node ; Iterative function to print diagonal view ; base case ; inbuilt queue of Treenode ; push root ; push delimiter ; if current is delimiter then insert another for next diagonal and cout nextline ; if queue is empty return ; output nextline ; push delimiter again ; if left child is present push into queue ; current equals to right child ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * node = ( Node * ) malloc ( sizeof ( Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } void diagonalPrint ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; q . push ( NULL ) ; while ( ! q . empty ( ) ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp == NULL ) { if ( q . empty ( ) ) return ; cout << endl ; q . push ( NULL ) ; } else { while ( temp ) { cout << temp -> data << " ▁ " ; if ( temp -> left ) q . push ( temp -> left ) ; temp = temp -> right ; } } } } int main ( ) { Node * root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> right = newNode ( 10 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; diagonalPrint ( root ) ; }
Count triplets in a sorted doubly linked list whose sum is equal to a given value x | C ++ implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count pairs whose sum equal to given ' value ' ; The loop terminates when either of two pointers become NULL , or they cross each other ( second -> next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if sum is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with sum ( x - current -> data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver program to test above ; start with an empty doubly linked list ; insert values in sorted order
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next , * prev ; } ; int countPairs ( struct Node * first , struct Node * second , int value ) { int count = 0 ; while ( first != NULL && second != NULL && first != second && second -> next != first ) { if ( ( first -> data + second -> data ) == value ) { count ++ ; first = first -> next ; second = second -> prev ; } else if ( ( first -> data + second -> data ) > value ) second = second -> prev ; else first = first -> next ; } return count ; } int countTriplets ( struct Node * head , int x ) { if ( head == NULL ) return 0 ; struct Node * current , * first , * last ; int count = 0 ; last = head ; while ( last -> next != NULL ) last = last -> next ; for ( current = head ; current != NULL ; current = current -> next ) { first = current -> next ; count += countPairs ( first , last , x - current -> data ) ; } return count ; } void insert ( struct Node * * head , int data ) { struct Node * temp = new Node ( ) ; temp -> data = data ; temp -> next = temp -> prev = NULL ; if ( ( * head ) == NULL ) ( * head ) = temp ; else { temp -> next = * head ; ( * head ) -> prev = temp ; ( * head ) = temp ; } } int main ( ) { struct Node * head = NULL ; insert ( & head , 9 ) ; insert ( & head , 8 ) ; insert ( & head , 6 ) ; insert ( & head , 5 ) ; insert ( & head , 4 ) ; insert ( & head , 2 ) ; insert ( & head , 1 ) ; int x = 17 ; cout << " Count ▁ = ▁ " << countTriplets ( head , x ) ; return 0 ; }
Sorted insert in a doubly linked list with head and tail pointers | C ++ program to insetail nodes in doubly linked list such that list remains in ascending order on printing from left to right ; A linked list node ; Function to insetail new node ; If first node to be insetailed in doubly linked list ; If node to be insetailed has value less than first node ; If node to be insetailed has value more than last node ; Find the node before which we need to insert p . ; Insert new node before temp ; Function to print nodes in from left to right ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : Node * prev ; int info ; Node * next ; } ; void nodeInsetail ( Node * * head , Node * * tail , int key ) { Node * p = new Node ( ) ; p -> info = key ; p -> next = NULL ; if ( ( * head ) == NULL ) { ( * head ) = p ; ( * tail ) = p ; ( * head ) -> prev = NULL ; return ; } if ( ( p -> info ) < ( ( * head ) -> info ) ) { p -> prev = NULL ; ( * head ) -> prev = p ; p -> next = ( * head ) ; ( * head ) = p ; return ; } if ( ( p -> info ) > ( ( * tail ) -> info ) ) { p -> prev = ( * tail ) ; ( * tail ) -> next = p ; ( * tail ) = p ; return ; } Node * temp = ( * head ) -> next ; while ( ( temp -> info ) < ( p -> info ) ) temp = temp -> next ; ( temp -> prev ) -> next = p ; p -> prev = temp -> prev ; temp -> prev = p ; p -> next = temp ; } void printList ( Node * temp ) { while ( temp != NULL ) { cout << temp -> info << " ▁ " ; temp = temp -> next ; } } int main ( ) { Node * left = NULL , * right = NULL ; nodeInsetail ( & left , & right , 30 ) ; nodeInsetail ( & left , & right , 50 ) ; nodeInsetail ( & left , & right , 90 ) ; nodeInsetail ( & left , & right , 10 ) ; nodeInsetail ( & left , & right , 40 ) ; nodeInsetail ( & left , & right , 110 ) ; nodeInsetail ( & left , & right , 60 ) ; nodeInsetail ( & left , & right , 95 ) ; nodeInsetail ( & left , & right , 23 ) ; cout << " Doubly ▁ linked ▁ list ▁ on ▁ printing " " ▁ from ▁ left ▁ to ▁ right STRNEWLINE " ; printList ( left ) ; return 0 ; }
Doubly Circular Linked List | Set 1 ( Introduction and Insertion ) | Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; If list is not empty Find last node ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last
void insertEnd ( struct Node * * start , int value ) { if ( * start == NULL ) { struct Node * new_node = new Node ; new_node -> data = value ; new_node -> next = new_node -> prev = new_node ; * start = new_node ; return ; } Node * last = ( * start ) -> prev ; struct Node * new_node = new Node ; new_node -> data = value ; new_node -> next = * start ; ( * start ) -> prev = new_node ; new_node -> prev = last ; last -> next = new_node ; }
Boundary Traversal of binary tree | C program for boundary traversal of a binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; A simple function to print leaf nodes of a binary tree ; Print it if it is a leaf node ; A function to print all left boundary nodes , except a leaf node . Print the nodes in TOP DOWN manner ; to ensure top down order , print the node before calling itself for left subtree ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to print all right boundary nodes , except a leaf node Print the nodes in BOTTOM UP manner ; to ensure bottom up order , first call for right subtree , then print this node ; do nothing if it is a leaf node , this way we avoid duplicates in output ; A function to do boundary traversal of a given binary tree ; Print the left boundary in top - down manner . ; Print all leaf nodes ; Print the right boundary in bottom - up manner ; A utility function to create a node ; Driver program to test above functions
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE struct node { int data ; struct node * left , * right ; } ; void printLeaves ( struct node * root ) { if ( root == NULL ) return ; printLeaves ( root -> left ) ; if ( ! ( root -> left ) && ! ( root -> right ) ) printf ( " % d ▁ " , root -> data ) ; printLeaves ( root -> right ) ; } void printBoundaryLeft ( struct node * root ) { if ( root == NULL ) return ; if ( root -> left ) { printf ( " % d ▁ " , root -> data ) ; printBoundaryLeft ( root -> left ) ; } else if ( root -> right ) { printf ( " % d ▁ " , root -> data ) ; printBoundaryLeft ( root -> right ) ; } } void printBoundaryRight ( struct node * root ) { if ( root == NULL ) return ; if ( root -> right ) { printBoundaryRight ( root -> right ) ; printf ( " % d ▁ " , root -> data ) ; } else if ( root -> left ) { printBoundaryRight ( root -> left ) ; printf ( " % d ▁ " , root -> data ) ; } } void printBoundary ( struct node * root ) { if ( root == NULL ) return ; printf ( " % d ▁ " , root -> data ) ; printBoundaryLeft ( root -> left ) ; printLeaves ( root -> left ) ; printLeaves ( root -> right ) ; printBoundaryRight ( root -> right ) ; } struct node * newNode ( int data ) { struct node * temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { struct node * root = newNode ( 20 ) ; root -> left = newNode ( 8 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 12 ) ; root -> left -> right -> left = newNode ( 10 ) ; root -> left -> right -> right = newNode ( 14 ) ; root -> right = newNode ( 22 ) ; root -> right -> right = newNode ( 25 ) ; printBoundary ( root ) ; return 0 ; }