Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- typedef struct phoneaddress
- {
- char name[30];
- char tel[15];
- char email[30];
- } KeyType;
- typedef struct NodeType
- {
- KeyType key;
- struct NodeType *right, *left;
- } node_Type;
- typedef struct NodeType *treetype;
- void MakeNullTree(treetype (*T))
- {
- (*T)==NULL;
- }
- int EmptyTree(treetype T)
- {
- return T==NULL;
- }
- treetype LeftChild(treetype T)
- {
- if(T!=NULL) return T->left;
- else return NULL;
- }
- treetype RightChild(treetype T)
- {
- if(T!=NULL) return T->right;
- else return NULL;
- }
- node_Type *create_node(KeyType NewData)
- {
- node_Type *NewNode = (node_Type*)malloc(sizeof(node_Type));
- if(NewNode!=NULL)
- {
- NewNode->left=NULL;
- NewNode->right=NULL;
- NewNode->key=NewData;
- }
- return NewNode;
- }
- int IsLeaf(treetype T)
- {
- if(T!=NULL) return (LeftChild(T)==NULL&&RightChild(T)==NULL);
- else return -1;
- }
- treetype Add_Right(treetype *Tree, KeyType NewData)
- {
- node_Type *NewNode=create_node(NewData);
- if(NewNode==NULL) return NewNode;
- if(*Tree==NULL) *Tree=NewNode;
- else{
- node_Type *Rnode= *Tree;
- while(Rnode->right!=NULL)
- Rnode=Rnode->right;
- Rnode->right==NewNode;
- }
- return NewNode;
- }
- treetype Add_Left(treetype *Tree,KeyType NewData)
- {
- node_Type *NewNode=create_node(NewData);
- if(NewNode==NULL) return NewNode;
- if(*Tree==NULL) return NewNode;
- else{
- node_Type *Lnode= *Tree;
- while(Lnode->left!=NULL)
- Lnode=Lnode->left;
- Lnode->left==NewNode;
- }
- return NewNode;
- }
- treetype Search(char *email, treetype Root)
- {
- if(Root!=NULL)
- {
- if(strcmp((Root->key).email, email)==0) return Root;
- else if(strcmp((Root->key).email, email)<0) return Search(email,Root->right);
- else return Search(email,Root->left);
- }
- else return NULL;
- }
- //chen mot node
- void InsertNode(KeyType x,treetype *Root)
- {
- if(*Root!=NULL)
- {
- if(((*Root)->key).email == x.email) return -1;
- if(strcmp(((*Root)->key).email, x.email)>0)
- InsertNode(x, (*Root)->left);
- else if(strcmp(((*Root)->key).email, x.email)<0)
- InsertNode(x, (*Root)->right);
- }
- *Root=(node_Type*)malloc(sizeof(node_Type));
- if(*Root==NULL) return 0;
- (*Root)->key = x;
- (*Root)->left = (*Root)->right = NULL;
- return 1;
- }
- void printInorder(treetype Root)
- {
- if(Root!=NULL)
- {
- printInorder(Root->left);
- printf("%-30s %-15s %-30s",(Root->key).name,(Root->key).tel,(Root->key).email);
- printInorder(Root->right);
- }
- }
- int main()
- {
- FILE *fp;
- KeyType phonearr[MAX];
- treetype root=NULL;
- int i,irc;
- int n=10;
- fp=fopen("dulieu.txt","r+");
- if(fp==NULL)
- {
- printf("can not open file\n");
- return -1;
- }
- irc=fread(phonearr, sizeof(KeyType), n, fp);
- fclose(fp);
- for(i=0;i<n;i++)
- InsertNode(phonearr[i],&root);
- printf("%-30s %-15s %-30s\n","NAME","TEL","EMAIL");
- printInorder(root);
- return 0;
- }
- Nga 0903230451 ngalan@gmail.com
- Phuong 0932430111 phuongnk@gmail.com
- Hung 0903231253 hungln@yahoo.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement