数据结构——次优查找树的构造

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

typedef struct treenode
{
    struct treenode *left;
    char data;
    int weight;
    struct treenode *right;
}Treenode,* Treep;
int low=1,high=10;
char *R;
int *weight;    
int *sw;
//初始化二叉树
void init_tree(Treep &root)
{
    root=NULL;
    cout<<"初始化成功!"<<endl;
}

//创建二叉树
void SecondOptimal(Treep &rt, char R[],int sw[], int low, int high)
{
    //由有序表R[low....high]及其累积权值表sw(其中sw[0]==0)递归构造次优查找树T
    int i=low;
    int min = fabs(sw[high] - sw[low]);
    int dw = sw[high] + sw[low-1];
    for(int j=low+1; j<=high; ++j)        //选择最小的ΔPi值
    {
        if(fabs(dw-sw[j]-sw[j-1]) < min)
        {
            i=j;
            min=fabs(dw-sw[j]-sw[j-1]);
        }
    }
    rt=(Treep)malloc(sizeof(Treenode));
    rt->data=R[i];        //生成节点
    if(i==low)            //左子树为空
        rt->left = NULL;
    else                //构造左子树
        SecondOptimal(rt->left, R, sw, low, i-1);

    if(i==high)            //右子树为空
        rt->right = NULL;
    else                //构造右子树
        SecondOptimal(rt->right, R, sw, i+1, high);
}//SecondOptimal

//前序遍历二叉树
void pre_order(Treep &rt)
{
    if(rt!=NULL)
    {
        cout<<rt->data<<"  ";
        pre_order(rt->left);
        pre_order(rt->right);
    }
}


//查找二叉树中是否存在某元素
int seach_tree(Treep &rt,char key)
{
    if(rt==NULL) 
        return 0; 
    else 
    { 
        if(rt->data==key) 
        {
            return 1;
        }
        else
        {
            if(seach_tree(rt->left,key) || seach_tree(rt->right,key))
                return 1;    //如果左右子树有一个搜索到,就返回1
            else
                return 0;    //如果左右子树都没有搜索到,返回0
        }
    }
}


int main()
{
    Treep root;
    init_tree(root);        //初始化树


    R=(char *)malloc( high*sizeof(char) );
    for(int i=low; i<high; i++)
        R[i]='A'+i-1;
    cout<<"构造次优查找树的点R[]:"<<endl;
    for(i=low-1; i<high; i++)
        cout<<setw(3)<<R[i]<<"  ";
    cout<<endl;
    
    weight=(int *)malloc( high*sizeof(int) );
    weight[0]=0;
    weight[1]=1;
    weight[2]=1;
    weight[3]=2;
    weight[4]=5;
    weight[5]=3;
    weight[6]=4;
    weight[7]=4;
    weight[8]=3;
    weight[9]=5;
    cout<<"构造次优查找树的点的权值weight[]:"<<endl;
    for(i=low-1; i<high; i++)
        cout<<setw(3)<<weight[i]<<"  ";
    cout<<endl;
    

    sw=(int *)malloc( high*sizeof(int) );
    sw[0]=0;
    for(i=low; i<high; i++)
    {
        sw[i]=sw[i-1]+weight[i];
    }
    cout<<"构造次优查找树的点累积权值sw[]:"<<endl;
    for(i=low-1; i<high; i++)
        cout<<setw(3)<<sw[i]<<"  ";
    cout<<endl;


    //创建二叉树
    SecondOptimal(root, R, sw, low, high-1);

    //前序遍历二叉树
    cout<<endl<<"前序遍历序列是:"<<endl;
    pre_order(root);
    cout<<endl;


    //查找二叉树中是否存在某元素
    cout<<"输入要查找的元素!"<<endl;
    char ch;
    cin>>ch;
    if(seach_tree(root,ch)==1)
        cout<<"yes!"<<endl;
    else
        cout<<"no!"<<endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

minyuanxiani

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

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

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

打赏作者

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

抵扣说明:

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

余额充值