Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- ////////////////////////////////////////////////////
- class Node
- {
- public:
- char data;
- Node* left,
- * right;
- Node() : data(-1), left(NULL), right(NULL) {};
- } ;
- ////////////////////////////////////////////////////
- class Tree
- {
- private:
- Node* btree;
- bool isNULL_(Node* leaf);
- char rootBT_(Node* leaf);
- Node* right_(Node* leaf);
- Node* left_(Node* leaf);
- Node* constrBT_(const char& data, Node*& left, Node*& right);
- void destroy_(Node* leaf);
- void prefix_(Node* leaf);
- Node* enterBT_();
- void displayBT_(Node* leaf, int n);
- void prefix_();
- public:
- Tree() : btree(NULL) {};
- ~Tree() { destroy_(btree); }
- void enterBT();
- void displayBT(int n);
- void prefix();
- } ;
- ////////////////////////////////////////////////////
- //Tree class PRIVATE SECTION
- bool Tree::isNULL_(Node* leaf)
- {
- return (leaf == NULL);
- }
- char Tree::rootBT_(Node* leaf)
- {
- if (leaf == NULL)
- {
- std::cerr << "Error: RootBT(null) \n";
- exit(1);
- }
- else
- return leaf->data;
- }
- Node* Tree::left_(Node* leaf)
- {
- if (leaf == NULL)
- {
- std::cerr << "Error: Left(null) \n";
- exit(1);
- }
- else
- leaf->left;
- }
- Node* Tree::right_(Node* leaf)
- {
- if (leaf == NULL)
- {
- std::cerr << "Error: Left(null) \n";
- exit(1);
- }
- else
- leaf->right;
- }
- Node* Tree::constrBT_(const char& data, Node*& left, Node*& right)
- {
- Node* temp = new Node;
- if (temp != NULL)
- {
- temp->data = data;
- temp->left = left;
- temp->right = right;
- return temp;
- }
- else
- {
- std::cerr << "Memory not enough\n";
- exit(1);
- }
- }
- void Tree::destroy_(Node* leaf)
- {
- if (isNULL_(leaf) == 0)
- {
- destroy_(leaf->left);
- destroy_(leaf->right);
- delete leaf;
- leaf = NULL;
- }
- }
- Node* Tree::enterBT_()
- {
- char data;
- Node* leaf = new Node;
- std::cin >> data;
- if (data == '/')
- return NULL;
- else
- {
- leaf->left = enterBT_();
- leaf->right = enterBT_();
- return constrBT_(data, leaf->left, leaf->right);
- }
- }
- void Tree::displayBT_(Node* leaf, int n)
- {
- // n - уровень узла
- if (leaf != NULL)
- {
- std::cout << ' ' << rootBT_(leaf);
- if (!isNULL_(right_(leaf)))
- {
- displayBT_(right_(leaf), n + 1);
- }
- else
- std::cout << std::endl; //вниз
- if(!isNULL_(left_(leaf)))
- {
- for (int i = 1; i <= n; ++i)
- std::cout << " "; // вправо
- displayBT_(left_(leaf), n + 1);
- }
- }
- else
- { };
- }
- void Tree::prefix_(Node* leaf)
- {
- if (!isNULL_(leaf))
- {
- prefix_(left_(leaf));
- prefix_(right_(leaf));
- }
- }
- ////////////////////////////////////////////////////
- //Tree class PUBLIC SECTION
- void Tree::enterBT()
- {
- btree = enterBT_();
- }
- void Tree::displayBT(int n)
- {
- displayBT_(btree, n);
- }
- void Tree::prefix()
- {
- prefix_(btree);
- }
Advertisement
Add Comment
Please, Sign In to add comment