基础树
#include <stdio.h>
#include <stdlib.h>
#include <queue.h>
#define NAMESIZE 32
struct node_st
{
char data;
struct node_st *l,*r;
};
static struct node_st *tree = NULL;
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//can be cancel
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 main()
{
int i;
char arr[] = "cefadjbh";
//i < sizeof(arr)/sizeof(*arr) has the end of 0,so must cut 1-----(-1)
for(i = 0;i < sizeof(arr)/sizeof(*arr)-1; i++)
{
insert(&tree,arr[i]);
}
draw(tree);
exit(0);
}
arr[] = "cefadjbh" 中左右,左小右大
转存广义表save.c
#include <stdio.h>
#include <stdlib.h>
#define FNAME "/home/wjy/Desktop/code/tree/save_load/out"
#define NAMESIZE 32
struct node_st
{
char data;
struct node_st *l,*r;
};
static struct node_st *tree = NULL;
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//can be cancel
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);
return 0;
}
int main()
{
int i;
char arr[] = "cefadjbh";
//i < sizeof(arr)/sizeof(*arr) has the end of 0,so must cut 1-----(-1)
for(i = 0;i < sizeof(arr)/sizeof(*arr)-1; i++)
{
insert(&tree,arr[i]);
}
draw(tree);
save(tree,FNAME);
exit(0);
}
广义表转存二叉树
#include <stdio.h>
#include <stdlib.h>
#define NAMESIZE 32
#define FNAME "/home/wjy/Desktop/code/tree/save_load/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);
}
// static int get_num(struct node_st *root)
// {
// if(root == NULL)
// return 0;
// return get_num(root->l) + 1 + get_num(root->r);
// }
// static struct node_st *find_min(struct node_st *root)//find the most left node
// {
// if(root->l == NULL)
// return root;
// return find_min(root->l);
// }
// static struct node_st *find_max(struct node_st *root)//find the most right node
// {
// if(root->r == NULL)
// return root;
// return find_max(root->r);
// }
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);
// printf("%c\n",c);
/*if error*/
if(c != '(')
{
fprintf(stderr,"fgetc():error.\n");
// printf("%c\n",c);
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;
}
struct node_st *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);
}