找出这个代码的错误并进行纠正:#include<stdio.h> #include<malloc.h> #include<conio.h> typedef struct tree { int data; struct tree*lchild; struct tree*rchild; }TREE; typedef struct stack { TREE*t; int flag; struct stack*link; }STACK; int push(STACK**top,TREE*tree) { STACK*p; p=(STACK*)malloc(sizeof(STACK)); p->t=tree; p->link=*top; *top=p; } int pop(STACK**top,TREE**tree) { STACK*p; if(*top==NULL) *tree=NULL; else { *tree=(*top)->t; p=*top; *top=(*top)->link; free(p); } } int SearchNode(TREE*tree, int key, TREE**pkpt, TREE**kpt) { *pkpt=NULL; *kpt=tree; while(*kpt!=NULL) { if((*kpt)->data==key) return 0; *pkpt=*kpt; if(key<(*kpt)->data) *kpt=(*kpt)->lchild; else *kpt=(*kpt)->rchild; } } int InsertNode(TREE**tree,int key) { TREE*p,*q,*r; SearchNode(*tree,key,&p,&q); if(q!=NULL) return 1; if((r=(TREE*)malloc(sizeof(TREE)))==NULL) return-1; r->data=key; r->lchild=r->rchild=NULL; if(p==NULL) *tree=r; else if(p->data>key) p->lchild=r; else p->rchild=r; return 0; } int DeleteNode(TREE**tree,int key) { TREE*p,*q,*r; SearchNode(*tree,key,&p,&q); if(q==NULL) return 1; if(p==NULL) if(q->lchild==NULL) *tree=q->rchild; else { *tree=q->lchild; r=q->lchild; while(r->rchild!=NULL) r=r->rchild; r->rchild=q->rchild; } else if(q->lchild==NULL) if(q==p->lchild) p->lchild=q->rchild; else p->rchild=q->rchild; else { r=q->lchild; r->rchild=q->rchild; if(q==p->lchild) p->lchild=q->lchild; else p->rchild=q->lchild; } free(q); return 0; }
时间: 2024-03-06 13:49:24 浏览: 88
这段代码存在以下错误:
1. push函数和pop函数的返回类型应该为void,因为它们没有返回值。
2. 在SearchNode函数中,如果key小于等于当前节点的值,应该将kpt指向左子节点,不然应该将kpt指向右子节点。
3. 在DeleteNode函数中,当q为要删除的节点时,如果p为NULL,应该将q的左子节点连接到q的右子节点的最左边节点,而不是最右边节点。
下面是修改后的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct tree {
int data;
struct tree *lchild;
struct tree *rchild;
} TREE;
typedef struct stack {
TREE *t;
int flag;
struct stack *link;
} STACK;
void push(STACK **top, TREE *tree) {
STACK *p;
p = (STACK*)malloc(sizeof(STACK));
p->t = tree;
p->link = *top;
*top = p;
}
void pop(STACK **top, TREE **tree) {
STACK *p;
if(*top == NULL) {
*tree = NULL;
}
else {
*tree = (*top)->t;
p = *top;
*top = (*top)->link;
free(p);
}
}
void SearchNode(TREE *tree, int key, TREE **pkpt, TREE **kpt) {
*pkpt = NULL;
*kpt = tree;
while(*kpt != NULL) {
if((*kpt)->data == key) {
return;
}
*pkpt = *kpt;
if(key <= (*kpt)->data) {
*kpt = (*kpt)->lchild;
}
else {
*kpt = (*kpt)->rchild;
}
}
}
int InsertNode(TREE **tree, int key) {
TREE *p, *q, *r;
SearchNode(*tree, key, &p, &q);
if(q != NULL) {
return 1;
}
if((r = (TREE*)malloc(sizeof(TREE))) == NULL) {
return -1;
}
r->data = key;
r->lchild = r->rchild = NULL;
if(p == NULL) {
*tree = r;
}
else if(p->data > key) {
p->lchild = r;
}
else {
p->rchild = r;
}
return 0;
}
int DeleteNode(TREE **tree, int key) {
TREE *p, *q, *r;
SearchNode(*tree, key, &p, &q);
if(q == NULL) {
return 1;
}
if(p == NULL) {
if(q->lchild == NULL) {
*tree = q->rchild;
}
else {
*tree = q->lchild;
r = q->lchild;
while(r->rchild != NULL) {
r = r->rchild;
}
r->rchild = q->rchild;
}
}
else if(q->lchild == NULL) {
if(q == p->lchild) {
p->lchild = q->rchild;
}
else {
p->rchild = q->rchild;
}
}
else {
r = q->lchild;
r->rchild = q->rchild;
if(q == p->lchild) {
p->lchild = q->lchild;
}
else {
p->rchild = q->lchild;
}
}
free(q);
return 0;
}
```
阅读全文