mittimus

Implementazione ABR con puntatori

Jul 14th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. 5.2.File abr.h
  2. #include<stdlib.h>
  3. #define ALBEROVUOTO NULL
  4. typedef short boolean;
  5. typedef struct nodoABR {
  6. tipobaseABR info;
  7. struct nodoABR * leftchild , * rightchild;
  8. } * abr;
  9. boolean EmptyABR(abr n){
  10. return(n==ALBEROVUOTO);
  11. }
  12. boolean FullABR(abr n){
  13. return(0);
  14. }
  15. abr LeftChild(abr n){
  16. return(n->leftchild);
  17. }
  18. abr RightChild(abr n){
  19. return(n->rightchild);
  20. }
  21. tipobaseABR Label(abr n){
  22. return(n->info);
  23. }
  24. void MakeNullABR(abr *p){
  25. *p=ALBEROVUOTO;
  26. }
  27. boolean Member(abr n, tipobaseABR x){
  28. if (EmptyABR(n)) return(0);
  29. if (!Confronta(n->info,x)) return(1);
  30. if (Confronta(n->info,x)>0) return(Member(n->leftchild,x));
  31. else return(Member(n->rightchild,x));
  32. }
  33. void InsertABR(abr *p, tipobaseABR x){
  34. if (!FullABR(*p)){
  35. if (EmptyABR(*p)) {
  36. *p=(struct nodoABR *)malloc(sizeof(struct nodoABR));
  37. (*p)->info=x;
  38. (*p)->leftchild=(*p)->rightchild=ALBEROVUOTO;
  39. } else if (Confronta((*p)->info,x)>0) InsertABR(&(*p)->leftchild,x);
  40. else if (Confronta((*p)->info,x)<0) InsertABR(&(*p)->rightchild,x);
  41. }
  42. }
  43. abr DelMin(abr *p){
  44. abr pmin;
  45. if ((*p)->leftchild==ALBEROVUOTO) {
  46. pmin=*p;
  47. *p=(*p)->rightchild;
  48. return(pmin);
  49. } else return(DelMin(&(*p)->leftchild));
  50. }
  51. void DeleteABR(abr *p, tipobaseABR x){
  52. abr tmp;
  53. if (!EmptyABR(*p)) {
  54. if (!Confronta((*p)->info,x)) {
  55. tmp=*p;
  56. if (EmptyABR((*p)->leftchild)) {
  57. *p=(*p)->rightchild;
  58. free (tmp);
  59. } else if (EmptyABR((*p)->rightchild)) {
  60. *p=(*p)->leftchild;
  61. free (tmp);
  62. } else {
  63. *p=DelMin(&(*p)->rightchild);
  64. (*p)->leftchild=tmp->leftchild;
  65. (*p)->rightchild=tmp->rightchild;
  66. free (tmp);
  67. }
  68. } else if (Confronta((*p)->info,x)>0) DeleteABR(&(*p)->leftchild,x);
  69. else DeleteABR(&(*p)->rightchild,x);
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment