Banana-Leopard/TASTY huggingface dataset:
// 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 data;
    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->data = 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->data << " ";
    inorder(temp->right);
}
 
struct Node* deletion(struct Node* root, int key)
{
    if (root == NULL)
        return NULL;
    if (root->left == NULL && root->right == NULL) {
        if (root->data == key)
            return NULL;
        else
            return root;
    }
    Node* key_node = NULL;
    Node* temp;
    Node* last;
    queue<Node*> q;
    q.push(root);
    // Do level order traversal to find deepest
    // node(temp), node to be deleted (key_node)
    // and parent of deepest node(last)
    while (!q.empty()) {
        temp = q.front();
        q.pop();
        if (temp->data == key)
            key_node = temp;
        if (temp->left) {
            last = temp; // storing the parent node
            q.push(temp->left);
        }
        if (temp->right) {
            last = temp; // storing the parent node
            q.push(temp->right);
        }
    }
    if (key_node != NULL) {
        key_node->data
            = temp->data; // replacing key_node's data to
                          // deepest node's data
        if (last->right == temp)
            last->right = NULL;
        else
            last->left = NULL;
        delete (temp);
    }
    return root;
}
// Driver code
int main()
{
    struct Node* root = newNode(9);
    root->left = newNode(2);
    root->left->left = newNode(4);
    root->left->right = newNode(7);
    root->right = newNode(8);
 
    cout << "Inorder traversal before deletion : ";
    inorder(root);
 
    int key = 7;
    root = deletion(root, key);
 
    cout << endl;
    cout << "Inorder traversal after deletion : ";
    inorder(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation of tree using array
// numbering starting from 0 to n-1.
#include<bits/stdc++.h>
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 << "\nCan't 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 << "\nCan't set child at "
    << (parent * 2) + 2
    << " , no parent found";
  else
    tree[(parent * 2) + 2] = key;
  return 0;
}
 
int print_tree() {
  cout << "\n";
  for (int i = 0; i < 10; i++) {
    if (tree[i] != '\0')
      cout << tree[i];
    else
      cout << "-";
  }
  return 0;
}
 
// Driver Code
int main() {
  root('A');
  set_left('B',0);
  set_right('C', 0);
  set_left('D', 1);
  set_right('E', 1);
  set_right('F', 2);
  print_tree();
  return 0;
}

Time complexity: logn
Space complexity: linear
// C++ program to evaluate an expression tree
#include <bits/stdc++.h>
using namespace std;
 
// Class to represent the nodes of syntax tree
class node
{
public:
    string info;
    node *left = NULL, *right = NULL;
    node(string x)
    {
        info = x;
    }
};
 
// Utility function to return the integer value
// of a given string
int toInt(string s)
{
    int num = 0;
         
    // Check if the integral value is
    // negative or not
    // If it is not negative, generate the number
    // normally
    if(s[0]!='-')
        for (int i=0; i<s.length(); i++)
            num = num*10 + (int(s[i])-48);
    // If it is negative, calculate the +ve number
    // first ignoring the sign and invert the
    // sign at the end
    else
    {
      for (int i=1; i<s.length(); i++)
        num = num*10 + (int(s[i])-48);
      num = num*-1;
    }
     
    return num;
}
 
// This function receives a node of the syntax tree
// and recursively evaluates it
int eval(node* root)
{
    // empty tree
    if (!root)
        return 0;
 
    // leaf node i.e, an integer
    if (!root->left && !root->right)
        return toInt(root->info);
 
    // Evaluate left subtree
    int l_val = eval(root->left);
 
    // Evaluate right subtree
    int r_val = eval(root->right);
 
    // Check which operator to apply
    if (root->info=="+")
        return l_val+r_val;
 
    if (root->info=="-")
        return l_val-r_val;
 
    if (root->info=="*")
        return l_val*r_val;
 
    return l_val/r_val;
}
 
//driver function to check the above program
int main()
{
    // create a syntax tree
    node *root = new node("+");
    root->left = new node("*");
    root->left->left = new node("5");
    root->left->right = new node("-4");
    root->right = new node("-");
    root->right->left = new node("100");
    root->right->right = new node("20");
    cout << eval(root) << endl;
 
    delete(root);
 
    root = new node("+");
    root->left = new node("*");
    root->left->left = new node("5");
    root->left->right = new node("4");
    root->right = new node("-");
    root->right->left = new node("100");
    root->right->right = new node("/");
    root->right->right->left = new node("20");
    root->right->right->right = new node("2");
 
    cout << eval(root);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to check if a given Binary Tree is symmetric
// or not
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    int key;
    struct Node *left, *right;
};
 
// Utility function to create new Node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    temp->left = temp->right = NULL;
    return (temp);
}
 
// Returns true if trees with roots as root1 and root2 are
// mirror
bool isMirror(struct Node* root1, struct Node* root2)
{
    // If both trees are empty, then they are mirror images
    if (root1 == NULL && root2 == NULL)
        return true;
 
    // For two trees to be mirror images, the following
    // three conditions must be true
    // 1.) Their root node's key must be same
    // 2.) left subtree of left tree and right subtree of
    // right tree have to be mirror images
    // 3.) right subtree of left tree and left subtree of
    // right tree have to be mirror images
    if (root1 && root2 && root1->key == root2->key)
        return isMirror(root1->left, root2->right)
               && isMirror(root1->right, root2->left);
 
    // if none of above conditions is true then root1
    // and root2 are not mirror images
    return false;
}
 
// Returns true if a tree is symmetric i.e. mirror image of itself
bool isSymmetric(struct Node* root)
{
    // Check if tree is mirror of itself
    return isMirror(root, root);
}
 
// Driver code
int main()
{
    // Let us construct the Tree shown in the above figure
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(2);
    root->left->left = newNode(3);
    root->left->right = newNode(4);
    root->right->left = newNode(4);
    root->right->right = newNode(3);
 
    if (isSymmetric(root))
        cout << "Symmetric";
    else
        cout << "Not symmetric";
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: linear
Space complexity: logn
// C++ program to print inorder traversal
// using stack.
#include<bits/stdc++.h>
using namespace std;
 
/* A binary tree Node has data, pointer to left child
   and a pointer to right child */
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
    Node (int data)
    {
        this->data = data;
        left = right = NULL;
    }
};
 
/* Iterative function for inorder tree
   traversal */
void inOrder(struct Node *root)
{
    stack<Node *> s;
    Node *curr = root;
 
    while (curr != NULL || s.empty() == false)
    {
        /* Reach the left most Node of the
           curr Node */
        while (curr !=  NULL)
        {
            /* place pointer to a tree node on
               the stack before traversing
              the node's left subtree */
            s.push(curr);
            curr = curr->left;
        }
 
        /* Current must be NULL at this point */
        curr = s.top();
        s.pop();
 
        cout << curr->data << " ";
 
        /* we have visited the node and its
           left subtree.  Now, it's right
           subtree's turn */
        curr = curr->right;
 
    } /* end of while */
}
 
