|
| 1 | +include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +struct node { |
| 5 | + int data; |
| 6 | + |
| 7 | + struct node *lChild; |
| 8 | + struct node *rChild; |
| 9 | +}; |
| 10 | + |
| 11 | +struct node *root = NULL; |
| 12 | + |
| 13 | +void insert(int data) { |
| 14 | + struct node *tempNode = (struct node*) malloc(sizeof(struct node)); |
| 15 | + struct node *current; |
| 16 | + struct node *parent; |
| 17 | + |
| 18 | + tempNode->data = data; |
| 19 | + tempNode->lChild = NULL; |
| 20 | + tempNode->rChild = NULL; |
| 21 | + |
| 22 | + |
| 23 | + if(root == NULL) { |
| 24 | + root = tempNode; |
| 25 | + } else { |
| 26 | + current = root; |
| 27 | + parent = NULL; |
| 28 | + |
| 29 | + while(1) { |
| 30 | + parent = current; |
| 31 | + if(data < parent->data) { |
| 32 | + current = current->lChild; |
| 33 | + |
| 34 | + if(current == NULL) { |
| 35 | + parent->lChild = tempNode; |
| 36 | + return; |
| 37 | + } |
| 38 | + } |
| 39 | + else { |
| 40 | + current = current->rChild; |
| 41 | + |
| 42 | + |
| 43 | + if(current == NULL) { |
| 44 | + parent->rChild = tempNode; |
| 45 | + return; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +struct node* search(int data) { |
| 53 | + struct node *current = root; |
| 54 | + printf("Visiting elements: "); |
| 55 | + |
| 56 | + while(current->data != data) { |
| 57 | + if(current != NULL) |
| 58 | + printf("%d ",current->data); |
| 59 | + |
| 60 | + |
| 61 | + if(current->data > data) { |
| 62 | + current = current->lChild; |
| 63 | + } |
| 64 | + |
| 65 | + else { |
| 66 | + current = current->rChild; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + if(current == NULL) { |
| 71 | + return NULL; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return current; |
| 76 | +} |
| 77 | + |
| 78 | +void pre_order_traversal(struct node* root) { |
| 79 | + if(root != NULL) { |
| 80 | + printf("%d ",root->data); |
| 81 | + pre_order_traversal(root->lChild); |
| 82 | + pre_order_traversal(root->rChild); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void inorder_traversal(struct node* root) { |
| 87 | + if(root != NULL) { |
| 88 | + inorder_traversal(root->lChild); |
| 89 | + printf("%d ",root->data); |
| 90 | + inorder_traversal(root->rChild); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void post_order_traversal(struct node* root) { |
| 95 | + if(root != NULL) { |
| 96 | + post_order_traversal(root->lChild); |
| 97 | + post_order_traversal(root->rChild); |
| 98 | + printf("%d ", root->data); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + int i; |
| 104 | + int array[7] = { 29, 14, 32, 8, 19, 30, 42 }; |
| 105 | + |
| 106 | + for(i = 0; i < 7; i++) |
| 107 | + insert(array[i]); |
| 108 | + |
| 109 | + i = 31; |
| 110 | + struct node * temp = search(i); |
| 111 | + |
| 112 | + if(temp != NULL) { |
| 113 | + printf("[%d] Element found.", temp->data); |
| 114 | + printf("\n"); |
| 115 | + }else { |
| 116 | + printf("[ x ] Element not found (%d).\n", i); |
| 117 | + } |
| 118 | + |
| 119 | + i = 15; |
| 120 | + temp = search(i); |
| 121 | + |
| 122 | + if(temp != NULL) { |
| 123 | + printf("[%d] Element found.", temp->data); |
| 124 | + printf("\n"); |
| 125 | + }else { |
| 126 | + printf("[ x ] Element not found (%d).\n", i); |
| 127 | + } |
| 128 | + |
| 129 | + printf("\nPreorder traversal: "); |
| 130 | + pre_order_traversal(root); |
| 131 | + |
| 132 | + printf("\nInorder traversal: "); |
| 133 | + inorder_traversal(root); |
| 134 | + |
| 135 | + printf("\nPost order traversal: "); |
| 136 | + post_order_traversal(root); |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
0 commit comments