目录
问题 A: 二叉树非递归前序遍历-附加代码模式
问题 B: 二叉树非递归中序遍历-附加代码模式
问题 C: 二叉树非递归后序遍历-附加代码模式
问题 D: 求二叉树中序遍历序根节点的下标
问题 E: 根据前序+中序还原二叉树
问题 F: 算法6-12:自底向上的赫夫曼编码
问题 G: 视频合并问题
问题 H: 二叉树实现的简单物种识别系统-附加代码模式
问题 I: 静态链表存储的二叉树查找根节点
问题 J: 基础实验4-2.1:树的同构
问题 K: 哈夫曼树--查找值最小的两个叶节点
问题 L: 静态链表存储的二叉树的非递归遍历算法-附加代码模式
考是考完了但是。。。懒
文字部分能不能小小的鸽一次(QAQ)
题外话(快一个月之前因为忘记做核酸然后被迫出校做核酸,
晴导当时说找谈话)
之后一直鸽以为过久了就忘了
没想到他今天居然想起来了!
全当看个乐呵,大家别忘做核酸
问题 A: 二叉树非递归前序遍历-附加代码模式
#include <bits/stdc++.h>
using namespace std;
struct BiNode
{
string data;
BiNode *lchild, *rchild;
};
typedef BiNode *BiTree;
int InitBiTree(BiTree &T)
{
T = NULL;
return 0;
}
string PreTraverse_nonRec(BiTree T)
{
stack<BiTree> q;
string result = "";
BiTree temp;
if (T!=NULL) q.push(T);
while(!q.empty())
{
temp=q.top();
q.pop();
result=result+temp->data;
if(temp->rchild!=NULL) q.push(temp->rchild);
if(temp->lchild!=NULL) q.push(temp->lchild);
}
return result;
}
char *CreateBiTree(BiTree &T, char *str)
{
if (*str == '#')
{
T = NULL;
return str + 1;
}
T = new BiNode;
T->data = *str;
// 继续输入并构造左子树和右子树
char * strAfterLeft = CreateBiTree(T->lchild, str + 1);
char * strAfterRight = CreateBiTree(T->rchild, strAfterLeft);
return strAfterRight;
}
int PreTraverse(BiTree T){
if (T == NULL) return 0;
cout << T->data;
PreTraverse(T->lchild);
PreTraverse(T->rchild);
return 0;
}
int DestroyBiTree(BiTree &T){
if (T == NULL) return 0;
DestroyBiTree(T->lchild);
DestroyBiTree(T->rchild);
delete T;
T = NULL;
return 0;
}
问题 B: 二叉树非递归中序遍历-附加代码模式
#include <bits/stdc++.h>
using namespace std;
struct BiNode
{
string data;
BiNode* lchild, * rchild;
};
typedef BiNode* BiTree;
int InitBiTree(BiTree& T)
{
T = NULL;
return 0;
}
string InTraverse_nonRec(BiTree T)
{
string result = "";
stack<BiTree> s;
BiTree p = T;
while (p != NULL || !s.empty())
{
while (p != NULL)
{
s.push(p);
p = p->lchild;
}
if (!s.empty())
{
p = s.top();
result += p->data;
s.pop();
p = p->rchild;
}
}
return result;
}
char* CreateBiTree(BiTree& T, char* str)
{
if (*str == '#')
{
T = NULL;
return str + 1;
}
T = new BiNode;
T->data = *str;
char* strAfterLeft = CreateBiTree(T->lchild, str + 1);
char* strAfterRight = CreateBiTree(T->rchild, strAfterLeft);
return strAfterRight;
}
int InTraverse(BiTree T) {
if (T == NULL) return 0;
InTraverse(T->lchild);
cout << T->data;
InTraverse(T->rchild);
return 0;
}
int DestroyBiTree(BiTree& T) {
if (T == NULL) return 0;
DestroyBiTree(T->lchild);
DestroyBiTree(T->rchild);
delete T;
T = NULL;
return 0;
}
问题 C: 二叉树非递归后序遍历-附加代码模式
#include <bits/stdc++.h>
using namespace std;
struct BiNode
{
string data;
BiNode* lchild, * rchild;
};
typedef BiNode* BiTree;
int InitBiTree(BiTree& T)
{
T = NULL;
return 0;
}
string SucTraverse_nonRec(BiTree T)
{
string result="";
stack<BiTree> s;
BiTree cur=T;
BiTree prev=NULL;
while(cur!=NULL || !s.empty()){
while(cur!=NULL){
s.push(cur);
cur=cur->lchild;
}
BiTree top=s.top();
if(top->rchild==NULL || top->rchild==prev){
result+=top->data;
s.pop();
prev=top;
}
else{
cur=top->rchild;
}
}
return result;
}
char* CreateBiTree(BiTree& T, char* str)
{
if (*str == '#')
{
T = NULL;
return str + 1;
}
T = new BiNode;
T->data = *str;
char* strAfterLeft = CreateBiTree(T->lchild, str + 1);
char* strAfterRight = CreateBiTree(T->rchild, strAfterLeft);
return strAfterRight;
}
int SucTraverse(BiTree T) {
if (T == NULL) return 0;
SucTraverse(T->lchild);
SucTraverse(T->rchild);
cout << T->data;
return 0;
}
int DestroyBiTree(BiTree& T) {
if (T == NULL) return 0;
DestroyBiTree(T->lchild);
DestroyBiTree(T->rchild);
delete T;
T = NULL;
return 0;
}
// int main(){
// // char *str = "abd###ceg##h##f#i##";
// char str[2000];
// while(cin >> str)
// {
// BiTree tree;
// InitBiTree(tree);
// // 根据带空节点的前序遍历字符串构造二叉树
// CreateBiTree(tree, str);
// // 后序遍历递归算法
// SucTraverse(tree);
// cout << endl;
// // 后序遍历非递归算法
// string result = SucTraverse_nonRec(tree);
// cout << result << endl;
// DestroyBiTree(tree);
// }
// return 0;
// }
问题 D: 求二叉树中序遍历序根节点的下标
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fer(i,a,b) for(int i=a;i<=b;++i)
#define der(i,a,b) for(int i=a;i>=b;--i)
#define all(x) (x).begin(),(x).end()
#define pll pair<int,int>
#define et cout<<'\n'
#define xx first
#define yy second
using namespace std;
template <typename _Tp>void input(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
const int N=1e6+10;
signed main()
{
string a,b;
while(cin>>a>>b){
cout<<b.find(a[0])<<endl;
}
}
问题 E: 根据前序+中序还原二叉树
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fer(i,a,b) for(int i=a;i<=b;++i)
#define der(i,a,b) for(int i=a;i>=b;--i)
#define all(x) (x).begin(),(x).end()
#define pll pair<int,int>
#define et cout<<'\n'
#define xx first
#define yy second
using namespace std;
template <typename _Tp>void input(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
signed main()
{
function<void(string,string)> post1=[&](string a,string b){
int l=a.length();
if(l==0) return;
if(l==1)
{
cout<<a[0];
return;
}
int p=b.find(a[0]);
post1(a.substr(1,p),b.substr(0,p));
post1(a.substr(p+1,l-p-1),b.substr(p+1,l-p-1));
cout<<a[0];
};
string a,b;
while(cin>>a>>b)
{
post1(a,b);
cout<<'\n';
}
}
问题 F: 算法6-12:自底向上的赫夫曼编码
#include<bits/stdc++.h>
using namespace std;
typedef struct {
int parent;
int weight;
int lch;
int rch;
}Huffnode;
Huffnode h[205];
typedef struct {
char code[15];
}Hufftree;
Hufftree hcd[102];
int CreateHufftree(Huffnode hn[],int n)
{
for(int i=n;i<2*n-1;i++)
h[i].weight=0;
for(int i=0;i<2*n-1;i++)
h[i].parent=h[i].lch=h[i].rch=-1;
for(int i=n;i<2*n-1;i++)
{
int min1=INT_MAX,min2=INT_MAX;
int index1,index2;
for(int j=0;j<i;j++)
{
if(h[j].weight<min1&&h[j].parent==-1)
{
min1=h[j].weight;
index1=j;
}
}
h[index1].parent=i;
for(int j=0;j<i;j++)
{
if(h[j].weight<min2&&h[j].parent==-1)
{
min2=h[j].weight;
index2=j;
}
}
h[index2].parent=i;
if(index1>index2)
{
swap(min1,min2);
swap(index1,index2);
}
h[i].weight=min1+min2;
h[i].lch=index1;
h[i].rch=index2;
}
return 0;
}
int Createhuffcode(Huffnode h[],Hufftree hcd[],int n)
{
int head=0;
char s[15];
for(int i=0;i<n;i++)
{
int par=h[i].parent;
int j=i;
while(par!=-1)
{
if(h[par].lch==j) s[head]='0';
else s[head]='1';
head++;
j=par;
par=h[par].parent;
}
int k=0;
while(head>0)
{
head--;
hcd[i].code[k]=s[head];
k++;
}
cout<<hcd[i].code<<endl;
head=0;
}
return 0;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>h[i].weight;
CreateHufftree(h,n);
Createhuffcode(h,hcd,n);
}
}
问题 G: 视频合并问题
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fer(i,a,b) for(int i=a;i<=b;++i)
#define der(i,a,b) for(int i=a;i>=b;--i)
#define all(x) (x).begin(),(x).end()
#define pll pair<int,int>
#define et cout<<'\n'
#define xx first
#define yy second
using namespace std;
template <typename _Tp>void input(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
const int N=100010;
int a[N];
signed main(){
int n,x,res=0;
priority_queue<int,vector<int>,greater<int> >heap;
cin>>n;
while(n--){
cin>>x;
heap.push(x);
}
while(heap.size()>1){
int a=heap.top();
heap.pop();
int b=heap.top();
heap.pop();
res+=(a+b);
heap.push(a+b);
}
cout<<res;
return 0;
}
问题 H: 二叉树实现的简单物种识别系统-附加代码模式
#include <bits/stdc++.h>
using namespace std;
struct BiNode
{
string data;
BiNode *lchild, *rchild;
};
typedef BiNode *BiTree;
int InitBiTree(BiTree &T)
{
T = NULL;
return 0;
}
BiNode *StartRecognize(BiTree T)
{
BiTree p=T;
char c;
for(int i=0;i<2;i++){
cin>>c;
if(c=='y'||c=='Y'){
p=p->rchild;
}else{
p=p->lchild;
}
}
return p;
}
BiNode* getNode(string data){
BiNode* node = new BiNode();
node->data = data;
node->lchild = node->rchild = nullptr;
}
数据的同学强烈建议python+if
a = input()
b = input()
if a == 'y' or a =='Y':
if b == 'y' or b =='Y':
print("the answer is:eagle")
else:
print("the answer is:swallow")
else:
if b == 'y' or b =='Y':
print("the answer is:tiger")
else:
print("the answer is:rabbit")
问题 I: 静态链表存储的二叉树查找根节点
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fer(i,a,b) for(int i=a;i<=b;++i)
#define der(i,a,b) for(int i=a;i>=b;--i)
#define all(x) (x).begin(),(x).end()
#define pll pair<int,int>
#define et cout<<'\n'
#define xx first
#define yy second
using namespace std;
template <typename _Tp>void input(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
const int N=1e6+10;
int s1[N];
char s2[N];
signed main(){
int T=2;
while(T--)
{
memset(s1,0,sizeof s1);
int n;
cin>>n;
fer(j,0,n-1)
{
cin>>s2[j];
char a,b;
cin>>a>>b;
if (a != '-')
s1[a - '0']++;
if (b != '-')
s1[b - '0']++;
}
fer(j,0,n-1)
{
if(s1[j]==0)
{
cout<<s2[j]<<endl;
break;
}
}
}
}
问题 J: 基础实验4-2.1:树的同构
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
char data;
int left;
int right;
}T1[10],T2[10];
int a[100];
int BuildTree(TreeNode T[])
{
memset(a,0,sizeof a);
int n;
int root=-1;
cin>>n;
for(int i=0;i<n;i++)
{ char cl,cr;
cin>>T[i].data>>cl>>cr;
if(cl!='-')
{
T[i].left=cl-'0';
a[cl-'0']=1;
}
else{
T[i].left=-1;
}
if(cr!='-')
{
T[i].right=cr-'0';
a[cr-'0']=1;
}
else{
T[i].right=-1;
}
}
for(int i=0;i<n;i++)
{
if(a[i]==0)
{
root=i;
break;
}
}
return root;
}
int judge(int R1,int R2)
{
if(R1==-1&&R2==-1){
return 1;
}
if((R1==-1&&R2!=-1)||(R1!=-1&&R2==-1)){
return 0;
}
if((R1!=-1&&R2!=-1)&&T1[R1].data!=T2[R2].data){
return 0;
}
if(T1[R1].left==-1&&T2[R2].left==-1){
return judge(T1[R1].right,T2[R2].right);
}
if((T1[R1].left!=-1&&T2[R2].left!=-1)&&T1[T1[R1].left].data==T2[T2[R2].left].data){
return judge(T1[R1].left,T2[R2].left)&&judge(T1[R1].right,T2[R2].right);
}
else{
return judge(T1[R1].left,T2[R2].right)&&judge(T1[R1].right,T2[R2].left);
}
}
int main()
{
int root1=BuildTree(T1);
int root2=BuildTree(T2);
if(judge(root1,root2))
{
cout<<"Yes";
}
else{
cout<<"No";
}
}
问题 K: 哈夫曼树--查找值最小的两个叶节点
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define fer(i,a,b) for(int i=a;i<=b;++i)
#define der(i,a,b) for(int i=a;i>=b;--i)
#define all(x) (x).begin(),(x).end()
#define pll pair<int,int>
#define et cout<<'\n'
#define xx first
#define yy second
using namespace std;
template <typename _Tp>void input(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
const int N=1e6+10;
pll a[N];
signed main(){
int n;
cin>>n;
fer(i,1,n){
cin>>a[i].first;
a[i].second=i-1;
}
sort(a+1,a+1+n);
cout<<a[1].second<<" "<<a[2].second;
}
问题 L: 静态链表存储的二叉树的非递归遍历算法-附加代码模式
#include <bits/stdc++.h>
using namespace std;
struct BiNode
{
char data;
char lchild;
char rchild;
int flag = 0;
};
struct BiTree
{
int nodeNumber;
BiNode data[10];
int rootIndex;
};
void FindRootIndex(BiTree & T){
vector<char> v;
for (int i = 0; i < T.nodeNumber; ++i) {
if (T.data[i].lchild != '-') v.push_back(T.data[i].lchild-'0');
if (T.data[i].rchild != '-') v.push_back(T.data[i].rchild-'0');
}
for (int i = 0; i < T.nodeNumber; ++i) {
if (find(v.begin(), v.end(), i) == v.end())
{
T.rootIndex = i;
return;
}
}
}
string getPreTraverseStr(const BiTree & T){
if (T.nodeNumber <= 0) return "";
string s;
stack<BiNode> sta;
sta.push(T.data[T.rootIndex]);
while (!sta.empty())
{
BiNode t;
t = sta.top();
sta.pop();
s += t.data;
if (t.rchild != '-') sta.push(T.data[t.rchild-'0']);
if (t.lchild != '-') sta.push(T.data[t.lchild-'0']);
}
return s;
}
string getInTraverseStr(const BiTree & T){
if (T.nodeNumber <= 0) return "";
string s;
stack<BiNode> sta;
sta.push(T.data[T.rootIndex]);
while (!sta.empty())
{
if (sta.top().flag == 0) {
sta.top().flag++;
if (sta.top().lchild != '-') {
sta.push(T.data[sta.top().lchild - '0']);
}
}
else
{
BiNode t;
t = sta.top();
sta.pop();
s += t.data;
if (t.rchild != '-')
{
sta.push(T.data[t.rchild-'0']);
}
}
}
return s;
}
string getSucTraverseStr(const BiTree & T){
if (T.nodeNumber <= 0) return "";
string s;
stack<BiNode> sta;
sta.push(T.data[T.rootIndex]);
while (!sta.empty())
{
if (sta.top().flag == 0)
{
sta.top().flag++;
if (sta.top().lchild != '-')
{
sta.push(T.data[sta.top().lchild-'0']);
}
}
else if (sta.top().flag == 1)
{
sta.top().flag++;
if (sta.top().rchild != '-')
{
sta.push(T.data[sta.top().rchild-'0']);
}
}
else
{
s += sta.top().data;
sta.pop();
}
}
return s;
}