Binary tree _Alpha (need modify)
"binary tree.h"
"binary tree.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 | #ifndef _BINARY_TREE_ #define _BINARY_TREE_ struct Child_Node_Type_Of_Binary_Tree { char data; struct Child_Node_Type_Of_Binary_Tree* Pointer_Left_Child; struct Child_Node_Type_Of_Binary_Tree* Pointer_Right_Child; }; struct Parents_Node_Type_Of_Binary_Tree { struct Child_Node_Type_Of_Binary_Tree* Pointer_Root; }; struct Binary_Tree_Type { int Number_Of_Current_Elements; struct Parents_Node_Type_Of_Binary_Tree* Header; }; struct Binary_Tree_Type* Create_Binary_Tree(void); int Insert_Binary_Tree_Element(struct Binary_Tree_Type* Pointer_Binary_Tree, struct Child_Node_Type_Of_Binary_Tree element); void Display_Binary_Tree(struct Binary_Tree_Type* Pointer_Binary_Tree, int level, char type); #endif // !_BINARY_TREE_ #ifndef _COMMON_DEFINITION_ #define _COMMON_DEFINITION_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_DEFINITION_ | 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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "binary tree.h" struct Binary_Tree_Type* Create_Binary_Tree(void) { struct Binary_Tree_Type* Pointer_Binary_Tree = (struct Binary_Tree_Type*) malloc(1 * sizeof(struct Binary_Tree_Type)); if ((struct Binary_Tree_Type*)Pointer_Binary_Tree == NULL) { printf("Error! The dynamic memory allocation failed. Create_Binary_Tree()\n"); return NULL; } (void*)memset((struct Binary_Tree_Type*)Pointer_Binary_Tree, 0, 1 * sizeof(struct Binary_Tree_Type)); Pointer_Binary_Tree->Header = (struct Parents_Node_Type_Of_Binary_Tree*) malloc(1 * sizeof(struct Parents_Node_Type_Of_Binary_Tree)); if ((struct Parents_Node_Type_Of_Binary_Tree*)Pointer_Binary_Tree->Header == NULL) { printf("Error! The dynamic memory allocation failed(2). Create_Binary_Tree()\n"); free((struct Binary_Tree_Type*)Pointer_Binary_Tree); return NULL; } (void*)memset((struct Parents_Node_Type_Of_Binary_Tree*) Pointer_Binary_Tree->Header, 0, 1 * sizeof(struct Parents_Node_Type_Of_Binary_Tree)); return (struct Binary_Tree_Type*)Pointer_Binary_Tree; } int Insert_Binary_Tree_Element(struct Binary_Tree_Type* Pointer_Binary_Tree, struct Child_Node_Type_Of_Binary_Tree element) { if ((struct Binary_Tree_Type*)Pointer_Binary_Tree == NULL) { printf("Error! The binary tree was null. Insert_Binary_Tree_Element()\n"); return FALSE; } struct Child_Node_Type_Of_Binary_Tree* Pointer_New_Node = (struct Child_Node_Type_Of_Binary_Tree*) malloc(1 * sizeof(struct Child_Node_Type_Of_Binary_Tree)); if ((struct Child_Node_Type_Of_Binary_Tree*)Pointer_New_Node == NULL) { printf("Error! The dynamic memory allocation failed. Inset_Binary_Tree_Element()\n"); return FALSE; } (void*)memset((struct Child_Node_Type_Of_Binary_Tree*) Pointer_New_Node, 0, 1 * sizeof(struct Child_Node_Type_Of_Binary_Tree)); *Pointer_New_Node = *(struct Child_Node_Type_Of_Binary_Tree*) &element; struct Parents_Node_Type_Of_Binary_Tree* Pointer_Header_Node = (struct Parents_Node_Type_Of_Binary_Tree*) Pointer_Binary_Tree->Header; // first inset if ((struct Parents_Node_Type_Of_Binary_Tree*)Pointer_Header_Node->Pointer_Root == NULL) { Pointer_Header_Node->Pointer_Root = (struct Child_Node_Type_Of_Binary_Tree*)Pointer_New_Node; Pointer_Binary_Tree->Number_Of_Current_Elements++; return TRUE; } // else insert struct Child_Node_Type_Of_Binary_Tree* Pointer_Current_Node = (struct Child_Node_Type_Of_Binary_Tree*) Pointer_Header_Node->Pointer_Root; while ((struct Child_Node_Type_Of_Binary_Tree*)Pointer_Current_Node != NULL) { // when less then the parents node if (Pointer_Current_Node->data > Pointer_New_Node->data) { if ((struct Child_Node_Type_Of_Binary_Tree*)Pointer_Current_Node->Pointer_Left_Child == NULL) break; Pointer_Current_Node = (struct Child_Node_Type_Of_Binary_Tree*) Pointer_Current_Node->Pointer_Left_Child; } // when larger then the parents node else if (Pointer_Current_Node->data < Pointer_New_Node->data) { if ((struct Child_Node_Type_Of_Binary_Tree*)Pointer_Current_Node->Pointer_Right_Child == NULL) break; Pointer_Current_Node = (struct Child_Node_Type_Of_Binary_Tree*)Pointer_Current_Node->Pointer_Right_Child; } // when inserting duplicate data else { printf("Error! Duplicate data cannot be inserted. Insert_Binary_Tree_Element()\n"); free((struct Child_Node_Type_Of_Binary_Tree*)Pointer_New_Node); return FALSE; } } // when less then the parents node if (Pointer_Current_Node->data > Pointer_New_Node->data) { Pointer_Current_Node->Pointer_Left_Child = (struct Child_Node_Type_Of_Binary_Tree*) Pointer_New_Node; } // when larger then the parents node else if (Pointer_Current_Node->data < Pointer_New_Node->data) { Pointer_Current_Node->Pointer_Right_Child = (struct Child_Node_Type_Of_Binary_Tree*)Pointer_New_Node; } Pointer_Binary_Tree->Number_Of_Current_Elements++; return TRUE; } | 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 | #include <stdio.h> #include <stdlib.h> #include <time.h> #include "binary tree.h" int main(int argc, char* argv[]) { typedef struct Binary_Tree_Type Binary_Tree; typedef struct Parents_Node_Type_Of_Binary_Tree Parents_Node; typedef struct Child_Node_Type_Of_Binary_Tree Child_Node; Binary_Tree* Pointer_Tree = (Binary_Tree*)Create_Binary_Tree(); Child_Node node = { 0, }; srand((unsigned)time(NULL)); int loop = 0; for (loop = 0; loop < 10; loop++) { node.data = rand() % 30; Insert_Binary_Tree_Element((Binary_Tree*)Pointer_Tree, node); } system("pause"); return 0; } | cs |