讲评二叉树.docx
- 文档编号:30181045
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:13
- 大小:34.09KB
讲评二叉树.docx
《讲评二叉树.docx》由会员分享,可在线阅读,更多相关《讲评二叉树.docx(13页珍藏版)》请在冰豆网上搜索。
讲评二叉树
1003:
逆序路径
ProblemDescription
设有一二叉树,其节点值为字符型并假设各值互不相等,采用顺序存储结构存储,空二叉树用'#'表示。
现要求设计一个算法,逆序输出从根到层序编号为i的结点的路径。
如对下面二叉树顺序存储为A#B###C#######D,从根到层序编号为15的逆路径为DCBA。
Input
有多组数据,每组测试数据有两行,第一行为数组元素个数n(n<=50)和结点的层序编号m,第二行为数组各元素。
Output
若编号m所对应的结点有效,则逆序输出从根到该点的路径;若无效,则输出"dataerror"。
SampleInput
157
A#B###C#######D
135
ABC#DE###F##G
SampleOutput
CBA
DBA
#include
#include
usingnamespacestd;
constintMaxSize=100;
voidOperate(chars[],intn,intm)
{
inti;
if(m<1||m>n||s[m]=='#')
cout<<"dataerror"< else { i=m; while(i! =0) { cout< i=i/2; } cout< } } intmain() { intn,m,i; chart[MaxSize],c; while(cin>>n>>m) { cin>>t+1; Operate(t,n,m); } return0; } 1003: 求最近公共祖先 ProblemDescription 设有一棵非空二叉树,其节点值为字符型并假设各值互不相等,采用顺序存储结构表示。 现输入其数组各元素值(空二叉树用'#'表示),建立该二叉树。 请设计一个算法,输出编号分别为i和j的两个结点的最近的公共祖先结点的值。 Input 有多组测试数据,每组数据占两行,第一行为两个整数,分别表示编号i和j(i! =j),第二行为其数组各元素值(空二叉树用'#'表示)。 Output 输出编号i和j的最近公共祖先结点的值,若该祖先结点不存在则输出"dataerror"。 SampleInput 35 ABC#D 25 ABCDE#F SampleOutput A B #include #include usingnamespacestd; constintMaxSize=100; voidOperate(chars[],inti,intj) { intn; n=strlen(s+1); if(i<1||j<1||i>n||j>n||s[i]=='#'||s[j]=='#') cout<<"dataerror"< else { while(i! =j) { if(i>j) i=i/2; else j=j/2; } cout< } } intmain() { intn,m; chart[MaxSize]; while(cin>>n>>m) { cin>>t+1; Operate(t,n,m); } return0; } 1002: 判满二叉树 ProblemDescription 设有一棵非空二叉树,其节点值为字符型并假设各值互不相等,采用二叉链表存储表示。 设计一个算法,判断该二叉树是否为满二叉树。 若是输出"yes",不是则输出"no"。 Input 第一行为一个整数n,表示以下有n组数据,每组数据占一行,为扩展二叉树的前序遍历序列。 Output 若该二叉树是满二叉树输出"yes",不是则输出"no". SampleInput 2 AB#D##C## ABD##E##C#F## SampleOutput no no #include #include usingnamespacestd; constintMaxSize=100; structBiNode //二叉树的结点结构 { chardata; BiNode*lchild,*rchild; }; classBiTree { public: BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入 ~BiTree(); //析构函数,释放二叉链表中各结点的存储空间 BiNode*Getroot(); //获得指向根结点的指针 intheight(BiNode*root); voidnodes(BiNode*root); private: BiNode*root; //指向根结点的头指针 BiNode*Creat(); //有参构造函数调用 voidRelease(BiNode*root); //析构函数调用 }; BiTree: : BiTree() { this->root=Creat(); } BiTree: : ~BiTree() { Release(root); } BiNode*BiTree: : Getroot() { returnroot; } intBiTree: : height(BiNode*root) { if(root==NULL) return0; else { inthl,hr; hl=height(root->lchild); hr=height(root->rchild); if(hl>=hr) returnhl+1; else returnhr+1; } } intnum; voidBiTree: : nodes(BiNode*root) { if(root) { num++; nodes(root->lchild); nodes(root->rchild); } } BiNode*BiTree: : Creat() { BiNode*root; charch; cin>>ch; if(ch=='#')root=NULL; else { root=newBiNode; //生成一个结点 root->data=ch; root->lchild=Creat(); //递归建立左子树 root->rchild=Creat(); //递归建立右子树 } returnroot; } voidBiTree: : Release(BiNode*root) { if(root! =NULL) { Release(root->lchild); //释放左子树 Release(root->rchild); //释放右子树 deleteroot; } } intmain() { intn; cin>>n; while(n--) { BiTreebt;//按扩展前序序列创建一棵二叉树 BiNode*root=bt.Getroot(); //获取指向根结点的指针 num=0; bt.nodes(root); inth; h=bt.height(root); if(num==pow(2,h)-1) cout<<"yes"< else cout<<"no"< } return0; } 1005: 括号表示法输出二叉树 ProblemDescription 设有一棵非空二叉树,其节点值为字符型并假设各值互不相等,采用二叉链表存储表示。 设计一个算法,采用括号表示法输出该二叉树。 Input 第一行为一个整数n,表示以下有n组数据,每组数据占一行,为扩展二叉树的前序遍历序列。 Output 用括号表示法输出二叉树。 SampleInput 6 A## AB### A#B## AB##C## AB#D##C## ABD##E##C#F## SampleOutput A A(B) A(,B) A(B,C) A(B(,D),C) A(B(D,E),C(,F)) #include #include usingnamespacestd; constintMaxSize=100; structBiNode //二叉树的结点结构 { chardata; BiNode*lchild,*rchild; }; classBiTree { public: BiTree(); //构造函数,初始化一棵二叉树,其前序序列由键盘输入 ~BiTree(); //析构函数,释放二叉链表中各结点的存储空间 BiNode*Getroot(); //获得指向根结点的指针 voidPrint(BiNode*root); private: BiNode*root; //指向根结点的头指针 BiNode*Creat(); //有参构造函数调用 voidRelease(BiNode*root); //析构函数调用 }; BiTree: : BiTree() { this->root=Creat(); } BiTree: : ~BiTree() { Release(root); } BiNode*BiTree: : Getroot() { returnroot; } voidBiTree: : Print(BiNode*root) { if(root) { cout< if(root->lchild||root->rchild) { cout<<'('; Print(root->lchild); if(root->rchild) cout<<','; Print(root->rchild); cout<<')'; } } } BiNode*BiTree: : Creat() { BiNode*root; charch; cin>>ch; if(ch=='#')root=NULL; else
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 讲评 二叉