C++ 实现
——引用是C++不同于C的地方
//其中扩充结点用'0'号表示。即‘0’表示空树节点
#include<iostream>
#include<stdio.h>#include <iomanip>
#include<stdlib.h>
using namespace std;
#define OVERFLOW 0
#define OK 1
typedef int Status;
typedef char ElemType;
typedef struct BiTree
{
ElemType data;
struct BiTree *Lchild;
struct BiTree *Rchild;
}BiTree,*Binary_Tree;
//创建一个二叉树
Status CreateBiTree(Binary_Tree &T)
{
char ch;
T=(Binary_Tree)malloc(sizeof(BiTree));
if(!T)
exit(OVERFLOW);
cin>>ch;
if(ch=='0')
T=NULL;
else
{
T->data=ch;
CreateBiTree(T->Lchild);
CreateBiTree(T->Rchild);
}
return OK;
}
//先遍历二叉树
Status PreShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
cout<<T->data<<setw(3);
PreShowBiTree(T->Lchild);
PreShowBiTree(T->Rchild);
}
return OK;
}
//中遍历二叉树
Status MidShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
MidShowBiTree(T->Lchild);
cout<<T->data<<setw(3);
MidShowBiTree(T->Rchild);
}
return OK;
}
//后遍历二叉树
Status BehShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
BehShowBiTree(T->Lchild);
BehShowBiTree(T->Rchild);
cout<<T->data<<setw(3);
}
return OK;
}
int main()
{
BiTree *T;
cout<<"请创建一个二叉树: "<<endl;
CreateBiTree(T);
cout<<"先序遍历: ";
PreShowBiTree(T);
cout<<endl;
cout<<"中序遍历: ";
MidShowBiTree(T);
cout<<endl;
cout<<"后序遍历: ";
BehShowBiTree(T);
cout<<endl;
}
实验结果:

C 语言代码 :
—— scanf,能读取空格!
由于“指向指针的指针”这个概念不容易理解,我们可以用函数返回值来传递动态内存。这种方法更加简单,但是,得检查是静态内存指针还是堆内存指针还是栈内存指针,栈内存指针是绝对要不得滴!
//其中扩充结点用'0'号表示。
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW 0
#define OK 1
typedef int Status;
typedef char ElemType;
typedef struct BiTree
{
ElemType data;
struct BiTree *Lchild;
struct BiTree *Rchild;
}BiTree,*Binary_Tree;
//创建一个二叉树
Binary_Tree CreateBiTree(Binary_Tree T)
{
char ch;
T=(Binary_Tree)malloc(sizeof(BiTree));
if(!T)
exit(OVERFLOW);
scanf("%c",&ch);
if(ch=='0')
T=NULL;
else
{
T->data=ch;
T->Lchild=CreateBiTree(T->Lchild);
T->Rchild=CreateBiTree(T->Rchild);
}
return T;
}
//先遍历二叉树
Status PreShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
printf("%c ",T->data);
PreShowBiTree(T->Lchild);
PreShowBiTree(T->Rchild);
}
return OK;
}
//中遍历二叉树
Status MidShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
MidShowBiTree(T->Lchild);
printf("%c ",T->data);
MidShowBiTree(T->Rchild);
}
return OK;
}
//后遍历二叉树
Status BehShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
BehShowBiTree(T->Lchild);
BehShowBiTree(T->Rchild);
printf("%c ",T->data);
}
return OK;
}
int main()
{
BiTree *T;
printf("请创建一个二叉树:\n");
T=CreateBiTree(T);
printf("先序遍历: ");
PreShowBiTree(T);
printf("\n");
printf("中序遍历: \n");
MidShowBiTree(T);
printf("\n");
printf("后序遍历: \n");
BehShowBiTree(T);
printf("\n");
}
执行结果:

C 语言,指针的指针方式实现
如果非得要用指针参数去申请内存,那么应该改用“指向指针的指针”。如果传入的是一级指针S的话,那么函数中将使用的是S的拷贝,要改变S的值,只能传入指向S的指针,即二级指针。
#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW 0
#define OK 1
typedef int Status;
typedef char ElemType;
typedef struct BiTree
{
ElemType data;
struct BiTree *Lchild;
struct BiTree *Rchild;
}BiTree,*Binary_Tree;
//创建一个二叉树
Status CreateBiTree(Binary_Tree *T)
{
char ch;
*T=(Binary_Tree)malloc(sizeof(BiTree));
if(!(*T))
exit(OVERFLOW);
scanf("%c",&ch);
if(ch=='0')
*T=NULL;
else
{
(*T)->data=ch;
CreateBiTree(&((*T)->Lchild));
CreateBiTree(&((*T)->Rchild));
}
return OK;
}
//先遍历二叉树
Status PreShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
printf("%c ",T->data);
PreShowBiTree(T->Lchild);
PreShowBiTree(T->Rchild);
}
return OK;
}
//中遍历二叉树
Status MidShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
MidShowBiTree(T->Lchild);
printf("%c ",T->data);
MidShowBiTree(T->Rchild);
}
return OK;
}
//后遍历二叉树
Status BehShowBiTree(Binary_Tree T)
{
if(T!=NULL)
{
BehShowBiTree(T->Lchild);
BehShowBiTree(T->Rchild);
printf("%c ",T->data);
}
return OK;
}
int main()
{
BiTree *T=NULL;
printf("请创建一个二叉树:\n");
CreateBiTree(&T);
printf("先序遍历: \n");
PreShowBiTree(T);
printf("\n");
printf("中序遍历: \n");
MidShowBiTree(T);
printf("\n");
printf("后序遍历: \n");
BehShowBiTree(T);
printf("\n");
}
结果:


5903

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



