Binary Tree Traversals ,create binary tree as follow (Figure-1) in computerwrite out the functions of inorder ,preorder ,postorder and levelorder,and output the their results.And compute the leaf number and height of the binary tree.Hint:You may choo
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/08 14:23:04
Binary Tree Traversals ,create binary tree as follow (Figure-1) in computerwrite out the functions of inorder ,preorder ,postorder and levelorder,and output the their results.And compute the leaf number and height of the binary tree.Hint:You may choo
Binary Tree Traversals ,create binary tree as follow (Figure-1) in computer
write out the functions of inorder ,preorder ,postorder and levelorder,and output the their results.And compute the leaf number and height of the binary tree.
Hint:You may choose suitable representation,such as linked representation is often used.
不是翻译题,回答请C黏贴代码
Binary Tree Traversals ,create binary tree as follow (Figure-1) in computerwrite out the functions of inorder ,preorder ,postorder and levelorder,and output the their results.And compute the leaf number and height of the binary tree.Hint:You may choo
如有不合题意之处,谅解,可以自己再改改.
#include
using namespace std;
#include
#include
#define OK 1
#define ERROR 0
#define OVERFLOE -2
typedef char ElemType;
const int maxlength=30;//结点个数不超过30个
typedef struct BiTreeNode
{
ElemType data;
struct BiTreeNode*leftchild,*rightchild;
}BiTreeNode,*BiTree;
void createBiTree(BiTree&t)
{
ElemType ch;
cin>>ch;
if(ch=='#')t=NULL;
else
{
if(!(t=new BiTreeNode))
exit(OVERFLOE);
t->data=ch;//生成根节点
createBiTree(t->leftchild);//构造左子树
createBiTree(t->rightchild);//构造右子树
}
}//createBiTree
void Inorder(BiTree&t)//递归函数:中序次序遍历以t为跟的子树
{
if(t!=NULL)
{
Inorder(t->leftchild);
coutleftchild);
coutrightchild);
depthval=1+(depthleft>depthright?depthleft:depthright);
}
return depthval;
}
int countleaf(BiTree t)
{
int m,n;
if(!t)
{
return 0;
}
if(!t->leftchild&&!t->rightchild)
{
return 1;
}
else
{
m=countleaf(t->leftchild);
n=countleaf(t->rightchild);
return (m+n);
}
}
#include"BiTree.h"
void main()
{
cout