Banana-Leopard/TASTY huggingface dataset:
/* C++ program to convert a Binary Tree to Threaded Tree */
#include <bits/stdc++.h>
using namespace std;
 
/* Structure of a node in threaded binary tree */
struct Node {
    int key;
    Node *left, *right;
 
    // Used to indicate whether the right pointer is a
    // normal right pointer or a pointer to inorder
    // successor.
    bool isThreaded;
};
 
// Helper function to put the Nodes in inorder into queue
void populateQueue(Node* root, std::queue<Node*>* q)
{
    if (root == NULL)
        return;
    if (root->left)
        populateQueue(root->left, q);
    q->push(root);
    if (root->right)
        populateQueue(root->right, q);
}
 
// Function to traverse queue, and make tree threaded
void createThreadedUtil(Node* root, std::queue<Node*>* q)
{
    if (root == NULL)
        return;
 
    if (root->left)
        createThreadedUtil(root->left, q);
    q->pop();
 
    if (root->right)
        createThreadedUtil(root->right, q);
 
    // If right pointer is NULL, link it to the
    // inorder successor and set 'isThreaded' bit.
    else {
        root->right = q->front();
        root->isThreaded = true;
    }
}
 
// This function uses populateQueue() and
// createThreadedUtil() to convert a given binary tree
// to threaded tree.
void createThreaded(Node* root)
{
    // Create a queue to store inorder traversal
    std::queue<Node*> q;
 
    // Store inorder traversal in queue
    populateQueue(root, &q);
 
    // Link NULL right pointers to inorder successor
    createThreadedUtil(root, &q);
}
 
// A utility function to find leftmost node in a binary
// tree rooted with 'root'. This function is used in
// inOrder()
Node* leftMost(Node* root)
{
    while (root != NULL && root->left != NULL)
        root = root->left;
    return root;
}
 
// Function to do inorder traversal of a threaded binary
// tree
void inOrder(Node* root)
{
    if (root == NULL)
        return;
 
    // Find the leftmost node in Binary Tree
    Node* cur = leftMost(root);
 
    while (cur != NULL) {
        cout << cur->key << " ";
 
        // If this Node is a thread Node, then go to
        // inorder successor
        if (cur->isThreaded)
            cur = cur->right;
 
        else // Else go to the leftmost child in right
             // subtree
            cur = leftMost(cur->right);
    }
}
 
// A utility function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->left = temp->right = NULL;
    temp->key = key;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    /*       1
            / \
           2   3
          / \ / \
         4  5 6  7     */
    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);
 
    cout << "Inorder traversal of created threaded tree "
            "is\n";
    inOrder(root);
   
    return 0;
}

Time complexity: linear
Space complexity: linear
/* C++ program to convert a Binary Tree to
    Threaded Tree */
#include <bits/stdc++.h>
using namespace std;
 
/* Structure of a node in threaded binary tree */
struct Node
{
    int key;
    Node *left, *right;
 
    // Used to indicate whether the right pointer
    // is a normal right pointer or a pointer
    // to inorder successor.
    bool isThreaded;
};
 
// Converts tree with given root to threaded
// binary tree.
// This function returns rightmost child of
// root.
Node *createThreaded(Node *root)
{
    // Base cases : Tree is empty or has single
    //              node
    if (root == NULL)
        return NULL;
    if (root->left == NULL &&
        root->right == NULL)
        return root;
 
    // Find predecessor if it exists
    if (root->left != NULL)
    {
        // Find predecessor of root (Rightmost
        // child in left subtree)
        Node* l = createThreaded(root->left);
 
        // Link a thread from predecessor to
        // root.
        l->right = root;
        l->isThreaded = true;
    }
 
    // If current node is rightmost child
    if (root->right == NULL)
        return root;
 
    // Recur for right subtree.
    return createThreaded(root->right);
}
 
// A utility function to find leftmost node
// in a binary tree rooted with 'root'.
// This function is used in inOrder()
Node *leftMost(Node *root)
{
    while (root != NULL && root->left != NULL)
        root = root->left;
    return root;
}
 
