Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <string>
- using namespace std;
- /* ========================
- binary Tree class - just mention about author
- by Tooster
- ========================
- */
- template<class T>
- class binTree {
- // #===---
- // Struktura węzła
- // #===---
- struct Node {
- T data;
- Node *left;
- Node *right;
- // #===---
- // konstruktur
- // #===---
- Node(T v = 0) : data(v), left(NULL), right(NULL) {}
- };
- Node* root;
- long long size_v = 0;
- long long sum_v = 0; // !!!
- // #===---
- // metody:
- // isEmpty() OK = bool - czy puste
- // size() OK = long long - rozmiar
- // sum() OK !!! = long long - zwraca sume (niebezpieczne ze względu na szablon)
- // add() OK = --- - dodaje do drzewa
- // left() OK = T - zwraca najmniejszy element
- // right() OK = T - zwraca najwiekszy element
- // remLeft() OK = T - usuwa najmniejszy
- // remRight() OK = T - usuwa najwiekszy
- // #===---
- public:
- binTree() { root = NULL; }
- bool isEmpty() {
- if (root == NULL) return true;
- return false;
- }
- long long size()const {
- return (long long)size_v;
- }
- // !!!
- long long sum()const {
- return (long long)sum_v;
- }
- void add(T v) {
- size_v++;
- sum_v += v; // !!!
- if (isEmpty()) {
- root = new Node;
- root->data = v;
- return;
- }
- Node *tmp = root;
- Node *newNode = new Node;
- newNode->data = v;
- while (true) {
- if (v < tmp->data) {
- if (tmp->left == NULL) { tmp->left = newNode; return; }
- else tmp = tmp->left;
- }
- else {
- if (tmp->right == NULL) { tmp->right = newNode; return; }
- else tmp = tmp->right;
- }
- }
- }
- T left() {
- if (isEmpty()) return NULL;
- Node* tmp = root;
- while (tmp->left != NULL) {
- tmp = tmp->left;
- }
- return tmp->data;
- }
- T right() {
- if (isEmpty()) return NULL;
- Node* tmp = root;
- while (tmp->right != NULL) {
- tmp = tmp->right;
- }
- return tmp->data;
- }
- void remLeft() {
- if (isEmpty()) return;
- Node* tmp = root;
- Node* parent = tmp;
- // #===
- // ustawienie nowego korzenia - jeśli okarze się, że korzeń nie ma lewej gałęzi
- // #===
- if (root->left == NULL) {
- root = tmp->right;
- size_v--;
- sum_v -= tmp->data; // !!!
- delete tmp;
- return;
- }
- // #===---
- // przepina gałąź
- // #===---
- while (tmp->left != NULL) {
- parent = tmp;
- tmp = tmp->left;
- }
- parent->left = tmp->right;
- size_v--;
- sum_v -= tmp->data; // !!!
- delete tmp;
- return;
- }
- void remRight() {
- if (isEmpty()) return;
- Node* tmp = root;
- Node* parent = tmp;
- if (root->right == NULL) {
- root = tmp->left;
- size_v--;
- sum_v -= tmp->data; // !!!
- delete tmp;
- return;
- }
- while (tmp->right != NULL) {
- parent = tmp;
- tmp = tmp->right;
- }
- parent->right = tmp->left;
- size_v--;
- sum_v -= tmp->data; // !!!
- delete tmp;
- return;
- }
- };
- // #===---
- // Program kontrolny
- // #===---
- int main() {
- binTree<int> drzewo;
- printf("# dommands: add <a> / left / right /size /sum / remL / remR/ info/ empty/ stop #\n");
- string cmd;
- int a;
- do {
- printf("> ");
- cin >> cmd;
- if (cmd == "info") {
- cout << " ============\n";
- printf(" left: %d\n", drzewo.left());
- printf(" right: %d\n", drzewo.right());
- printf(" size: %d\n", drzewo.size());
- printf(" sum: %d\n", drzewo.sum());
- }
- else if (cmd == "add") {
- cin >> a;
- drzewo.add(a);
- }
- else if (cmd == "empty") printf(" empty= %d\n", drzewo.isEmpty());
- else if (cmd == "left") printf(" left= %d\n", drzewo.left());
- else if (cmd == "right") printf(" right= %d\n", drzewo.right());
- else if (cmd == "size") printf(" size= %d\n", drzewo.size());
- else if (cmd == "sum") printf(" sum= %d\n", drzewo.sum());
- else if (cmd == "remL") { printf(" removed= %d\n", drzewo.left()); drzewo.remLeft(); }
- else if (cmd == "remR") { printf(" removed= %d\n", drzewo.right()); drzewo.remRight(); }
- else printf("# Nieznana komenda\n");
- } while (cmd != "stop");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement