Skip to content

Instantly share code, notes, and snippets.

@estebanz01
Last active November 14, 2017 05:06
Show Gist options
  • Save estebanz01/b20fa00f92b35b3a296810589749eee1 to your computer and use it in GitHub Desktop.
Save estebanz01/b20fa00f92b35b3a296810589749eee1 to your computer and use it in GitHub Desktop.
Binary tree with multiple operations
// Example program
#include <iostream>
#include <string>
using namespace std;
class Leave {
public:
int data;
Leave* left;
Leave* right;
Leave(): data(-1), left(NULL), right(NULL) {}
Leave(int d, Leave* l = NULL, Leave* r = NULL) {
this->data = d;
this->left = l;
this->right = r;
}
};
class Tree {
public:
Leave *root;
Tree(): root(NULL) {}
void preorder(Leave* r) {
cout << r->data << endl;
if(r->left != NULL) {
this->preorder(r->left);
}
if(r->right != NULL) {
this->preorder(r->right);
}
}
void inorder(Leave* r) {
if(r->left != NULL) {
this->inorder(r->left);
}
cout << r->data << endl;
if(r->right != NULL) {
this->inorder(r->right);
}
}
void postorder(Leave* r) {
if(r->left != NULL) {
this->postorder(r->left);
}
if(r->right != NULL) {
this->postorder(r->right);
}
cout << r->data << endl;
}
Leave* search(Leave* r, int id) {
if(r == NULL) {
cout << "dato no existe" << endl;
return NULL;
}
if(r->data == id) {
return r;
} else {
if(r->data > id) {
// go left
this->search(r->left, id);
} else if(r->data < id) {
// go right
this->search(r->right, id);
}
}
}
Leave* parentOf(Leave* l) {
Leave* parent = NULL;
if (this->root->data == l->data) {
cout << "Id no tiene padre" << endl;
} else {
parent = this->root;
Leave* r = this->root;
while (r != NULL) {
if (r->data > l->data) {
parent = r;
r = r->left;
} else if (r->data < l->data) {
parent = r;
r = r->right;
} else if (r->data == l->data) {
break;
}
}
}
return parent;
}
int level(Leave* r) {
int left, right;
if (r->left == NULL && r->right == NULL) {
return 1;
} else {
left = 1 + this->level(r->left);
right = 1 + this->level(r->right);
if (left > right) {
return left;
} else {
return right;
}
}
}
int grade(Leave* r) {
int result = 0;
result += r->left != NULL ? 1 : 0;
result += r->right != NULL ? 1 : 0;
return result;
}
int height() {
return this->level(this->root);
}
void children(Leave* r) {
if (r->left != NULL) {
cout << "left " << r->left->data << endl;
}
if(r->right != NULL) {
cout << "right " << r->right->data << endl;
}
}
};
int main()
{
Tree* arbol = new Tree();
Leave* r = new Leave(25);
arbol->root = r;
arbol->root->left = new Leave(20, new Leave(14), new Leave(23));
arbol->root->right = new Leave(30, new Leave(28), new Leave(33));
cout << "Entire tree" << endl;
arbol->inorder(arbol->root);
cout << "Parent of 14 " << endl;
cout << arbol->parentOf(arbol->search(arbol->root, 14))->data << endl;
cout << "Level of 20" << endl;
cout << arbol->level(arbol->search(arbol->root, 20)) << endl;
cout << "Height of tree" << endl;
cout << arbol->height() << endl;
cout << "Children of 30" << endl;
arbol->children(arbol->search(arbol->root, 30));
cout << "grade of 25" << endl;
cout << arbol->grade(arbol->root) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment