search

Напишите бинарное дерево


/*
Athor Igor Dombrovsky
Realization binary tree
*/
#include <iostream>
using namespace std;
#define TEXT_COLOR_CMD_WINDOWS system("color 0A"); 
#define STOP_CMD_WINDOWS system("pause"); 

template<class T>
class Tree
{
public:
	Tree();
	~Tree();
	void addElement(T data);
	void showMinMax(Tree * root);
	void showMaxMin(Tree * root);
	int searchElement(Tree * root, T element);
public:
	Tree * root;
	Tree * right;
	Tree * left;
	T data;
	void searchPlace(Tree * root, Tree * temp, T data);
};

template<class T>
Tree<T>::Tree() : root(NULL), right(NULL), left(NULL) // CONSTRUCTOR
{
	//DEFAULT
}

template<class T>
Tree<T>::~Tree() // DESTRUCTOR
{
	root = NULL;
	right = NULL;
	left = NULL;
}

template<class T>
void Tree<T>::addElement(T data) // AD ELEMENT AT TREE
{
	Tree * temp = new Tree;
	temp->data = data;
	temp->left = NULL;
	temp->right = NULL;

	if (root == NULL)
	{
		root = temp;
	}
	else
	{
		searchPlace(root, temp, data);
	}
}

template<class T>
void Tree<T>::searchPlace(Tree<T> * root, Tree<T> * temp, T data) // PRIVATE RECURSION SEARCH PLACE IN TREE
{
	Tree * tmp = root;
	if (tmp->data < data && tmp->right == NULL)
	{
		tmp->right = temp;
	}
	else if (tmp->data > data && tmp->left == NULL)
	{
		tmp->left = temp;
	}
	else
	{
		if (tmp->data < data)
		{
			searchPlace(tmp->right, temp, data); // RECURSION CALL
		}
		if (tmp->data > data)
		{
			searchPlace(tmp->left, temp, data); // RECURSION CALL
		}
	}
}

template<class T>
void Tree<T>::showMinMax(Tree<T> * root) // SHOW MIN MAX
{
	if (root != NULL)
	{
		showMinMax(root->left);
		cout << "LEFT ==>> Address = " << root << " Data = " << root->data << "\n";
		showMinMax(root->right);
	}
}

template<class T>
void Tree<T>::showMaxMin(Tree<T> * root) // SHOW MAX MIN
{
	if (root != NULL)
	{
		showMaxMin(root->right);
		cout << "RIGHT ==>> Address = " << root << " Data = " << root->data << "\n";
		showMaxMin(root->left);
	}
}

template<class T>
int Tree<T>::searchElement(Tree<T>* root, T element) // SEARCH ELEMENT IN TREE RETURN ONE OR ZERO
{
	if (root->data > element)
	{
		if (root->data == element) return 1;
		if (root->left != NULL) searchElement(root->left, element);
		else return 0; 
	}
	else 
	{
		if (root->data == element) return 1;
		if (root->right != NULL) searchElement(root->right, element);
		else return 0;
	}
}

int main() { // MAIN
	TEXT_COLOR_CMD_WINDOWS
		Tree<int> t; // TREE
	t.addElement(8);
	t.addElement(4);
	t.addElement(6);
	t.addElement(5);
	t.addElement(7);
	t.addElement(2);
	t.addElement(3);
	t.addElement(1);
	t.addElement(15);
	t.addElement(25);
	t.addElement(11);
	t.addElement(10);
	t.addElement(55);
	//--------------------------------------------------------------
	cout << "//****************************************" << endl;
	cout << "// SHOW MIN MAX" << endl;
	cout << "//****************************************" << endl;
	t.showMinMax(t.root);
	cout << endl;
	//--------------------------------------------------------------
	cout << "//****************************************" << endl;
	cout << "// SHOW MAX MIN" << endl;
	cout << "//****************************************" << endl;
	t.showMaxMin(t.root);
	cout << endl;
	//--------------------------------------------------------------
	cout << "//****************************************" << endl;
	int found = 0, key = 25; 
	found = t.searchElement(t.root, key);
	cout << "Key search = " << key << endl; 
	cout << "Found = " << found << endl;

	cout << "//****************************************" << endl;
	cout << "// END PROGRAM "							 << endl; 
	cout << "//****************************************" << endl;
	STOP_CMD_WINDOWS
	return 0;
}	

array