Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project3;
- {$APPTYPE CONSOLE}
- uses
- SysUtils,
- Math;
- const
- max_n = 100000;
- type
- Tree = ^Node;
- Node = record
- l_son, r_son, parent: Tree;
- key, value: longint;
- end;
- var
- s, num: string;
- x, i, counter, cur: longint;
- build: Tree;
- flag: boolean;
- ch1, ch2: char;
- arr: array [1..max_n] of longint;
- function search_key(tree_key: longint): boolean;
- var
- t: longint;
- begin
- search_key := false;
- for t := 1 to counter do
- if (arr[t] = tree_key) then
- search_key := true;
- end;
- procedure QSort(lt, rt: longint);
- var
- i, j, x_key, tmp: longint;
- begin
- i := lt;
- j := rt;
- x_key := arr[(lt + rt) div 2];
- repeat
- while (arr[i] < x_key) do
- inc(i);
- while (arr[j] > x_key) do
- dec(j);
- if (i <= j) then begin
- tmp := arr[i];
- arr[i] := arr[j];
- arr[j] := tmp;
- inc(i);
- dec(j);
- end;
- until i > j;
- if (lt < j) then
- QSort(lt, j);
- if (i < rt) then
- QSort(i, rt);
- end;
- procedure delete(tree_key: longint);
- var
- t: longint;
- begin
- for t := 1 to counter do
- if (arr[t] = tree_key) then begin
- arr[t] := arr[counter];
- counter := counter - 1;
- exit;
- end;
- QSort(1, counter);
- end;
- function next_search(tree_key: longint): longint;
- var
- t: longint;
- begin
- QSort(1, counter);
- for t := 1 to counter do
- if (arr[t] > tree_key) then begin
- next_search := arr[t];
- exit;
- end;
- flag := false;
- end;
- function prev_search(tree_key: longint): longint;
- var
- t: longint;
- begin
- QSort(1, counter);
- for t := counter downto 1 do
- if (arr[t] < tree_key) then begin
- prev_search := arr[t];
- exit;
- end;
- flag := false;
- end;
- procedure insert(tree_key: longint);
- begin
- counter := counter + 1;
- arr[counter] := tree_key;
- QSort(1, counter);
- end;
- begin
- reset(input, 'bstsimple.in');
- rewrite(output, 'bstsimple.out');
- New(build);
- build := nil;
- counter := 0;
- randomize;
- randseed := 17000;
- while not eof do begin
- num := '';
- read(ch1);
- read(ch2);
- while(ch2 <> ' ') do
- read(ch2);
- readln(x);
- if (ch1 = 'i') then
- if (search_key(x) = false) then
- insert(x);
- if (ch1 = 'd') then
- if (search_key(x) <> false) then
- delete(x);
- if (ch1 = 'e') then begin
- if (search_key(x) = true ) then
- writeln('true')
- else
- writeln('flase');
- end;
- if (ch1 = 'n') then
- begin
- flag := true;
- cur := next_search(x);
- if (flag) then
- writeln(cur)
- else
- writeln('none');
- end;
- if (ch1 = 'p') then begin
- flag := true;
- cur := prev_search(x);
- if (flag) then
- writeln(cur)
- else
- writeln('none');
- end;
- end;
- closefile(output);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement