数据结构上机实验-根据前序遍历和中序遍历构造二叉树

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

{ Q.front=Q.rear=new QNode; if(!Q.front) cout<<"Storage failure distribution!"<<endl; Q.front->next=NULL; } void EnQueue(LinkQueue &Q,binaryTree e)//将元素 e 压入队列 { QueuePtr p=new QNode; p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; } void DeQueue(LinkQueue &Q,binaryTree &e)//删除队头元素,并将其值储存在 e中 { if(Q.front!=Q.rear) { QueuePtr p=new QNode; p=Q.front->next; e=p->data; Q.front->next=p->next; if(Q.rear==p) Q.rear=Q.front; delete p; } } void dele(binaryTree &T)//删除二叉树 T { if(T) { dele(T->leftc); dele(T->rightc); } delete T; } void menu()//输出第三次上机作业题目及要求 { int i; for(i=1;i<=33;i++) cout<<'-'; cout<<"第三次上机作业"; for(i=1;i<=33;i++)
void main() { string DLR;//存储前序遍历字的符串 string LDR;//存储中序遍历字的符串 menu(); cout<<"Please input the posorder traversal of the binary tree:"<<endl; cin>>DLR; cout<<"Please input the inorder traversal of the binary tree:"<<endl; cin>>LDR; binaryTree T; binaryTreeCreate(T,DLR,LDR); cout<<"The posorder traversal of the binary tree is:"; posorderTraversal(T); cout<<endl<<"The levelorder traversal of the binary tree is:"; levelorderTraversal(T); cout<<endl; int i; for(i=1;i<=80;i++) cout<<'-'; Search(T); dele(T); system("pause"); } void binaryTreeCreate(binaryTree & T,string DLR,string LDR)// 根据前序 遍历和中序遍历构造二叉树 { if(DLR.size()||LDR.size()) { T=new node; T->data=DLR[0]; T->leftc=NULL; T->rightc=NULL; int pos=getPos(LDR,DLR[0]); binaryTreeCreate(T->leftc,DLR.substr(1,pos),LDR.substr(0,pos)); binaryTreeCreate(T->rightc,DLR.substr(pos+1,DLR.size()-1-pos),LDR .substr(pos+1,LDR.size()-1-pos)); } }
八、分工及签名
附:源代码 //第三次上机作业 #include<iostream> #include<string> #include<string.h> using namespace std; typedef struct node//二叉树结点二叉树结构体 { char data; struct node * leftc; struct node * rightc; }node,*binaryTree; typedef struct QNode //队列结点结构体 { binaryTree data ; struct QNode * next ; }QNode,*QueuePtr; typedef struct//队列结构体 { QueuePtr front; QueuePtr rear; }LinkQueue; void binaryTreeCreate(binaryTree & T,string DLR,string LDR);// 根据前序 遍历和中序遍历构造二叉树 void posorderTraversal(binaryTree T);//后续遍历二叉树 void levelorderTraversal(binaryTree T);//按层次遍历二叉树 void Search(binaryTree T);//查找二叉树中是否含有指定结点,若有,输出从 根到该结点的路径,否则提示错误信息 int findPath(binaryTree T,char key,LinkQueue & path);// 查找并构造二叉 树中从根结点到值为 key 的结点的路径 int findNode(binaryTree T,char key);//查找二叉树 T 中是否含有值为 key 的结点 void dele(binaryTree &T);//删除一棵二叉树 int getPos(string ch,char key);//返回字符 key 在字符串 ch 中的位置 int Empty(LinkQueue Q);//判断队列是否为空 void InitQueue(LinkQueue &Q);//队列初始化 void PrintQueue(LinkQueue & Q);//输出队列元素 void EnQueue(LinkQueue &Q,binaryTree e);//将元素 e 压入队列 void DeQueue(LinkQueue &Q,binaryTree &e);//删除队头元素,并将其值赋给 e void menu();//输出上机题目及要求
int getPos(string ch,char key)//返回字符 key 在字符串 ch 中的位置 { int i; for(i=0;i<=ch.size()-1;i++) if(ch[i]==key) return i; } void posorderTraversal(binaryTree T)//后序遍历二叉树 { if(T) { posorderTraversal(T->leftc); posorderTraversal(T->rightc); cout<<T->data; } } void levelorderTraversal(binaryTree T)//按层次遍历二叉树 { LinkQueue Q; InitQueue(Q); if(T) EnQueue(Q,T); while(!Empty(Q)) { DeQueue(Q,T); cout<<T->data; if(T->leftc) EnQueue(Q,T->leftc); if(T->rightc) EnQueue(Q,T->rightc); } } void Search(binaryTree T)//查找二叉树中是否含有指定结点,若有,输出从 根到该结点的路径,否则提示错误信息 { char key; LinkQueue path; InitQueue(path); cout<<"Please input the value of the node you want to find:"; cin>>key; if(findPath(T,key,path)) { cout<<"The path from root to your node is:"<<endl; PrintQueue(path);
三、算法描述
void binaryTreeCreate(binaryTree & T,string DLR,string LDR);// 根据前序 遍历和中序遍历构造二叉树 void posorderTraversal(binaryTree T);//后续遍历二叉树 void levelorderTraversal(binaryTree T);//按层次遍历二叉树 void Search(binaryTree T);//查找二叉树中是否含有指定结点,若有,输出从 根到该结点的路径,否则提示错误信息 int findPath(binaryTree T,char key,LinkQueue & path);// 查找并构造二叉 树中从根结点到值为 key 的结点的路径 int findNode(binaryTree T,char key);//查找二叉树 T 中是否含有值为 key 的结点 void dele(binaryTree &T);//删除一棵二叉树 int getPos(string ch,char key);//返回字符 key 在字符串 ch 中的位置 int Empty(LinkQueue Q);//判断队列是否为空 void InitQueue(LinkQueue &Q);//队列初始化 void PrintQueue(LinkQueue & Q);//输出队列元素 void EnQueue(LinkQueue &Q,binaryTree e);//将元素 e 压入队列 void DeQueue(LinkQueue &Q,binaryTree &e);//删除队头元素,并将其值赋给 e
cout<<key<<endl; } else cout<<"Not exist such node!"<<endl; } int findNode(binaryTree T,char key)//查找二叉树 T 中是否含有值为 key 的 结点 { if(!T) return 0; if(T->data==key) return 1; return findNode(T->leftc,key)+findNode(T->rightc,key); } int findPath(binaryTree T,char key,LinkQueue & path)// 查找并构造二叉树 中从根结点到值为 key 的结点的路径 { if(!T) return 0; if(T->data==key) return 1; if(findNode(T,key)) EnQueue(path,T); if(findNode(T->leftc,key)) findPath(T->leftc,key,path); else if(findNode(T->rightc,key)) findPath(T->rightc,key,path); } void PrintQueue(LinkQueue & Q)//输出队列元素 { while(!Empty(Q)) { binaryTree T; DeQueue(Q,T); cout<<T->data<<"->"; } } int Empty(LinkQueue Q)//判断队列是否为空 { if(Q.front==Q.rear==NULL) return 0; return 1; } void InitQueue(LinkQueue &Q)//初始化队列
六、结果分析和结论
测试中采用了以下三种典型二叉树, 即满二叉树, 完全二叉树, 和只含左子树 (右 子树)的二叉树,运行结果均以理论相符,可知程序正确性可靠。 1、满二叉树测试用例
2、完全二叉树测试用例
3、只含左(右)子树二叉树测试用例
七、心得体会
实验中不仅综合运用了树的各类操作,还借助队列的相关内容,使问题得到 更巧妙更高效地解决,体会到程序设计的综合性要求。
根据前序遍历和中序遍历构造二叉树
一、问题描述:
设二叉树结点值为大写字母,输入二叉树的前序遍历和中序遍历序列,生成 此二叉树,输出该二wenku.baidu.com树的后序遍历和按层次遍历序列。输入某结点值,在二叉 树中查找该结点,若该结点存在,则输出从根到该结点的路径,否则给出不存在 信息。
二、算法思路
通过用户输入的前序遍历序列 DLR 及中序遍历序列 LDR 可构造一课确定的二 叉树。方法如下: 1、 将前序遍历序列中第一个结点赋值为根结点 2、 以此取前序遍历中的结点,在中序遍历中定位该结点的位置,将其左边 的子列作为左子树的中序遍历,其右边的子列作为右子树的中序遍历, 递归直到所有取完前序遍历中所有节点。
四、源程序及驱动程序
源程序:第三次上机作业.cpp 开发环境:visual studio 2010
五、测试数据
1、DLR=ABDECFG LDR=DBEAFCG key=F
2、DLR=ABDFGEC LDR=FDGBEAC key=G
3、DLR=ABCDEFG LDR=GFEDCBA key=G
相关文档
最新文档