关于逻辑树

paul2002 2006-02-27 01:25:32
请问各位高手:
怎样根据一颗逻辑树,生成一个逻辑表达式?
比如:
OR
|
-----
| |
V1 AND
|
-----
| |
V2 V3

生成:( V2 AND V3 ) OR V1
...全文
250 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenzhichao2008 2006-02-28
  • 打赏
  • 举报
回复
后序遍历一次就可以了
ckp 2006-02-27
  • 打赏
  • 举报
回复
树可能不是二叉树,应该使用先序搜索算法,在根据先序搜索的顺序生成序列的同时,生成表达式,此外,由于,设计算符优先级的问题,所以,这个算法有点复杂.不过,数据结构的教材上应该有类似的算法.
du51 2006-02-27
  • 打赏
  • 举报
回复
这是你的责任了.
这个东西不会是最终结果,一般用于中间转换.
在早期,你应该控制它,使它向二叉树转.
毕竟处理二叉树,我们更熟练一些.
paul2002 2006-02-27
  • 打赏
  • 举报
回复
树不是二叉树,可能会:
AND
|
-----------
| | |
V2 V3 V4
du51 2006-02-27
  • 打赏
  • 举报
回复
//Main.cpp
#include<iostream>
#include<string>
#include<stack>
#include "Tree.h"
using namespace std;
//////////////////////Checks if expression if ok////////////////
bool isok(string exp) //此函数验证式子是否正确,即是否符合运算规则。
{
char check;
int error=0;
int lb=0;
int rb=0;
if(exp.size()==1)return false;
else if((IsOperator(exp[0])||IsOperator(exp[exp.size()-1]))&&exp[0]!='('&&exp[exp.size()-1]!=')') //此处若不加,在遇到某些式子时,会出现非法操作。
return false;
for(int m=0;m<exp.size();m++)
{
check=exp[m];
if(IsOperand(check)); //如果是数字,跳过,不管。
else if(IsOperator(check))
{
if(check==')')
{
rb++;
if(IsOperator(exp[m+1])&&(exp[m+1]=='+'||exp[m+1]=='-'||exp[m+1]=='*'||exp[m+1]=='/'||exp[m+1]=='^'||exp[m+1]==')'))
{
m++;
if(exp[m]==')')
rb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
else if(check=='(')
{
lb++;
if(exp[m+1]=='(')
{
m++;
lb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
else
{
if(IsOperator(exp[m+1])&&exp[m+1]=='(')
{
m++;
lb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
}
else
error++;
}
if(error==0&&lb==rb)
return(true);
else
return(false);
}
void changstr(string &str)
{
for(int i=0;i<str.size();i++)
{
if(str[i]=='{'||str[i]=='[')str[i]='(';
else if(str[i]=='}'||str[i]==']')str[i]=')';
}
}
////////////////////////////////////////////////////////////////////
int main() //主函数开始
{
binary_tree etree;
stack<binary_tree>NodeStack;
stack<char>OpStack;
string infix;
char choice='y';
char c;

while(choice=='y'||choice=='Y')
{
cout<<"\n\n请输入表达式,不要带空格;\n";
cin>>infix;
cout<<"--------------------------------------------------------------------------------"<<'\n';
cout<<"表达式为: "<<infix<<'\n';
changstr(infix);

if(isok(infix))
{
for(int i=0;i<infix.size();i++)
{
c=infix[i];
/*if(c=='{'||c=='[')infix[i]=c='(';
if(c=='}'||c==']')infix[i]=c=')';*/
if(IsOperand(c))
{
string tempstring;
tempstring=tempstring+c;
while(i+1<infix.size()&&IsOperand(infix[i+1]))
{
tempstring=tempstring+infix[++i];
}
binary_tree temp;
temp.root=build_node(tempstring);
NodeStack.push(temp);
}
////////////////////////////////////////////////
else if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
{
if(OpStack.empty())
OpStack.push(c);
else if(OpStack.top()=='(')
OpStack.push(c);
else if(TakesPrecedence(c,OpStack.top()))
OpStack.push(c);
else
{
while(!OpStack.empty()&&(TakesPrecedence(OpStack.top(),c)||addition(OpStack.top(),c)))
{
binary_tree temp_tree;
string thisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
etree.root=NULL;
}
OpStack.push(c);
}

}
else if(c=='(')
OpStack.push(c);
else if(c==')')
{
while(OpStack.top()!='(')
{
binary_tree temp_tree;
string thisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
etree.root=NULL;
}
OpStack.pop();
}
}
////////////////////////////////////////////////////////
while(!OpStack.empty())
{
binary_tree temp_tree;
string thisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
if(!OpStack.empty())
{
etree.root=NULL;
}
}
cout<<"打印结点如下: ";
etree.print();
binary_tree temp;
copy(temp.root,etree.root);
cout<<'\n';
cout<<"结点个数为:"<<etree.counter()<<'\n';
cout<<"以下是,中间的计算结果:"<<'\n';
etree.evaluate();
cout<<'\n';
cout<<"结果是: ";
cout<<etree.root->data<<'\n';
cout<<"--------------------------------------------------------------------------------"<<'\n';
cout<<"是不要进行二叉树排序,并显示?若是升序点A,若是降序点B,若不是点C"<<'\n';
char c1;
cin>>c1;
if(c1=='A'||c1=='a')
{
binary_tree temp1;
copy(temp1.root,temp.order().root); //此处代码无需再改
temp1.inprint(temp1.root);//前边程序有错,此处TEMP1为空.所以没有输出.
}
else if(c1=='B'||c1=='b')
{
binary_tree temp1;
copy(temp1.root,temp.order().root);
temp1.deprint(temp1.root);
}

cout<<"\n\nRun Program again?Enter<y/n>:";
cin>>choice;
}
else
{
cout<<"************************************************"<<'\n';
cout<<"ERROR:Invalid Expression"<<'\n';
cout<<"************************************************"<<'\n';
cout<<"\n\nRun Program again?Enter<y/n>:";
cin>>choice;
}
}
return 0;
}
//主程序至此完!!!!!!
du51 2006-02-27
  • 打赏
  • 举报
回复
我给你个例子.
//Tree.h
#ifndef _TREE_H_
#define _TREE_H_ //防止反复包含

#include<iostream>
#include<string>
#include<math.h>
#include<sstream>
using namespace std;
////////////////////////////////////////////////////////////////////////

bool IsOperator(string mystring) //验证操作符
{
if(mystring=="-"||mystring=="+"||mystring=="/"||mystring=="*"||mystring=="^")
return(true);
else
return(false);
}
bool IsOperator(char ops)//重载
{
if(ops=='+'||ops=='-'||ops=='*'||ops=='/'||ops=='^'||ops=='('||ops==')')
return(true);
else
return(false);
}
/////////////////////////////////////////////////////////////
class node_type//结点类
{
public:
string data;
node_type *left_child;
node_type *right_child;
node_type(string k)
{
data=k;
left_child=NULL;
right_child=NULL;
}
};
///////////////////////////////////////////////////////////////////
class binary_tree //二叉树类开始
{
public:
node_type *root;
void print(node_type *r);
binary_tree(void){root=NULL;}
void inprint(node_type *p)
{
if(p!=NULL)
{

inprint(p->left_child);
cout<<p->data<<" ";
inprint(p->right_child);
}
}
void deprint(node_type *p)
{
if(p!=NULL)
{

deprint(p->right_child);
cout<<p->data<<" ";
deprint(p->left_child);
}
}
void print(void){print(root);}
void inprint(void){print(root);}
void evaluate()
{
evaluate(root);
}
void evaluate(node_type *prt)
{
if(IsOperator(prt->data)&&!IsOperator(prt->left_child->data)&&!IsOperator(prt->right_child->data))
{
int num=0;
int num1=atoi(prt->left_child->data.c_str()); //C类型的字符串 常量不可改变
int num2=atoi(prt->right_child->data.c_str()); //同上
if(prt->data=="+")
num=num1+num2;
else if(prt->data=="-")
num=num1-num2;
else if(prt->data=="*")
num=num1*num2;
else if(prt->data=="/")
num=num1/num2;
else if(prt->data=="^")
num=(int)pow((double)num1,(double)num2);
cout<<num<<'\t'; //打印中间结果
stringstream bob;
bob<<num;
string suzzy(bob.str());
prt->data=suzzy;
prt->left_child=NULL;
prt->right_child=NULL;

}
else if(prt->left_child==NULL&&prt->right_child==NULL);
else
{
evaluate(prt->left_child);
evaluate(prt->right_child);
evaluate(prt);
}
}
void clear_help(node_type *rt) //删除者
{
if(rt!=NULL)
{
clear_help(rt->left_child);
clear_help(rt->right_child);
delete rt;
}
}
void clear() //删除整个二叉树。
{
clear_help(root);
}
/////////////////////////////////////////////
void count_help(node_type *rt,int &a) //计数者//引用可以带回改变值。
{
a=0;
int f1,f2;
if(rt!=NULL)
{
count_help(rt->left_child,f1);
count_help(rt->right_child,f2);
a=f1+f2+1;
}
}
int counter() //返回结点的个数
{
int a;
count_help(root,a);
return a;
}
bool compare(string str1,string str2) //比较两结点的大小。若str1大于str2返回TRUE。否则为FALSE。
{
int i=0;
while(str1[i]&&str2[i])
{
if((int)str1[i]>(int)str2[i])
return true;
else if((int)str1[i]<(int)str2[i])
return false;
else i++;
}
if(str1[i])return true;
else return false;
}
/////////////////////////////////////////////////////////////////////////////////////////
void inserter(node_type *&p,string data) //插入者。
{
if(p==NULL)
{
p=new node_type(data);
p->left_child=NULL;
p->right_child=NULL;
}
else if(compare(p->data,data))
inserter(p->left_child,data);
else inserter(p->right_child,data);
}

void found(node_type *&p,node_type *&p1) //对一个树排序,并建新树。依靠插入者。
{
if(p!=NULL)
{
found(p->left_child,p1);
found(p->right_child,p1);
inserter(p1,p->data);
}
}
binary_tree order(binary_tree aa) //排序的函数
{
binary_tree bb;
found(aa.root,bb.root);
return bb;
}
binary_tree order()
{
return order(*this);
}
}; //二叉树类结束。


node_type *build_node(string x) //建立一个结点
{
node_type *new_node;
new_node=new node_type(x);
return(new_node);
}
void binary_tree::print(node_type *p)//打印
{
if(p!=NULL)
{
print(p->left_child);
print(p->right_child);
cout<<p->data<<" ";
}
}
bool IsOperand(char ch)//验证数据
{
if((ch>='0')&&(ch<='9'))
return true;
else
return false;
}
/////////////////////////////////////////////////////////////////////////
bool addition(char OperatorA,char OperatorB) //A=B返回TRUE.
{
if(OperatorA==OperatorB||(OperatorA=='*'&&OperatorB=='/')||(OperatorA=='/'&&OperatorB=='*')||(OperatorA=='+'&&OperatorB=='-')||(OperatorA=='-'&&OperatorB=='+'))
return true;
else return false;
}

bool TakesPrecedence(char OperatorA,char OperatorB) //判别符号的优先级。A>B,返回为TRUE。
{
if(OperatorA=='(')
return false;
else if(OperatorB=='(')
return false;
else if(OperatorB==')')
return true;
else if(addition(OperatorA,OperatorB)) //本函数原有逻辑错误.此处加以修正
return false;
else if(OperatorA=='^')
return true;
else if(OperatorB=='^')
return false;
else if((OperatorA=='*')||(OperatorA=='/'))
return true;
else if((OperatorB=='*')||(OperatorB=='/'))
return false;
else if((OperatorA=='+')||(OperatorA=='-'))
return true;
else return false;
}
////////////////////////////////////////////////////////////////
void copy(node_type *&r1,node_type *r2) //挎贝整个二叉树
{
if(r2==NULL)
r1=NULL;
else
{
r1=build_node(r2->data);
copy(r1->left_child,r2->left_child);
copy(r1->right_child,r2->right_child);
}
}
#endif //条件编绎结束
paul2002 2006-02-27
  • 打赏
  • 举报
回复
能给个例子吗?
piaochen_2002 2006-02-27
  • 打赏
  • 举报
回复
用递归可以实现!
widowss 2006-02-27
  • 打赏
  • 举报
回复
和中序输出一棵树类似
yelling 2006-02-27
  • 打赏
  • 举报
回复
用递归可以实现,除根以外,每递归一个非叶子加一对括号

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