【二叉树的建立操作】集合

一、 扩展二叉树

扩展二叉树的建立可以用先序序列 后序序列 不能用 中序序列

以字符串的形式定义一棵二叉树的先序序列,若字符是‘#’, 表示该二叉树是空树,否则该字符是相应结点的数据元素。读入相应先序序列,建立二叉树,然后按层次遍历该二叉树并输出结点数据。

输入格式:

字符串形式的先序序列(即结点的数据类型为单个字符)

输出格式:

按层次遍历二叉树的结果

输入样例:

在这里给出一组输入。例如:

ABDG##HI####CE#J##F##

解法:

先序序列,根左右;

中序序列,左根右;

后序序列,左右根;

//适用于前序
#include<bits/stdc++.h>
using namespace std;
typedef struct BuildTree
{
	char ch;
	struct BuildTree *left;
	struct BuildTree *right;
}btree, *bt;
bt buildtree(bt root) 
{
	char kk;
	cin>>kk;
	root=(bt)malloc(sizeof(btree));
	if(kk=='#')
	return NULL;
	root->ch=kk;
	root->left=buildtree(root->left);
	root->right=buildtree(root->right);
	return root;
}
int main()
{
	bt root;
	root=buildtree(root);
}

二、给定遍历顺序,建立二叉树

先序序列+中序序列

9
前序:ABDFGHIEC
中序:FDHGIBEAC

#include<bits/stdc++.h>
using namespace std;
typedef char type;
typedef struct node
{
	type data;
	struct node *left;
	struct node *right;
}treebox,*bt;
bt creattree(char pre[],char mid[],int size)
{
	if(size<=0)
	return NULL;
	bt root;	int m;
	for(m=0;m<size;m++) if(mid[m]==pre[0]) break;
	root=(bt)malloc(sizeof(treebox));
	root->data=pre[0];
	root->left=creattree(pre+1,mid,m);
	root->right=creattree(pre+m+1,mid+m+1,size-m-1);
	return root;
}
//void midorder (bt root)  检查函数 中序遍历 
// {
// 	if(root==NULL) return ;
// 	if(root->left) midorder(root->left);
// 	cout<<root->data<<" ";
// 	if(root->right) midorder(root->right);
// }
int main()
{
	char pre[1000];
	char mid[1000];
	int n;
	cin>>n;
	for(int i=0 ;i<n;i++)
	cin>>pre[i];
	for(int i=0;i<n;i++)
	cin>>mid[i];
	bt root;
	root=creattree(pre,mid,n);	
	midorder(root);
} 

后序序列+中序序列

7

后序:2 3 1 5 7 6 4

中序:1 2 3 4 5 6 7

#include<bits/stdc++.h>
using namespace std;
typedef char type;
typedef struct node
{
	type data;
	struct node *left;
	struct node *right;
}treebox,*bt;

bt creattree( char post[],char middd[],int size)
{
	if(size<=0) return NULL;
	bt root;	int m;	
	root=(bt)malloc(sizeof(treebox));
	for(m=0;m<size;m++) if(middd[m]==post[size-1])	break;
	root->data=post[size-1];
	root->left=creattree(post,middd,m);
	root->right=creattree(post+m,middd+m+1,size-m-1);
	return root;
 } 
// void midorder (bt root)  检查函数 中序遍历 
// {
// 	if(root==NULL) return ;
// 	if(root->left) midorder(root->left);
// 	cout<<root->data<<" ";
// 	if(root->right) midorder(root->right);
// }
 int main()
 {
 	int n;
 	cin>>n;
 	char post[10000],middd[10000];
 	for(int i=0;i<n;i++)
 	cin>>post[i];
 	for(int i=0;i<n;i++)
 	cin>>middd[i];
 	bt root;
 	root=creattree(post,middd,n);
 	midorder(root);
 }

三、完全二叉树

#include<bits/stdc++.h>
using namespace std;
int a[100];
int b[100];
int con=0;
int n;
//  根据完全二叉树的性质  i=1 左 i=2*i 右 i=2*+1;
//  dfs 深度遍历 找到最左边的点,  模拟前序遍历 中序遍历  后序遍历 过程 , 
// 最后的出的新数组 就是 层序遍历的数据  
void dfs(int i)
{
    if(i>n) return ;
    dfs(i*2);
    b[i]=a[con++];  // 添加,前中后 对应 相应的序列;     
    dfs(i*2+1);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    dfs(1);
    for(int i=1;i<=n;i++)
    {
        if(i>1)
            cout<<" "<<b[i];
        else cout<<b[i];
    }
}

四、哈夫曼树

未待续完》》》》》

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

反手敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值