% Rekurziv fa teszt % Ez a teszt a treetest.in file-bol olvas be egy fat % Kulonbozo muveleteket vegez rajta % Az informaciokat es az eredmenyeket a treetest.out file-ba irja my_tree = tree_type[string] start_up = proc () tree1, tree2, tree3, tree4 : my_tree output : stream := stream$open(file_name$parse("treetest.out"),"write") found : bool output!putl("") output!putl("*** Tree1 fa betoltese ***") tree1 := load_tree("treetest.in") show_tree(tree1, "Tree1", output) show_tree_in_order(tree1, "Tree1", output) show_leafs(tree1, "Tree1", output) tree1!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree1", mess, ptree, tree, output) end output!putl("") output!putl("*** equate (Tree2 := Tree1) ***") tree2 := tree1 output!putl(" Tree1 equal to Tree2: " || sbool(tree1!equal(tree2),"true","false")) output!putl(" Tree1 similar to Tree2: " || sbool(tree1!similar(tree2),"true","false")) output!putl("") output!putl("*** copy (Tree2 := Tree1!Copy) ***") tree2 := tree1!copy output!putl(" Tree1 equal to Tree2: " || sbool(tree1!equal(tree2),"true","false")) output!putl(" Tree1 similar to Tree2: " || sbool(tree1!similar(tree2),"true","false")) show_tree(tree2, "Tree2", output) tree2!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree2", mess, ptree, tree, output) end output!putl("") output!putl("*** set value (Tree1 -?-> Tree2) ***") tree1.value := "0 root" output!putl(" Tree1 equal to Tree2: " || sbool(tree1!equal(tree2),"true","false")) output!putl(" Tree1 similar to Tree2: " || sbool(tree1!similar(tree2),"true","false")) show_tree(tree1, "Tree1", output) show_tree(tree2, "Tree2", output) output!putl("") output!putl("*** search ('1 alma' in Tree1 -> Tree3) ***") tree3, found := tree1!search("1 alma") output!putl(" found: " || sbool(found,"true","false")) output!putl(" Tree1 is parent of Tree3: " || sbool(tree1!is_parent_of(tree3),"true","false")) output!putl(" Tree3 is parent of Tree1: " || sbool(tree3!is_parent_of(tree1),"true","false")) output!putl(" Tree1 is child of Tree3: " || sbool(tree1!is_child_of(tree3),"true","false")) output!putl(" Tree3 is child of Tree1: " || sbool(tree3!is_child_of(tree1),"true","false")) output!putl(" Tree3 is parent of Tree3: " || sbool(tree3!is_parent_of(tree3),"true","false")) output!putl(" Tree3 is child of Tree3: " || sbool(tree3!is_child_of(tree3),"true","false")) show_tree(tree3, "Tree3", output) output!putl("") output!putl("*** search ('2.1 borz' in Tree2 -> Tree4) ***") tree4, found := tree2!search("2.1 borz") output!putl(" found: " || sbool(found,"true","false")) output!putl(" Tree2 is parent of Tree4: " || sbool(tree2!is_parent_of(tree4),"true","false")) output!putl(" Tree2 is ancestor of Tree4: " || sbool(tree2!is_ancestor_of(tree4),"true","false")) output!putl(" Tree4 is child of Tree2: " || sbool(tree4!is_child_of(tree2),"true","false")) output!putl(" Tree4 is descendant of Tree2: " || sbool(tree4!is_descendant_of(tree2),"true","false")) show_tree(tree4, "Tree4", output) output!putl("") output!putl("*** move_to (Tree3 to Tree4) ***") tree3!move_to(tree4) output!putl(" Tree1 is parent of Tree3: " || sbool(tree1!is_parent_of(tree3),"true","false")) output!putl(" Tree4 is parent of Tree3: " || sbool(tree4!is_parent_of(tree3),"true","false")) output!putl(" Tree1 is ancestor of Tree3: " || sbool(tree1!is_ancestor_of(tree3),"true","false")) output!putl(" Tree2 is ancestor of Tree3: " || sbool(tree2!is_ancestor_of(tree3),"true","false")) show_tree(tree1, "Tree1", output) show_tree(tree2, "Tree2", output) tree1!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree1", mess, ptree, tree, output) end tree2!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree2", mess, ptree, tree, output) end tree3!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree3", mess, ptree, tree, output) end tree4!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree4", mess, ptree, tree, output) end output!putl("") output!putl("*** search_all ('1.2 angolna' in Tree2) ***") for t: my_tree in tree2!search_all("1.2 angolna") do show_node(t, output) end %for output!putl("") output!putl("*** exceptions (insert_to no_root) ***") tree3!insert_to(tree1) except when no_root: output!putl(" Csak gyoker illesztheto masik faba!") end output!putl("*** exceptions (insert_to own_tree) ***") tree2!insert_to(tree3) except when own_tree: output!putl(" Csomopont nem illesztheto a sajat fajaba!") end output!putl("*** exceptions (extract is_root) ***") tree1!extract except when is_root: output!putl(" Csak gyerek veheto ki egy fabol!") end output!putl("*** exceptions (move_to own_tree) ***") tree2!move_to(tree4) except when own_tree: output!putl(" Csomopont nem mozgathato a sajat fajaba!") end output!putl("*** exceptions (parent is_root) ***") tree1!parent except when is_root: output!putl(" Gyokernek nincs szuloje!") end tree1!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree1", mess, ptree, tree, output) end tree2!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree2", mess, ptree, tree, output) end tree3!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree3", mess, ptree, tree, output) end tree4!invariant except when bad_struct(ptree, tree : my_tree, mess : string): show_error("Tree4", mess, ptree, tree, output) end end start_up % Betolti egy fa adatait egy szoveges file-bol % A file szerkezetenek leirasa a treetest.in file-ban talalhato load_tree = proc (name : string) returns (my_tree) input : stream := stream$open(file_name$parse(name),"read") tree : my_tree created : bool := false line : string i,l : int while ~input!empty do line := input!getl l := 1 while l <= line!size cand line[l] ~= '*' do l := l + 1 end %while if l > line!size then continue end l := l - 1 line := line!rest(l + 2) while line!size > 0 cand line[1] = ' ' do line := line!rest(2) end %while if l = 0 then if created then break end tree := my_tree$new(line) created := true else if l > tree!level then while l > tree!level + 1 do tree := tree!new_child("") end %while else while l <= tree!level do tree := tree!parent end %while end %if tree := tree!new_child(line) end %if end %while input!close return (tree!root) end load_tree % Kiirja egy fa osszes informaciojat - pre order sorrendben show_tree = proc (tree : my_tree, name : string, output : stream) output!putl("-- " || name || " pre order --") for t : my_tree in tree!pre_order do show_node(t, output) end %for output!putl("-- end " || name || " --") end show_tree % Kiirja egy fa osszes informaciojat - in order sorrendben show_tree_in_order = proc (tree : my_tree, name : string, output : stream) output!putl("-- " || name || " in order --") for t : my_tree in tree!in_order do show_node(t, output) end %for output!putl("-- end " || name || " --") end show_tree_in_order % Kiirja egy fa leveleinek osszes informaciojat - pre order sorrendben show_leafs = proc (tree : my_tree, name : string, output : stream) output!putl("-- " || name || " leafs --") for t : my_tree in tree!search_by(is_leaf) do show_node(t, output) end %for output!putl("-- end " || name || " --") end show_leafs is_leaf = proc (tree : my_tree) returns (bool) return (tree!is_empty) end is_leaf % Egy csomopont adatait irja ki show_node = proc (tree : my_tree, output : stream) for i : int in int$from_to(1, tree!level) do output!putc(' ') end %for output!putl("* " || tree.value || " // " || sbool(tree!is_root,"root, ","child, ") || sbool(tree!is_empty,"empty, ","has child, ") || "child_no: " || int$unparse(tree!child_no) || ", " || "size: " || int$unparse(tree!size)) end show_node % Segedfuggveny bool tipus megjelenitesehez sbool = proc (b : bool, strue, sfalse : string) returns (string) if b then return(strue) else return(sfalse) end %if end sbool show_error = proc (name, mess : string, ptree, tree: my_tree, output : stream) output!putl("### " || name || " strukturaja serult ###") output!putl(" Parent : " || ptree.value) output!putl(" Child : " || tree.value) output!putl(" Message: " || mess) signal failure ("bad tree struct") end show_error