Skip to content

Instantly share code, notes, and snippets.

@senapk
Last active May 31, 2016 21:24
Show Gist options
  • Save senapk/b3f635ec0a196f77c27610c2ac907cb8 to your computer and use it in GitHub Desktop.
Save senapk/b3f635ec0a196f77c27610c2ac907cb8 to your computer and use it in GitHub Desktop.
Árvore binária genérica com template e iteradores
#ifndef BTREE_H
#define BTREE_H
#include <iostream>
using namespace std;
template <class T>
class bnode
{
public:
T value;
bnode<T> * left {nullptr};
bnode<T> * right{nullptr};
bnode(){
}
bnode(T value){
this->value = value;
}
};
template <class T>
class btree {
public:
bnode<T> * root{nullptr};
btree(){
}
btree(const btree<T>& other){
root = clone(other.root);
}
btree(string serial){
stringstream aux(serial);
root = strclone(aux);
}
bnode<T> * strclone(istream & fluxo){
string svalue;
fluxo >> svalue;
if(svalue == "#")
return nullptr;
T value;
stringstream(svalue) >> value;
auto node = new bnode<T>(value);
node->left = strclone(fluxo);
node->right = strclone(fluxo);
return node;
}
bnode<T> * clone(bnode<T> * other){
if(other == nullptr)
return nullptr;
auto node = new bnode<T>(other->value);
node->left = clone(other->left);
node->right = clone(other->right);
return node;
}
~btree(){
clear(root);
root = nullptr;
}
void clear(bnode<T> * n){
if(n == nullptr)
return;
clear(n->left);
clear(n->right);
delete(n);
}
void show(bnode<T> * n){
if(n == nullptr)
return;
cout << n->value << " ";
show(n->left);
show(n->right);
}
private:
int rsize(bnode<T> * node){
if(node == nullptr)
return 0;
return 1 + rsize(node->left) + rsize(node->right);
}
public:
int rsize(){
return rsize(root);
}
T soma(bnode<T> * node, T empty){
if(node == nullptr)
return empty;
return node->value + soma(node->left, empty) +
soma(node->right, empty);
}
bnode<T> * find(bnode<T> * node, T value){
if(node == nullptr)
return nullptr;
if(node->value == value)
return node;
auto nodele = find(node->left, value);
if(nodele == nullptr)
return find(node->right, value);
return nodele;
}
bool node_equals(bnode<T> * node, bnode<T> * other_node){
if(node == nullptr && other_node == nullptr)
return true;
if((node != nullptr) && (other_node == nullptr))
return false;
if((node == nullptr) && (other_node != nullptr))
return false;
if(node->value != other_node->value)
return false;
return node_equals(node->left, other_node->left) &&
node_equals(node->right, other_node->right);
}
bool equals(btree other){
return node_equals(root, other.root);
}
T min(bnode<T> * node){
if(node->left == nullptr && node->right == nullptr)
return node->value;
T minimo = node->value;
if(node->left != nullptr)
minimo = std::min(minimo, min(node->left));
if(node->right != nullptr)
minimo = std::min(minimo, min(node->right));
return minimo;
}
int altura(bnode<T> * node){
if(node == nullptr)
return -1;
int fr = altura(node->right);
int fl = altura(node->left);
return 1 + std::max(fr, fl);
}
void fshow(bnode<T> * node, int nivel, ostream & fout){
int tab = 4;
if(node == nullptr){
fout << string(tab * nivel, ' ') << "#" << endl;
return;
}
fout << string(tab * nivel, ' ') << node->value << endl;
fshow(node->left, nivel + 1, fout);
fshow(node->right, nivel + 1, fout);
}
void serialize(bnode<T> * n, ostream & fout){
if(n == nullptr){
fout << "# ";
return;
}
fout << n->value << " ";
serialize(n->left, fout);
serialize(n->right, fout);
}
void bshow(bnode<T> * node, string her, ostream & fout){
int tam = her.size();
for(int i = 0; i < tam - 1; i++){
if(her[i] == 'l')
fout << "│" << " ";
else
fout << " " << " ";
}
if(her != ""){
if(her.back() == 'l')
fout << "├───";
else
fout << "└───";
}
if(node == nullptr){
fout << "#" << endl;
return;
}
fout << node->value << endl;
bshow(node->left , her + "l", fout);
bshow(node->right, her + "r", fout);
}
};
#endif // BTREE_H
#include <iostream>
#include "btree.h"
using namespace std;
void gambiFill(btree<int> & t){
t.root = new bnode<int>(1);
t.root->left = new bnode<int>(3);
t.root->right = new bnode<int>(2);
t.root->left->left = new bnode<int>(5);
t.root->left->right = new bnode<int>(7);
t.root->right->right = new bnode<int>(4);
t.root->right->right->left = new bnode<int>(9);
}
void gambiFill(btree<string> & t){
t.root = new bnode<string>("pai");
t.root->left = new bnode<string>("flef");
t.root->right = new bnode<string>("frit");
t.root->left->left = new bnode<string>("nlef");
}
#include <sstream>
#include <fstream>
int main(){
btree<int> tree;
gambiFill(tree);
stringstream aux;
tree.fshow(tree.root, 0, aux);
cout << aux.str();
btree<int> other(aux.str());
other.bshow(other.root, "", cout);
// cout << stree.soma(stree.root, "") << endl;
//tree.show(tree.root);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment