Cod sursa(job #2902830)

Utilizator Bogdan197Putineanu Bogdan Bogdan197 Data 16 mai 2022 20:53:30
Problema Schi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <fstream>
#include <algorithm>
#include <iostream>

using namespace std;

struct TreeNode {
    static int count;
    static int left_nodes_count;
    int order;
    int left_subtree_size;
    TreeNode* left, * right;


    TreeNode();
    TreeNode* Insert(TreeNode*, int);
    void Inorder(TreeNode*);
};
TreeNode::TreeNode()
{
    count++;
    order = count;
    left = NULL;
    right = NULL;
    left_subtree_size = 0;
}


TreeNode* TreeNode::Insert(TreeNode* root, int value)
{
    if (!root) {
        return new TreeNode();
    }

    if (value <= root->left_subtree_size + left_nodes_count + 1) {
        left_subtree_size++;
        root->left = Insert(root->left, value);
    }
    else {
        left_nodes_count += left_subtree_size + 1;
        root->right = Insert(root->right, value);
    }
    return root;
}


void TreeNode::Inorder(TreeNode* root)
{
    if (!root) {
        return;
    }
    Inorder(root->left);
    cout << root->order << endl;
    Inorder(root->right);
}

int TreeNode::count = 0;
int TreeNode::left_nodes_count;

int main()
{
	ifstream f("schi.in");
	ofstream g("schi.out");
	int nr_participanti;
	int pozitie;
	f >> nr_participanti;
    TreeNode* root = NULL;
	for (int i = 0; i < nr_participanti; i++)
	{
        TreeNode::left_nodes_count = 0;
		f >> pozitie;
        root = root->Insert(root, pozitie);
	}
    root->Inorder(root);

}