Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.91 KB | None | 0 0
  1.  
  2. program Project3;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7.   SysUtils,
  8.   Math;
  9.  
  10. const
  11.   max_n = 100000;
  12.  
  13. type
  14.   Tree = ^Node;
  15.   Node = record
  16.     l_son, r_son, parent: Tree;
  17.     key, value: longint;
  18.   end;
  19.  
  20. var
  21.   s, num: string;
  22.   x, i, counter, cur: longint;
  23.   build: Tree;
  24.   flag: boolean;
  25.   ch1, ch2: char;
  26.   arr: array [1..max_n] of longint;
  27.  
  28. function search_key(tree_key: longint): boolean;
  29. var
  30.   t: longint;
  31. begin
  32.   search_key := false;
  33.  
  34.   for t := 1 to counter do
  35.     if (arr[t] = tree_key) then
  36.       search_key := true;
  37. end;
  38.  
  39. procedure QSort(lt, rt: longint);
  40. var
  41.   i, j, x_key, tmp: longint;
  42. begin
  43.   i := lt;
  44.   j := rt;
  45.   x_key := arr[(lt + rt) div 2];
  46.  
  47.   repeat
  48.     while (arr[i] < x_key) do
  49.       inc(i);
  50.     while (arr[j] > x_key) do
  51.       dec(j);
  52.     if (i <= j) then begin
  53.       tmp := arr[i];
  54.       arr[i] := arr[j];
  55.       arr[j] := tmp;
  56.       inc(i);
  57.       dec(j);
  58.     end;
  59.   until i > j;
  60.  
  61.   if (lt < j) then
  62.     QSort(lt, j);
  63.   if (i < rt) then
  64.     QSort(i, rt);
  65. end;
  66.  
  67. procedure delete(tree_key: longint);
  68. var
  69.   t: longint;
  70. begin
  71.   for t := 1 to counter do
  72.     if (arr[t] = tree_key) then begin
  73.       arr[t] := arr[counter];
  74.       counter := counter - 1;
  75.       exit;
  76.     end;
  77.  
  78.   QSort(1, counter);
  79. end;
  80.  
  81. function next_search(tree_key: longint): longint;
  82. var
  83.   t: longint;
  84. begin
  85.   QSort(1, counter);
  86.  
  87.   for t := 1 to counter do
  88.     if (arr[t] > tree_key) then begin
  89.       next_search := arr[t];
  90.       exit;
  91.     end;
  92.  
  93.   flag := false;
  94. end;
  95.  
  96. function prev_search(tree_key: longint): longint;
  97. var
  98.   t: longint;
  99. begin
  100.   QSort(1, counter);
  101.    
  102.   for t := counter downto 1 do
  103.     if (arr[t] < tree_key) then begin
  104.       prev_search := arr[t];
  105.       exit;
  106.     end;
  107.  
  108.   flag := false;
  109. end;
  110.  
  111. procedure insert(tree_key: longint);
  112. begin
  113.   counter := counter + 1;
  114.   arr[counter] := tree_key;
  115.  
  116.   QSort(1, counter);
  117. end;
  118.  
  119. begin
  120.   reset(input, 'bstsimple.in');
  121.   rewrite(output, 'bstsimple.out');
  122.  
  123.   New(build);
  124.   build := nil;
  125.   counter := 0;
  126.  
  127.   randomize;
  128.   randseed := 17000;
  129.  
  130.   while not eof do begin
  131.     num := '';
  132.     read(ch1);
  133.     read(ch2);
  134.     while(ch2 <> ' ') do
  135.       read(ch2);
  136.     readln(x);
  137.  
  138.     if (ch1 = 'i') then
  139.       if (search_key(x) = false) then
  140.         insert(x);
  141.  
  142.     if (ch1 = 'd') then
  143.       if (search_key(x) <> false) then
  144.         delete(x);
  145.  
  146.     if (ch1 = 'e') then begin
  147.       if (search_key(x) = true ) then
  148.         writeln('true')
  149.       else
  150.         writeln('flase');
  151.     end;
  152.  
  153.     if (ch1 = 'n') then
  154.     begin
  155.       flag := true;
  156.       cur := next_search(x);
  157.        
  158.       if (flag) then
  159.         writeln(cur)
  160.       else
  161.         writeln('none');
  162.     end;
  163.  
  164.     if (ch1 = 'p') then begin
  165.       flag := true;
  166.       cur := prev_search(x);
  167.  
  168.       if (flag) then
  169.         writeln(cur)
  170.       else
  171.         writeln('none');
  172.     end;
  173.   end;
  174.  
  175.   closefile(output);
  176. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement