An instance of an affix tree to a suffix tree in an C language data structure

  • 2020-05-27 06:50:53
  • OfStack

An instance of an affix tree to a suffix tree in an C language data structure

For 1 infix expression a+b*c*(d-e /f) convert to the suffix in the form abc*def/-+

Postfix expressions are quite useful, and are much easier to evaluate when converted to postfix expressions.

There's a ton of stuff on the web about this, and every data structure book mentions this algorithm, and in this algorithm, the stack data structure is used.

1, the key is to compare the precedence of the operator, whose priority is higher, who appears in the previous expression above, when there are parentheses, the highest precedence of the parentheses,*/ next,+- finally.

2. Always output directly when encountering operands without any comparison

3, the open parenthesis is always directly pushed into the stack, the close parenthesis is always flipped,1 straight up to the open parenthesis

First when I was 4, meet the operator will be the operator and the operator is priority in front of it, and if the priority of higher than the previous, pressure stack it first, if less than or equal to the front of the operator priority, the priority is higher than it in front of or equal to the order of play, play to meet priority 1 straight than it is lower or the stack, and then the operator in the stack.

Knowing the above four rules is enough to design the code implementation,

The code is as follows:


#include<iostream> 
#include<string> 
#include<stack> 
#include<map> 
using namespace std; 
void InerStringDevide(string InerStr,string DeviStr[],int &num) 
{ 
  int count,i; 
  int numbe=InerStr.size(); 
  for(i=0;i<numbe;i++) 
    DeviStr[i][0]='\0'; 
  count=0; 
  for(i=0;i<numbe;) 
  { 
    if(InerStr[i]=='+'||InerStr[i]=='-'||InerStr[i]=='*'|| 
      InerStr[i]=='/'||InerStr[i]=='%'||InerStr[i]=='^' 
      ||InerStr[i]=='('||InerStr[i]==')') 
    { 
      DeviStr[count].push_back(InerStr[i]); 
      count++; 
      i++; 
    } 
    else 
    { 
      while(InerStr[i]!='+'&&InerStr[i]!='-'&&InerStr[i]!='*'&& 
      InerStr[i]!='/'&&InerStr[i]!='%'&&InerStr[i]!='^' 
      &&InerStr[i]!='('&&InerStr[i]!=')') 
      { 
        DeviStr[count].push_back(InerStr[i]); 
        i++; 
        if(i>=numbe) 
          break; 
      } 
      count++; 
    } 
  } 
  num=count; 
} 
void InerTreeToPostTree(string InerStr,string &PostStr) 
{ 
  PostStr[0]='\0'; 
  map<char,int>OpC; 
  typedef map<char,int>::value_type ValType; 
  OpC.insert(ValType('+',1)); 
  OpC.insert(ValType('-',1)); 
  OpC.insert(ValType('*',2)); 
  OpC.insert(ValType('/',2)); 
  OpC.insert(ValType('%',2)); 
  OpC.insert(ValType('^',3)); 
  OpC.insert(ValType('(',-1)); 
  OpC.insert(ValType(')',0)); 
  int num,i,j,StrNum; 
  num=InerStr.size(); 
  string *DevedeStr=new string[num]; 
  InerStringDevide(InerStr,DevedeStr,StrNum); 
 
  stack<char> ChStack; 
  int count=0; 
  for(int i=0;i<StrNum;i++) 
  { 
    // If the input string is an operator  
    if(DevedeStr[i][0]=='+'||DevedeStr[i][0]=='-'||DevedeStr[i][0]=='*'|| 
      DevedeStr[i][0]=='/'||DevedeStr[i][0]=='%'||DevedeStr[i][0]=='^' 
      ||DevedeStr[i][0]=='('||DevedeStr[i][0]==')') 
    { 
      // If the operator stack is empty, you can push the operator directly onto the stack  
      if(ChStack.empty()) 
      { 
        ChStack.push(DevedeStr[i][0]); 
      } 
      // If it is not empty, it is judged and pushed according to the priority of the operator and its category  
      else 
      { 
        char TopCh=ChStack.top(); 
        // If it is (, push it directly  
        if(OpC[DevedeStr[i][0]]==-1) 
        { 
          ChStack.push(DevedeStr[i][0]); 
        } 
        // If the operator priority is greater than the current operator in the stack, push directly onto the stack  
        else if(OpC[TopCh]<OpC[DevedeStr[i][0]]) 
        { 
          ChStack.push(DevedeStr[i][0]); 
        } 
        // Otherwise, it will be handled differently according to the operator's category  
        else 
        { 
          // If encountered), the operator is pushed out of the stack into the string  
          if(OpC[DevedeStr[i][0]]==0) 
          { 
            TopCh=ChStack.top(); 
            while(OpC[TopCh]!=-1) 
            { 
              if(!PostStr.empty()) 
              { 
                PostStr.push_back(' '); 
              } 
              PostStr.push_back(TopCh); 
              ChStack.pop(); 
              TopCh=ChStack.top(); 
            } 
            ChStack.pop(); 
            TopCh=ChStack.top(); 
          } 
          else 
          { 
            while(OpC[TopCh]>=OpC[DevedeStr[i][0]]&&OpC[TopCh]!=-1) 
            { 
              if(!PostStr.empty()) 
              { 
                PostStr.push_back(' '); 
              } 
              PostStr.push_back(TopCh); 
              ChStack.pop(); 
              if(!ChStack.empty()) 
                TopCh=ChStack.top(); 
              else 
                break; 
            } 
            ChStack.push(DevedeStr[i][0]); 
          } 
        } 
      } 
    } 
    // If the input string is a number  
    else 
    { 
      int DevideSize=DevedeStr[i].size(); 
      if(!PostStr.empty()) 
      { 
        PostStr.push_back(' '); 
      } 
      for(int j=0;j<DevideSize;j++) 
      { 
        PostStr.push_back(DevedeStr[i][j]); 
      } 
    } 
  } 
  while(!ChStack.empty()) 
  { 
    if(!PostStr.empty()) 
    { 
      PostStr.push_back(' '); 
    } 
    PostStr.push_back(ChStack.top()); 
    ChStack.pop(); 
  } 
} 

Above is the header file InerTreeToPostTree.h. This file is used to input infix strings and output suffix strings, where infix strings do not have Spaces, and suffix strings have Spaces. Another function in the header file divides the string into an array of strings that store Numbers and operators.


#include<iostream> 
#include<stack> 
#include<string> 
using namespace std; 
void StringDevide(string str,int &num,string st1[]) 
{ 
  for(int i=0;i<100;i++) 
    st1[i][0]='\0'; 
  int n=str.size(); 
  int j=0,count=0; 
  for(int i=0;i<n;i++) 
  { 
    if(str[i]!=' ') 
    { 
      st1[count].push_back(str[i]); 
    } 
    else 
    { 
      count++; 
    } 
  } 
  num=count+1; 
} 
void StringToNum(string str,int &num) 
{ 
  num=0; 
  int n=str.size(); 
  for(int i=0;i<n;i++) 
  { 
    num=num*10; 
    num+=str[i]-'0'; 
  } 
} 
class InterTreeComputer 
{ 
private: 
  // The expression to evaluate  
  string m_expresion; 
  // Store the number on the stack  
  stack<int> m_num; 
 
public: 
  InterTreeComputer(string expression):m_expresion(expression) 
  {} 
  // To determine a 1 Is the operator an operator  
  bool IsOperator(char ch)const; 
  // Gets the two operands to evaluate  
  void GetOperands(int &left,int &right); 
  // The two obtained Numbers are denoted ch To calculate  
  int computer(int left,int right,char ch)const; 
  // Get expression  
  string GetPostoperation()const; 
  void SetPostoperator(); 
  // Evaluate the expression and return the result  
  int Evaluate(); 
}; 
bool InterTreeComputer::IsOperator(char ch)const 
{ 
  switch(ch) 
  { 
  case '+': 
  case '-': 
  case '*': 
  case '/': 
  case '%': 
  case '^': 
    return 1; 
  default: 
    return 0; 
  } 
} 
void InterTreeComputer::GetOperands(int &left,int &right) 
{ 
  if(m_num.empty()) 
  { 
    cout<<"num stack is empty!"; 
    return ; 
  } 
  right=m_num.top(); 
  m_num.pop(); 
  if(m_num.empty()) 
  { 
    cout<<"the expression is wrong!"<<endl; 
    return ; 
  } 
  left=m_num.top(); 
  m_num.pop(); 
} 
int InterTreeComputer::computer(int left,int right,char ch)const 
{ 
  switch(ch) 
  { 
  case '+': 
    return left+right; 
    break; 
  case '-': 
    return left-right; 
    break; 
  case '*': 
    return left*right; 
    break; 
  case '/': 
    if(right==0) 
    { 
      cout<<"the expression is wrong"<<endl; 
      return -1; 
    } 
    return left/right; 
    break; 
  case '%': 
    return left%right; 
    break; 
  case '^': 
    if(left==0&&right==0) 
    { 
      cout<<"the expression is wrong"<<endl; 
      return -1; 
    } 
    int value=1; 
    while(right>0) 
    { 
      value*=left; 
      right--; 
    } 
    return value; 
    break; 
  } 
} 
string InterTreeComputer::GetPostoperation()const 
{ 
  return m_expresion; 
} 
void InterTreeComputer::SetPostoperator() 
{} 
int InterTreeComputer::Evaluate() 
{ 
  string *str=new string[100]; 
  int num; 
  StringDevide(m_expresion,num,str); 
  for(int i=0;i<num;i++) 
  { 
    if(str[i][0]=='+'||str[i][0]=='-'||str[i][0]=='*'||str[i][0]=='/' 
      ||str[i][0]=='%'||str[i][0]=='^') 
    { 
      char ch=str[i][0]; 
      int left,right; 
      GetOperands(left,right); 
      int number=computer(left,right,ch); 
      m_num.push(number); 
    } 
    else 
    { 
      int numb=0; 
      StringToNum(str[i],numb); 
      m_num.push(numb); 
    } 
  } 
  return m_num.top(); 
} 

The above code is the InerTreeComputer.h header file, which is used to enter a postfix expression and evaluate the expression.


#include<iostream> 
using namespace std; 
#include<string> 
#include<stack> 
#include"InterTreeComputer.h" 
#include"InerTreeToPostTree.h" 
int main() 
{ 
  string str="3*(4-2^5)+6"; 
  string st1="2 3 ^ 1 +"; 
  string st2="2 2 3 ^ ^ 4 /"; 
  string StrRe; 
  InerTreeToPostTree(str,StrRe); 
  InterTreeComputer Comp(StrRe); 
  cout<<Comp.GetPostoperation()<<endl; 
  cout<<Comp.Evaluate()<<endl; 
  return 0; 
} 

The test file tests the above two header files.

Above is the data structure C language data structure in the subfix tree to suffix tree examples, if you have any questions please leave a message or to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support to the site, we make progress together!


Related articles: