Generate a binary tree from a digital subaccount
/* Generate a binary tree by sub-statement * // For example, input: 1-2*3 + 4/(5 + 6)-7*8 # include
# Include
//////////////////////////////////////// //////////////////////////////////////// /////////////// Define the data structure # define MaxSize 50 typedef struct {int B; // indicates whether it is a number or an operator. If it is a number of 0, or an operator, it is also used to distinguish between the first operations of the same kind, int pri; int data;} Array; typedef struct BitNode {// ElemType data; Array data; struct BitNode * lchild, * rchild;} BitNode, * BiTree; //////////////////////////////////////// //////////////////////////////////////// //// // void Print (Array A [], int length) {int I = 0; while (I
Temp) C [k ++] = D [top --]; // if (top! =-1 & D [top]. pri = temp) {top --; // output stack '(' and discard ') 'continue;} D [++ top] = A [I]; // switch (A [I]. data) {// modify the intra-stack priority case '+': D [top]. pri = 3; break; case '-': D [top]. pri = 3; break; case '*': D [top]. pri = 5; break; case '/': D [top]. pri = 5; break; case '(': D [top]. pri = 1; break; case ')': D [top]. pri = 6; break;} elseC [k ++] = A [I]; // if it is A number, direct output} while (top! =-1) // C [k ++] = D [top --]; return k;} BiTree PostInCreateTree (Array A [], Array B [], int l1, int h1, int l2, int h2) {// construct a tree in the middle and back order. l1 and h1 are the minimum subscript and maximum subscript int I = 0, llen = 0, rlen = 0; BiTree T = (BiTree) malloc (sizeof (BitNode); T-> data = A [h1]; // A is the back order for (I = l2; ilchild = PostInCreateTree (A, B, l1, l1 + llen-1, l2, l2 + llen-1); elseT-> lchild = NULL; if (rlen) T-> rchild = PostInCreateTree (A, B, h1-rlen, h1-1, h2-rlen + 1, h2); elseT-> rchild = NULL; return T;} BiTr Ee PreInCreateTree (Array A [], Array B [], int l1, int h1, int l2, int h2) {// construct A tree in the middle and first order, l1, h1 is the minimum subscript and maximum subscript int I = 0, llen = 0, rlen = 0; BiTree T = (BiTree) malloc (sizeof (BitNode )); t-> data = A [l1]; // A is in the first order for (I = l2; ilchild = PreInCreateTree (A, B, l1 + 1, l1 + llen, l2, l2 + llen-1); elseT-> lchild = NULL; if (rlen) T-> rchild = PreInCreateTree (A, B, h1-rlen + 1, h1, h2-rlen + 1, h2 ); elseT-> rchild = NULL; return T;} void InOrder (BiTree T) {// perform a middle-order traversal to verify that the generated tree is correct if (T) {InOrder (T-> lchild); if (T-> data. B = 0) printf ("% d", T-> data. data); elseprintf ("% c", T-> data. data); InOrder (T-> rchild) ;}} int Calculate (BiTree T) {// use tree traversal to solve int l = 0, r = 0; if (T) {l = Calculate (T-> lchild); r = Calculate (T-> rchild); if (T-> data. b! = 0) switch (T-> data. data) {case '+': return l + r; case '-': return l-r; case '*': return l * r; case '/': return l/r; default: break;} else return T-> data. data ;}} int Calculate2 (Array A [], int length) {// use the ordinal sequence to solve Array D [MaxSize]; // stack int top =-1, I = 0; for (I = 0; I
Lchild); ReverseTree (T-> rchild); temp = T-> lchild; T-> lchild = T-> rchild; T-> rchild = temp ;}} int FormulaToPre (Array A [], Array C [], int length) {Array B [MaxSize]; Reverse (A, C, length); Array D [MaxSize]; // stack int top =-1, I = 0, k = 0, temp; for (I = 0; I
Temp) B [k ++] = D [top --]; // output stack} D [++ top] = C [I]; // inbound stack} elseB [k ++] = C [I]; // if it is a number, direct output} while (top! =-1) // output stack B [k ++] = D [top --]; Reverse (B, C, k); return k ;} // main function int main () {// input and process; char ch = 0; int temp, index = 0, q1 = 1, q2 = 1, q3 = 1, q4 = 1, q5 = 1, q6 = 1; int lenB = 0, lenC = 0, result = 0, result2 = 0; Array A [MaxSize], B [MaxSize], C [MaxSize]; Array B1 [MaxSize], C1 [MaxSize]; Array D [MaxSize]; int lenD; BiTree T, T2, T3; while (1) {scanf ("% c", & ch); if (ch> = 48 & ch <= 57) {// consecutively input a number into Multiple Digits temp = ch-48; while (1) {scanf ("% c", & ch); if (ch> = 48 & ch <= 57) temp = temp * 10 + ch-48; else break;} A [index]. data = temp; A [index]. B = 0; // indicates the number index ++;} switch (ch) {case '+': A [index]. data = ch; A [index]. B = q1 ++; A [index]. pri = 2; index ++; break; case '-': A [index]. data = ch; A [index]. B = q2 ++; A [index]. pri = 2; index ++; break; case '*': A [index]. data = ch; A [index]. B = q3 ++; A [index]. pri = 4; index ++; break; case '/': A [index]. data = ch; A [index]. B = q4 ++; A [index]. pri = 4; index ++; break; case '(': A [index]. data = ch; A [index]. B = q5 ++; A [index]. pri = 6; index ++; break; case ')': A [index]. data = ch; A [index]. B = q6 ++; A [index]. pri = 1; index ++; break; default: break;} if (ch = '#') break;} // Print (A, index ); lenB = ToInOrder (A, B, index); // convert to the middle order of the tree // Print (B, lenB); lenC = ToPostOrder (A, C, index ); // convert to the back order of the tree // Print (C, lenC); // Method 1: Middle Order + back order ====> binary tree T = PostInCreateTree (C, B, 0, lenC-1, 0, lenB-1); // generate Binary Tree // InOrder (T); // result = Calculate (T ); // printf ("\ n % d \ n", result); // result2 = Calculate2 (C, lenC); // printf ("% d \ n ", result2); // Method 2: Medium-> medium-Order 1, post-order-> first-order: Medium-Order 1 + first-order => binary tree, then, the inverted binary tree is the requested Binary Tree Reverse (B, B1, lenB); // obtain the central order Reverse (C, C1, lenC); // obtain the first order T2 = PreInCreateTree (C1, b1, 0, lenC-1, 0, lenB-1); ReverseTree (T2); InOrder (T2); printf ("\ n"); // method 3: sub-statements are converted to the first and middle orders. Binary Trees are directly generated from the first and middle orders. // The first is the middle order to the first order algorithm lenD = FormulaToPre (A, D, index ); // get the first order // printf ("% d \ n", lenD, lenB); // Print (D, lenD); T3 = PreInCreateTree (D, B, 0, lenD-1, 0, lenB-1); InOrder (T3); return 0 ;}