Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(prog1).
- -compile([export_all]).
- insertTree(Value, nil) -> {Value,nil,nil};
- insertTree(Value, {Data,Left,Right}) when Value =< Data -> {Data,insertTree(Value,Left),Right};
- insertTree(Value, {Data,Left,Right}) when Value > Data -> {Data,Left,insertTree(Value,Right)};
- insertTree(_,{Data,Left,Right}) -> {Data,Left,Right}.
- insertRandom(0,Tree) -> Tree;
- insertRandom(N,Tree) when N > 0 ->
- insertTree(lists:seq(1, 1000),Tree),
- insertRandom(N-1,Tree).
- listToTree([],Tree) -> Tree;
- listToTree([H|L],Tree) ->
- insertTree(H,Tree),
- listToTree(L,Tree).
- preOrder(nil) -> [];
- preOrder({Value,Left,Right}) ->
- [Value] ++ preOrder(Left) ++ preOrder(Right).
- inOrder(nil) -> [];
- inOrder({Value,Left,Right}) ->
- inOrder(Left) ++ [Value] ++ inOrder(Right).
- postOrder(nil) -> [];
- postOrder({Value,Left,Right}) ->
- postOrder(Left) ++ postOrder(Right) ++ [Value].
- search(_,nil) -> nil;
- search(V,{Value,Left,Right}) ->
- if V == Value -> true;
- V < Value -> search(V,Left);
- V > Value -> search(V,Right)
- end.
- ifTreeContaints(_,nil) -> throw(false);
- ifTreeContaints(V,{Val,_,_}) when V == Val -> throw(true);
- ifTreeContaints(V,{Val,Left,_}) when V < Val -> ifTreeContaints(V,Left);
- ifTreeContaints(V,{Val,_,Right}) when V > Val -> ifTreeContaints(V,Right).
- searchX(Val,Tree) ->
- try
- ifTreeContaints(Val,Tree)
- catch
- throw:true -> 'true';
- throw:false -> 'false'
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement