小铃铛想要的数据结构答案

1
struct elemtype{
    Keytype key;  //关键字域
};
struct SStable{
    //顺序表结构类型定义
    elemtype *elem; //表基址
    int length; //表长
};
SStable L;//定义顺序表L

//在有序的顺序表中,折半查找(或叫二分查找),是效率最快的算法
int search_result(SStable L,Keytype key)
{
    
    int low=1;
    int high=L.length;
    while(low<=high)
    {
        
        mid=(low+high)/2;
        if(L.elem[mid].key==key) //找到待查元素
            return mid;
        else if(key<L.elem[mid].key) //待查元素比中间元素小
            high=mid-1; //在坐半区间查找
        else
            low=mid+1; //否则 在右半区间查找
    }
    return 0;
}
2
void LinkList_clear(LinkList &L,int min,int max)
{
  node *q,*t,*p,*r;
  p = L->next;
	while (p!=NULL&& p->date <= min)
	{//查找第一个值大于min的结点
		t = p;
		p = p->next;
	}
	if (p!=NULL)
	{
		while (p && p->date < max)
		{		// 当数小于max的时候一直往下找
			p = p->next;
		}
		q = t->next;//把第一个大于min值的地址存起来,用于释放结点
		t->next = p;//在min前的结点的地址指向max后的结点从而完成删除
		while (q != p)
		{// 释放节点空间
			r = q->next;
			free(q);
			q = r;
		}
	}
}

3

#include <bits/stdc++.h>
using namespace std;
typedef struct
{	
	char data[MaxSize];
	int length;	//串长
} SqString;

void GetNext(SqString t,int next[])		//由模式串求出next值
{
	int j,k;
	j=0;k=-1;
	next[0]=-1;
	while (j<t.length-1) 

	{	
		if (k==-1 || t.data[j]==t.data[k]) 	
		{	
			j++;k++;
			next[j]=k;
			
       	}
       	else
		{
			k=next[k];
			
		}
	}
}

int KMP(SqString s,SqString t)  //KMP算法
{

	int next[MaxSize],i=0,j=0;
	GetNext(t,next);
	while (i<s.length && j<t.length) 
	{
		if (j==-1 || s.data[i]==t.data[j]) 
		{
			i++;j++;  			
		}
		else j=next[j]; 	
    }
    if (j>=t.length)
		return(1);  	//匹配成功
    else  
		return(-1);        		//返回不匹配标志
}


int main()
{
    int a=KMP(STR1,Pattern);
    int b=KMP(STR,Pattern);
    if((a+b))==2)
    printf("模式串Pattern是字符串STR1和STR2的公共子串");
    return 0;
}

4

int count=0;
int findcount(tree root)
{
    if(root==NULL)
        count=0;
    else if(!root->l&&!root->r)
        return 1;
    else
        return findcount(root->l)+findcount(root->r);
}

5 压轴题

int getheight(tree t) {    //求树高
	if (!t) return -1;
	else {
		int lheight = getheight(t->left);
		int rheight = getheight(t->right);
		return max(lheight, rheight) + 1;
	}
}
tree Lrotation(tree A) {  //LL
	tree B = A->left;
	A->left = B->right;
	B->right = A;
	A->height = getheight(A);
	B->height = getheight(B);
	return B;
}
tree Rrotation(tree A) {  //RR
	tree B = A->right;
	A->right = B->left;
	B->left = A;
	A->height = getheight(A);
	B->height = getheight(B);
	return B;
}
tree LRrotation(tree A) //LR
{   
	A->left = Rrotation(A->left);
	return Lrotation(A);
	
}

tree RLrotation(tree A) //RL
{
	A->right = Lrotation(A->right);
	return Rrotation(A);
}
  tree solve(tree p,int ) //判断是那种类型
  {
      
      if(getheight(p->letf)-getheight(p->right)==2)
      {
          if(getheight(p->left->left)-getheight(p->left->right)==1)
          {
              p=Lrotation(p);//LL
          }
          if(getheight(p->left->left)-getheight(p->left->right)==-1)
          {
              p=LRrotation(p);//LR
          }
      }
      if(getheight(p->right)-getheight(p->left)==2)
      {
          if(getheight(p->right->right)-getheight(p->right->left)==1)
          {
              p=Rrotation(p); //RR
          }
            if(getheight(p->right->right)-getheight(p->right->left)==-1)
            {
                p=RLrotation(p);//RL
            }
      }
  }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值