/home/toolbox/public_html/solutions/5/536/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14 #define MAX_LIST 27
15 #define VAL 0
16 #define RIGHT 1
17 #define LEFT 2
18
19 /*
20 * Author:
21 * Date:
22 * Purpose:
23 * Problem:
24 */
25
26 /*
27 * This template reads data until a terminating value is reached.
28 */
29
30 char preorder[MAX_LIST];
31 char inorder[MAX_LIST];
32 char postorder[MAX_LIST];
33 int orderLength;
34 int tree[MAX_LIST][3];
35 int nextNode = 0;
36
37 void init()
38 {
39 /* FUNCTION init */
40 int i;
41
42 for (i=0; i<orderLength; i++)
43 {
44 /* for each item */
45 tree[i][VAL] = 0;
46 tree[i][RIGHT] = 0;
47 tree[i][LEFT] = 0;
48 } /* for each item */
49 inorder[1 + orderLength] = 0;
50 } /* FUNCTION init */
51
52 void dump()
53 {
54 /* FUNCTION dump */
55 printf("in: [%s] pre: [%s] post: [%s]\n", inorder, preorder, postorder);
56 } /* FUNCTION dump */
57
58 int getInput()
59 {
60 /* FUNCTION getInput */
61 int dataReadFlag;
62
63 dataReadFlag = (0 < scanf(" %s %s ", preorder, inorder));
64 return (dataReadFlag);
65 } /* FUNCTION getInput */
66
67 int findVal(int cur)
68 {
69 /* FUNCTION findVal */
70 int notFound = FALSE;
71 int i;
72
73 for (i=0; (notFound) && (i<orderLength); i++)
74 {
75 /* for i */
76 notFound = (preoder[cur] != inorder[i]);
77 } /* for i */
78 return i;
79 } /* FUNCTION findVal */
80
81
82 void insertNode(int i, int start)
83 {
84 /* FUNCTION insertNode */
85 int orderHead;
86 int orderVal;
87
88 orderVal = findVal(i);
89 orderHead = findVal(start);
90 if (orderVal < OrderHead)
91 {
92 /* new value less than current */
93 if (0 == tree[start][LEFT])
94 {
95 /* no left tree -- insert node */
96 tree[start][LEFT] = nextNode;
97 tree[nextNode++][VAL] = i;
98 } /* no left tree -- insert node */
99 if (0 == start)
100 {
101 /* head node insert */
102 if
103 } /* head node insert */
104 } /* new value less than current */
105
106 } /* FUNCTION insertNode */
107
108 void process()
109 {
110 /* FUNCTION process */
111
112 orderLength=strlen(preorder);
113 init();
114 tree[nextNode++][VAL] = 0;
115
116 for (i=1; i<orderLength; i++)
117 {
118 /* for each char */
119 insertNode(i, 0);
120 } /* for each char */
121
122 } /* FUNCTION process */
123
124 int main ()
125 {
126 /* main */
127 int moreToDo;
128
129 moreToDo = getInput();
130 while (moreToDo)
131 {
132 /* while */
133 process();
134 dump();
135 moreToDo = getInput();
136 } /* while */
137
138 return EXIT_SUCCESS;
139 } /* main */
140
141
142