/* Driver program to test above functions*/
int main()
{
 
    /* Constructed binary tree is
              1
            /   \
          2      3
        /  \
      4     5
    */
    struct Node *root = new Node(1);
    root->left        = new Node(2);
    root->right       = new Node(3);
    root->left->left  = new Node(4);
    root->left->right = new Node(5);
 
    inOrder(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for finding postorder
// traversal of BST from preorder traversal
#include <bits/stdc++.h>
using namespace std;
 
// Function to find postorder traversal from
// preorder traversal.
void findPostOrderUtil(int pre[], int n, int minval,
                       int maxval, int& preIndex)
{
 
    // If entire preorder array is traversed then
    // return as no more element is left to be
    // added to post order array.
    if (preIndex == n)
        return;
 
    // If array element does not lie in range specified,
    // then it is not part of current subtree.
    if (pre[preIndex] < minval || pre[preIndex] > maxval) {
        return;
    }
 
    // Store current value, to be printed later, after
    // printing left and right subtrees. Increment
    // preIndex to find left and right subtrees,
    // and pass this updated value to recursive calls.
    int val = pre[preIndex];
    preIndex++;
 
    // All elements with value between minval and val
    // lie in left subtree.
    findPostOrderUtil(pre, n, minval, val, preIndex);
 
    // All elements with value between val and maxval
    // lie in right subtree.
    findPostOrderUtil(pre, n, val, maxval, preIndex);
 
    cout << val << " ";
}
 
// Function to find postorder traversal.
void findPostOrder(int pre[], int n)
{
 
    // To store index of element to be
    // traversed next in preorder array.
    // This is passed by reference to
    // utility function.
    int preIndex = 0;
 
    findPostOrderUtil(pre, n, INT_MIN, INT_MAX, preIndex);
}
 
// Driver code
int main()
{
    int pre[] = { 40, 30, 35, 80, 100 };
 
    int n = sizeof(pre) / sizeof(pre[0]);
 
    // Calling function
    findPostOrder(pre, n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to replace each node
// in binary tree with the sum of its inorder
// predecessor and successor
#include <bits/stdc++.h>
  
using namespace std;
  
// node of a binary tree
struct Node {
    int data;
    struct Node* left, *right;
};
  
// function to get a new node of a binary tree
struct Node* getNode(int data)
{
    // allocate node
    struct Node* new_node =
       (struct Node*)malloc(sizeof(struct Node));
  
    // put in the data;
    new_node->data = data;
    new_node->left = new_node->right = NULL;
  
    return new_node;
}
  
 
// function to print the preorder traversal
// of a binary tree
void preorderTraversal(struct Node* root)
{
    // if root is NULL
    if (!root)
        return;
  
    // first print the data of node
    cout << root->data << " ";
  
    // then recur on left subtree
    preorderTraversal(root->left);
  
    // now recur on right subtree
    preorderTraversal(root->right);
}
 void inOrderTraverse(struct Node* root, struct Node* &prev, int &prevVal)
 {
     if(root == NULL) return;
     inOrderTraverse(root->left, prev, prevVal);
     if(prev == NULL)
     {
         prev = root;
         prevVal = 0;
     }
     else
     {
         int temp = prev->data;
         prev->data = prevVal + root->data;
         prev = root;
         prevVal = temp;
     }
     inOrderTraverse(root->right, prev, prevVal);
 }
// Driver program to test above
int main()
{
    // binary tree formation
    struct Node* root = getNode(1); /*         1        */
    root->left = getNode(2);        /*       /   \      */
    root->right = getNode(3);       /*     2      3     */
    root->left->left = getNode(4);  /*    /  \  /   \   */
    root->left->right = getNode(5); /*   4   5  6   7   */
    root->right->left = getNode(6);
    root->right->right = getNode(7);
  
    cout << "Preorder Traversal before tree modification:\n";
    preorderTraversal(root);
    struct Node* prev = NULL;
    int prevVal = -1;
    inOrderTraverse(root, prev, prevVal);
    // update righmost node.
    prev->data = prevVal;
  
    cout << "\nPreorder Traversal after tree modification:\n";
    preorderTraversal(root);
  
    return 0;
}

Time complexity: linear
Space complexity: linear
// Recursive CPP program for level
// order traversal of Binary Tree
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data,
pointer to left child
and a pointer to right child */
class node {
public:
    int data;
    node *left, *right;
};
 
/* Function prototypes */
void printCurrentLevel(node* root, int level);
int height(node* node);
node* newNode(int data);
 
/* Function to print level
order traversal a tree*/
void printLevelOrder(node* root)
{
    int h = height(root);
    int i;
    for (i = 1; i <= h; i++)
        printCurrentLevel(root, i);
}
 
/* Print nodes at a current level */
void printCurrentLevel(node* root, int level)
{
    if (root == NULL)
        return;
    if (level == 1)
        cout << root->data << " ";
    else if (level > 1) {
        printCurrentLevel(root->left, level - 1);
        printCurrentLevel(root->right, level - 1);
    }
}
 
/* Compute the "height" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
int height(node* node)
{
    if (node == NULL)
        return 0;
    else {
        /* compute the height of each subtree */
        int lheight = height(node->left);
        int rheight = height(node->right);
 
        /* use the larger one */
        if (lheight > rheight) {
            return (lheight + 1);
        }
        else {
            return (rheight + 1);
        }
    }
}
 
/* Helper function that allocates
a new node with the given data and
NULL left and right pointers. */
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
 
    return (Node);
}
 
/* Driver code*/
int main()
{
    node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
 
    cout << "Level Order traversal of binary tree is \n";
    printLevelOrder(root);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: linear
/* C++ program to print level
    order traversal using STL */
#include <bits/stdc++.h>
using namespace std;
 
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
 
// Iterative method to find height of Binary Tree
void printLevelOrder(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // Create an empty queue for level order traversal
    queue<Node*> q;
 
    // Enqueue Root and initialize height
    q.push(root);
 
    while (q.empty() == false) {
        // Print front of queue and remove it from queue
        Node* node = q.front();
        cout << node->data << " ";
        q.pop();
 
        /* Enqueue left child */
        if (node->left != NULL)
            q.push(node->left);
 
        /*Enqueue right child */
        if (node->right != NULL)
            q.push(node->right);
    }
}
 
// Utility function to create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in above diagram
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
 
    cout << "Level Order traversal of binary tree is \n";
    printLevelOrder(root);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for recursive level
// order traversal in spiral form
#include <bits/stdc++.h>
using namespace std;
 
// A binary tree node has data,
// pointer to left child and a
// pointer to right child
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
// Function prototypes
void printGivenLevel(struct node* root, int level, int ltr);
int height(struct node* node);
struct node* newNode(int data);
 
// Function to print spiral traversal of a tree
void printSpiral(struct node* root)
{
    int h = height(root);
    int i;
 
    // ltr -> Left to Right. If this variable
    // is set,then the given level is traversed
    // from left to right.
    bool ltr = false;
    for (i = 1; i <= h; i++) {
        printGivenLevel(root, i, ltr);
 
        // Revert ltr to traverse next
        // level in opposite order
        ltr = !ltr;
    }
}
 
// Print nodes at a given level
void printGivenLevel(struct node* root, int level, int ltr)
{
    if (root == NULL)
        return;
    if (level == 1)
        cout << root->data << " ";
 
    else if (level > 1) {
        if (ltr) {
            printGivenLevel(root->left, level - 1, ltr);
            printGivenLevel(root->right, level - 1, ltr);
        }
        else {
            printGivenLevel(root->right, level - 1, ltr);
            printGivenLevel(root->left, level - 1, ltr);
        }
    }
}
 
// Compute the "height" of a tree -- the number of
// nodes along the longest path from the root node
// down to the farthest leaf node.
int height(struct node* node)
{
    if (node == NULL)
        return 0;
    else {
 
        // Compute the height of each subtree
        int lheight = height(node->left);
        int rheight = height(node->right);
 
        // Use the larger one
        if (lheight > rheight)
            return (lheight + 1);
        else
            return (rheight + 1);
    }
}
 
// Helper function that allocates a new
// node with the given data and NULL left
// and right pointers.
struct node* newNode(int data)
{
    node* newnode = new node();
    newnode->data = data;
    newnode->left = NULL;
    newnode->right = NULL;
 
    return (newnode);
}
 
// Driver code
int main()
{
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(7);
    root->left->right = newNode(6);
    root->right->left = newNode(5);
    root->right->right = newNode(4);
    printf("Spiral Order traversal of "
           "binary tree is \n");
 
    printSpiral(root);
 
    return 0;
}
 
// This code is contributed by samrat2825

Time complexity: quadratic
Space complexity: linear
// C++ implementation of a O(n) time method for spiral order
// traversal
#include <iostream>
#include <stack>
using namespace std;
 
// Binary Tree node
struct node {
    int data;
    struct node *left, *right;
};
 
void printSpiral(struct node* root)
{
    if (root == NULL)
        return; // NULL check
 
    // Create two stacks to store alternate levels
    stack<struct node*>
        s1; // For levels to be printed from right to left
    stack<struct node*>
        s2; // For levels to be printed from left to right
 
    // Push first level to first stack 's1'
    s1.push(root);
 
    // Keep printing while any of the stacks has some nodes
    while (!s1.empty() || !s2.empty()) {
        // Print nodes of current level from s1 and push
        // nodes of next level to s2
        while (!s1.empty()) {
            struct node* temp = s1.top();
            s1.pop();
            cout << temp->data << " ";
 
            // Note that is right is pushed before left
            if (temp->right)
                s2.push(temp->right);
            if (temp->left)
                s2.push(temp->left);
        }
 
        // Print nodes of current level from s2 and push
        // nodes of next level to s1
        while (!s2.empty()) {
            struct node* temp = s2.top();
            s2.pop();
            cout << temp->data << " ";
 
            // Note that is left is pushed before right
            if (temp->left)
                s1.push(temp->left);
            if (temp->right)
                s1.push(temp->right);
        }
    }
}
 
// A utility function to create a new node
struct node* newNode(int data)
{
    struct node* node = new struct node;
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
int main()
{
    struct node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(7);
    root->left->right = newNode(6);
    root->right->left = newNode(5);
    root->right->right = newNode(4);
    cout << "Spiral Order traversal of binary tree is \n";
    printSpiral(root);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation of above approach
#include <iostream>
#include <stack>
using namespace std;
 
// Binary Tree node
struct Node {
    int key;
    struct Node *left, *right;
};
 
void spiralPrint(struct Node* root)
{
    // Declare a deque
    deque<Node*> dq;
 
    // Insert the root of the tree into the deque
    dq.push_back(root);
 
    // Create a  variable that will switch in each iteration
    bool reverse = true;
 
    // Start iteration
    while (!dq.empty()) {
 
        // Save the size of the deque here itself, as in
        // further steps the size of deque will frequently
        // change
        int n = dq.size();
 
        // If we are printing left to right
        if (!reverse) {
 
            // Iterate from left to right
            while (n--) {
 
                // Insert the child from the back of the
                // deque Left child first
                if (dq.front()->left != NULL)
                    dq.push_back(dq.front()->left);
 
                if (dq.front()->right != NULL)
                    dq.push_back(dq.front()->right);
 
                // Print the current processed element
                cout << dq.front()->key << "  ";
                dq.pop_front();
            }
            // Switch reverse for next traversal
            reverse = !reverse;
        }
        else {
 
            // If we are printing right to left
            // Iterate the deque in reverse order and insert
            // the children from the front
            while (n--) {
                // Insert the child in the front of the
                // deque Right child first
                if (dq.back()->right != NULL)
                    dq.push_front(dq.back()->right);
 
                if (dq.back()->left != NULL)
                    dq.push_front(dq.back()->left);
 
                // Print the current processed element
                cout << dq.back()->key << "  ";
                dq.pop_back();
            }
            // Switch reverse for next traversal
            reverse = !reverse;
        }
    }
}
 
// A utility function to create a new node
struct Node* newNode(int data)
{
    struct Node* node = new struct Node;
    node->key = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
int main()
{
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(7);
    root->left->right = newNode(6);
    root->right->left = newNode(5);
    root->right->right = newNode(4);
    cout << "Spiral Order traversal of binary tree is :\n";
    spiralPrint(root);
 
    return 0;
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear