二叉链表存储结构,实现二叉树的遍历
来源:百度知道 编辑:UC知道 时间:2024/05/31 08:10:43
要求:1、设计思想(说明基本操作及问题实现的思想)
2、算法实现(加必要的注释)
前几天写的,输入二叉树的广义表形式,建立二叉树的链式存储。输出的是中序。有注释,看懂了应该其他的都能写了吧。#include<stdio.h>
#include<stdlib.h>
int n=0; //全局变量
struct tree //二叉树结构体
{
char data;
struct tree *lc;
struct tree *rc;
};
tree *creat(char a[]) //创建树的二叉树
{
tree *h;
h=(tree *)malloc(sizeof(tree));
h->lc=NULL;
h->rc=NULL;
if(a[n]!=')'&&a[n]!='('&&a[n]!=',') //当a[n]为字母存入a[]
{
h->data=a[n];
n++;
}
if(a[n]=='(') //a[n]为左括弧对h->lc递归操作
{
n++;
h->lc=creat(a);
}
if(a[n]==',') //a[n]为逗号对h->rc递归操作
{
n++;
h->rc=creat(a);
return h;
}
if(a[n]==')') //a[n]为右括弧返回h
{
n++;
return h;