// Function to do inorder traversal of a threadded
// binary tree
void inOrder(Node *root)
{
    if (root == NULL) return;
 
    // Find the leftmost node in Binary Tree
    Node *cur = leftMost(root);
 
    while (cur != NULL)
    {
        cout << cur->key << " ";
 
        // If this Node is a thread Node, then go to
        // inorder successor
        if (cur->isThreaded)
            cur = cur->right;
 
        else // Else go to the leftmost child in right subtree
            cur = leftMost(cur->right);
    }
}
 
// A utility function to create a new node
Node *newNode(int key)
{
    Node *temp = new Node;
    temp->left = temp->right = NULL;
    temp->key = key;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    /*       1
            / \
           2   3
          / \ / \
         4  5 6  7   */
    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);
 
    cout << "Inorder traversal of created "
            "threaded tree is\n";
    inOrder(root);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ Program to find ceil of a given value in BST
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, left child and right child */
class node {
public:
    int key;
    node* left;
    node* right;
};
 
/* Helper function that allocates a new node with the given
key and NULL left and right pointers.*/
node* newNode(int key)
{
    node* Node = new node();
    Node->key = key;
    Node->left = NULL;
    Node->right = NULL;
    return (Node);
}
 
// Function to find ceil of a given input in BST. If input
// is more than the max key in BST, return -1
int Ceil(node* root, int input)
{
    // Base case
    if (root == NULL)
        return -1;
 
    // We found equal key
    if (root->key == input)
        return root->key;
 
    // If root's key is smaller, ceil must be in right
    // subtree
    if (root->key < input)
        return Ceil(root->right, input);
 
    // Else, either left subtree or root has the ceil value
    int ceil = Ceil(root->left, input);
    return (ceil >= input) ? ceil : root->key;
}
 
// Driver code
int main()
{
    node* root = newNode(8);
 
    root->left = newNode(4);
    root->right = newNode(12);
 
    root->left->left = newNode(2);
    root->left->right = newNode(6);
 
    root->right->left = newNode(10);
    root->right->right = newNode(14);
 
    for (int i = 0; i < 16; i++)
        cout << i << " " << Ceil(root, i) << endl;
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: logn
Space complexity: logn
// C++ Program to find floor of a given value in BST
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, left child and right child */
class node {
public:
    int key;
    node* left;
    node* right;
};
 
/* Helper function that allocates a new node with the given
key and NULL left and right pointers.*/
node* newNode(int key)
{
    node* Node = new node();
    Node->key = key;
    Node->left = NULL;
    Node->right = NULL;
    return (Node);
}
 
// Function to find floor of a given input in BST. If input
// is more than the min key in BST, return -1
int Floor(node* root, int input)
{
    // Base case
    if (root == NULL)
        return -1;
 
    // We found equal key
    if (root->key == input)
        return root->key;
 
    // If root's key is larger, floor must be in left
    // subtree
    if (root->key > input)
        return Floor(root->left, input);
 
    // Else, either right subtree or root has the floor
    // value
    else {
        int floor = Floor(root->right, input);
        // exception for -1 because it is being returned in
        // base case
        return (floor <= input && floor != -1) ? floor
                                               : root->key;
    }
}
 
// Driver code
int main()
{
    node* root = newNode(8);
 
    root->left = newNode(4);
    root->right = newNode(12);
 
    root->left->left = newNode(2);
    root->left->right = newNode(6);
 
    root->right->left = newNode(10);
    root->right->right = newNode(14);
 
    for (int i = 0; i < 16; i++)
        cout << i << " " << Floor(root, i) << endl;
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: logn
Space complexity: logn
// C++ program to find floor and ceil of a given key in BST
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, left child and right child */
struct Node {
    int data;
    Node *left, *right;
 
    Node(int value)
    {
        data = value;
        left = right = NULL;
    }
};
 
// Helper function to find floor and ceil of a given key in
// BST
void floorCeilBSTHelper(Node* root, int key, int& floor,
                        int& ceil)
{
 
    while (root) {
 
        if (root->data == key) {
            ceil = root->data;
            floor = root->data;
            return;
        }
 
        if (key > root->data) {
            floor = root->data;
            root = root->right;
        }
        else {
            ceil = root->data;
            root = root->left;
        }
    }
    return;
}
 
// 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;
void floorCeilBST(Node* root, int key)
{
 
    // Variables 'floor' and 'ceil' are passed by reference
    int floor = -1, ceil = -1;
 
    floorCeilBSTHelper(root, key, floor, ceil);
 
    cout << key << ' ' << floor << ' ' << ceil << '\n';
}
 
// Driver code
int main()
{
    Node* root = new Node(8);
 
    root->left = new Node(4);
    root->right = new Node(12);
 
    root->left->left = new Node(2);
    root->left->right = new Node(6);
 
    root->right->left = new Node(10);
    root->right->right = new Node(14);
 
    for (int i = 0; i < 16; i++)
        floorCeilBST(root, i);
 
    return 0;
}

Time complexity: logn
Space complexity: constant
// C++ program to print leaf node from
// preorder of binary search tree.
#include<bits/stdc++.h>
using namespace std;
 
// Binary Search
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);
}
 
