Created
July 2, 2018 13:18
-
-
Save aa-ahmed-aa/081b18be2481e89ccba76edbc7651da7 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 <string> | |
# include <cstring> | |
# include <string.h> | |
# include <windows.h> | |
using namespace std; | |
struct node | |
{ | |
int value; | |
node *left; | |
node *right; | |
}; | |
node *root = NULL; | |
void insert_tree(int key,node *root) | |
{ | |
if (::root == NULL) | |
{ | |
::root = new node; | |
::root->value = key; | |
::root->left = NULL; | |
::root->right = NULL; | |
} | |
else | |
{ | |
if (key < root->value) | |
{ | |
if (root->left == NULL) | |
{ | |
//insert | |
root->left = new node; | |
root->left->value = key; | |
root->left->left = NULL; | |
root->left->right = NULL; | |
} | |
else | |
{ | |
insert_tree(key, root->left); | |
} | |
} | |
else if (key > root->value) | |
{ | |
if (root->right == NULL) | |
{ | |
//insert | |
root->right = new node; | |
root->right->value = key; | |
root->right->left = NULL; | |
root->right->right = NULL; | |
} | |
else | |
{ | |
insert_tree(key,root->right); | |
} | |
} | |
else | |
{ | |
cout << "you entered this value before so i will ignore it" << endl; | |
return; | |
} | |
} | |
} | |
node* search(int key, node *root) | |
{ | |
if (root != NULL) | |
{ | |
if (key == root->value) | |
return root; | |
if (key<root->value) | |
search(key, root->left); | |
else | |
search(key, root->right); | |
} | |
else | |
{ | |
cout << "Can't Find the Value you searched" << endl; | |
exit(0); | |
} | |
} | |
int main() | |
{ | |
insert_tree(10,root); | |
insert_tree(6, root); | |
insert_tree(14, root); | |
insert_tree(5, root); | |
insert_tree(8, root); | |
insert_tree(11, root); | |
insert_tree(18, root); | |
node *found = search(1, root); | |
cout << found->value <<" was Found" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment