当前位置:   article > 正文

C-数据结构-树转存广义表-广义表转成树-实例

C-数据结构-树转存广义表-广义表转成树-实例

树转存广义表
save.c

#include<stdio.h>
#include<stdlib.h>

#define FNAME "/tmp/out"


struct node_st
{
	char data;
	struct node_st *l,*r;
};

static struct node_st *tree = NULL;//把tree提升到全局变量,当前文件

int insert(struct node_st **root,int data)
{
	struct node_st *node;
	if(*root == NULL)
	{
		node = malloc(sizeof(*node));
		if(node == NULL)
			return -1;
		node->data = data;
		node->l = NULL;
		node->r = NULL;
		*root = node;
		return 0;
	}
	if(data <= (*root)->data)
		return insert(&(*root)->l,data);
	else
		return insert(&(*root)->r,data);
}

void draw_(struct node_st *root,int level)
{
	int i;
	if(root == NULL)
		return ;
	draw_(root->r,level+1);
	for(i = 0;i<level;i++)
		printf("    ");
	printf("%c\n",root->data);
	draw_(root->l,level+1);
}
void draw(struct node_st *root)
{
	draw_(root,0);
	printf("\n\n");
	//getchar();//相当于暂停
}

int save_(struct node_st *root,FILE *fp)
{
	fputc('(',fp);
	/*if error*/
	
	if(root == NULL)
	{
		fputc(')',fp);
		return 0;
	}
	fputc(root->data,fp);
	/*if error*/
	
	save_(root->l,fp);
	save_(root->r,fp);
	
	fputc(')',fp);
	/*if error*/
	
	return 0;
}


int save(struct node_st *root,const char *path)
{
	FILE *fp;
	fp = fopen(path,"w");
	if(fp == NULL)
		return -1;
	save_(tree,fp);
	fclose(fp); // 别忘了关闭文件
	return 0;
}


int main()
{
	char arr[] = "cefadjbh";
	int i;
	
	for(i = 0;i<sizeof(arr)/sizeof(*arr) - 1;i++)
	{
		insert(&tree,arr[i]);
	}
	draw(tree);

	save(tree,FNAME);
	exit(0);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102

广义表转成树
load.c

#include<stdio.h>
#include<stdlib.h>

#define FNAME "/tmp/out"


struct node_st
{
	char data;
	struct node_st *l,*r;
};

void draw_(struct node_st *root,int level)
{
	int i;
	if(root == NULL)
		return ;
	draw_(root->r,level+1);
	for(i = 0;i<level;i++)
		printf("    ");
	printf("%c\n",root->data);
	draw_(root->l,level+1);
}
void draw(struct node_st *root)
{
	draw_(root,0);
	printf("\n\n");
	//getchar();//相当于暂停
}

struct node_st *load_(FILE *fp)
{
	int c;
	struct node_st *root;
	
	c = fgetc(fp);
	if(c != '(')
	{
		fprintf(stderr,"fgetc():error.\n");
		exit(1);
	}
	c = fgetc(fp);
	if(c == ')')
		return NULL;
	root = malloc(sizeof(*root));
	if(root == NULL)
		exit(1);
	root->data = c;
	root->l = load_(fp);
	root->r = load_(fp);
	fgetc(fp);
	/*if error */
	return root;
}


load(const char *path)
{
	FILE *fp;
	struct node_st *root;
	fp = fopen(path,"r");
	if(fp == NULL)
		return NULL;
	
	root = load_(fp);
	
	fclose(fp);
	return root;

}

int main()
{
	struct node_st *root;

	root = load(FNAME);
	draw(root);
	exit(0);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/659435
    推荐阅读
    相关标签
      

    闽ICP备14008679号