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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#pragma warning(disable: 4996)
#define SSIZE 5
 
typedef struct _tree
{
    int key;
    struct _tree *left;
    struct _tree *right;
}Tree;
 
void preorder(Tree *p)
{
    if (p != NULL)
    {
        printf("%d\t", p->key);
        preorder(p->left);
        preorder(p->right);
    }
 
}
 
void inorder(Tree *p)
{
    if (p != NULL)
    {
 
        inorder(p->left);
        printf("%d\t", p->key);
        inorder(p->right);
    }
 
}
 
void postorder(Tree *p)
{
    if (p != NULL)
    {
 
        postorder(p->left);
        postorder(p->right);
        printf("%d\t", p->key);
    }
 
}
 
Tree * stack[SSIZE];
int top;
 
void init_stack(void)
{
    top = -1;
}
 
void push(Tree * data)
{
    if (top >= SSIZE - 1)
    {
        printf("stack overflow");
        return;
    }
    stack[++top] = data;
 
}
 
Tree * pop(void)
{
    if (top == -1)
    {
        printf("stack empty");
        return NULL;
    }
 
    return stack[top--];
}
 
void disp()
{
    int i;
    for (i = 0; i < top + 1; i++)
    {
        printf("%d\n"stack[i]);
    }
}
 
int stack_empty()
{
    if (top == -1//stack이 비어있음
    {
        return 1;
    }
    else
        return 0//stack이 비어있지 않음
}
 
void preorder_stack(Tree *p)
{
    //루트 push
    //스택이 비어있지 않으면 빼고 출력
    //뺀 좌우측 노드가 있으면 우측push 좌측 push
 
    init_stack();
    push(p);
    while (!stack_empty()) //stack이 비어있지 않으면 돌아가야함
    {
        p = pop();
        printf("%d\t", p->key);
        if (p->right != NULL)
        {
            push(p->right);
        }
        if (p->left != NULL)
        {
            push(p->left);
        }
 
    }
}
 
int main()
{
    Tree a1 = { 1,NULL,NULL },
        b2 = { 2,NULL,NULL },
        c3 = { 3,NULL,NULL },
        d4 = { 4,NULL,NULL },
        e5 = { 5,NULL,NULL };
 
    c3.left = &a1; //                        c3
    a1.right = &b2; //                a1                d4
    c3.right = &d4; //                    b2                e5
    d4.right = &e5; //
 
    printf("전위 재귀함수\n");
    preorder(&c3);
    puts("\n\n");// 근좌우
    printf("전위 스택\n"); preorder_stack(&c3); puts("\n\n");// 근좌우
 
    printf("중위 재귀함수\n");
    inorder(&c3);
    puts("\n\n");// 좌근우 tree sort - 균형잡힌 이진트리 구현할때 절대적 필요
 
    printf("후위 재귀함수\n");
    postorder(&c3);
    puts("\n\n");// 좌우근
 
}
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

+ Recent posts