qq_36679429
xue1314gx
2017-11-23 05:39

学渣求解,二叉树code blocks

  • 二叉树
  • 学渣求解

```#include "iostream"
using namespace std;

struct BiNode
{
char data;
BiNode *lchild,*rchild;
};

class BiTree
{
public:
BiTree(){root=Creat(root);}
~BiTree(){Release(root);}
void PreOrder(){PreOrder(root);}
void InOrder(){InOrder(root);}
void PostOrder(){PostOrder(root);}
private:
BiNode *root;
BiNode *Creat(BiNode *bt);
void Release(BiNode *bt);
void PreOrder(BiNode *bt);
void InOrder(BiNode *bt);
void PostOrder(BiNode *bt);
};

BiNode *BiTree::Creat(BiNode *bt)
{
char ch;

cin>>ch;
if(ch=='#')return NULL;
else
{
bt=new BiNode;
bt->data=ch;
bt->lchild=Creat(bt->lchild);
bt->rchild=Creat(bt->rchild);
}
return bt;
}

void BiTree::Release(BiNode *bt)
{
if(bt!=NULL)
{
Release(bt->lchild);
Release(bt->rchild);
delete bt;
}
}
#include "iostream"
using namespace std;

struct BiNode
{
char data;
BiNode *lchild,*rchild;
};

class BiTree
{
public:
BiTree(){root=Creat(root);}
~BiTree(){Release(root);}
void PreOrder(){PreOrder(root);}
void InOrder(){InOrder(root);}
void PostOrder(){PostOrder(root);}
private:
BiNode *root;
BiNode *Creat(BiNode *bt);
void Release(BiNode *bt);
void PreOrder(BiNode *bt);
void InOrder(BiNode *bt);
void PostOrder(BiNode *bt);
};

BiNode *BiTree::Creat(BiNode *bt)
{
char ch;

cin>>ch;
if(ch=='#')return NULL;
else
{
bt=new BiNode;
bt->data=ch;
bt->lchild=Creat(bt->lchild);
bt->rchild=Creat(bt->rchild);
}
return bt;
}

void BiTree::Release(BiNode *bt)
{
if(bt!=NULL)
{
Release(bt->lchild);
Release(bt->rchild);
delete bt;
}
}

void BiTree::PreOrder(BiNode *bt)
{
if(bt==NULL) return;
else
{
cout<data<<" ";
PreOrder(bt->lchild);
PreOrder(bt->rchild);
}
}

void BiTree::InOrder(BiNode *bt)
{
if(bt==NULL) return;
else
{
InOrder(bt->lchild);
cout<data<<" ";
InOrder(bt->rchild);
}
}

void BiTree::PostOrder(BiNode *bt)
{
if(bt==NULL) return;
else
{
PostOrder(bt->lchild);
PostOrder(bt->rchild);
cout<data<<" ";
}
}

void Copy(BiNode *bt,BiNode *nt) //fuzhi
{
if(bt==NULL)
{nt=NULL;
return;
}
else
{
nt=new BiNode;
nt->data=bt->data;
Copy(bt->lchild,nt->lchild);
Copy(bt->rchild,nt->rchild);
}
}
int Depth(BiNode *bt)
{
if(T==NULL) return 0;
else
{
m=Depth(bt->lchild);
n=Depth(bt->rchild);
if(m>n) return(m+1);
else return(n+1);
}
}
int NodeCount(BiNode *bt)
{
if(bt==NULL) return 0;
else return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1;
}
int main()
{
cout<<"请输入二叉树的结点数据"<<endl;
BiTree T;

cout<<"-----前序遍历-----"<<endl;
T.PreOrder();
cout<<endl;
cout<<"-----中序遍历-----"<<endl;
T.InOrder();
cout<<endl;
cout<<"-----后序遍历-----"<<endl;
T.PostOrder();
cout<<endl;
cout<<"-----二叉树复制为-----"<<endl;
cout<<endl;
cout<<"-----二叉树深度为-----"<<endl;
cout<<endl;
cout<<"-----二叉树结点个数为-----"<<endl;
cout<<endl;
return 0;
}



```C:\Users\\Desktop\zz.cpp|54|error: redefinition of 'struct BiNode'|
C:\Users\\Desktop\zz.cpp|4|error: previous definition of 'struct BiNode'|
C:\Users\\Desktop\zz.cpp|60|error: redefinition of 'class BiTree'|
C:\Users\\Desktop\zz.cpp|10|error: previous definition of 'class BiTree'|
C:\Users\\Desktop\zz.cpp|77|error: redefinition of 'BiNode* BiTree::Creat(BiNode*)'|
C:\Users\\Desktop\zz.cpp|27|error: 'BiNode* BiTree::Creat(BiNode*)' previously defined here|
C:\Users\\Desktop\zz.cpp|92|error: redefinition of 'void BiTree::Release(BiNode*)'|
C:\Users\\Desktop\zz.cpp|42|error: 'void BiTree::Release(BiNode*)' previously defined here|
C:\Users\Desktop\zz.cpp||In function 'int Depth(BiNode*)':|
C:\Users\\Desktop\zz.cpp|151|error: 'T' was not declared in this scope|
C:\Users\\Desktop\zz.cpp|154|error: 'm' was not declared in this scope|
C:\Users\\Desktop\zz.cpp|155|error: 'n' was not declared in this scope|
||=== Build 失败了: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换