binary search tree
"binarysearchtree.h"
"binarysearchtree.c"
"main.c"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #ifndef _BINARY_SEARCH_TREE_ #define _BINARY_SEARCH_TREE_ struct Binary_Search_Tree_Node_Type { int key; char value; struct Binary_Search_Tree_Node_Type* Pointer_Left_Child; struct Binary_Search_Tree_Node_Type* Pointer_Right_Child; }; struct Binary_Search_Tree_Type { struct Binary_Search_Tree_Node_Type* Pointer_Root_Node; }; typedef struct Binary_Search_Tree_Node_Type Binary_Search_Tree_Node; typedef struct Binary_Search_Tree_Type Binary_Search_Tree; Binary_Search_Tree* Create_Binary_Search_Tree(void); int Insert_Element_Binary_Search_Tree(Binary_Search_Tree* Pointer_Search_Tree, Binary_Search_Tree_Node element); int Delete_Element_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key); Binary_Search_Tree_Node* Search_Recursive_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key); Binary_Search_Tree_Node* Search_Internal_Recursive_Binary_Search_Tree(Binary_Search_Tree_Node* Pointer_Tree_Node, int key); Binary_Search_Tree_Node* Search_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key); void Delete_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree); void Delete_Binary_Search_Tree_Internal(Binary_Search_Tree_Node* Pointer_Tree_Node); #endif // !_BINARY_SEARCH_TREE_ #ifndef _COMMON_TREE_DEFAULT_ #define _COMMON_TREE_DEFAULT_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_TREE_DEFAULT_ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | #include <stdio.h> #include <stdlib.h> #include "binarysearchtree.h" Binary_Search_Tree* Create_Binary_Search_Tree(void) { Binary_Search_Tree* Pointer_Return = (Binary_Search_Tree*)malloc(1 * sizeof(Binary_Search_Tree)); if ((Binary_Search_Tree*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation. Create_Search_Tree()\n"); return NULL; } (struct Binary_Search_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node = NULL; return (Binary_Search_Tree*)Pointer_Return; } int Insert_Element_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, Binary_Search_Tree_Node element) { if ((Binary_Search_Tree*)Pointer_Binary_Search_Tree == NULL) return FALSE; Binary_Search_Tree_Node* Pointer_Parent_Node = (Binary_Search_Tree_Node*)Pointer_Binary_Search_Tree->Pointer_Root_Node; while ((Binary_Search_Tree_Node*)Pointer_Parent_Node != NULL) { if (element.key == Pointer_Parent_Node->key) { printf("Error! Duplicate key-[%d], Insert_Element_Binary_Search_Tree()\n", element.key); return FALSE; } else if ((element.key < Pointer_Parent_Node->key) && ((struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child != NULL)) { (Binary_Search_Tree_Node*)Pointer_Parent_Node = (Binary_Search_Tree_Node*)Pointer_Parent_Node->Pointer_Left_Child; } else if ((element.key > Pointer_Parent_Node->key) && ((struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child != NULL)) { (Binary_Search_Tree_Node*)Pointer_Parent_Node = (Binary_Search_Tree_Node*)Pointer_Parent_Node->Pointer_Right_Child; } else break; } Binary_Search_Tree_Node* Pointer_New_Node = (Binary_Search_Tree_Node*)malloc(1 * sizeof(Binary_Search_Tree_Node)); if ((Binary_Search_Tree_Node*)Pointer_New_Node == NULL) { printf("Error! Dynamic memory allocation. Insert_Element_Binary_Search_Tree()\n"); return FALSE; } *(Binary_Search_Tree_Node*)Pointer_New_Node = *(Binary_Search_Tree_Node*)&element; (struct Binary_Search_Tree_Node_Type*)Pointer_New_Node->Pointer_Left_Child = NULL; (struct Binary_Search_Tree_Node_Type*)Pointer_New_Node->Pointer_Right_Child = NULL; if ((Binary_Search_Tree_Node*)Pointer_Parent_Node == NULL) { (struct Binary_Search_Tree_Node_Type*)Pointer_Binary_Search_Tree->Pointer_Root_Node = (struct Binary_Search_Tree_Node_Type*)Pointer_New_Node; } else if (Pointer_New_Node->key < Pointer_Parent_Node->key) { (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_New_Node; } else (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_New_Node; return TRUE; } int Delete_Element_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key) { if ((Binary_Search_Tree*)Pointer_Binary_Search_Tree == NULL) return FALSE; Binary_Search_Tree_Node* Pointer_Parent_Node = NULL; Binary_Search_Tree_Node* Pointer_Delete_Node = (Binary_Search_Tree_Node*)Pointer_Binary_Search_Tree->Pointer_Root_Node; while ((Binary_Search_Tree_Node*)Pointer_Delete_Node != NULL) { if (key == Pointer_Delete_Node->key) break; (Binary_Search_Tree_Node*)Pointer_Parent_Node = (Binary_Search_Tree_Node*)Pointer_Delete_Node; if (key < Pointer_Delete_Node->key) (Binary_Search_Tree_Node*)Pointer_Delete_Node = (Binary_Search_Tree_Node*)Pointer_Delete_Node->Pointer_Left_Child; else (Binary_Search_Tree_Node*)Pointer_Delete_Node = (Binary_Search_Tree_Node*)Pointer_Delete_Node->Pointer_Right_Child; } if ((Binary_Search_Tree_Node*)Pointer_Delete_Node == NULL) { printf("Error! Duplicate key-[%d], Delete_Element_Binary_Search_Tree()\n", key); return FALSE; } if ((struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Left_Child == NULL && (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Right_Child == NULL) { if ((Binary_Search_Tree_Node*)Pointer_Parent_Node == NULL) (struct Binary_Search_Tree_Node_Type*)Pointer_Binary_Search_Tree->Pointer_Root_Node = NULL; else if ((struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child == (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node) { (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = NULL; } else (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = NULL; free((Binary_Search_Tree_Node*)Pointer_Delete_Node); return TRUE; } else if ((struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Left_Child != NULL && (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Right_Child != NULL) { Binary_Search_Tree_Node* Pointer_Predecessor = (Binary_Search_Tree_Node*)Pointer_Delete_Node; Binary_Search_Tree_Node* Pointer_Successor = (Binary_Search_Tree_Node*)Pointer_Delete_Node->Pointer_Left_Child; while ((Binary_Search_Tree_Node*)Pointer_Successor->Pointer_Right_Child != NULL) { (Binary_Search_Tree_Node*)Pointer_Predecessor = (Binary_Search_Tree_Node*)Pointer_Successor; (Binary_Search_Tree_Node*)Pointer_Successor = (Binary_Search_Tree_Node*)Pointer_Successor->Pointer_Right_Child; } (struct Binary_Search_Tree_Node_Type*)Pointer_Predecessor->Pointer_Right_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Successor->Pointer_Left_Child; (struct Binary_Search_Tree_Node_Type*)Pointer_Successor->Pointer_Left_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Left_Child; (struct Binary_Search_Tree_Node_Type*)Pointer_Successor->Pointer_Right_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Right_Child; if ((Binary_Search_Tree_Node*)Pointer_Parent_Node == NULL) { (struct Binary_Search_Tree_Node_Type*)Pointer_Binary_Search_Tree->Pointer_Root_Node = (struct Binary_Search_Tree_Node_Type*)Pointer_Successor; } else if ((struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child == (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node) { (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Successor; } else (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Successor; free((Binary_Search_Tree_Node*)Pointer_Delete_Node); return TRUE; } Binary_Search_Tree_Node* Pointer_Child_Node = NULL; if ((struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node->Pointer_Left_Child == NULL) { (Binary_Search_Tree_Node*)Pointer_Child_Node = (Binary_Search_Tree_Node*)Pointer_Delete_Node->Pointer_Left_Child; } else (Binary_Search_Tree_Node*)Pointer_Child_Node = (Binary_Search_Tree_Node*)Pointer_Delete_Node->Pointer_Right_Child; if ((Binary_Search_Tree_Node*)Pointer_Parent_Node == NULL) { (struct Binary_Search_Tree_Node_Type*)Pointer_Binary_Search_Tree->Pointer_Root_Node = (struct Binary_Search_Tree_Node_Type*)Pointer_Child_Node; } else if ((struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Delete_Node) { (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Child_Node; } else (struct Binary_Search_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = (struct Binary_Search_Tree_Node_Type*)Pointer_Child_Node; free((Binary_Search_Tree_Node*)Pointer_Delete_Node); return TRUE; } Binary_Search_Tree_Node* Search_Recursive_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key) { if ((Binary_Search_Tree*)Pointer_Binary_Search_Tree == NULL) return NULL; return (Binary_Search_Tree_Node*)Search_Internal_Recursive_Binary_Search_Tree((Binary_Search_Tree_Node*)Pointer_Binary_Search_Tree->Pointer_Root_Node, key); } Binary_Search_Tree_Node* Search_Internal_Recursive_Binary_Search_Tree(Binary_Search_Tree_Node* Pointer_Tree_Node, int key) { if ((Binary_Search_Tree_Node*)Pointer_Tree_Node == NULL) return NULL; else if (key == Pointer_Tree_Node->key) return (Binary_Search_Tree_Node*)Pointer_Tree_Node; else if (key < Pointer_Tree_Node->key) return Search_Internal_Recursive_Binary_Search_Tree((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Left_Child, key); return Search_Internal_Recursive_Binary_Search_Tree((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Right_Child, key); } Binary_Search_Tree_Node* Search_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree, int key) { if ((Binary_Search_Tree*)Pointer_Binary_Search_Tree == NULL) return NULL; Binary_Search_Tree_Node* Pointer_Return = (Binary_Search_Tree_Node*)Pointer_Binary_Search_Tree->Pointer_Root_Node; while ((Binary_Search_Tree_Node*)Pointer_Return != NULL && key != Pointer_Return->key) { if (key < Pointer_Return->key) (Binary_Search_Tree_Node*)Pointer_Return = (Binary_Search_Tree_Node*)Pointer_Return->Pointer_Left_Child; else (Binary_Search_Tree_Node*)Pointer_Return = (Binary_Search_Tree_Node*)Pointer_Return->Pointer_Right_Child; } return (Binary_Search_Tree_Node*)Pointer_Return; } void Delete_Binary_Search_Tree(Binary_Search_Tree* Pointer_Binary_Search_Tree) { if ((Binary_Search_Tree*)Pointer_Binary_Search_Tree == NULL) return; Delete_Binary_Search_Tree_Internal((Binary_Search_Tree_Node*)Pointer_Binary_Search_Tree->Pointer_Root_Node); free((Binary_Search_Tree*)Pointer_Binary_Search_Tree); return; } void Delete_Binary_Search_Tree_Internal(Binary_Search_Tree_Node* Pointer_Tree_Node) { if ((Binary_Search_Tree_Node*)Pointer_Tree_Node == NULL) return; Delete_Binary_Search_Tree_Internal((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Left_Child); Delete_Binary_Search_Tree_Internal((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Right_Child); free((Binary_Search_Tree_Node*)Pointer_Tree_Node); return; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <stdio.h> #include <stdlib.h> #include "binarysearchtree.h" void Display_Binary_Search_Tree_Internal(Binary_Search_Tree_Node* Pointer_Tree_Node, int level, char type); int main(int argc, char** argv) { Binary_Search_Tree_Node element[10] = { 0, }; int index = 0; for (index = 0; index < 10; index++) { element[index].value = (int)'A' + index; } element[0].key = 30; element[1].key = 20; element[2].key = 40; element[3].key = 10; element[4].key = 24; element[5].key = 34; element[6].key = 46; element[7].key = 6; element[8].key = 14; element[9].key = 22; Binary_Search_Tree* Pointer_Tree = (Binary_Search_Tree*)Create_Binary_Search_Tree(); if ((Binary_Search_Tree*)Pointer_Tree == NULL) return -1; for (index = 0; index < 10; index++) { Insert_Element_Binary_Search_Tree((Binary_Search_Tree*)Pointer_Tree, element[index]); } Display_Binary_Search_Tree_Internal((struct Binary_Search_Tree_Node_Type*)Pointer_Tree->Pointer_Root_Node, 0, 'R'); int key = 14; Binary_Search_Tree_Node* Pointer_Search_Node = (Binary_Search_Tree_Node*)Search_Binary_Search_Tree((Binary_Search_Tree*) Pointer_Tree, key); if ((Binary_Search_Tree_Node*)Pointer_Search_Node == NULL) { printf("Search : Key-[%d], No targets found :(\n", key); } else printf("Search : Key-[%d], result-[%c]\n", key, Pointer_Search_Node->value); key = 30; printf("Delete Data : Key-[%d]\n", key); Delete_Element_Binary_Search_Tree((Binary_Search_Tree*)Pointer_Tree, key); Display_Binary_Search_Tree_Internal((struct Binary_Search_Tree_Node_Type*)Pointer_Tree->Pointer_Root_Node, 0, 'R'); (Binary_Search_Tree_Node*) Pointer_Search_Node = (Binary_Search_Tree_Node*)Search_Binary_Search_Tree((Binary_Search_Tree*)Pointer_Tree, key); if ((Binary_Search_Tree_Node*)Pointer_Search_Node == NULL) { printf("Search : Key-[%d], No targets found :(\n", key); } else printf("Search : Key-[%d], result-[%c]\n", key, Pointer_Search_Node->value); Delete_Binary_Search_Tree((Binary_Search_Tree*)Pointer_Tree); return 0; } void Display_Binary_Search_Tree_Internal(Binary_Search_Tree_Node* Pointer_Tree_Node, int level, char type) { int loop = 0; for (loop = 0; loop < level; loop++) printf(" "); if ((Binary_Search_Tree_Node*)Pointer_Tree_Node == NULL) { printf("NULL\n"); return; } printf("-[%i,%c]%i-%c\n", level, type, Pointer_Tree_Node->key, Pointer_Tree_Node->value); Display_Binary_Search_Tree_Internal((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Left_Child, level + 1, 'L'); Display_Binary_Search_Tree_Internal((Binary_Search_Tree_Node*)Pointer_Tree_Node->Pointer_Right_Child, level + 1, 'R'); return; } | cs |