// Function to print Leaf Nodes by doing preorder
// traversal of tree using preorder and inorder arrays.
void leafNodesRec(int preorder[], int inorder[],
                  int l, int r, int *ind, int n)
{
    // If l == r, therefore no right or left subtree.
    // So, it must be leaf Node, print it.
    if(l == r)
    {
        printf("%d ", inorder[l]);
        *ind = *ind + 1;
        return;
    }
 
    // If array is out of bound, return.
    if (l < 0 || l > r || r >= n)
        return;
 
    // Finding the index of preorder element
    // in inorder array using binary search.
    int loc = binarySearch(inorder, l, r, preorder[*ind]);
 
    // Incrementing the index.
    *ind = *ind + 1;
 
    // Finding on the left subtree.
    leafNodesRec(preorder, inorder, l, loc - 1, ind, n);
 
    // Finding on the right subtree.
    leafNodesRec(preorder, inorder, loc + 1, r, ind, n);
}
 
// Finds leaf nodes from given preorder traversal.
void leafNodes(int preorder[], int n)
{
    int inorder[n];  // To store inorder traversal
 
    // Copy the preorder into another array.
    for (int i = 0; i < n; i++)
        inorder[i] = preorder[i];
 
    // Finding the inorder by sorting the array.
    sort(inorder, inorder + n);
 
    // Point to the index in preorder.
    int ind = 0;
 
    // Print the Leaf Nodes.
    leafNodesRec(preorder, inorder, 0, n - 1, &ind, n);
}
 
// Driven Program
int main()
{
    int preorder[] = { 890, 325, 290, 530, 965 };
    int n = sizeof(preorder)/sizeof(preorder[0]);
 
    leafNodes(preorder, n);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program to find number of pairs  and minimal
// possible value
#include <bits/stdc++.h>
using namespace std;
 
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
    // initialize smallest and count
    int smallest = INT_MAX;
    int count = 0;
 
    // iterate over all pairs
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            // is abs value is smaller than smallest
            // update smallest and reset count to 1
            if (abs(arr[i] + arr[j] - k) < smallest) {
                smallest = abs(arr[i] + arr[j] - k);
                count = 1;
            }
 
            // if abs value is equal to smallest
            // increment count value
            else if (abs(arr[i] + arr[j] - k) == smallest)
                count++;
        }
 
    // print result
    cout << "Minimal Value = " << smallest << "\n";
    cout << "Total Pairs = " << count << "\n";
}
 
