Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define MAXLENGTH 50
  4. #define NIL -1
  5. typedef char DataType;
  6. typedef int Node;
  7. typedef struct {
  8.  
  9. DataType Data[MAXLENGTH];
  10. Node Parent[MAXLENGTH];
  11. int MaxNode;
  12. } Tree;
  13. Tree T;
  14. void MakeNull_Tree (Tree *T){
  15. (*T).MaxNode=0;
  16. }
  17. int EmptyTree(Tree T){
  18. return T.MaxNode == 0;
  19. }
  20. Node Parent(Node n,Tree T){
  21. if (EmptyTree(T) || (n>T.MaxNode-1))
  22. return NIL;
  23. else return T.Parent[n];
  24. }
  25. DataType Label_Node(Node n,Tree T){
  26. if (!EmptyTree(T) && (n<=T.MaxNode-1))
  27. return T.Data[n];
  28. }
  29. Node Root(Tree T){
  30. if (!EmptyTree(T)) return 0;
  31. else return NIL;
  32. }
  33. Node LeftMostChild(Node n,Tree T){
  34. Node i;
  35. int found;
  36. i=n+1;/* V? tr¡ n£t d?u tiˆn hy v?ng l… con c?a n£t n */
  37. found=0;
  38. while ((i<=T.MaxNode-1) && !found)
  39. if (T.Parent[i]==n) found=1; /* Da tm th?y con tr i nh?t c?a n£t n */
  40. else i=i+1;
  41. if (found) return i;
  42. else return NIL;
  43. }
  44. Node RightSibling(Node n,Tree T){
  45. Node i,parent;
  46. int found;
  47.  
  48. if (n<=0||n>=T.MaxNode-1)
  49. return NIL;
  50. else
  51. {parent=T.Parent[n];
  52. i=n+1;
  53. found=0;
  54. while ((i<=T.MaxNode-1) && !found)
  55. if (T.Parent[i]==parent) found=1;
  56. else i=i+1;
  57. if (found) return i;
  58. else return NIL; }
  59. }
  60. void PreOrder(Node n,Tree T){
  61. Node i;
  62. printf("%c ",Label_Node(n,T));
  63. i=LeftMostChild(n,T);
  64. while (i!=NIL){
  65. PreOrder(i,T);
  66. i=RightSibling(i,T);
  67. }
  68. }
  69. void InOrder(Node n,Tree T){
  70. Node i;
  71. i=LeftMostChild(n,T);
  72. if (i!=NIL) InOrder(i,T);
  73. printf("%c ",Label_Node(n,T));
  74. // if (i!=NIL)
  75. i=RightSibling(i,T);
  76. while (i!=NIL){
  77. InOrder(i,T);
  78. i=RightSibling(i,T);
  79. }
  80. }
  81. void PostOrder(Node n,Tree T){
  82. Node i;
  83. i=LeftMostChild(n,T);
  84. while (i!=NIL){
  85. PostOrder(i,T);
  86. i=RightSibling(i,T);
  87. }
  88. printf("%c ",Label_Node(n,T));
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement