binary tree operation
"binarytree.h"
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 | #ifndef _BINARY_TREE_ #define _BINARY_TREE_ struct Binary_Tree_Node_Type { char data; struct Binary_Tree_Node_Type* Pointer_Left_Child; struct Binary_Tree_Node_Type* Pointer_Right_Child; }; struct Binary_Tree_Type { struct Binary_Tree_Node_Type* Pointer_Root_Node; }; typedef struct Binary_Tree_Node_Type Binary_Tree_Node; typedef struct Binary_Tree_Type Binary_Tree; Binary_Tree* Make_Binary_Tree(Binary_Tree_Node Root_Node); Binary_Tree_Node* Get_Root_Node_Binary_Tree(Binary_Tree* Pointer_Binary_Tree); Binary_Tree_Node* Insert_Left_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Parent_Node, Binary_Tree_Node element); Binary_Tree_Node* Insert_Right_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Parent_Node, Binary_Tree_Node element); Binary_Tree_Node* Get_Left_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Node); Binary_Tree_Node* Get_Right_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Node); void Delete_Binary_Tree(Binary_Tree* Pointer_Binary_Tree); void Delete_Binary_Tree_Node(Binary_Tree_Node* Pointer_Node); #endif // !_BINARY_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 | #ifndef _BINARY_TREE_OPERATION_ #define _BINARY_TREE_OPERATION_ #include "binarytree.h" Binary_Tree* Copy_Binary_Tree(Binary_Tree* Pointer_Source); Binary_Tree_Node* Copy_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source_Node); int Equal_Binary_Tree(Binary_Tree* Pointer_First, Binary_Tree* Pointer_Second); int Equal_Binary_Tree_Node(Binary_Tree_Node* Pointer_First, Binary_Tree_Node* Pointer_Second); int Get_Node_Count_Binary_Tree(Binary_Tree* Pointer_Source); int Get_Node_Count_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source); int Get_Leaf_Node_Count_Binary_Tree(Binary_Tree* Pointer_Source); int Get_Leaf_Node_Count_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source); int Get_Height_Binary_Tree(Binary_Tree* Pointer_Source); int Get_Height_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source); void Display_Binary_Tree(Binary_Tree* Pointer_Tree); void Display_Binary_Tree_Node(Binary_Tree_Node* Pointer_Node, int level, char type); #endif // !_BINARY_TREE_OPERATION_ | 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 | #include <stdio.h> #include <stdlib.h> #include "binarytree.h" Binary_Tree* Make_Binary_Tree(Binary_Tree_Node Root_Node) { Binary_Tree* Pointer_Return = NULL; (Binary_Tree*)Pointer_Return = (Binary_Tree*)malloc(1 * sizeof(Binary_Tree)); if ((Binary_Tree*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation(1). Make_Binary_Tree()\n"); return NULL; } (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node = (struct Binary_Tree_Node_Type*)malloc(1 * sizeof(struct Binary_Tree_Node_Type)); if ((struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node == NULL) { printf("Error! Dynamic memory allocation(2). Make_Binary_Tree()\n"); free((Binary_Tree*)Pointer_Return); return NULL; } *(struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node = *(struct Binary_Tree_Node_Type*)&Root_Node; (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node->Pointer_Left_Child = NULL; (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node->Pointer_Right_Child = NULL; return (Binary_Tree*)Pointer_Return; } Binary_Tree_Node* Insert_Left_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Parent_Node, Binary_Tree_Node element) { if ((Binary_Tree_Node*)Pointer_Parent_Node == NULL) { printf("Error! Binary tree node is null. Insert_Left_Child_Node_Binary_Tree()\n"); return NULL; } else if ((struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child != NULL) { printf("Error! The node already exists. Insert_Left_Child_Node_Binary_Tree()\n"); return NULL; } (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = (struct Binary_Tree_Node_Type*)malloc(1 * sizeof(struct Binary_Tree_Node_Type)); if ((struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child == NULL) { printf("Error! Dynamic memory allocation. Insert_Left_Child_Node_Binary_Tree()\n"); return NULL; } *(struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child = *(struct Binary_Tree_Node_Type*)&element; (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child->Pointer_Left_Child = NULL; (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Left_Child->Pointer_Right_Child = NULL; return (Binary_Tree_Node*)Pointer_Parent_Node->Pointer_Left_Child; } Binary_Tree_Node* Insert_Right_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Parent_Node, Binary_Tree_Node element) { if ((Binary_Tree_Node*)Pointer_Parent_Node == NULL) { printf("Error! Binary tree node is null. Insert_Right_Child_Node_Binary_Tree()\n"); return NULL; } else if ((struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child != NULL) { printf("Error! The node already exists. Insert_Right_Child_Node_Binary_Tree()\n"); return NULL; } (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = (struct Binary_Tree_Node_Type*)malloc(1 * sizeof(struct Binary_Tree_Node_Type)); if ((struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child == NULL) { printf("Error! Dynamic memory allocation. Insert_Right_Child_Node_Binary_Tree()\n"); return NULL; } *(struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child = *(struct Binary_Tree_Node_Type*)&element; (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child->Pointer_Left_Child = NULL; (struct Binary_Tree_Node_Type*)Pointer_Parent_Node->Pointer_Right_Child->Pointer_Right_Child = NULL; return (Binary_Tree_Node*)Pointer_Parent_Node->Pointer_Right_Child; } Binary_Tree_Node* Get_Root_Node_Binary_Tree(Binary_Tree* Pointer_Binary_Tree) { if ((Binary_Tree*)Pointer_Binary_Tree == NULL) return NULL; return (Binary_Tree_Node*)Pointer_Binary_Tree->Pointer_Root_Node; } Binary_Tree_Node* Get_Left_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Node) { if ((Binary_Tree_Node*)Pointer_Node == NULL) return NULL; return (Binary_Tree_Node*)Pointer_Node->Pointer_Left_Child; } Binary_Tree_Node* Get_Right_Child_Node_Binary_Tree(Binary_Tree_Node* Pointer_Node) { if ((Binary_Tree_Node*)Pointer_Node == NULL) return NULL; return (Binary_Tree_Node*)Pointer_Node->Pointer_Right_Child; } void Delete_Binary_Tree(Binary_Tree* Pointer_Binary_Tree) { if ((Binary_Tree*)Pointer_Binary_Tree == NULL) return; Delete_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Binary_Tree->Pointer_Root_Node); free((Binary_Tree*)Pointer_Binary_Tree); return; } void Delete_Binary_Tree_Node(Binary_Tree_Node* Pointer_Node) { if ((Binary_Tree_Node*)Pointer_Node == NULL) return; Delete_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Node->Pointer_Left_Child); Delete_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Node->Pointer_Right_Child); free((Binary_Tree_Node*)Pointer_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 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 | #include <stdio.h> #include <stdlib.h> #include "binarytree.h" #include "binarytreeoperation.h" Binary_Tree* Copy_Binary_Tree(Binary_Tree* Pointer_Source) { if ((Binary_Tree*)Pointer_Source == NULL) { printf("Error! The binary tree is null. Copy_Binary_Tree()\n"); return NULL; } Binary_Tree* Pointer_Return = (Binary_Tree*)malloc(1 * sizeof(Binary_Tree)); if ((Binary_Tree*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation. Copy_Binary_Tree()\n"); return NULL; } (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Root_Node = (struct Binary_Tree_Node_Type*)Copy_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Root_Node); return (Binary_Tree*)Pointer_Return; } Binary_Tree_Node* Copy_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source_Node) { if ((Binary_Tree_Node*)Pointer_Source_Node == NULL) return NULL; Binary_Tree_Node* Pointer_Return = (Binary_Tree_Node*)malloc(1 * sizeof(Binary_Tree_Node)); if ((Binary_Tree_Node*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation. Copy_Binary_Tree_Node()\n"); return NULL; } *(Binary_Tree_Node*)Pointer_Return = *(Binary_Tree_Node*)Pointer_Source_Node; (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Left_Child = (struct Binary_Tree_Node_Type*)Copy_Binary_Tree_Node((Binary_Tree_Node*)Get_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Source_Node)); (struct Binary_Tree_Node_Type*)Pointer_Return->Pointer_Right_Child = (struct Binary_Tree_Node_Type*)Copy_Binary_Tree_Node((Binary_Tree_Node*)Get_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Source_Node)); return (Binary_Tree_Node*)Pointer_Return; } int Equal_Binary_Tree(Binary_Tree* Pointer_First, Binary_Tree* Pointer_Second) { int Root_Node_result = (int)Equal_Binary_Tree_Node((Binary_Tree_Node*)Pointer_First->Pointer_Root_Node, (Binary_Tree_Node*)Pointer_Second->Pointer_Root_Node); if ((Binary_Tree*)Pointer_First == NULL && (Binary_Tree*)Pointer_Second == NULL || (Binary_Tree*)Pointer_First != NULL && (Binary_Tree*)Pointer_Second != NULL && Root_Node_result == TRUE) return TRUE; return FALSE; } int Equal_Binary_Tree_Node(Binary_Tree_Node* Pointer_First, Binary_Tree_Node* Pointer_Second) { if ((Binary_Tree*)Pointer_First == NULL && (Binary_Tree*)Pointer_Second == NULL) return TRUE; int Left_Node_Result = (int)Equal_Binary_Tree_Node((Binary_Tree_Node*)Pointer_First->Pointer_Left_Child, (Binary_Tree_Node*)Pointer_Second->Pointer_Left_Child); int Right_Node_Result = (int)Equal_Binary_Tree_Node((Binary_Tree_Node*)Pointer_First->Pointer_Right_Child, (Binary_Tree_Node*)Pointer_Second->Pointer_Right_Child); if ((Binary_Tree*)Pointer_First != NULL && (Binary_Tree*)Pointer_Second != NULL && Pointer_First->data == Pointer_Second->data && Left_Node_Result == TRUE && Right_Node_Result == TRUE) return TRUE; return FALSE; } int Get_Node_Count_Binary_Tree(Binary_Tree* Pointer_Source) { if ((Binary_Tree*)Pointer_Source == NULL) return FALSE; return (int)Get_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Root_Node); } int Get_Node_Count_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source) { if ((Binary_Tree_Node*)Pointer_Source == NULL) return FALSE; return (int)Get_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Left_Child) + ((int)Get_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Right_Child) + 1); } int Get_Leaf_Node_Count_Binary_Tree(Binary_Tree* Pointer_Source) { if ((Binary_Tree*)Pointer_Source == NULL) return FALSE; return (int)Get_Leaf_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Root_Node); } int Get_Leaf_Node_Count_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source) { if ((Binary_Tree_Node*)Pointer_Source == NULL) return FALSE; else if ((struct Binary_Tree_Node_Type*)Pointer_Source->Pointer_Left_Child == NULL && (struct Binary_Tree_Node_Type*)Pointer_Source->Pointer_Right_Child == NULL) return 1; return (int)(Get_Leaf_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Left_Child) + Get_Leaf_Node_Count_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Right_Child)); } int Get_Height_Binary_Tree(Binary_Tree* Pointer_Source) { if ((Binary_Tree*)Pointer_Source == NULL) return FALSE; return (int)Get_Height_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Root_Node); } int Get_Height_Binary_Tree_Node(Binary_Tree_Node* Pointer_Source) { if ((Binary_Tree_Node*)Pointer_Source == NULL) return FALSE; else if ((struct Binary_Tree_Node_Type*)Pointer_Source->Pointer_Left_Child == NULL && (struct Binary_Tree_Node_Type*)Pointer_Source->Pointer_Right_Child == NULL) return 1; int Left_Child_Height = (int)Get_Height_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Left_Child); int Right_Child_Height = (int)Get_Height_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Source->Pointer_Right_Child); if (Left_Child_Height >= Right_Child_Height) return ++Left_Child_Height; return ++Right_Child_Height; } void Display_Binary_Tree(Binary_Tree* Pointer_Tree) { if ((Binary_Tree*)Pointer_Tree == NULL) { printf("The Tree is null.\n"); return; } Display_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Tree->Pointer_Root_Node, 0, 'O'); return; } void Display_Binary_Tree_Node(Binary_Tree_Node* Pointer_Node, int level, char type) { int loop = 0; for (loop = 0; loop < level; loop++) { printf(" "); } if ((Binary_Tree_Node*)Pointer_Node == NULL) { printf("NULL\n"); return; } printf("-[%i, %c]%c\n", level, type, Pointer_Node->data); Display_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Node->Pointer_Left_Child, level + 1, 'L'); Display_Binary_Tree_Node((Binary_Tree_Node*)Pointer_Node->Pointer_Right_Child, level + 1, 'R'); 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <stdio.h> #include <stdlib.h> #include "binarytree.h" #include "binarytreeoperation.h" Binary_Tree* Create_Example_Binary_Tree(void); int main(int argc, char** argv) { Binary_Tree* Pointer_Binary_Tree = (Binary_Tree*)Create_Example_Binary_Tree(); printf("Original binary tree:\n"); Display_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); Binary_Tree* Pointer_Copy_Binary_Tree = (Binary_Tree*)Copy_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); printf("Copy binary tree:\n"); Display_Binary_Tree((Binary_Tree*)Pointer_Copy_Binary_Tree); int Compare_Result = (int)Equal_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree, (Binary_Tree*)Pointer_Copy_Binary_Tree); printf("\nCompare result : (%d)\n", Compare_Result); int count = (int)Get_Node_Count_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); printf("\nNumber of node : %d\n", count); count = (int)Get_Leaf_Node_Count_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); printf("\nNumber of leaf node : %d\n", count); count = (int)Get_Height_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); printf("\nNumber of height : %d\n", count); Delete_Binary_Tree((Binary_Tree*)Pointer_Binary_Tree); (Binary_Tree*)Pointer_Binary_Tree = NULL; Delete_Binary_Tree((Binary_Tree*)Pointer_Copy_Binary_Tree); (Binary_Tree*)Pointer_Copy_Binary_Tree = NULL; return 0; } Binary_Tree* Create_Example_Binary_Tree(void) { Binary_Tree_Node node = { 0, }; node.data = 'A'; Binary_Tree* Pointer_Return = (Binary_Tree*)Make_Binary_Tree(node); if ((Binary_Tree*)Pointer_Return == NULL) return NULL; Binary_Tree_Node* Pointer_Node_A = (Binary_Tree_Node*)Get_Root_Node_Binary_Tree((Binary_Tree*)Pointer_Return); node.data = 'B'; Binary_Tree_Node* Pointer_Node_B = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_A, node); if ((Binary_Tree_Node*)Pointer_Node_B == NULL) return NULL; node.data = 'C'; Binary_Tree_Node* Pointer_Node_C = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_A, node); if ((Binary_Tree_Node*)Pointer_Node_C == NULL) return NULL; node.data = 'D'; Binary_Tree_Node* Pointer_Node_D = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_B, node); if ((Binary_Tree_Node*)Pointer_Node_D == NULL) return NULL; node.data = 'E'; Binary_Tree_Node* Pointer_Node_E = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_B, node); if ((Binary_Tree_Node*)Pointer_Node_E == NULL) return NULL; node.data = 'F'; Binary_Tree_Node* Pointer_Node_F = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_C, node); if ((Binary_Tree_Node*)Pointer_Node_F == NULL) return NULL; node.data = 'G'; Binary_Tree_Node* Pointer_Node_G = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_C, node); if ((Binary_Tree_Node*)Pointer_Node_G == NULL) return NULL; node.data = 'H'; Binary_Tree_Node* Pointer_Node_H = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_D, node); if ((Binary_Tree_Node*)Pointer_Node_H == NULL) return NULL; node.data = 'I'; Binary_Tree_Node* Pointer_Node_I = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_D, node); if ((Binary_Tree_Node*)Pointer_Node_I == NULL) return NULL; node.data = 'J'; Binary_Tree_Node* Pointer_Node_J = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_E, node); if ((Binary_Tree_Node*)Pointer_Node_J == NULL) return NULL; node.data = 'K'; Binary_Tree_Node* Pointer_Node_K = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_F, node); if ((Binary_Tree_Node*)Pointer_Node_K == NULL) return NULL; node.data = 'L'; Binary_Tree_Node* Pointer_Node_L = (Binary_Tree_Node*)Insert_Left_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_G, node); if ((Binary_Tree_Node*)Pointer_Node_L == NULL) return NULL; node.data = 'M'; Binary_Tree_Node* Pointer_Node_M = (Binary_Tree_Node*)Insert_Right_Child_Node_Binary_Tree((Binary_Tree_Node*)Pointer_Node_G, node); if ((Binary_Tree_Node*)Pointer_Node_M == NULL) return NULL; return (Binary_Tree*)Pointer_Return; } | cs |
0 개의 댓글:
댓글 쓰기