Witam.
Podłącze się, nie będę rozpoczynał nowego tematu.
Może ktoś sprawdzić czego ten kod nie przechodzi?
[bbone=cpp,3126]
include
include
typedef struct drzewo BST;
struct drzewo
{
int key;
BST *left;
BST *right;
BST *p;
};
BST *tree=NULL;
void inorder(BST *root)
{
if(!root)
return;
if(root->left)
inorder(root->left);
printf("%d ",root->key);
if(root->right)
inorder(root->right);
}
void preorder(BST *root)
{
if(root != NULL)
{
printf("%d ",root->key);
preorder(root->left);
preorder(root->right);
}
}
void postorder(BST *root)
{
if(root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->key);
}
}
BST *search(BST *root,int val)
{
if((root == NULL) || (val == root->key))
return root;
if(val < root->key)
return search(root->left,val);
else
return search(root->right,val);
return root;
}
BST *min(BST *root)
{
while(root->left != NULL)
root=root->left;
return root;
}
BST *max(BST *root)
{
while(root->right != NULL)
root=root->right;
return root;
}
BST *nast(BST *root)
{
BST *y=root;
if(root == NULL)
return y;
if(root->right != NULL)
return min(root->right);
y=root->p;
while(y != NULL && root == y->right)
{
root=y;
y=y->p;
}
return y;
}
BST *pop(BST *root)
{
BST *y=root;
if(root == NULL)
return y;
if(root->left != NULL)
return max(root->left);
y=root->p;
while(y != NULL && root == y->left)
{
root=y;
y=y->p;
}
return y;
}
BST *del(BST *root,int val)
{
BST *x=root;
BST *y=(BST *)malloc(sizeof(BST));
BST *del=search(root,val);
if(del == NULL)
return y;
if(del->left == NULL || del->right == NULL)
y=del;
else
y=nast(del);
if(y->left != NULL)
x=y->left;
else
x=y->right;
if(x != NULL)
x->p=y->p;
if(y->p == NULL)
root=x;
else
if(y == y->p->left)
y->p->left=x;
else
y->p->right=x;
if(y != del)
del->key=y->key;
return y;
}
BST *add(BST *root,int val)
{
BST *x=root;
BST *nowe=(BST *)malloc(sizeof(BST));
nowe->key=val;
nowe->p=nowe->left=nowe->right=NULL;
BST *y=NULL;
while(x != NULL)
{
y=x;
if(val < x->key)
x=x->left;
else
x=x->right;
}
nowe->p=y;
if(y == NULL)
root=nowe;
else
{
if(nowe->key < y->key)
y->left=nowe;
else
y->right=nowe;
}
return root;
}
void delete(BST *root)
{
}
int main(void)
{
BST *p;
int i,j,z,x,a;
char c;
scanf("%d",&i);
z=1;
while(i--)
{
scanf("%d%*c",&j);
printf("test %d\n",z++);
for(x=0;x {
scanf("%c %d%*c",&c,&a);
if(c == 'I')
tree=add(tree,a);
else
if(c == 'D')
del(tree,a);
else
if(c == 'S')
{
p=search(tree,a);
if(p == NULL)
printf("-\n");
else
printf("%d\n",p->key);
}
else
if(c == 'X')
{
switch(a)
{
case 0:
p=min(tree);
printf("%d\n",p->key);
break;
case 1:
p=max(tree);
printf("%d\n",p->key);
break;
}
}
else
if(c == 'N')
{
p=nast(search(tree,a));
if(p == NULL)
printf("-\n");
else
printf("%d\n",p->key);
}
else
if(c == 'P')
{
p=pop(search(tree,a));
if(p == NULL)
printf("-\n");
else
printf("%d\n",p->key);
}
else
if(c == 'R')
{
switch(a)
{
case 0:
inorder(tree);
putchar('\n');
break;
case 1:
preorder(tree);
putchar('\n');
break;
case 2:
postorder(tree);
putchar('\n');
break;
}
}
}
tree=NULL;
}
return 0;
}
[/bbone]
Testy podane w zadaniu mi przechodzi może ktoś zerknąć?
Dzięki.