/ Published in: C++
Expand |
Embed | Plain Text
#include <string> #include <vector> struct element { std::string label; element *left,*right; element() { this->left = NULL; this->right = NULL; } }; typedef element *node; typedef element btree; node LeftChildB(node n, btree *T) { return n->left; } node RightChildB(node n, btree *T) { return n->right; } std::string LabelB(node n, btree *T) { return n->label; } void ChangeLabelB(std::string x, node n, btree *T) { n->label = x; } node RootB(btree *T) { return T; } bool CreateLeftB(std::string x, node n, btree *T) { if (!LeftChildB(n, T)) { node newborn = new element; newborn->label = x; n->left = newborn; return true; } else { return false; } } bool CreateRightB(std::string x, node n, btree *T) { if (!RightChildB(n, T)) { node newborn = new element; newborn->label = x; n->right = newborn; return true; } else { return false; } } node ParentB(node n, btree *T) { std::vector<node> lista; node tmp = RootB(T); lista.push_back(tmp); do { tmp = lista[0]; if (LeftChildB(tmp, T) == n || RightChildB(tmp, T) == n) return tmp; if (LeftChildB(tmp, T) != NULL) { lista.push_back(LeftChildB(tmp, T)); } if (RightChildB(tmp, T) != NULL) { lista.push_back(RightChildB(tmp, T)); } lista.erase(lista.begin()); } while (lista.size() > 0); return NULL; } void DeleteB(node n, btree *T) { if (n->left) { DeleteB(n->left, T); } if (n->right ) { DeleteB(n->right, T); } node tmp = ParentB(n, T); if (n == LeftChildB(tmp, T)) { tmp->left = NULL; } else { tmp->right = NULL; } delete n; } void InitB(std::string x, btree *T) { T->label = x; }
You need to login to post a comment.
