社区
C语言
帖子详情
关于逻辑树
paul2002
2006-02-27 01:25:32
请问各位高手:
怎样根据一颗逻辑树,生成一个逻辑表达式?
比如:
OR
|
-----
| |
V1 AND
|
-----
| |
V2 V3
生成:( V2 AND V3 ) OR V1
...全文
261
10
打赏
收藏
关于逻辑树
请问各位高手: 怎样根据一颗逻辑树,生成一个逻辑表达式? 比如: OR | ----- | | V1 AND | ----- | | V2 V3 生成:( V2 AND V3 ) OR V1
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
用递归可以实现,除根以外,每递归一个非叶子加一对括号
WPF全系列事例代码
WPF基础教程所有相关的源代码,博客文章请查阅:http://blog.csdn.net/fwj380891124 之WPF板块类容。非常详细的介绍了WPF,适合初学者入门学习。
逻辑
树
分析模型
什么是
逻辑
树
?
逻辑
树
又称为问题数,演绎
树
或者分解
树
,是麦肯锡公司提出的分析问题,解决问题的重要方法,首先它的形态像一颗
树
,把已知的问题比作
树
干,然后考虑哪些问题或者任务与已知问题有关,将这些问题或子任务比作
逻辑
树
的
树
枝,一个大的
树
枝还可以继续延续伸出更小的
树
枝,逐步列出所有与已知问题相关联的问题。
逻辑
树
的原理? 首先将一个已知问题当成
树
干,然后开始思考这个问题与那些相关问题或者子任务有关,每想到一点就给这个问题(也就是
树
干)加一个‘
树
枝’,并标明这个‘
树
枝’代表什么问题,一个大的‘
树
枝’上还可以有小的的
视觉
树
和
逻辑
树
的区别
可以使用System.Windows.Media中的LoginTreeHelper和VisualTreeHelper对视觉
树
和
逻辑
树
进行遍历。1、观察可以看到
逻辑
树
其实就是跟xaml的布局结构是一样的(应用程序中所有控件的元素的层次结构)。使用工具snoop可以观察到视觉
树
(使用:运行软件->将该软件右边的十字架拉到在运行的wpf窗体中)3、视觉
树
渲染细节:颜色、动画、布局,边框、背景、布局、样式,暴露视觉的实现细节。6、
逻辑
树
注重结构,视觉
树
包含渲染的所有细节。4、模板内的元素不包含在
逻辑
树
中。
逻辑
树
与视觉
树
的区别,有什么关联性
在WPF(Windows Presentation Foundation)中,
逻辑
树
(Logical Tree)和视觉
树
(Visual Tree)是构建和管理用户界面的两个核心概念。- 在WPF中,
逻辑
树
和视觉
树
共同工作,确保用户界面既能正确表示业务
逻辑
,又能以合适的方式呈现给用户。- 属性值的继承和资源查找也是通过
逻辑
树
进行的,但它们的最终表现是在视觉
树
中实现的。- 视觉
树
的渲染效果(如样式和布局更改)可以反映
逻辑
树
中的结构。-
逻辑
树
中的更改(如添加或移除控件)会影响视觉
树
的构成。
逻辑
树
分析方法
文章目录什么是
逻辑
树
拆解方法如何搭建并使用一个
逻辑
树
适用场景 什么是
逻辑
树
拆解方法 含义:
逻辑
树
又被称为问题
树
、演绎
树
或分解
树
等,是麦肯锡公司提出的分析问题、解决问题的一个方法。 它的形状像一棵
树
: 在实际分析时我们通过思维导图来进行绘制。
逻辑
树
的核心在于拆解,能够完全拆解目标问题能否全面分析问题的关键。
逻辑
树
的优点: 通过对问题的拆解,可以找出问题的所有相关项,确保问题获得完整的解决; 通过问题与问题的关联,识别哪些是必须,哪些是证明前提假设的重点; 个人使用,能帮助理清思路,将大问题分解为利于
C语言
70,023
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章