typedef struct node* tree_pointer;
typedef struct node{
int data;
tree_pointer left_child,right_child;
};
//중위순회
void inorder(tree_pointer ptr)
{
if(ptr){
inorder(ptr->left_child);
printf("%d\n",ptr->data);
inorder(ptr->right_child);
}
}
//전위순회
void preorder(tree_pointer ptr)
{
if(ptr){
printf("%d\n",ptr->data);
preorder(ptr->left_child);
preorder(ptr->right_child);
}
}
//후위순회
void postorder(tree_pointer ptr)
{
if(ptr){
postorder(ptr->left_child);
postorder(ptr->right_child);
printf("%d\n",ptr->data);
}
}
//반복적인 중위순회(stack)
void iter_inorder(tree_pointer node)
{
int top = -1;
tree_pointer stack[MAX_STACK_SIZE];
for( ; ; ){
for( ; node; node->left_child)
add(&top,node);
node = delete(&top);
if(!node) break;
printf("%d\n", ptr->data);
node = node->right_child;
}
}
//레벨순서 트리 순회
void level_order(tree_pointer ptr)
{
int front,rear = 0;
tree_pointer queue[MAX_QUEUE_SIZE];
if (!ptr) return;
addq(front, &rear, ptr);
for( ; ; ){
ptr = deleteq(&front, rear);
if(ptr){
printf("%d\n",ptr->data);
if(ptr->left_child)
addq(front,&rear,ptr->left_child);
if(ptr->right_child)
addq(front,&rear,ptr->right_child);
}
else break;
}
}
/////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct Treenode{
int data;
struct Treenode *left,*right;
}Treenode;
Treenode n1={4,NULL,NULL};
Treenode n2={2,&n1,NULL};
Treenode n3={5,NULL,NULL};
Treenode n4={6,NULL,NULL};
Treenode n5={3,&n3,&n4};
Treenode n6={1,&n2,&n5};
Treenode* root = &n6;
inorder(Treenode *root){
if(root){
inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
}
preorder(Treenode* root){
if(root){
printf("%d->",root->data);
preorder(root->left);
preorder(root->right);
}
}
postorder(Treenode* root){
if(root){
postorder(root->left);
postorder(root->right);
printf("%d->",root->data);
}
}
void main()
{
printf("중위순회 : ");
inorder(root);
printf("\n");
printf("전위순회 : ");
preorder(root);
printf("\n");
printf("후위순회 : ");
postorder(root);
printf("\n");
}