这两周的上机可以说是很糟糕了…
一直很粗心…在调试简单问题上浪费了很多时间
限时的上机…我可太难了
但是要努力克服呀😢😢 期中还要上机考试的呢!!
下周数据结构期中,复习冲冲冲!!!
构造二叉树
给定一棵二叉树的前序遍历序列和中序遍历序列,设计程序构造出与序列对应的二叉树,并输出二叉树的后序遍历序列
输入:
[4 0 1 3 2 /5 7 6 8 10 9] (前序遍历序列)
[0 1 2 3 4 /5 6 7 8 9 10] (中序遍历序列)
输出:
[2 3 1 0 /6 9 10 8 7 5 4] (后序遍历序列)
这题可厉害了,刚刚才写完……
上机时真的真的毫无思路……😭😭😭
康康刚写完的代码:
#include<iostream>
#include<stack>
#include<string>
#include<stdlib.h>
using namespace std;
int a[100]={4,0,1,3,2,5,7,6,8,10,9},b[100]={0,1,2,3,4,5,6,7,8,9,10},num=10;
template<class Type>
struct BinNode{
int data;
BinNode <Type> *lchild;BinNode<Type> *rchild;
};
template<class Type> class BiTree{
friend class BinNode<Type>;
public:
BinNode<Type> *root;
BiTree(){root=new BinNode<Type>;};
BinNode<Type> * create(BinNode<Type> *root,int n,int r);
void showpre(BinNode<Type> *root);
void showmid(BinNode<Type> *root);
void showaft(BinNode<Type> *root);
};
template<class Type>
void BiTree<Type>::showpre(BinNode<Type> *root)
{
if(root==NULL) return ;
cout<<root->data<<" ";
showpre(root->lchild);
showpre(root->rchild);
}
template<class Type>
void BiTree<Type>::showmid(BinNode<Type> *root)
{
if(root==NULL) return ;
showmid(root->lchild);
cout<<root->data<<" ";
showmid(root->rchild);
}
template<class Type>
void BiTree<Type>::showaft(BinNode<Type> *root)
{
if(root==NULL) return ;
showaft(root->lchild);
showaft(root->rchild);
cout<<root->data<<" ";
}
template<class Type>
BinNode<Type> * BiTree<Type>::create(BinNode<Type> *root,int n,int r)
{ if(n>r) return NULL;
BinNode<int> *p=new BinNode<int>;
root=p;root->lchild=NULL;root->rchild=NULL;
int i=0;
while(b[i]!=a[n]) i++;root->data=a[n];
if(i==0||b[i-1]==-1) {b[i]=-1;root->rchild=create(root->rchild,n+1,r);}
else {b[i]=-1;root->lchild=new BinNode<int>;root->lchild->data=a[n+1];a[n+1]=-1;root->lchild->lchild=NULL;root->lchild->rchild=NULL;root->rchild=create(root->rchild,n+2,r);}
return root;
}
int main()
{ BiTree<int> tree;
tree.root->data=a[0];
int i=0;
while(b[i]!=a[0]) i++;b[i]=-1;
tree.root->lchild=tree.create(tree.root->lchild, 1, i);
tree.root->rchild=tree.create(tree.root->rchild, i+1, num);
cout<<"preorder:"<<endl;
tree.showpre(tree.root);cout<<endl;
cout<<"midorder:"<<endl;
tree.showmid(tree.root);cout<<endl;
cout<<"afterorder:"<<endl;
tree.showaft(tree.root);cout<<endl;
}
思路:
我是真的菜🥬
主要思路是这样的,先找到根节点(前序的a[0])并创建好根节点,然后将数组划分成两段,分别作为左子树和右子树~
参数r就是划分的限制
每次都取前序数组a的下一个数作为此时的根节点,找到这个数a[n]在中序数组b中的位置~如果其之前都是已经创建过的结点,则递归创建它的右子树(此时中序和前序是一毛一样的!),如果之前还有没创建过的结点(这个结点必然是它的左孩子,且在a数组中就是它后面的那个~),于是把两个节点都创建好,再递归创建右子树即可
这个思路真的很……诡异,分析的时候听听助教的介绍!
左叶子节点之和
给定一个二叉树,要求求出该二叉树中所有左叶子节点的数值之和
如图所示的一棵二叉树,最终的输出应该是17(4 + 6 + 7)
注:要求使用非递归的方式实现该功能,二叉树基于二叉链表构建
#include<iostream>
#include<stack>
using namespace std;
template<class Type>
struct BinNode{
Type data;
BinNode <Type> *lchild;BinNode<Type> *rchild;
};
template<class Type> class BiTree{
friend class BinNode<Type>;
public:
BinNode<Type> *root;
BiTree(){root=new BinNode<Type>;};
int leftleavesum(BinNode<Type> *root);
void create(BinNode<Type> *root);
void show(BinNode<Type> *root);
};
template<class Type>
int BiTree<Type>::leftleavesum(BinNode<Type> *root)
{ stack<BinNode<int> *> leaf;
int sum=0;
if(root==NULL) return 0;
else leaf.push(root);
BinNode<int> *p=root;
while(!leaf.empty())
{
if(p->lchild!=NULL) {leaf.push(p->lchild);p=p->lchild;}
else{sum+=p->data;leaf.pop();
if(leaf.empty()) break;
else {p=leaf.top();p=p->rchild;leaf.pop();}}
}
p=root->rchild;
do{
if(p->lchild!=NULL) {leaf.push(p->lchild);p=p->lchild;}
else{sum+=p->data;leaf.pop();
if(leaf.empty()) break;
else {p=leaf.top();p=p->rchild;leaf.pop();}}
}while(!leaf.empty());
return sum;
}
template<class Type>
void BiTree<Type>::create(BinNode<Type> *root)
{
root->data=1;
BinNode<Type> *p1=new BinNode<Type>;
p1->data=2;root->lchild=p1;
BinNode<Type> *p2=new BinNode<Type>;
p2->data=4;p1->lchild=p2;p2->lchild=NULL;p2->rchild=NULL;
BinNode<Type> *p3=new BinNode<Type>;
p3->data=5;p1->rchild=p3;p3->rchild=NULL;
BinNode<Type> *p4=new BinNode<Type>;
p4->data=6;p3->lchild=p4;p4->lchild=NULL;p4->rchild=NULL;
BinNode<Type> *p5=new BinNode<Type>;
p5->data=3;root->rchild=p5;
BinNode<Type> *p6=new BinNode<Type>;
p6->data=7;p5->lchild=p6;p6->lchild=NULL;p6->rchild=NULL;
BinNode<Type> *p7=new BinNode<Type>;
p7->data=8;p5->rchild=p7;p7->lchild=NULL;p7->rchild=NULL;
}
template<class Type>
void BiTree<Type>::show(BinNode<Type> *root)
{
if(root==NULL) return ;
cout<<root->data<<" ";
show(root->lchild);
show(root->rchild);
}
int main()
{ int i=0;
BiTree<int> b;
b.create(b.root);
cout<<"tree:"<<endl;
b.show(b.root);cout<<endl;
i=b.leftleavesum(b.root);
cout<<"result:"<<endl;
cout<<i<<endl;
}
这个是上机时写的,创建二叉树的方法也是没谁了…
思路:还是利用栈的思路先进先出,这个不难…
- 学习二叉树的创建方法(循环或递归)
- 复习数据结构!!