#include<iostream> using namespace std; class treenode{ int data; treenode* lchild,* rchild; friend class tree; public: treenode(){ lchild=NULL; rchild=NULL; } }; class tree{ treenode* root,* current; public: tree(); void insert(int value); void insert(treenode*&r,int value); int deletenode(int value); int deletenode(treenode*&r,int value); void inorder(); void inorder(treenode*&r); }; tree::tree() { root=NULL; current=root; } void tree::insert(int value) { current=root; insert(current,value); } void tree::insert(treenode *&r,int value) { if(r==NULL) { r=new treenode; r->data=value; return; } if(value<r->data)insert(r->lchild,value); else if(value>r->data)insert(r->rchild,value); else return; } int tree::deletenode(int value) { current=root; return deletenode(current,value); } int tree::deletenode(treenode*&r,int value) { treenode* s; if(r!=NULL) if(value>r->data)deletenode(r->lchild,value); else if(value<r->data)deletenode(r->rchild,value); else if(r->lchild!=NULL&&r->rchild!=NULL) { treenode* p=r->rchild; while(p->lchild!=NULL)p=p->lchild; s=p; r->data=s->data; deletenode(r->rchild,r->data); return 1; } else { s=r; if(r->lchild==NULL) r=r->rchild; else if(r->rchild==NULL) r=r->lchild; delete s; return 1; } return 0; } void tree::inorder() { inorder(root); } void tree::inorder(treenode*&r) { inorder(r->lchild); cout<<r->data<<" "; inorder(r->rchild); } int main() { int n,i,x; tree t; cin>>n; cout<<"原始数据:"; for(i=0;i<n;i++) { cin>>x; t.insert(x); cout<<x<<" "; } cout<<"\n中序遍历结果:"; t.inorder(); cin>>x; t.deletenode(x); cout<<"\n删除结点后结果:"; t.inorder(); cout<<endl; return 0; }有什么问题?为什么无法正常插入?
时间: 2023-11-28 11:47:23 浏览: 73
这段代码有一个问题是在插入节点时,如果当前节点为NULL,则会创建一个新的节点。但是并没有将这个新节点连接到上一个节点的子节点上,导致插入操作无法正常进行。解决方法是需要在创建新节点时,将其连接到上一个节点的子节点上。
修改后的代码如下:
```
#include<iostream>
using namespace std;
class treenode{
int data;
treenode* lchild,* rchild;
friend class tree;
public:
treenode(){
lchild=NULL;
rchild=NULL;
}
};
class tree{
treenode* root,* current;
public:
tree();
void insert(int value);
void insert(treenode*&r,int value);
int deletenode(int value);
int deletenode(treenode*&r,int value);
void inorder();
void inorder(treenode*&r);
};
tree::tree() {
root=NULL;
current=root;
}
void tree::insert(int value) {
current=root;
insert(current,value);
}
void tree::insert(treenode *&r,int value) {
if(r==NULL) {
r=new treenode;
r->data=value;
r->lchild=NULL; // 将新节点连接到上一个节点的子节点上
r->rchild=NULL;
return;
}
if(value<r->data) insert(r->lchild,value);
else if(value>r->data) insert(r->rchild,value);
else return;
}
int tree::deletenode(int value) {
current=root;
return deletenode(current,value);
}
int tree::deletenode(treenode*&r,int value) {
treenode* s;
if(r!=NULL)
if(value>r->data) deletenode(r->lchild,value);
else if(value<r->data) deletenode(r->rchild,value);
else if(r->lchild!=NULL&&r->rchild!=NULL) {
treenode* p=r->rchild;
while(p->lchild!=NULL) p=p->lchild;
s=p;
r->data=s->data;
deletenode(r->rchild,r->data);
return 1;
}
else {
s=r;
if(r->lchild==NULL) r=r->rchild;
else if(r->rchild==NULL) r=r->lchild;
delete s;
return 1;
}
return 0;
}
void tree::inorder() {
inorder(root);
}
void tree::inorder(treenode*&r) {
if(r != NULL) { // 判断节点是否为空
inorder(r->lchild);
cout<<r->data<<" ";
inorder(r->rchild);
}
}
int main() {
int n,i,x;
tree t;
cin>>n;
cout<<"原始数据:";
for(i=0;i<n;i++) {
cin>>x;
t.insert(x);
cout<<x<<" ";
}
cout<<"\n中序遍历结果:";
t.inorder();
cin>>x;
t.deletenode(x);
cout<<"\n删除结点后结果:";
t.inorder();
cout<<endl;
return 0;
}
```
阅读全文