|
| 1 | +#include<stdio.h> |
| 2 | +#include<stdlib.h> |
| 3 | + |
| 4 | +typedef struct |
| 5 | +{ |
| 6 | + int number; |
| 7 | + struct node *leftChild; |
| 8 | + struct node *rightChild; |
| 9 | + |
| 10 | +} node; |
| 11 | + |
| 12 | +node* root = NULL; |
| 13 | + |
| 14 | +void insertNode(int value) |
| 15 | +{ |
| 16 | + node* tempNode; |
| 17 | + node* currentNode = NULL; |
| 18 | + |
| 19 | + tempNode = (node *) malloc(sizeof(node)); |
| 20 | + tempNode->number = value; |
| 21 | + tempNode->leftChild = NULL; |
| 22 | + tempNode->rightChild = NULL; |
| 23 | + |
| 24 | + |
| 25 | + if(root == NULL) //For the very first call |
| 26 | + { |
| 27 | + root = tempNode; |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + currentNode = root; |
| 32 | + |
| 33 | + while(1) |
| 34 | + { |
| 35 | + if(value <= currentNode->number) |
| 36 | + { |
| 37 | + if(currentNode->leftChild == NULL) |
| 38 | + { |
| 39 | + currentNode->leftChild = tempNode; |
| 40 | + break; |
| 41 | + } |
| 42 | + |
| 43 | + currentNode = currentNode->leftChild; |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + if(currentNode->rightChild == NULL) |
| 48 | + { |
| 49 | + currentNode->rightChild = tempNode; |
| 50 | + break; |
| 51 | + } |
| 52 | + |
| 53 | + currentNode = currentNode->rightChild; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void inOrderPrint(node *rootNode) // InOrder Traversal (left-root-right) |
| 60 | +{ |
| 61 | + if(rootNode==NULL) |
| 62 | + return; |
| 63 | + |
| 64 | + inOrderPrint(rootNode->leftChild); |
| 65 | + |
| 66 | + printf("%d ", rootNode->number); |
| 67 | + |
| 68 | + inOrderPrint(rootNode->rightChild); |
| 69 | +} |
| 70 | + |
| 71 | +node* searchOnTree(int value) |
| 72 | +{ |
| 73 | + node* currentNode = root; |
| 74 | + |
| 75 | + while(currentNode != NULL) |
| 76 | + { |
| 77 | + if(value == currentNode->number) |
| 78 | + { |
| 79 | + break; |
| 80 | + } |
| 81 | + else if(value <= currentNode->number) |
| 82 | + currentNode = currentNode->leftChild; |
| 83 | + else |
| 84 | + currentNode = currentNode->rightChild; |
| 85 | + } |
| 86 | + |
| 87 | + return currentNode; |
| 88 | +} |
| 89 | + |
| 90 | +node * findMinimum(node *currentNode) |
| 91 | +{ |
| 92 | + if(currentNode->leftChild==NULL) |
| 93 | + return currentNode; |
| 94 | + |
| 95 | + return findMinimum(currentNode->leftChild); |
| 96 | +} |
| 97 | + |
| 98 | +node * deleteNode(node *currentNode, int value) |
| 99 | +{ |
| 100 | + if(currentNode==NULL) // empty tree |
| 101 | + return NULL; |
| 102 | + else if(value < currentNode->number) // value is less than node's number. so go to left subtree |
| 103 | + currentNode->leftChild = deleteNode(currentNode->leftChild, value); |
| 104 | + else if(value > currentNode->number) // value is greater than node's number. so go to right subtree |
| 105 | + currentNode->rightChild = deleteNode(currentNode->rightChild, value); |
| 106 | + else // node found. Let's delete it! |
| 107 | + { |
| 108 | + //node has no child |
| 109 | + if(currentNode->leftChild == NULL && currentNode->rightChild == NULL) |
| 110 | + { |
| 111 | + currentNode = NULL; |
| 112 | + } |
| 113 | + else if(currentNode->leftChild == NULL) // node has only right child |
| 114 | + { |
| 115 | + currentNode = currentNode->rightChild; |
| 116 | + } |
| 117 | + else if(currentNode->rightChild == NULL) // node has only left child |
| 118 | + { |
| 119 | + currentNode = currentNode->leftChild; |
| 120 | + } |
| 121 | + else // node has two children |
| 122 | + { |
| 123 | + node *tempNode = findMinimum(currentNode->rightChild); |
| 124 | + currentNode->number = tempNode->number; |
| 125 | + currentNode->rightChild = deleteNode(currentNode->rightChild, tempNode->number); |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + return currentNode; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +int main() |
| 135 | +{ |
| 136 | + int i, value; |
| 137 | + //int numbers[] = {45, 54, 40, 49, 38, 70, 30, 39, 41, 45, 44}; |
| 138 | + int numbers[] = {5, 2, 17, 6, 20}; |
| 139 | + int numbers_length = sizeof(numbers)/sizeof(numbers[0]); |
| 140 | + node* node_to_delete; |
| 141 | + |
| 142 | + for(i=0;i<numbers_length;i++) |
| 143 | + { |
| 144 | + insertNode(numbers[i]); |
| 145 | + } |
| 146 | + |
| 147 | + printf("\nIn-Order Tree printing:\n"); |
| 148 | + inOrderPrint(root); |
| 149 | + puts(""); |
| 150 | + |
| 151 | + value = 70; |
| 152 | + if(searchOnTree(value) != NULL) |
| 153 | + printf("\n%d is found on Tree.\n", value); |
| 154 | + else |
| 155 | + printf("\n%d is not found on Tree.\n", value); |
| 156 | + |
| 157 | + value = 100; |
| 158 | + if(searchOnTree(value) != NULL) |
| 159 | + printf("\n%d is found on Tree.\n", value); |
| 160 | + else |
| 161 | + printf("\n%d is not found on Tree.\n", value); |
| 162 | + |
| 163 | + deleteNode(root, 5); |
| 164 | + |
| 165 | + printf("\nIn-Order Tree printing:\n"); |
| 166 | + inOrderPrint(root); |
| 167 | + puts(""); |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
0 commit comments