数据结构第7章

╄→尐↘猪︶ㄣ 提交于 2019-12-19 00:57:02
Position BinarySearch( List L, ElementType X )
{
    int begin = 0,last = L->Last;
    
    for(int i = 1;i <=last;i++)
    {
        if(L->Data[i] == X){
            return i;
        }
    }
    return NotFound;
}
bool Insert( List L, ElementType X )
{

    if(L->Last == MAXSIZE - 1){
        return false;
    }
    

    Position last = L->Last;
    Position index = 0;
    for(Position i = 0;i < last;i++)
    {
        if(L->Data[i]==X)return false;
        if(L->Data[i]<X){
            index = i;
            break;
        }
    }
    
    for(Position j = last+1;j > index;j--)
    {
        L->Data[j] = L->Data[j-1];
    }
    L->Data[index] = X;
    L->Last++;

    return true;
}

BinTree Insert( BinTree BST, ElementType X )
{
	if(BST==NULL){
		BinTree tr = malloc(sizeof (BinTree));
		tr->Left = tr->Right = NULL;
		tr->Data = X;
		BST = tr;
		return BST;
	}
	if(X < BST->Data)
	{
		BST->Left = Insert(BST->Left,X);
	}
	else
	{
		BST->Right = Insert(BST->Right,X);
	}
	return BST;
}
BinTree Delete( BinTree BST, ElementType X )
{
	BinTree tr = Find(BST,X);
	if(tr == NULL)
	{ 
		printf("Not Found\n");
		return BST;
	}
	else if(tr->Data == X)
	{
		if(tr->Left == NULL && tr->Right ==NULL)
		{
            	if(BST==tr){
				BST = NULL;
				return BST;
			}
			BinTree pre = BST;
			BinTree p = BST;
			while(1)
			{
				if(X > p->Data){
					pre =p;
					p=p->Right;
				}
				else if(X == p->Data){
					break;
				}
				else{
					pre = p;
					p = p->Left;
				}
			}
			
			if(pre->Left){
				if(pre->Left->Data==X){
					pre->Left = NULL;
				}
				else{
					pre->Right = NULL;
				}
			}
			else{
				pre->Right = NULL;
			}
			
			return BST;
		}
		else if(tr->Left)
		{
			BinTree pre = FindMax(tr->Left);
			tr->Data = pre->Data;
			if(pre==tr->Left)
			{
				tr->Left = pre->Left;
				return BST;
			}
			Delete(tr->Left,pre->Data);
			return BST;
		}
		else
		{
			BinTree pre = FindMin(tr->Right);
			tr->Data = pre->Data;
			if(tr->Right==pre)
			{
				tr->Right = pre->Right;
				return BST;
			}
			Delete(tr->Right,pre->Data);
			return BST;
		}
	}
}
Position Find( BinTree BST, ElementType X )
{
	if(BST==NULL){
		return NULL;
	}
	if(X == BST->Data){
		return BST;
	}
	if(X < BST->Data)
	{
		Find(BST->Left,X);
	}else{
		Find(BST->Right,X);
	}
}
Position FindMin( BinTree BST )
{
    if(!BST)return BST;
	while(BST->Left){
		BST = BST->Left;
	}
	return BST;
}
Position FindMax( BinTree BST )
{
    if(!BST)return BST;
	while(BST->Right){
		BST = BST->Right;
	}
	return BST;
}
#include <iostream>
using namespace std;
const int N = 100000+10;
int a[N],b[N],c[N*2];

int n;
int main()
{
    cin >> n;
    for(int i = 0;i< n;i++)
    {
        cin >> a[i];
    }

    for(int i = 0;i < n;i++)
    {
        cin >> b[i];
    }

    int i = 0,j = 0,cnt = 0;

    while(i<n&&j<n)
    {
        if(a[i]<b[j]){
            c[cnt++] = a[i++];
        }else{
            c[cnt++] = b[j++];
        }
    }

    while(i<n)
    {
        c[cnt++] = a[i++];
    }
    while(j<n)
    {
        c[cnt++] = b[j++];
    }
//
//    for(int i = 0;i < cnt;i++)
//    {
//        cout <<c[i] <<" ";
//    }

    int t = cnt+1>>1;
    t--;
    cout << c[t] << endl;

    return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!