Created
June 23, 2019 12:13
-
-
Save daniilgri/1af0e4982d9cbce14c4a07cf1b0fe44c to your computer and use it in GitHub Desktop.
Tree cpp example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "Tree.h" | |
int main() | |
{ | |
Tree* tree1 = new Tree("C:\\Users\\ozzy\\source\\repos\\three\\three\\Data.txt", "tree1"); | |
tree1->view_all(); | |
cout << "Amount of even nodes: " << tree1->countEven() << endl; | |
delete tree1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Tree.h" | |
#include <cmath> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <sstream> | |
using namespace std; | |
Tree::Tree(string name, int data, int key) | |
{ | |
this->name = name; | |
Node* node = new Node(data, key); | |
root = node; | |
} | |
Tree::Tree(string fpath, string name) | |
{ | |
this->name = name; | |
ifstream fin(fpath); | |
if (!fin.is_open()) { | |
} | |
else { | |
string str; | |
int data, key; | |
int i = 0; | |
while (!fin.eof()) { | |
str = ""; | |
getline(fin, str); | |
if (i == 0) { | |
istringstream{ str } >> data >> key; | |
Node* t = new Node(data, key); | |
this->root = t; | |
} | |
else { | |
istringstream{ str } >> data >> key; | |
this->add(data, key); | |
} | |
i++; | |
} | |
fin.close(); | |
} | |
} | |
Node* Tree::find(int fnd) | |
{ | |
return find(fnd, root); | |
} | |
Node* Tree::find(int fnd, Node* dr) // поиск в заданной ветке root если веэде | |
{ | |
if (!dr) return NULL; | |
if (dr->key == fnd) return dr; | |
if (dr->key > fnd) return find(fnd, dr->l); | |
else if (dr->key < fnd) return find(fnd, dr->r); | |
} | |
Node* Tree::add(int data, int key) | |
{ | |
int find = 0; | |
Node* prev = NULL; | |
Node* t = root; // Признак поиска | |
while (t && !find) { | |
prev = t; | |
if (key == t->key) { | |
find = 1; // Ключи должны быть уникальны | |
} | |
else { | |
if (key < t->key) { | |
t = t->l; | |
} | |
else { | |
t = t->r; | |
} | |
} | |
} | |
if (!find) { // Нашли место с адресом prev | |
t = new Node(data, key); // Создаем новый узел | |
if (key < prev->key) // и присоединяем его, либо | |
prev->l = t; // на левую ветвь, | |
else prev->r = t; // либо на правую ветвь | |
return t; | |
} | |
else { | |
return NULL; | |
} | |
} | |
Node::Node(int inf, int key) | |
{ | |
this->inf = inf; | |
this->key = key; | |
l = r = NULL; | |
} | |
void Tree::view_all() | |
{ | |
view(root, 0); | |
} | |
void Tree::view(Node * t, int lv) | |
{ | |
if (t) { | |
view(t->r, lv + 1); // Вывод правого поддерева | |
for (int i = 0; i < lv; i++) cout << " "; | |
cout << " " << t->key << endl; | |
view(t->l, lv + 1); // Вывод левого поддерева | |
} | |
} | |
Tree::~Tree() | |
{ | |
del_vet(root->r); | |
del_vet(root->l); | |
delete root; | |
} | |
// удаление ветвей указанного узла | |
void Tree::del_vet(Node * d) | |
{ | |
if (d) { | |
del_vet(d->r); d->r = NULL; | |
del_vet(d->l); d->l = NULL; | |
delete d; | |
} | |
} | |
int Tree::countEven(Node* node) | |
{ | |
int c = 0; | |
if (node) { | |
c += countEven(node->r); // Вывод правого поддерева | |
if ((node->key % 2) == 0) { | |
c++; | |
} | |
c += countEven(node->l); // Вывод левого поддерева | |
} | |
return c; | |
} | |
int Tree::countEven() | |
{ | |
return countEven(root); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Node | |
{ | |
public: | |
int key; | |
int inf; | |
Node* l, * r; | |
Node(int data, int key); | |
}; | |
class Tree | |
{ | |
private: | |
string name; | |
Node* root; | |
public: | |
Tree(string name, int data, int key); | |
Tree(string fpath, string name); | |
~Tree(); | |
Node* add(int i, int n_key); | |
void del_vet(Node* dr); | |
void view(Node* t, int lv); | |
void view_all(); | |
Node* find(int fnd); | |
Node* find(int fnd, Node* dr); | |
int countEven(Node* t); | |
int countEven(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment