BINTREE.PAS 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Program BinTree;
  2. {�ணࠬ¬  ¤«ï £¥­¥à æ¨¨, ¬®¤¨ä¨æ¨à®¢ ­¨ï, ®â®¡à ¦¥­¨ï ¡¨­ à­®£® ¤¥à¥¢  ¯®¨áª .}
  3. Uses CRT;
  4. type
  5. PTree = ^TTree;
  6. TTree = record
  7. key : integer;
  8. L, R : PTree;
  9. end;
  10. {-------------------------}
  11. Procedure Add(aKey : integer; var aP : PTree);
  12. begin
  13. if aP = nil then
  14. begin
  15. New(aP);
  16. with aP^ do
  17. begin
  18. key := aKey;
  19. l:=nil;
  20. r:=nil;
  21. end;
  22. end
  23. else
  24. begin
  25. if aKey > aP^.Key then Add(aKey,aP^.R) else
  26. if aKey < aP^.Key then Add(aKey,aP^.L);
  27. end;
  28. end;{Add}
  29. {-------------------------}
  30. Procedure Delete( aKey : integer; var aP:PTree);
  31. var
  32. q : PTree;
  33. {-}
  34. procedure Del(var aP:PTree);
  35. begin
  36. if aP^.R <> nil then Del(aP^.R)
  37. else
  38. begin
  39. q^.Key := aP^.Key;
  40. q := aP;
  41. aP := aP^.L;
  42. end;
  43. end;
  44. {}
  45. begin
  46. if aP = nil then WriteLn('�«-â  ',aKey, ' ­¥â ¢ ¤¥à¥¢¥!') else
  47. if aKey > aP^.Key then Delete(aKey,aP^.R) else
  48. if aKey < aP^.Key then Delete(aKey,aP^.L) else
  49. begin
  50. q := aP;
  51. if q^.L = nil then aP := q^.L else
  52. if q^.R = nil then aP := q^.R else Del(q^.L);
  53. Dispose(q);
  54. end;
  55. end;{Delete}
  56. {----------------------------}
  57. Procedure DelAll(var r : PTree);
  58. begin
  59. if r <> nil then begin
  60. if (R^.L = nil ) and (R^.R = nil ) then
  61. begin
  62. Dispose(r);
  63. r:=nil;
  64. end
  65. else
  66. begin
  67. DelAll(r^.L);
  68. DelAll(r^.R);
  69. Dispose(r);
  70. r:=nil;
  71. end;
  72. end;
  73. end;
  74. {-----------------------}
  75. Procedure Draw(P:PTree; x , h : word);
  76. var
  77. i : byte;
  78. begin
  79. GotoXY(x, 1 + h * 2);
  80. Write(P^.Key:2);
  81. if P^.L <> nil then
  82. begin
  83. GotoXY(x - (1 shl (4-h))+1,2+h*2);
  84. Write(#218);
  85. for i:=1 to (1 shl (4-h)-2) do Write(#196);
  86. Write(#217);
  87. Draw(P^.L, x - (1 shl (4-h)), h+1);
  88. end;
  89. if P^.R <> nil then
  90. begin
  91. GotoXY(x+1,2+h*2);
  92. Write(#192);
  93. for i:=1 to (1 shl (4-h)-2) do Write(#196);
  94. Write(#191);
  95. Draw(P^.R, x + (1 shl (4-h)), h+1);
  96. end;
  97. end; {Draw}
  98. {-------------------}
  99. var
  100. c : integer;
  101. INP : TEXT;
  102. root : PTree;
  103. begin
  104. Assign(INP,'treeval.txt');
  105. Reset(INP);
  106. root := nil;
  107. while (NOT EOF(INP)) do
  108. begin
  109. Read(INP,c);
  110. Add(c,Root);
  111. end;
  112. ClrScr;
  113. Draw(root,40,0);
  114. GotoXY(1,24);
  115. Write('‚¢¥¤¨â¥ ª®£® 㤠«¨âì: ');
  116. Read(c);
  117. Delete(c,root);
  118. ClrScr;
  119. Draw(root,40,0);
  120. ReadLn;
  121. ReadLn;
  122. DelAll(Root);
  123. end.