#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
struct node *root = NULL;
void insert(int data){
struct node
*tempNode
= (struct node
*) malloc(sizeof(struct node
)); struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty
if(root == NULL){
root = tempNode;
} else {
current = root;
parent = NULL;
while(1){
parent = current;
//go to left of the tree
if(data < parent->data){
current = current->leftChild;
//insert to the left
if(current == NULL){
parent->leftChild = tempNode;
return;
}
}//go to right of the tree
else{
current = current->rightChild;
//insert to the right
if(current == NULL){
parent->rightChild = tempNode;
return;
}
}
}
}
}
struct node* search(int data){
struct node *current = root;
printf("Visiting elements: "); while(current->data != data){
if(current != NULL)
//go to left tree
if(current->data > data){
current = current->leftChild;
}//else go to right tree
else{
current = current->rightChild;
}
//not found
if(current == NULL){
return NULL;
}
}
return current;
}
void preOrder(struct node* root){
if(root!=NULL){
preOrder(root->leftChild);
preOrder(root->rightChild);
}
}
void inOrder(struct node* root){
if(root!=NULL){
inOrder(root->leftChild);
inOrder(root->rightChild);
}
}
void postOrder(struct node* root){
if(root!=NULL){
postOrder(root->leftChild);
postOrder(root->rightChild);
}
}
void traverse(int traversalType){
switch(traversalType){
case 1:
printf("\nPreorder traversal: "); preOrder(root);
break;
case 2:
printf("\nInorder traversal: "); inOrder(root);
break;
case 3:
printf("\nPostorder traversal: "); postOrder(root);
break;
}
}
int main()
{
/* 11 //Level 0
*/
insert(11);
/* 11 //Level 0
* |
* |---20 //Level 1
*/
insert(20);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
*/
insert(3);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* |
* |--42 //Level 2
*/
insert(42);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* |
* |--42 //Level 2
* |
* |--54 //Level 3
*/
insert(54);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* |
* 16--|--42 //Level 2
* |
* |--54 //Level 3
*/
insert(16);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* |
* 16--|--42 //Level 2
* |
* 32--|--54 //Level 3
*/
insert(32);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* | |
* |--9 16--|--42 //Level 2
* |
* 32--|--54 //Level 3
*/
insert(9);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* | |
* |--9 16--|--42 //Level 2
* | |
* 4--| 32--|--54 //Level 3
*/
insert(4);
/* 11 //Level 0
* |
* 3---|---20 //Level 1
* | |
* |--9 16--|--42 //Level 2
* | |
* 4--|--10 32--|--54 //Level 3
*/
insert(10);
struct node * temp = search(32);
if(temp!=NULL){
} else {
printf("Element not found.\n"); }
struct node *node1 = search(2);
if(node1!=NULL){
} else {
printf("Element not found.\n"); }
//pre-order traversal
//root, left ,right
traverse(1);
//in-order traversal
//left, root ,right
traverse(2);
//post order traversal
//left, right, root
traverse(3);
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkYm9vbC5oPgoKc3RydWN0IG5vZGUgewogICBpbnQgZGF0YTsgICAKICAgc3RydWN0IG5vZGUgKmxlZnRDaGlsZDsKICAgc3RydWN0IG5vZGUgKnJpZ2h0Q2hpbGQ7Cn07CnN0cnVjdCBub2RlICpyb290ID0gTlVMTDsKdm9pZCBpbnNlcnQoaW50IGRhdGEpewogICBzdHJ1Y3Qgbm9kZSAqdGVtcE5vZGUgPSAoc3RydWN0IG5vZGUqKSBtYWxsb2Moc2l6ZW9mKHN0cnVjdCBub2RlKSk7CiAgIHN0cnVjdCBub2RlICpjdXJyZW50OwogICBzdHJ1Y3Qgbm9kZSAqcGFyZW50OwoKICAgdGVtcE5vZGUtPmRhdGEgPSBkYXRhOwogICB0ZW1wTm9kZS0+bGVmdENoaWxkID0gTlVMTDsKICAgdGVtcE5vZGUtPnJpZ2h0Q2hpbGQgPSBOVUxMOwoKICAgLy9pZiB0cmVlIGlzIGVtcHR5CiAgIGlmKHJvb3QgPT0gTlVMTCl7CiAgICAgIHJvb3QgPSB0ZW1wTm9kZTsKICAgfSBlbHNlIHsKICAgICAgY3VycmVudCA9IHJvb3Q7CiAgICAgIHBhcmVudCA9IE5VTEw7CiAgICAgIHdoaWxlKDEpeyAgICAgICAgICAgICAgICAKICAgICAgICAgcGFyZW50ID0gY3VycmVudDsKICAgICAgICAgLy9nbyB0byBsZWZ0IG9mIHRoZSB0cmVlCiAgICAgICAgIGlmKGRhdGEgPCBwYXJlbnQtPmRhdGEpewogICAgICAgICAgICBjdXJyZW50ID0gY3VycmVudC0+bGVmdENoaWxkOyAgICAgICAgICAgICAgICAKICAgICAgICAgICAgLy9pbnNlcnQgdG8gdGhlIGxlZnQKICAgICAgICAgICAgaWYoY3VycmVudCA9PSBOVUxMKXsKICAgICAgICAgICAgICAgcGFyZW50LT5sZWZ0Q2hpbGQgPSB0ZW1wTm9kZTsKICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgIH0vL2dvIHRvIHJpZ2h0IG9mIHRoZSB0cmVlCiAgICAgICAgIGVsc2V7CiAgICAgICAgICAgIGN1cnJlbnQgPSBjdXJyZW50LT5yaWdodENoaWxkOwogICAgICAgICAgICAvL2luc2VydCB0byB0aGUgcmlnaHQKICAgICAgICAgICAgaWYoY3VycmVudCA9PSBOVUxMKXsKICAgICAgICAgICAgICAgcGFyZW50LT5yaWdodENoaWxkID0gdGVtcE5vZGU7CiAgICAgICAgICAgICAgIHJldHVybjsKICAgICAgICAgICAgfQogICAgICAgICB9CiAgICAgIH0gICAgICAgCiAgIH0KfQpzdHJ1Y3Qgbm9kZSogc2VhcmNoKGludCBkYXRhKXsKICAgc3RydWN0IG5vZGUgKmN1cnJlbnQgPSByb290OwogICBwcmludGYoIlZpc2l0aW5nIGVsZW1lbnRzOiAiKTsKICAgd2hpbGUoY3VycmVudC0+ZGF0YSAhPSBkYXRhKXsKICAgICAgaWYoY3VycmVudCAhPSBOVUxMKQogICAgICAgICBwcmludGYoIiVkICIsY3VycmVudC0+ZGF0YSk7IAogICAgICAgICAvL2dvIHRvIGxlZnQgdHJlZQogICAgICAgICBpZihjdXJyZW50LT5kYXRhID4gZGF0YSl7CiAgICAgICAgICAgIGN1cnJlbnQgPSBjdXJyZW50LT5sZWZ0Q2hpbGQ7CiAgICAgICAgIH0vL2Vsc2UgZ28gdG8gcmlnaHQgdHJlZQogICAgICAgICBlbHNleyAgICAgICAgICAgICAgICAKICAgICAgICAgICAgY3VycmVudCA9IGN1cnJlbnQtPnJpZ2h0Q2hpbGQ7CiAgICAgICAgIH0KICAgICAgICAgLy9ub3QgZm91bmQKICAgICAgICAgaWYoY3VycmVudCA9PSBOVUxMKXsKICAgICAgICAgICAgcmV0dXJuIE5VTEw7CiAgICAgICAgIH0KICAgICAgfQogICByZXR1cm4gY3VycmVudDsKfQp2b2lkIHByZU9yZGVyKHN0cnVjdCBub2RlKiByb290KXsKICAgaWYocm9vdCE9TlVMTCl7CiAgICAgIHByaW50ZigiJWQgIixyb290LT5kYXRhKTsKICAgICAgcHJlT3JkZXIocm9vdC0+bGVmdENoaWxkKTsKICAgICAgcHJlT3JkZXIocm9vdC0+cmlnaHRDaGlsZCk7CiAgIH0KfQp2b2lkIGluT3JkZXIoc3RydWN0IG5vZGUqIHJvb3QpewogICBpZihyb290IT1OVUxMKXsKICAgICAgaW5PcmRlcihyb290LT5sZWZ0Q2hpbGQpOwogICAgICBwcmludGYoIiVkICIscm9vdC0+ZGF0YSk7ICAgICAgICAgIAogICAgICBpbk9yZGVyKHJvb3QtPnJpZ2h0Q2hpbGQpOwogICB9Cn0Kdm9pZCBwb3N0T3JkZXIoc3RydWN0IG5vZGUqIHJvb3QpewogICBpZihyb290IT1OVUxMKXsgICAgICAgICAgICAKICAgICAgcG9zdE9yZGVyKHJvb3QtPmxlZnRDaGlsZCk7CiAgICAgIHBvc3RPcmRlcihyb290LT5yaWdodENoaWxkKTsKICAgICAgcHJpbnRmKCIlZCAiLHJvb3QtPmRhdGEpOwogICB9Cn0Kdm9pZCB0cmF2ZXJzZShpbnQgdHJhdmVyc2FsVHlwZSl7CiAgIHN3aXRjaCh0cmF2ZXJzYWxUeXBlKXsKICAgICAgY2FzZSAxOgogICAgICAgICBwcmludGYoIlxuUHJlb3JkZXIgdHJhdmVyc2FsOiAiKTsKICAgICAgICAgcHJlT3JkZXIocm9vdCk7CiAgICAgICAgIGJyZWFrOwogICAgICBjYXNlIDI6CiAgICAgICAgIHByaW50ZigiXG5Jbm9yZGVyIHRyYXZlcnNhbDogIik7CiAgICAgICAgIGluT3JkZXIocm9vdCk7CiAgICAgICAgIGJyZWFrOwogICAgICBjYXNlIDM6CiAgICAgICAgIHByaW50ZigiXG5Qb3N0b3JkZXIgdHJhdmVyc2FsOiAiKTsKICAgICAgICAgcG9zdE9yZGVyKHJvb3QpOwogICAgICAgICBicmVhazsKICAgfSAgICAgICAgICAgIAp9ICAKCmludCBtYWluKCkKewogICAvKiAgICAgICAgICAgICAgICAgICAgIDExICAgICAgICAgICAgICAgLy9MZXZlbCAwCiAgICovCiAgIGluc2VydCgxMSk7CiAgIC8qICAgICAgICAgICAgICAgICAgICAgMTEgICAgICAgICAgICAgICAvL0xldmVsIDAKICAgKiAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAgICAgfC0tLTIwICAgICAgICAgICAvL0xldmVsIDEKICAgKi8KICAgaW5zZXJ0KDIwKTsKICAgLyogICAgICAgICAgICAgICAgICAgICAxMSAgICAgICAgICAgICAgIC8vTGV2ZWwgMAogICAqICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIDMtLS18LS0tMjAgICAgICAgICAgIC8vTGV2ZWwgMQogICAqLwogICBpbnNlcnQoMyk7ICAgICAgICAKICAgLyogICAgICAgICAgICAgICAgICAgICAxMSAgICAgICAgICAgICAgIC8vTGV2ZWwgMAogICAqICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIDMtLS18LS0tMjAgICAgICAgICAgIC8vTGV2ZWwgMQogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgfAogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgfC0tNDIgICAgICAgLy9MZXZlbCAyCiAgICovCiAgIGluc2VydCg0Mik7CiAgIC8qICAgICAgICAgICAgICAgICAgICAgMTEgICAgICAgICAgICAgICAvL0xldmVsIDAKICAgKiAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAzLS0tfC0tLTIwICAgICAgICAgICAvL0xldmVsIDEKICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgIHwtLTQyICAgICAgIC8vTGV2ZWwgMgogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB8LS01NCAgIC8vTGV2ZWwgMwogICAqLwogICBpbnNlcnQoNTQpOwogICAvKiAgICAgICAgICAgICAgICAgICAgIDExICAgICAgICAgICAgICAgLy9MZXZlbCAwCiAgICogICAgICAgICAgICAgICAgICAgICAgfAogICAqICAgICAgICAgICAgICAgICAgMy0tLXwtLS0yMCAgICAgICAgICAgLy9MZXZlbCAxCiAgICogICAgICAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAgICAgIDE2LS18LS00MiAgICAgICAvL0xldmVsIDIKICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfC0tNTQgICAvL0xldmVsIDMKICAgKi8KICAgaW5zZXJ0KDE2KTsKICAgLyogICAgICAgICAgICAgICAgICAgICAxMSAgICAgICAgICAgICAgIC8vTGV2ZWwgMAogICAqICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIDMtLS18LS0tMjAgICAgICAgICAgIC8vTGV2ZWwgMQogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgfAogICAqICAgICAgICAgICAgICAgICAgICAgICAxNi0tfC0tNDIgICAgICAgLy9MZXZlbCAyCiAgICogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfAogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgMzItLXwtLTU0ICAgLy9MZXZlbCAzCiAgICovCiAgIGluc2VydCgzMik7CiAgIC8qICAgICAgICAgICAgICAgICAgICAgMTEgICAgICAgICAgICAgICAvL0xldmVsIDAKICAgKiAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAzLS0tfC0tLTIwICAgICAgICAgICAvL0xldmVsIDEKICAgKiAgICAgICAgICAgICAgICAgIHwgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIHwtLTkgMTYtLXwtLTQyICAgICAgIC8vTGV2ZWwgMgogICAqICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgICAgICAgICAgIDMyLS18LS01NCAgIC8vTGV2ZWwgMwogICAqLwogICBpbnNlcnQoOSk7CiAgIC8qICAgICAgICAgICAgICAgICAgICAgMTEgICAgICAgICAgICAgICAvL0xldmVsIDAKICAgKiAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAzLS0tfC0tLTIwICAgICAgICAgICAvL0xldmVsIDEKICAgKiAgICAgICAgICAgICAgICAgIHwgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIHwtLTkgMTYtLXwtLTQyICAgICAgIC8vTGV2ZWwgMgogICAqICAgICAgICAgICAgICAgICAgICAgfCAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIDQtLXwgICAgIDMyLS18LS01NCAgIC8vTGV2ZWwgMwogICAqLwogICBpbnNlcnQoNCk7CiAgIC8qICAgICAgICAgICAgICAgICAgICAgMTEgICAgICAgICAgICAgICAvL0xldmVsIDAKICAgKiAgICAgICAgICAgICAgICAgICAgICB8CiAgICogICAgICAgICAgICAgICAgICAzLS0tfC0tLTIwICAgICAgICAgICAvL0xldmVsIDEKICAgKiAgICAgICAgICAgICAgICAgIHwgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIHwtLTkgMTYtLXwtLTQyICAgICAgIC8vTGV2ZWwgMgogICAqICAgICAgICAgICAgICAgICAgICAgfCAgICAgICAgIHwKICAgKiAgICAgICAgICAgICAgICAgIDQtLXwtLTEwIDMyLS18LS01NCAgIC8vTGV2ZWwgMwogICAqLwogICBpbnNlcnQoMTApOwoKICAgc3RydWN0IG5vZGUgKiB0ZW1wID0gc2VhcmNoKDMyKTsKICAgaWYodGVtcCE9TlVMTCl7CiAgICAgIHByaW50ZigiRWxlbWVudCBmb3VuZC5cbiIpOwogICAgICBwcmludGYoIiggJWQgKSIsdGVtcC0+ZGF0YSk7CiAgICAgIHByaW50ZigiXG4iKTsKICAgfSBlbHNlIHsKICAgICAgcHJpbnRmKCJFbGVtZW50IG5vdCBmb3VuZC5cbiIpOwogICB9CgogICBzdHJ1Y3Qgbm9kZSAqbm9kZTEgPSBzZWFyY2goMik7CiAgIGlmKG5vZGUxIT1OVUxMKXsKICAgICAgcHJpbnRmKCJFbGVtZW50IGZvdW5kLlxuIik7CiAgICAgIHByaW50ZigiKCAlZCApIixub2RlMS0+ZGF0YSk7CiAgICAgIHByaW50ZigiXG4iKTsKICAgfSBlbHNlIHsKICAgICAgcHJpbnRmKCJFbGVtZW50IG5vdCBmb3VuZC5cbiIpOwogICB9ICAgICAgICAKCiAgIC8vcHJlLW9yZGVyIHRyYXZlcnNhbAogICAvL3Jvb3QsIGxlZnQgLHJpZ2h0CiAgIHRyYXZlcnNlKDEpOwogICAvL2luLW9yZGVyIHRyYXZlcnNhbAogICAvL2xlZnQsIHJvb3QgLHJpZ2h0CiAgIHRyYXZlcnNlKDIpOwogICAvL3Bvc3Qgb3JkZXIgdHJhdmVyc2FsCiAgIC8vbGVmdCwgcmlnaHQsIHJvb3QKICAgdHJhdmVyc2UoMyk7ICAgICAgIAp9