西工大数据结构理论课NOJ,第七组,查找排序

//构造哈希表
#include <iostream>
#include<cstring>
using namespace std;
int hashfun(int d)
{
    return (3 * d) % 11;
}
int main()
{
    int data[8] = { 22,41,53,46,30,13,01,67 };
    int hasht[11];
    memset(hasht, -1, sizeof(hasht));
    int sum = 0;
    for (int i = 0; i < 8; i++)
    {
        int k = hashfun(data[i]);
        int num = 1;
        while (hasht[k] != -1)
        {
            k = (k + 12) % 11;
            num++;
        }
        hasht[k] = data[i];
        sum += num;
    }
    cout << sum / 8 << endl;
    return 0;
}

 emm……这道题怎么说呢,其实你直接输出一个2也可以AC通过。

//二叉排序树的判别
#include <iostream>
using namespace std;
typedef struct node{
    int data;
    node* lchild;
    node* rchild;
}*pnode;
int minn=-1;
void create(pnode root)
{
    int e;
    cin>>e;
    if(e==-1)
        return;
    pnode p=(pnode)malloc(sizeof(node));
    p->data=e;
    p->rchild=NULL;
    p->rchild=NULL;
    root=p;
    create(root->lchild);
    create(root->rchild);
}
bool is(pnode root)
{
    pnode temp=root;
    if(temp==NULL)
        return true;
    if(!is(root->lchild))
        return false;
    if(root->data<minn)
        return false;
    minn=root->data;
    if(!is(root->rchild))
       return false;
    return true;
}
int main()
{
    pnode root=NULL;;
    create(root);
    if(is(root))
        cout<<"yes"<<endl;
    else 
        cout<<"no"<<endl;
    return 0;
}

 

 

#include <iostream>
using namespace std;
typedef struct node{
    int data;
    node* lchild;
    node* rchild;
}*pnode;
pnode create()
{
    int e;
    cin>>e;
    if(e==-1)
        return NULL;
    pnode p=(pnode)malloc(sizeof(node));
    p->data=e;
    p->rchild=NULL;
    p->rchild=NULL;
    p->lchild=create();
    p->rchild=create();
    return p;
}
void print1(pnode root,int a,int b)
{
    if(root==NULL)
    {
        return;
    }
    print1(root->lchild,a,b);
    if(root->data<b&&root->data>a)
        cout<<root->data<<" ";
    print1(root->rchild,a,b);
}
void print(pnode root)
{
    if(root==NULL)
        return;
    print(root->lchild);
    cout<<root->data<<" ";
    print(root->rchild);
}
pnode treeinsert(pnode root,int in)
{
    if(root==NULL)
    {
        root=(pnode)malloc(sizeof(node));
        root->data=in;
        root->lchild=root->rchild=NULL;
        return root;
    }
    if(root->data==in) return root;
    if(root->data>in)
    {
        root->lchild=treeinsert(root->lchild,in);
        return root;
    }
    root->rchild=treeinsert(root->rchild,in);
    return root;
}
pnode treedelete(pnode root,int del)
{
    if(root==NULL)
        return NULL;
    if(root->data==del)
    {
        pnode p=root->lchild;
        if(p==NULL)
        {
            free(root);
            return root->rchild;
        }
        pnode L=root->lchild;
        while(L->rchild!=NULL)
            L=L->rchild;
        L->rchild=root->rchild;
        free(root);
        return p;
    }
    else if(root->data>del)
    {
        root->lchild=treedelete(root->lchild,del);
        return root;
    }
    else
    {
        root->rchild=treedelete(root->rchild,del);
        return root;
    }
}
int main()
{
    pnode root=create();
    pnode temp=root;
    int a,b;
    cin>>a>>b;
    print1(temp,a,b);
    cout<<endl;


    int in;
    cin>>in;
    root=treeinsert(root,in);
    print(root);
    cout<<endl;

    int del;
    cin>>del;
    root=treedelete(root,del);
    print(root);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
    int data;
    node*lchild;
    node*rchild;
}*pnode;
pnode create()
{
    int e;
    cin>>e;
    if(e==-1)
        return NULL;
    pnode root=(pnode)malloc(sizeof(node));
    root->lchild=NULL;
    root->rchild=NULL;
    root->data=e;
    root->lchild=create();
    root->rchild=create();
    return root;
}
pnode treeinsert(pnode root,int in)
{
    if(root==NULL)
    {
        root=(pnode)malloc(sizeof(node));
        root->data=in;
        root->lchild=root->rchild=NULL;
        return root;
    }
    if(root->data==in)
        return root;
    if(root->data>in)
    {
        root->lchild=treeinsert(root->lchild,in);
        return root;
    }
    root->rchild=treeinsert(root->rchild,in);
    return root;

}
void rootmerge(pnode root1,pnode root2)
{
    if(!root2||!root1)
        return;
    root1=treeinsert(root1,root2->data);
    rootmerge(root1,root2->lchild);// 合并左子树
    rootmerge(root1,root2->rchild);// 合并右子树
}
void print(pnode root)
{
    if(root==NULL)
        return;
    print(root->lchild);
    cout<<root->data<<" ";
    print(root->rchild);
}
int main()
{
    pnode root1=create();
    pnode root2=create();
    rootmerge(root1,root2);
    print(root1);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值