Created
October 11, 2018 18:13
-
-
Save SilvaEmerson/ae8e94d669fa0f7da0118c8e267bddd1 to your computer and use it in GitHub Desktop.
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> | |
#include <vector> | |
using namespace std; | |
vector<int> numbers; | |
typedef struct node { | |
struct node *left; | |
struct node *rigth; | |
int data; | |
} Node; | |
Node *createRoot(); | |
bool isBintTree(Node *root); | |
void buildTree(Node *root); | |
int main() { | |
string entry; | |
cin >> entry; | |
int currNum = 0; | |
bool found = false; | |
for(int i = 0; i < entry.size(); i++){ | |
if(isdigit(entry[i])){ | |
currNum = currNum * 10 + atoi(&entry[i]); | |
found = true; | |
}else{ | |
if(entry[i] == ')' && entry[i - 1] == '(') numbers.push_back(-1); | |
else if(found) numbers.push_back(currNum); | |
currNum = 0; | |
found = false; | |
} | |
} | |
Node *root = createRoot(); | |
buildTree(root); | |
cout << (isBintTree(root)? "VERDADEIRO": "FALSO") << endl; | |
return 0; | |
} | |
bool isBintTree(Node *root){ | |
if(root->data == NULL) return true; | |
if(root->left != NULL){ | |
if(root->left->data > root->data) return false; | |
isBintTree(root->left); | |
} | |
if(root->rigth != NULL){ | |
if(root->rigth->data < root->data && root->rigth->data != -1) return false; | |
isBintTree(root->rigth); | |
} | |
} | |
Node *createRoot(){ | |
Node *root = new Node; | |
root->data = numbers.front(); | |
root->left = root->rigth = NULL; | |
numbers.erase(numbers.begin()); | |
return root; | |
} | |
void buildTree(Node *root){ | |
if(root->data == -1) return; | |
if(!numbers.empty()) buildTree(root->left = createRoot()); | |
if(!numbers.empty()) buildTree(root->rigth = createRoot()); | |
} |
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> | |
#include <vector> | |
using namespace std; | |
vector<int> numbers; | |
typedef struct node { | |
struct node *left; | |
struct node *right; | |
int data; | |
} Node; | |
Node *createRoot(); | |
bool isBintTree(Node *root); | |
void buildTree(Node *root); | |
int getNodeLevel(int val, Node *node, int depth); | |
int main() { | |
string entry; | |
cin >> entry; | |
string currNum = ""; | |
bool found = false; | |
for(int i = 0; i < entry.size(); i++){ | |
if(isdigit(entry[i])){ | |
currNum += entry[i]; | |
found = true; | |
}else{ | |
if(entry[i] == ')' && entry[i - 1] == '(') numbers.push_back(-1); | |
else if(found) numbers.push_back(stoi(currNum)); | |
currNum = ""; | |
found = false; | |
} | |
} | |
Node *root = createRoot(); | |
buildTree(root); | |
int val; | |
cin>>val; | |
int level = getNodeLevel(val, root, 0); | |
cout << ((level == -1)? "NAO ESTA NA ARVORE": "ESTA NA ARVORE") << endl << level << endl; | |
return 0; | |
} | |
bool isBintTree(Node *root){ | |
if(root->data == NULL) return true; | |
if(root->left != NULL){ | |
if(root->left->data > root->data) return false; | |
isBintTree(root->left); | |
} | |
if(root->right != NULL){ | |
if(root->right->data < root->data && root->right->data != -1) return false; | |
isBintTree(root->right); | |
} | |
} | |
Node *createRoot(){ | |
Node *root = new Node; | |
root->data = numbers.front(); | |
root->left = root->right = NULL; | |
numbers.erase(numbers.begin()); | |
return root; | |
} | |
int getNodeLevel(int val, Node *node, int depth){ | |
if (node != NULL) | |
{ | |
if (val == node->data) | |
{ | |
return depth; | |
} | |
int level = getNodeLevel(val, node->left, depth + 1); | |
if(level != -1) | |
return level; | |
level = 0; | |
level = getNodeLevel(val, node->right, depth + 1); | |
if(level != -1) | |
return level; | |
} | |
else | |
{ | |
return -1; | |
} | |
} | |
void buildTree(Node *root){ | |
if(root->data == -1) return; | |
if(!numbers.empty()) buildTree(root->left = createRoot()); | |
if(!numbers.empty()) buildTree(root->right = createRoot()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment