Cod sursa(job #1255868)

Utilizator fhandreiAndrei Hareza fhandrei Data 5 noiembrie 2014 13:04:59
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 3.7 kb
// FMI - Grupa 135 - Semigrupa 2 - Hareza Andrei
// Include
#include <fstream>
using namespace std;

// Clase
class AVL;

class AVLNode
{
private:
	int value;
	AVLNode *leftSon, *rightSon, *father;
	int height;
	
	AVLNode()
	{
		value = 0;
		leftSon = rightSon = '\0';
		height = 0;
	}
	
	int balanceFactor()
	{
		return (leftSon? leftSon->height : -1) - (rightSon? rightSon->height : -1);
	}
	
	void resetHeight()
	{
		if(!leftSon && !rightSon)
		{
			height = 0;
			return;
		}
		if(!leftSon)
		{
			height = rightSon->height + 1;
			return;
		}
		if(!rightSon)
		{
			height = leftSon->height + 1;
			return;
		}
		
		max(leftSon->height, rightSon->height) + 1;
	}
	
	void insert(int value)
	{
		if(value <= this->value)
		{
			if(this->leftSon)
			{
				this->leftSon->insert(value);
				if(balanceFactor() == -2)
				{
					if(rightSon && rightSon->balanceFactor() == 1)
						rightSon->rightRotate();
					this->leftRotate();
				}
				
				if(balanceFactor() == 2)
				{
					if(leftSon && leftSon->balanceFactor() == -1)
						leftSon->leftRotate();
					this->rightRotate();
				}
			}
			else
			{
				this->leftSon = new AVLNode;
				this->leftSon->value = value;
				this->leftSon->father = this;
			}
		}
		else
		{
			if(this->rightSon)
			{
				this->rightSon->insert(value);
				if(balanceFactor() == -2)
				{
					if(rightSon && rightSon->balanceFactor() == 1)
						rightSon->rightRotate();
					this->leftRotate();
				}
				
				if(balanceFactor() == 2)
				{
					if(leftSon && leftSon->balanceFactor() == -1)
						leftSon->leftRotate();
					this->rightRotate();
				}
			}
			else
			{
				this->rightSon = new AVLNode;
				this->rightSon->value = value;
				this->rightSon->father = this;
			}
		}
		resetHeight();
	}
	
	void printSorted(ostream &file)
	{
		if(this->leftSon)
			this->leftSon->printSorted(file);
		file << this->value << ' ';
		if(this->rightSon)
			this->rightSon->printSorted(file);
	}
	
	void leftRotate()
	{
		AVLNode *node = this->rightSon;
		
		if(this->isLeftSon())
			this->father->leftSon = node;
		else
			this->father->rightSon = node;
		node->father = this->father;
		
		this->rightSon = node->leftSon;
		if(this->rightSon)
			this->rightSon->father = this;
		
		node->leftSon = this;
		this->father = node;
		
		this->resetHeight();
		node->resetHeight();
	}
	
	void rightRotate()
	{
		AVLNode *node = this->leftSon;
		
		if(this->isLeftSon())
			this->father->leftSon = node;
		else
			this->father->rightSon = node;
		node->father = this->father;
		
		this->leftSon = node->rightSon;
		if(this->leftSon)
			this->leftSon->father = this;
		
		node->rightSon = this;
		this->father = node;
		
		this->resetHeight();
		node->resetHeight();
	}
	
	bool isLeftSon()
	{
		return this == this->father->leftSon;
	}
	
	friend class AVL;
};

class AVL
{
private:
	AVLNode *abstractRoot;
	int size;
public:
	AVL()
	{
		abstractRoot = new AVLNode;
		size = 0;
	}
	void insert(int value)
	{
		if(!size++)
		{
			abstractRoot->leftSon = new AVLNode;
			abstractRoot->leftSon->father = abstractRoot;
			abstractRoot->leftSon->value = value;
		}
		else
			abstractRoot->leftSon->insert(value);
	}
	
	void printSorted(ostream &file)
	{
		if(!size)
			return;
		
		abstractRoot->leftSon->printSorted(file);
	}
	
	int getSize()
	{
		return size;
	}
	
} tree;

// Variabile
ifstream in("algsort.in");
ofstream out("algsort.out");

int num;

// Main
int main()
{
	in >> num;
	
	int value;
	for(int i=1 ; i<=num ; ++i)
	{
		in >> value;
		tree.insert(value);
	}
	
	tree.printSorted(out);
	
	in.close();
	out.close();
	return 0;
}