#include<string>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef struct Node
{
int key;
struct Node * left;
struct Node * right;
struct Node * parent;
}Node,*PNode;
void inseart(PNode * root,int key)
{
PNode p=(PNode)malloc(sizeof(Node));
p->key=key;
p->left=p->right=p->parent=NULL;
if((*root)==NULL){
*root=p;
return;
}
if((*root)->left == NULL && (*root)->key < key){
p->parent=(*root);
(*root)->left=p;
return;
}
if((*root)->right == NULL && (*root)->key > key){
p->parent=(*root);
(*root)->right=p;
return;
}
if((*root)->key < key)
inseart(&(*root)->left,key);
else if((*root)->key > key)
inseart(&(*root)->right,key);
else
return;
}
int a[maxn];
string ans ;
void search(PNode root,int key)
{
if(root == NULL)
return ;
if(key < root->key)
{ ans = ans + "E"; search(root->right,key);}
else if(key > root->key)
{ ans = ans + "W"; search(root->left,key); }
else
return ;
}
int main()
{
int t ; cin >> t; int q, n ;
while(t--){
PNode root=NULL;
scanf("%d", &n);
for(int i = 1; i <= n ; i ++){
scanf("%d", &a[i]);
}
for(int i = 1 ; i <= n ; i ++){
inseart(&root, a[i]);
}
scanf("%d", &q); int num;
while( q-- ){
scanf("%d", &num);
ans = "";
search(root , num);
cout << ans <<endl;
}
}
}
#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct Node
{
KeyType key;
struct Node * left;
struct Node * right;
struct Node * parent;
}Node,*PNode;
void inseart(PNode * root,KeyType key)
{
PNode p=(PNode)malloc(sizeof(Node));
p->key=key;
p->left=p->right=p->parent=NULL;
if((*root)==NULL){
*root=p;
return;
}
if((*root)->left == NULL && (*root)->key > key){
p->parent=(*root);
(*root)->left=p;
return;
}
if((*root)->right == NULL && (*root)->key < key){
p->parent=(*root);
(*root)->right=p;
return;
}
if((*root)->key > key)
inseart(&(*root)->left,key);
else if((*root)->key < key)
inseart(&(*root)->right,key);
else
return;
}
PNode search(PNode root,KeyType key)
{
if(root == NULL)
return NULL;
if(key > root->key)
return search(root->right,key);
else if(key < root->key)
return search(root->left,key);
else
return root;
}
PNode searchMin(PNode root)
{
if(root == NULL)
return NULL;
if(root->left == NULL)
return root;
else
return searchMin(root->left);
}
PNode searchMax(PNode root)
{
if(root == NULL)
return NULL;
if(root->right == NULL)
return root;
else
return searchMax(root->right);
}
PNode searchPredecessor(PNode p)
{
if(p==NULL)
return p;
if(p->left)
return searchMax(p->left);
else{
if(p->parent == NULL)
return NULL;
while(p){
if(p->parent->right == p)
break;
p=p->parent;
}
return p->parent;
}
}
PNode searchSuccessor(PNode p)
{
if(p==NULL)
return p;
if(p->right)
return searchMin(p->right);
else{
if(p->parent == NULL)
return NULL;
while(p){
if(p->parent->left == p)
break;
p=p->parent;
}
return p->parent;
}
}
int deleteNode(PNode* root,KeyType key)
{
PNode q;
PNode p=search(*root,key);
KeyType temp;
if(!p)
return 0;
if(p->left == NULL && p->right == NULL){
if(p->parent == NULL){
free(p);
(*root)=NULL;
}else{
if(p->parent->left == p)
p->parent->left=NULL;
else
p->parent->right=NULL;
free(p);
}
}
else if(p->left && !(p->right)){
p->left->parent=p->parent;
if(p->parent == NULL)
*root=p->left;
else if(p->parent->left == p)
p->parent->left=p->left;
else
p->parent->right=p->left;
free(p);
}
else if(p->right && !(p->left)){
p->right->parent=p->parent;
if(p->parent == NULL)
*root=p->right;
else if(p->parent->left == p)
p->parent->left=p->right;
else
p->parent->right=p->right;
free(p);
}
else{
q=searchSuccessor(p);
temp=q->key;
deleteNode(root,q->key);
p->key=temp;
}
return 1;
}
void create(PNode* root,KeyType *keyArray,int length)
{
int i;
for(i=0;i<length;i++)
inseart(root,keyArray[i]);
}
int main(void)
{
int i;
PNode root=NULL;
KeyType nodeArray[11]={15,6,18,3,7,17,20,2,4,13,9};
create(&root,nodeArray,11);
for(i=0;i<2;i++)
deleteNode(&root,nodeArray[i]);
printf("%d\n",searchPredecessor(root)->key);
printf("%d\n",searchSuccessor(root)->key);
printf("%d\n",searchMin(root)->key);
printf("%d\n",searchMax(root)->key);
printf("%d\n",search(root,13)->key);
return 0;
}