创建一棵后缀表达式树

运行时输入:

 abc*+de*f+g*+
 1 BinTree CreateExprTree()
 2 {
 3     std::stack<BinTree> S;
 4     BinTree T, Tl, Tr, BT;
 5 
 6     ElementType dt;
 7     scanf_s("%c", &dt);
 8     while (dt >= 'a' && dt <= 'z')
 9     {
10         T = (BinTree)malloc(sizeof(struct TNode));
11         T->Data = dt;
12         T->Left = T->Right = NULL;
13         S.push(T);
14         scanf_s("%c", &dt);
15     }
16     while (dt != '\n')
17     {
18         if (dt >= 'a' && dt <= 'z')
19         {
20             T = (BinTree)malloc(sizeof(struct TNode));
21             T->Data = dt;
22             T->Left = T->Right = NULL;
23             S.push(T);
24             scanf_s("%c", &dt);
25         }
26         else 
27         {
28             T = (BinTree)malloc(sizeof(struct TNode));
29             T->Data = dt;
30             Tr = S.top();
31             S.pop();
32             Tl = S.top();
33             S.pop();
34             T->Left = Tl;
35             T->Right = Tr;
36             S.push(T);
37             scanf_s("%c", &dt);
38         }
39         
40     }
41     BT = S.top();
42     
43     return BT;
44 }

 

上一篇:数据人如何提升自己


下一篇:TPLINK家庭网络组网配置