codeblocks 数据结构二叉树
其他问答
1
#include <iostream>
using namespace std;
//链式二叉树节点数据类型定义
typedef char ElemType;
typedef struct BinTreeNode
{
ElemType data;
struct BinTreeNode*lchild;
struct BinTreeNode*rchild;
}BinTreeNode;
//创建二叉树
void CreateBiTree(BiTree &T);
//先序遍历
Status PreOrderTraverse(BiTree T);
//中序遍历
Status InOrderTraverse(BiTree T);
int NodeCount(BiTree T){
int main()
{
cout<<"Hello 二叉树的应用!"<< endl;
BiNode *T;
cout<<endl<<"创建二叉树"<<endl;
CreateBiTree(T);
cout<<endl<<"先序遍历结果"<<endl;
PreOrderTraverse(T);
cout<<endl<<"中序遍历结果"<<endl;
InOrderTraverse(T);
cout<<endl<<"结点总数"<<endl;
int number =NodeCount(T);
cout<<number;
return 0;
}
void CreateBiTree (BiTree &T)
{
char ch;
cin>>ch;
if(ch** '#') //输入‘#’代表空树
T=NULL
else
{
T=new BiNode;
T->data=ch; //生成根节点
CreateBiTree(T->lchild);//递归创建左子树
CreateBiTree(T->rchild);//递归创建右子树
}
}
void PreOrderTraverse(BiTree T){
if(T=NULL)
return 1;
else{
cout<<T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
return 0;
}
Status InOrderTraverse(BiTree T){
if(T==NULL)
return 1; //空二叉树
else{
InOrderTraverse(T->lchild); //递归遍历左子树
cout<<T->data; //访问根结点
InOrderTraverse(T->rchild); //递归遍历右子树
}
return 0;
}
Status PostOrder(BiTree T)
{
if(T=NULL)
return 1;
else{
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
cout << T->data << endl;
}
return 0;
}
想用以上代码实现图片的运行 但是老是报错不知道该如何更改求大神指点
-
#include <iostream> using namespace std; typedef int Status; //链式二叉树节点数据类型定义 typedef char ElemType; typedef struct BinTreeNode { ElemType data; struct BinTreeNode*lchild; struct BinTreeNode*rchild; }BinTreeNode, *BiTree ; //创建二叉树 void CreateBiTree(BiTree &T); //先序遍历 Status PreOrderTraverse(BiTree T); //中序遍历 Status InOrderTraverse(BiTree T); //后续遍历 Status PostOrderTraverse(BiTree T); //节点总数 int NodeCount(BiTree T); //叶子节点总数 int LeafNodeCount(BiTree T); int main() { cout<<"Hello 二叉树的应用!"<< endl; BiTree T; cout<<"创建二叉树"<<endl; CreateBiTree(T); cout<<endl<<"先序遍历结果"<<endl; PreOrderTraverse(T); cout<<endl<<"中序遍历结果"<<endl; InOrderTraverse(T); cout<<endl<<"后序遍历结果"<<endl; PostOrderTraverse(T); cout<<endl<<"结点总数"<<endl; cout<<NodeCount(T)<<endl; cout<<"叶子结点总数"<<endl; cout<<LeafNodeCount(T)<<endl; return 0; } void CreateBiTree (BiTree &T) { char ch; cin>>ch; if(ch=='#') //输入‘#’代表空树 T=NULL; else{ T = new BinTreeNode(); T->data=ch; //生成根节点 CreateBiTree(T->lchild);//递归创建左子树 CreateBiTree(T->rchild);//递归创建右子树 } } Status PreOrderTraverse(BiTree T){ if(T==NULL) return 1; else{ cout<<T->data; PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); } return 0; } Status InOrderTraverse(BiTree T){ if(T==NULL) return 1; //空二叉树 else{ InOrderTraverse(T->lchild); //递归遍历左子树 cout<<T->data; //访问根结点 InOrderTraverse(T->rchild); //递归遍历右子树 } return 0; } Status PostOrderTraverse(BiTree T){ if(T==NULL)return 1; else{ PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); cout << T->data; } return 0; } int NodeCount(BiTree T){ if (T==NULL) return 0; else return 1+NodeCount(T->lchild)+NodeCount(T->rchild); } int LeafNodeCount(BiTree T){ if(T==NULL) return 0; if(T->lchild==NULL && T->rchild==NULL) return 1; else return LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild); } //12C##DE#G##F###
-
//交换左右孩子节点 Status LeftRight(BiTree &T){ if(T){ BiTree temp = T->lchild; T->lchild = T->rchild; T->rchild = temp; LeftRight(T->lchild); LeftRight(T->rchild); return 0; }else return 0; }
发表回复