Advertisement
ducanhZ9

Untitled

Dec 19th, 2017
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 100
  5. typedef struct phoneaddress
  6. {
  7. char name[30];
  8. char tel[15];
  9. char email[30];
  10. } KeyType;
  11.  
  12. typedef struct NodeType
  13. {
  14. KeyType key;
  15. struct NodeType *right, *left;
  16. } node_Type;
  17.  
  18. typedef struct NodeType *treetype;
  19.  
  20. void MakeNullTree(treetype (*T))
  21. {
  22. (*T)==NULL;
  23. }
  24.  
  25. int EmptyTree(treetype T)
  26. {
  27. return T==NULL;
  28. }
  29.  
  30. treetype LeftChild(treetype T)
  31. {
  32. if(T!=NULL) return T->left;
  33. else return NULL;
  34. }
  35.  
  36. treetype RightChild(treetype T)
  37. {
  38. if(T!=NULL) return T->right;
  39. else return NULL;
  40. }
  41.  
  42. node_Type *create_node(KeyType NewData)
  43. {
  44. node_Type *NewNode = (node_Type*)malloc(sizeof(node_Type));
  45. if(NewNode!=NULL)
  46. {
  47. NewNode->left=NULL;
  48. NewNode->right=NULL;
  49. NewNode->key=NewData;
  50. }
  51. return NewNode;
  52. }
  53.  
  54. int IsLeaf(treetype T)
  55. {
  56. if(T!=NULL) return (LeftChild(T)==NULL&&RightChild(T)==NULL);
  57. else return -1;
  58. }
  59.  
  60. treetype Add_Right(treetype *Tree, KeyType NewData)
  61. {
  62. node_Type *NewNode=create_node(NewData);
  63. if(NewNode==NULL) return NewNode;
  64. if(*Tree==NULL) *Tree=NewNode;
  65. else{
  66. node_Type *Rnode= *Tree;
  67. while(Rnode->right!=NULL)
  68. Rnode=Rnode->right;
  69. Rnode->right==NewNode;
  70.  
  71. }
  72. return NewNode;
  73. }
  74.  
  75. treetype Add_Left(treetype *Tree,KeyType NewData)
  76. {
  77. node_Type *NewNode=create_node(NewData);
  78. if(NewNode==NULL) return NewNode;
  79. if(*Tree==NULL) return NewNode;
  80. else{
  81. node_Type *Lnode= *Tree;
  82. while(Lnode->left!=NULL)
  83. Lnode=Lnode->left;
  84. Lnode->left==NewNode;
  85. }
  86. return NewNode;
  87. }
  88.  
  89. treetype Search(char *email, treetype Root)
  90. {
  91. if(Root!=NULL)
  92. {
  93. if(strcmp((Root->key).email, email)==0) return Root;
  94. else if(strcmp((Root->key).email, email)<0) return Search(email,Root->right);
  95. else return Search(email,Root->left);
  96. }
  97. else return NULL;
  98. }
  99.  
  100. //chen mot node
  101. void InsertNode(KeyType x,treetype *Root)
  102. {
  103. if(*Root!=NULL)
  104. {
  105. if(((*Root)->key).email == x.email) return -1;
  106. if(strcmp(((*Root)->key).email, x.email)>0)
  107. InsertNode(x, (*Root)->left);
  108. else if(strcmp(((*Root)->key).email, x.email)<0)
  109. InsertNode(x, (*Root)->right);
  110.  
  111. }
  112. *Root=(node_Type*)malloc(sizeof(node_Type));
  113. if(*Root==NULL) return 0;
  114. (*Root)->key = x;
  115. (*Root)->left = (*Root)->right = NULL;
  116. return 1;
  117.  
  118. }
  119. void printInorder(treetype Root)
  120. {
  121. if(Root!=NULL)
  122. {
  123. printInorder(Root->left);
  124. printf("%-30s %-15s %-30s",(Root->key).name,(Root->key).tel,(Root->key).email);
  125. printInorder(Root->right);
  126. }
  127. }
  128.  
  129. int main()
  130. {
  131. FILE *fp;
  132. KeyType phonearr[MAX];
  133. treetype root=NULL;
  134. int i,irc;
  135. int n=10;
  136. fp=fopen("dulieu.txt","r+");
  137. if(fp==NULL)
  138. {
  139. printf("can not open file\n");
  140. return -1;
  141. }
  142. irc=fread(phonearr, sizeof(KeyType), n, fp);
  143.  
  144. fclose(fp);
  145. for(i=0;i<n;i++)
  146. InsertNode(phonearr[i],&root);
  147. printf("%-30s %-15s %-30s\n","NAME","TEL","EMAIL");
  148. printInorder(root);
  149.  
  150.  
  151. return 0;
  152.  
  153. }
  154.  
  155.  
  156.  
  157.  
  158. Nga 0903230451 ngalan@gmail.com
  159. Phuong 0932430111 phuongnk@gmail.com
  160. Hung 0903231253 hungln@yahoo.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement