📄 reference.c
字号:
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct teacher
{ int num;
int password;
char name[20];
struct teacher *lchild;
struct teacher *rchild ;
};
struct teacher *teacherbt;
struct stu
{ int num;
int password;
char name[20];
int age;
char addr[20];
struct stu *lchild;
struct stu *rchild ;
} ;
struct stu *stubt;
struct book
{ int bnum;
char bname[20];
char bwriter[20];
double bprice;
struct book *lchild;
struct book *rchild;
} ;
struct book *bookbt;
struct bb
{ int snum;
int bnum;
struct bb *lchild;
struct bb *rchild;
};
struct bb *bbbt;
typedef long int num_size;
typedef struct _tnode *tpointer;
typedef struct _tnode
{
num_size num;
num_size count;
tpointer left;
tpointer right;
}tnode;
tpointer btree_find(tpointer root, num_size num)
{
if(root == NULL)
{
return NULL;
}
if(root ->num == num)
{
return root;
}
else if(num > root ->num){
return btree_find(root->right, num );
}
else if(num < root ->num)
{
return btree_find(root->left, num );
}
}
int btree_other_print1(tpointer root)
{
printf("num:\t%16ld\tcount:\t%16ld\n", root ->num, root ->count);
return 1;
}
int btree_del_value(tpointer *proot, num_size num)
{
tpointer root = *proot;
if(root == NULL){
return 0;
}
if(root ->num == num)
{
btree_del(proot);
}
else if(num > root ->num)
{
if(root -> right == NULL)
{
return 0;
}
if(root ->right -> num == num){
return btree_del( &(root->right));
}
else
{
return btree_del_value(&(root->right), num );
}
}
else if(num < root ->num)
{
if(root -> left == NULL)
{
return 0;
}
if(root ->left -> num == num)
{
return btree_del( &(root->left));
}
else
{
return btree_del_value(&(root->left), num );
}
}
}
int btree_del(tpointer *proot)
{
tpointer root = *proot;
if(root == NULL)
return 0;
if(root -> left == NULL)
{
*proot = root -> right;
free(root);
}
else if(root -> right == NULL)
{
*proot = root -> left;
free(root);
}
else
{
tpointer temp = root -> left;
while (temp->right != NULL)
{
temp = temp -> right;
}
temp -> right = root -> right;
free(root);
}
}
int btree_visit(tpointer root, int f(), int style)
{
if(root == NULL)
{
return 0;
}
if(style == 0)
(*f)(root);
btree_visit (root->left, f, style );
if(style == 1)
(*f)(root);
btree_visit (root->right, f, style );
if(style == 2)
(*f)(root);
}
int btree_destory(tpointer root)
{
if(root == NULL)
{
return 0;
}
btree_destory (root->left);
btree_destory (root->right);
printf("Killing num:\t%ld\tnum:\t%ld\n", root->num, root->count);
free(root);
}
int btree_add(tpointer* root, num_size data)
{
if(*root == NULL){
*root = (tpointer)malloc(sizeof (tnode) );
tpointer vroot = *root;
printf("%p ", *root);
vroot -> num = data;
vroot -> count = 1;
vroot -> left = vroot ->right = NULL;
return 1;
}
tpointer vroot = *root;
if(vroot ->num == data)
{
vroot ->count ++;
return 1;
}
else if(data > vroot ->num)
{
return btree_add(&(vroot->right), data );
}
else if(data < vroot ->num)
{
return btree_add(&(vroot->left), data );
}
return 0;
}
void rand_begin(num_size num)
{
srand(num);
}
num_size rand_range(num_size max)
{
num_size i = (double)(rand())* max / RAND_MAX ;
if(i == max)
return i - 1;
else
return i;
}
int btree(void)
{
rand_begin(2);
tpointer root = NULL;
int t = 999999;
while(t-- > 0)
{
btree_add (&root , rand_range (80));
}
printf("What you have created is :\n");
btree_visit(root, btree_other_print1, 1);
putchar('!');
num_size w = 12;
while(w-- > 0)
{
num_size want = w * w ;
tpointer result_p = btree_find(root, want);
num_size result;
if(result_p == NULL)
{
result = 0;
}
else
{
result = (result_p ) -> count;
want = result_p -> num;
}
printf("want:\t%ld\tresult:\t%ld\n", want, result);
btree_del_value(&root, want);
putchar('e');
btree_add(&root, want);
result = btree_find(root, want) -> count;
printf("new :want:\t%ld\tresult:\t%ld\n", want, result);
}
btree_destory(root);
root = NULL;
t = 99;
while(t-- > 0)
{
btree_add (&root , rand_range (99));
}
printf("What you have created is :\n");
btree_visit(root, btree_other_print1, 1);
return 0;
}
void strcpy(char *s ,char *t)
{
while(*s++=*t++);
}
struct stu *inputstu(int num, int password,char name[], int age, char addr[])
{
struct stu *p,*q;
p=(struct stu *)malloc(sizeof(struct stu));
p->num = num;
p->password=password;
strcpy(p->name,name);
strcpy(p->addr,addr);
p->age = age;
p->lchild=NULL;
p->rchild=NULL;
if((p->num)!=0)
{ q=stubt;
if(q==NULL)stubt=p;
else
{ while((q->lchild!=p)&&(q->rchild)!=p)
{
if((p->num)<(q->num))
{
if(q->lchild!=NULL)q=q->lchild;
else q->lchild=p;
}
else
{
if(q->rchild!=NULL){q=q->rchild;}
else q->rchild=p;
}
}
}
}
return stubt;
}
struct stu *inputbook(int num, char name[], char writer[],int price)
{
struct book *p,*q;
p=(struct book *)malloc(sizeof(struct book));
p->bnum = num;
strcpy(p->bname,name);
strcpy(p->bwriter,writer);
p->bprice = price;
p->lchild=NULL;
p->rchild=NULL;
if((p->bnum)!=0)
{ q=bookbt;
if(q==NULL)bookbt=p;
else
{ while((q->lchild!=p)&&(q->rchild)!=p)
{
if((p->bnum)<(q->bnum))
{
if(q->lchild!=NULL)q=q->lchild;
else q->lchild=p;
}
else
{
if(q->rchild!=NULL){q=q->rchild;}
else q->rchild=p;
}
}
}
}
return bookbt;
}
struct stu *inputteacher(int num, int password,char name[])
{
struct teacher *p,*q;
p=(struct teacher *)malloc(sizeof(struct teacher));
p->num = num;
strcpy(p->name,name);
p->password = password;
p->lchild=NULL;
p->rchild=NULL;
if((p->num)!=0)
{ q=teacherbt;
if(q==NULL)teacherbt=p;
else
{ while((q->lchild!=p)&&(q->rchild)!=p)
{
if((p->num)<(q->num))
{
if(q->lchild!=NULL)q=q->lchild;
else q->lchild=p;
}
else
{
if(q->rchild!=NULL){q=q->rchild;}
else q->rchild=p;
}
}
}
}
return teacherbt;
}
struct stu *inputbb(int bnum, int snum)
{
struct bb *p,*q;
p=(struct bb *)malloc(sizeof(struct bb));
p->snum = snum;
p->bnum=bnum;
p->lchild=NULL;
p->rchild=NULL;
if((p->bnum)!=0)
{ q=bbbt;
if(q==NULL)bbbt=p;
else
{ while((q->lchild!=p)&&(q->rchild)!=p)
{
if((p->bnum)<(q->bnum))
{
if(q->lchild!=NULL)q=q->lchild;
else q->lchild=p;
}
else
{
if(q->rchild!=NULL){q=q->rchild;}
else q->rchild=p;
}
}
}
}
return bookbt;
}
void readstudent(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -