Two-fork Tree
The leaf of the expression tree is the operand (operand), such as a constant or variable, while the other node is the operator (operator). Here the qualified operator can only be +,-,*,/four operators.
To transform a suffix expression into an expression tree:
Program:
Expression_tree.h
struct treenode;typedef struct TreeNode *ptrtonode;typedef ptrtonode tree;typedef char Type; Ptrtonode createnode (char ch); struct treenode{Type Element; Tree left; Tree right;};
Expression_tree.c
#include "expression_tree.h" #include <stdlib.h> #include <stdio.h>ptrtonode createnode (char ch) { Ptrtonode p=malloc (sizeof (struct TreeNode)); if (p==null) {printf ("The node is NULL!\n"); return p; } p->element=ch; return p; }
Stack.h
#include "expression_tree.h" typedef tree ElementType; #ifndef _stack_hstruct node;typedef struct Node *stack;int IsEmpty (stack s); int isfull (stack s); Stack createstack (int maxelements); void Disposestack (stack s); void Makeempty (stack s); void Push (ElementType X, Stack s) ; ElementType Top (stack s); void Pop (stack s); ElementType Topandpop (Stack S), #define EMPTYTOS ( -1) #define MINSTACKSIZE (5) struct node{int capacity; int topofstack; ElementType *array;}; #endif
Stack.c
#include "stack.h" #include <stdio.h> #include <error.h> #include <stdlib.h>int isempty ( Stack s) { return s->topofstack==emptytos;} Int isfull (stack s) { return s->topofstack== (S->Capacity-1);} Stack createstack ( int MaxElements ) { stack s; if (maxelements<minstacksize) error ("Stack size is too small"); s=malloc (sizeof (Struct node)), if (s==null) error ("Out of space!!! "); s->array=malloc (sizeof (ElementType) *maxelements); if (S-> Array==null) error ("Out of space!!!"); s->capacity=maxelements; makeempty (S); return s;} Void disposestack (stack s) { if (s!=null) &nbsP; { free (S->array); free (S); }}void makeempty (Stack S) { s->topofstack=emptytos;} Void push (elementtype x,stack s) { if (Isfull (S)) error ("Full Stack "); else { s->topofstack++; s->array[s->topofstack]=x; }}elementtype top ( stack s) { if (! IsEmpty (S)) Return s->array[s->topofstack]; error ("empty stack 3 "); return 0;} Void pop ( stack s) { if (IsEmpty (S)) error ("Empty stack 2"); else s->topofstack--;} Elementtype topandpop ( stack s) { if (!IsEmpty (S)) {return S->Array[ S->TopOfStack--]; } error ("empty stack 1"); return 0;}
Mainprogram.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" Tree createtree ( CHAR*&NBSP;STR) { tree t,p,q; int len=strlen (str); stack s=createstack (len); int i; for (i=0;i<len;++i) {switch (str[i]) { case ' + ' : case '-': case ' * ': case '/': {t=createnode (Str[i]); p=topandpop (s); Q=topandpop (s); t->left=q; t->right=p; Push (t,s); Break; } default: {t=createnode (Str[i]); Push (t,s); Break; }} } return top (S);} Void outtree (tree t) { if (t!=null) { outtree (T->left); outtree (t-> right); printf ("%c", t->element); }} Int main () { char* ss= "ab+cde+**"; tree tt= Createtree (ss); outtree (TT); printf ("\ n"); return 0;}
Use a suffix expression to construct an expression tree, and then output the tree with a post-order traversal policy:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/71/7D/wKioL1XSk6qghS84AAC9ZiA5MoE590.jpg "title=" capture. JPG "alt=" Wkiol1xsk6qghs84aac9zia5moe590.jpg "/>
"Data structure and algorithm analysis--c language description" after reading note 8