// driver program
int main()
{
    int arr[] = { 3, 5, 7, 5, 1, 9, 9 };
    int k = 12;
    int n = sizeof(arr) / sizeof(arr[0]);
    pairs(arr, n, k);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find number of pairs
// and minimal possible value
#include <bits/stdc++.h>
using namespace std;
 
// function for finding pairs and min value
void pairs(int arr[], int n, int k)
{
    // initialize smallest and count
    int smallest = INT_MAX, count = 0;
    set<int> s;
 
    // iterate over all pairs
    s.insert(arr[0]);
    for (int i = 1; i < n; i++) {
        // Find the closest elements to  k - arr[i]
        int lower
            = *lower_bound(s.begin(), s.end(), k - arr[i]);
 
        int upper
            = *upper_bound(s.begin(), s.end(), k - arr[i]);
 
        // Find absolute value of the pairs formed
        // with closest greater and smaller elements.
        int curr_min = min(abs(lower + arr[i] - k),
                           abs(upper + arr[i] - k));
 
        // is abs value is smaller than smallest
        // update smallest and reset count to 1
        if (curr_min < smallest) {
            smallest = curr_min;
            count = 1;
        }
 
        // if abs value is equal to smallest
        // increment count value
        else if (curr_min == smallest)
            count++;
        s.insert(arr[i]);
 
    } // print result
 
    cout << "Minimal Value = " << smallest << "\n";
    cout << "Total Pairs = " << count << "\n";
}
 
// driver program
int main()
{
    int arr[] = { 3, 5, 7, 5, 1, 9, 9 };
    int k = 12;
    int n = sizeof(arr) / sizeof(arr[0]);
    pairs(arr, n, k);
    return 0;
}

Time complexity: nlogn
Space complexity: linear
// CPP program to find rank of an
// element in a stream.
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    Node *left, *right;
    int leftSize;
};
 
Node* newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    temp->leftSize = 0;
    return temp;
}
 
// Inserting a new Node.
Node* insert(Node*& root, int data)
{
    if (!root)
        return newNode(data);
 
    // Updating size of left subtree.
    if (data <= root->data) {
        root->left = insert(root->left, data);
        root->leftSize++;
    }
    else
        root->right = insert(root->right, data);
 
    return root;
}
 
// Function to get Rank of a Node x.
int getRank(Node* root, int x)
{
    // Step 1.
    if (root->data == x)
        return root->leftSize;
 
    // Step 2.
    if (x < root->data) {
        if (!root->left)
            return -1;
        else
            return getRank(root->left, x);
    }
 
    // Step 3.
    else {
        if (!root->right)
            return -1;
        else {
            int rightSize = getRank(root->right, x);
              if(rightSize == -1 ) return -1;
            return root->leftSize + 1 + rightSize;
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 1, 4, 4, 5, 9, 7, 13, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 4;
 
    Node* root = NULL;
    for (int i = 0; i < n; i++)
        root = insert(root, arr[i]);
 
    cout << "Rank of " << x << " in stream is: "
         << getRank(root, x) << endl;
 
    x = 13;
    cout << "Rank of " << x << " in stream is: "
         << getRank(root, x) << endl;
 
    x = 8;
    cout << "Rank of " << x << " in stream is: "
         << getRank(root, x) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find rank of an
// element in a stream.
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a[] = {5, 1, 14, 4, 15, 9, 7, 20, 11};
    int key = 20;
    int arraySize = sizeof(a)/sizeof(a[0]);
    int count = 0;
    for(int i = 0; i < arraySize; i++)
    {
        if(a[i] <= key)
        {
            count += 1;
        }
    }
    cout << "Rank of " << key << " in stream is: "
            << count-1 << endl;
    return 0;
}
 
// This code is contributed by
// Ashwin Loganathan.

Time complexity: linear
Space complexity: constant
// C++ program to insert element in Binary Tree
#include <iostream>
#include <queue>
using namespace std;
 
/* A binary tree node has data, pointer to left child
and a pointer to right child */
 
struct Node {
    int data;
    Node* left;
    Node* right;
};
 
// Function to create a new node
Node* CreateNode(int data)
{
    Node* newNode = new Node();
    if (!newNode) {
        cout << "Memory error\n";
        return NULL;
    }
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
// Function to insert element in binary tree
Node* InsertNode(Node* root, int data)
{
    // If the tree is empty, assign new node address to root
    if (root == NULL) {
        root = CreateNode(data);
        return root;
    }
 
    // Else, do level order traversal until we find an empty
    // place, i.e. either left child or right child of some
    // node is pointing to NULL.
    queue<Node*> q;
    q.push(root);
 
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
 
        if (temp->left != NULL)
            q.push(temp->left);
        else {
            temp->left = CreateNode(data);
            return root;
        }
 
        if (temp->right != NULL)
            q.push(temp->right);
        else {
            temp->right = CreateNode(data);
            return root;
        }
    }
}
 
/* Inorder traversal of a binary tree */
 
void inorder(Node* temp)
{
    if (temp == NULL)
        return;
 
    inorder(temp->left);
    cout << temp->data << ' ';
    inorder(temp->right);
}
 
// Driver code
int main()
{
    Node* root = CreateNode(10);
    root->left = CreateNode(11);
    root->left->left = CreateNode(7);
    root->right = CreateNode(9);
    root->right->left = CreateNode(15);
    root->right->right = CreateNode(8);
 
    cout << "Inorder traversal before insertion: ";
    inorder(root);
    cout << endl;
 
    int key = 12;
    root = InsertNode(root, key);
 
    cout << "Inorder traversal after insertion: ";
    inorder(root);
    cout << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: logn
// C++ program to delete element in binary tree
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has key, pointer to left
child and a pointer to right child */
struct Node {
    int key;
    struct Node *left, *right;
};
 
/* function to create a new node of tree and
return pointer */
struct Node* newNode(int key)
{
    struct Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return temp;
};
 
/* Inorder traversal of a binary tree*/
void inorder(struct Node* temp)
{
    if (!temp)
        return;
    inorder(temp->left);
    cout << temp->key << " ";
    inorder(temp->right);
}
 
/* function to delete the given deepest node
(d_node) in binary tree */
void deletDeepest(struct Node* root, struct Node* d_node)
{
    queue<struct Node*> q;
    q.push(root);
 
    // Do level order traversal until last node
    struct Node* temp;
    while (!q.empty()) {
        temp = q.front();
        q.pop();
        if (temp == d_node) {
            temp = NULL;
            delete (d_node);
            return;
        }
        if (temp->right) {
            if (temp->right == d_node) {
                temp->right = NULL;
                delete (d_node);
                return;
            }
            else
                q.push(temp->right);
        }
 
        if (temp->left) {
            if (temp->left == d_node) {
                temp->left = NULL;
                delete (d_node);
                return;
            }
            else
                q.push(temp->left);
        }
    }
}
 
/* function to delete element in binary tree */
Node* deletion(struct Node* root, int key)
{
    if (root == NULL)
        return NULL;
 
    if (root->left == NULL && root->right == NULL) {
        if (root->key == key)
            return NULL;
        else
            return root;
    }
 
    queue<struct Node*> q;
    q.push(root);
 
    struct Node* temp;
    struct Node* key_node = NULL;
 
    // Do level order traversal to find deepest
    // node(temp) and node to be deleted (key_node)
    while (!q.empty()) {
        temp = q.front();
        q.pop();
 
        if (temp->key == key)
            key_node = temp;
 
        if (temp->left)
            q.push(temp->left);
 
        if (temp->right)
            q.push(temp->right);
    }
 
    if (key_node != NULL) {
        int x = temp->key;
        deletDeepest(root, temp);
        key_node->key = x;
    }
    return root;
}
 
// Driver code
int main()
{
    struct Node* root = newNode(10);
    root->left = newNode(11);
    root->left->left = newNode(7);
    root->left->right = newNode(12);
    root->right = newNode(9);
    root->right->left = newNode(15);
    root->right->right = newNode(8);
 
    cout << "Inorder traversal before deletion : ";
    inorder(root);
 
    int key = 11;
    root = deletion(root, key);
 
    cout << endl;
    cout << "Inorder traversal after deletion : ";
    inorder(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear