Tree Example
#include <stdio.h>
struct _list_node
{
int num;
char ch;
struct _list_node* nodePtr;
};
{
int num;
char ch;
struct _tree_node* left;
struct _tree_node* right;
};
/* 구성할 트리의 구조
A
B C
D E F G
H I J
*/
void printTree(struct _tree_node* root);
void printList(struct _list_node* root);
int main(void) {
struct _list_node list[] = {{1,'A'},{2,'B'},{3,'C'},{4,'D'},{5,'E'}};
{1,'A'},{2,'B'},{3,'C'},{4,'D'},{5,'E'},
{6,'F'},{7,'G'},{8,'H'},{9,'I'},{10,'J'}
};
/* LinkedList를 구성한다 */
list[0].nodePtr = &list[1];
list[1].nodePtr = &list[2];
list[2].nodePtr = &list[3];
list[3].nodePtr = &list[4];
/* LinkedList의 각 노드를 순회하면서 출력한다 */
puts("List 출력");
printList(&list[0]);
puts("\nList 끝");
/* Tree를 구성한다 */
tree[0].left = &tree[1];
tree[0].right = &tree[2];
tree[1].left = &tree[3];
tree[1].right = &tree[4];
tree[2].left = &tree[5];
tree[2].right = &tree[6];
tree[3].left = &tree[7];
tree[3].right = &tree[8];
tree[4].left = &tree[9];
/* Tree의 각 노드를 순회하면서 출력한다 */
puts("\nTree 출력");
printTree(&tree[0]);
puts("\nTree 끝");
return 0;
}
void printList(struct _list_node* root){
if(root==NULL) return;
printf("%c ", root->ch);
printList(root->nodePtr);
}
/* 트리 전위 순회(pre-order) */
printf("%c ", root->ch);
if(root->left !=NULL) printTree(root->left);
if(root->right !=NULL) printTree(root->right);
}