Tree Algorithm

The Tree Algorithm is a popular machine learning technique that is used to solve a variety of problems, such as classification, regression, and decision-making. It is based on the concept of hierarchically partitioning the input space into smaller regions, using a tree-like structure, and then making decisions or predictions based on the majority of data points in each region. The tree consists of nodes and branches, where each internal node represents a decision or split based on an attribute or feature, and each leaf node represents an outcome or a class label. The process of constructing a decision tree involves recursively splitting the input data based on the most relevant attribute, which is selected using various criteria such as information gain or Gini impurity. One of the main advantages of the tree algorithm is its interpretability, as the decision-making process can be easily visualized and understood by humans. This makes it an attractive choice for applications where explainability is important, such as in medical diagnosis, finance, and marketing. Additionally, tree algorithms can handle both numerical and categorical data, making them versatile in handling different types of datasets. However, tree algorithms can be prone to overfitting, especially if the tree is allowed to grow too deep, leading to complex models that do not generalize well to new data. To overcome this issue, techniques such as pruning, ensemble methods like random forests, and boosting algorithms like gradient boosting machines can be employed to improve the performance and generalization capabilities of tree-based models.
#include <iostream>
#include <list>
using namespace std;

struct node
{
    int val;
    node *left;
    node *right;
};

void CreateTree(node *curr, node *n, int x, char pos)
{
    if (n != NULL)
    {
        char ch;
    cout << "\nLeft or Right of " << n->val << " : ";
    cin >> ch;
    if (ch == 'l')
        CreateTree(n, n->left, x, ch);
    else if (ch == 'r')
        CreateTree(n, n->right, x, ch);
    }
    else
    {
        node *t = new node;
        t->val = x;
        t->left = NULL;
        t->right = NULL;
        if (pos == 'l')
        {
            curr->left = t;
        }
        else if (pos == 'r')
        {
            curr->right = t;
        }
    }
}

void BFT(node *n)
{
    list<node*> queue;

    queue.push_back(n);

    while(!queue.empty())
    {
        n = queue.front();
        cout << n->val << "  ";
        queue.pop_front();

        if(n->left != NULL)
            queue.push_back(n->left);
        if(n->right != NULL)
            queue.push_back(n->right);
    }
}

void Pre(node *n)
{
    if (n != NULL)
    {
        cout << n->val << "  ";
        Pre(n->left);
        Pre(n->right);
    }
}

void In(node *n)
{
    if (n != NULL)
    {
        In(n->left);
        cout << n->val << "  ";
        In(n->right);
    }
}

void Post(node *n)
{
    if (n != NULL)
    {
        Post(n->left);
        Post(n->right);
        cout << n->val << "  ";
    }
}

int main()
{
    int value;
    int ch;
    node *root = new node;
    cout << "\nEnter the value of root node :";
    cin >> value;
    root->val = value;
    root->left = NULL;
    root->right = NULL;
    do
    {
        cout << "\n1. Insert";
        cout << "\n2. Breadth First";
        cout << "\n3. Preorder Depth First";
        cout << "\n4. Inorder Depth First";
        cout << "\n5. Postorder Depth First";

        cout << "\nEnter Your Choice : ";
        cin >> ch;
        switch (ch)
        {
        case 1:
            int x;
            char pos;
            cout << "\nEnter the value to be Inserted : ";
            cin >> x;
            cout << "\nLeft or Right of Root : ";
            cin >> pos;
            if (pos == 'l')
                CreateTree(root, root->left, x, pos);
            else if (pos == 'r')
                CreateTree(root, root->right, x, pos);
            break;
        case 2:
            BFT(root);
            break;
        case 3:
            Pre(root);
            break;
        case 4:
            In(root);
            break;
        case 5:
            Post(root);
            break;
        }
    } while (ch != 0);
}

LANGUAGE:

DARK MODE: