创建二叉树类。二叉树的存储结构使用链表。提供操作:前序遍历、中序遍历、后序遍历、层次遍历、计算二叉树结点数目、计算二叉树高度。
格式
输入格式
第一行为一个数字n (10<=n<=100000),表示有这棵树有n个节点,编号为1~n。
之后n行每行两个数字,第 i 行的两个数字a、b表示编号为 i 的节点的左孩子节点为 a,右孩子节点为 b,-1表示该位置没有节点。
保证数据有效,根节点为1。
输出格式
第一行,n个数字,表示该树的层次遍历。
第二行,n个数字,第i个数字表示以 i 节点为根的子树的节点数目。
第三行,n个数字,第i个数字表示以 i 节点为根的子树的高度。
#include<iostream>
using namespace std;
struct treeNode
{
treeNode *leftchild;
treeNode *rightchild;
int element;
treeNode()
{
element=0;
leftchild=NULL;
rightchild=NULL;
}
treeNode(int element)
{
this->element=element;
leftchild=NULL;
rightchild=NULL;
}
treeNode(int element,treeNode* left,treeNode* right)
{
this->element=element;
leftchild=left;
rightchild=right;
}
};
//***********************************************************************************************************///
//***********************************************************************************************************///链表队列
struct chainNode
{
treeNode element; //用来装牌的号码
chainNode* next;
chainNode(treeNode element, chainNode *n)
{
this->element =element;
this->next = n;
}
};
class mylinkqueue
{
public:
mylinkqueue();
~mylinkqueue();
int getsize(); //返回大小
void pop(); //删除头元素
treeNode* front(); //返回首元素
void push(treeNode); //把元素加到队尾
private:
int size;
chainNode* thefront;
chainNode* theback;
};
//构造
mylinkqueue::mylinkqueue()
{
size = 0;
thefront = NULL;
theback = NULL;
}
//析构
mylinkqueue::~mylinkqueue()
{
chainNode *c=thefront;
while(c!=NULL)
{c=c->next;
delete thefront;
thefront = c;}
}
//求大小
int mylinkqueue::getsize()
{
return size;
}
//删除
void mylinkqueue::pop()
{
if(thefront==NULL)
{
return;
}
chainNode* nextNode = thefront->next;
delete thefront;
thefront = nextNode;
size--;
}
//添加
void mylinkqueue::push(treeNode a)
{
chainNode* newNode = new chainNode(a,NULL);
if(size==0)
thefront = newNode;
else
{
theback->next=newNode;
}
theback= newNode;
//newNode成为了尾
size++;
}
//返回首元素
treeNode* mylinkqueue::front()
{
return& thefront->element;
}
//***************************************************************************************************************///
//***************************************************************************************************************///
class linkBT
{
public:
linkBT();
// ~linkeBT();
void preOrder(const treeNode*); //前序
void inOrder(const treeNode*); //中序
void postOrder(const treeNode*); //后序
void levelOrder(const treeNode*); //层次
treeNode* getroot(){return root;}
void visit(const treeNode* t){cout<<t->element<<" ";}
int height(const treeNode*); //确认高度
void insert(treeNode*,int ); //插入
treeNode* find(int i); //寻找i节点
int numC(const treeNode*); //计算节点数目
void build(int); //构建
private:
treeNode* root;
treeNode** A; //用来装指针
};
//构造
linkBT::linkBT()
{
root = new treeNode(1);
A = new treeNode*[10005];
A[1]=root;
}
//析构
//前序
void linkBT::preOrder(const treeNode* t)
{
if(t!=NULL&&t->element!=-1)
{
visit(t);
//cout<<"进入左子树"<<endl;
preOrder(t->leftchild);
//cout<<"进入右子树"<<endl;
preOrder(t->rightchild);}
}
//中序
void linkBT::inOrder(const treeNode* t)
{
if(t!=NULL&&t->element!=-1)
{
// cout<<"进入左子树" <<endl;
inOrder(t->leftchild);
visit(t);//cout<<endl;
// cout<<"进入左右树" <<endl;
inOrder(t->rightchild);
}
}
//后序
void linkBT::postOrder(const treeNode* t)
{
if(t!=NULL&&t->element!=-1)
{
postOrder(t->leftchild);
postOrder(t->rightchild);
visit(t);
}
}
//层次
void linkBT::levelOrder(const treeNode* t)
{
mylinkqueue q;
q.push(*t);
while(q.getsize()>0)
{
visit(q.front());
if(q.front()->leftchild!=NULL&&q.front()->leftchild->element!=-1)
{
q.push(*(q.front()->leftchild));
}
if(q.front()->rightchild!=NULL&&q.front()->rightchild->element!=-1)
{
q.push(*(q.front()->rightchild));
}
q.pop();
}
}
//高度
int linkBT::height(const treeNode* t)
{
if(t == NULL||t->element==-1)
{
return 0;
}
int hl = height(t->leftchild);
int hr = height(t->rightchild);
if(hl>hr)
{
return ++hl;
}
else
{
return ++hr;
}
}
//插入
void linkBT::insert(treeNode* t,int i)
{
if(t!=NULL&&t->element!=-1)
{
if(t->element==i)
{
int a,b;
cin>>a>>b;
t->leftchild=new treeNode(a);
if(a!=-1)
A[t->leftchild->element]=t->leftchild;
t->rightchild=new treeNode(b);
if(b!=-1)
A[t->rightchild->element]=t->rightchild;
return;
//cout<<"进入左子树"<<endl;
insert(t->leftchild,i);
//cout<<"进入右子树"<<endl;
insert(t->rightchild,i);
}}
}
//构建二叉树
void linkBT::build(int n)
{
for(int i=1;i<=n;i++)
{
insert(A[i],i);
}
}
//寻找i节点
treeNode* linkBT::find(int i)
{
return A[i];
}
//计算节点个数
int linkBT::numC(const treeNode* t)
{
if(t==NULL||t->element==-1)
{
return 0;
}
else
{
//cout<<"+1"<<endl;
int count= numC(t->leftchild)+numC(t->rightchild)+1;
return count;
}
}
int main()
{
int n;
cin>>n;
linkBT a;
a.build(n);
a.levelOrder(a.getroot()); //输出层次遍历
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<a.numC(a.find(i))<<" ";
//cout<<c->element;
}
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<a.height(a.find(i))<<" ";
}
}
1058

被折叠的 条评论
为什么被折叠?



