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 | #include <stdio.h> #include <stdlib.h> struct tree { struct tree* left; // d1 7 int data; // d2 3 2 struct tree* right; // d3 1 4 5 9 }; // d4 11 6 10 21 12 13 17 19 struct tree* NodeInsert(int data) { struct tree* node = (struct tree*)malloc(sizeof(struct tree)); //공간 할당 node->left = NULL; node->right = NULL; node->data = data; return node; } void LNR(struct tree*); void NLR(int); void LRN(int); int main(void) { struct tree* root; root = NodeInsert(7); root->left = NodeInsert(3); root->left->right = NodeInsert(4); root->right = NodeInsert(2); root->right->left = NodeInsert(5); root->left->left = NodeInsert(1); root->right->right = NodeInsert(9); root->left->left->left = NodeInsert(11); root->right->left->right = NodeInsert(12); root->left->left->right = NodeInsert(6); root->right->left->left = NodeInsert(13); root->left->right->left = NodeInsert(10); root->right->right->left = NodeInsert(17); root->left->right->right = NodeInsert(21); root->right->right->right = NodeInsert(19); LNR(root); return 0; } void LNR(struct tree* node) { if (node == NULL) return; LNR(node->left); printf("%3d", node->data); LNR(node->right); } | cs |
야스
이젠 재귀가 아닌 루프로 traversal을 구현해보시오
메모 - dc App
b tree 구현해줘