Description
The binary sorting tree is used to design a data structure that supports fast sequential statistic operations. This data structure supports three operations: insert X inserts a number X into the dataset, delete x deletes a number X in the dataset, and order x outputs the number X in the current dataset. (Ensure that all elements in the dataset are not equal. The data in this question is Int)
-
Input
-
The initial data set of the first action. elements are separated by spaces. The number of M lines in the second action is M. The following M lines contain M commands. The Instruction format is described in the topic.
-
Output
-
Corresponding to each order x command, output the number X in the current dataset.
Simple search
#include <stdio.h>#include <string.h>char s[100000];int a[100000];main(){int number;int length,i,j;int p;int up;int temp;int count;char op;up=0;gets(s);length=strlen(s);i=0;p=0;for(i=0;i<length;i++){if(s[i]!=','){p=0;while(s[i] >= '0' && s[i] <= '9'&&i<length){p = p * 10 + s[i] - '0';i++;}a[up++]=p;}elsecontinue;}for(i=0;i<up;i++)for(j=i+1;j<up;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}scanf("%d",&count);while(count--){getchar();scanf("%c",&op);if(op=='O'){for(i=0;i<5;i++)scanf("%c",&op);scanf("%d",&temp);printf("%d\n",a[temp-1]);}if(op=='I'){for(i=0;i<6;i++) scanf("%c",&op);scanf("%d",&temp);if(temp>a[up-1])a[up++]=temp;else{for(i=0;i<up;i++){if(a[i]>temp){ j=i; break;}elsecontinue;}for(i=up;i>j;i--)a[i]=a[i-1];a[j]=temp; up++;}}if(op=='D'){for(i=0;i<6;i++)scanf("%c",&op);scanf("%d",&temp);for(i=0;i<up;i++)if(a[i]==temp)break;for(j=i;j<up-1;j++)a[j]=a[j+1];up--;}}}