#include<iostream> using namespace std; class treenode{ private: int data; treenode *lchild,*rchild; friend class tree; public: treenode() { rchild=NULL; lchild=NULL; } }; class tree{ treenode *current,*root; public: tree(); void creattree(int n,int in[],int past[]); void creattree(int n,int in[],int past[],treenode *&r); void preorder(); void preorder(treenode* r); }; tree::tree() { root=new treenode; current=root; } void tree::creattree(int n,int in[],int past[]) { creattree(n,in,past,root); } void tree::creattree(int n,int in[],int past[],treenode *&r) { int i=0; r=new treenode; r->data=past[n-1]; while(in[i]!=r->data&&i<n-1)i++; cout<<i<<n<<endl; if(i==1) { r->lchild=new treenode; r->lchild->data=in[0]; if(i==n-2) { r->rchild=new treenode; r->rchild->data=in[n-1]; } cout<<"a\n"; } if(i<n-2) { cout<<"b\n"; r->rchild=new treenode; creattree(n-1-i,in+i+1,past+i,r->rchild); } if(i>1) { cout<<"c\n"; r->lchild=new treenode; creattree(i,in,past,r->lchild); } } void tree::preorder() { preorder(root); cout<<endl; } void tree::preorder(treenode* r) { cout<<r->data; if(r->lchild!=NULL||r->rchild!=NULL)cout<<' '; preorder(r->lchild); preorder(r->rchild); } int main() { int in[1000],past[1000],n; cin>>n; for(int i=0;i<n;i++)cin>>in[i]; for(int i=0;i<n;i++)cin>>past[i]; tree t; t.creattree(n,in,past); t.preorder(); return 0; }
时间: 2023-06-24 13:09:29 浏览: 123
这段代码是一个求解树的先序遍历的程序,使用了树的结构体和类。其中,creattree函数用于构建树,preorder函数用于输出先序遍历结果。
具体实现是通过中序遍历和后序遍历来构建树。首先,根据后序遍历找到根节点,在中序遍历中找到根节点的位置,然后将中序遍历分成左子树和右子树两部分,再分别递归构建左子树和右子树。最终,使用先序遍历输出树的节点内容。
该程序还需要进行一些测试和错误处理,比如输入的数据是否合法等。
相关问题
#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; }有什么问题?为什么无法正常插入?
这段代码有一个问题是在插入节点时,如果当前节点为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;
}
```
阅读全文