1 with TEXT_IO ; use TEXT_IO ; 2 procedure MAIN is -- A SMALL SAMPLE TO INTRODUCE ACCESS TYPES 3 4 type NODE ; -- just introduce type name 5 type NODE_PTR is access NODE ; -- define an access type 6 -- ie the type of a pointer 7 type NODE is -- now, really define the type NODE 8 record 9 BF : INTEGER ; -- balance factor for later use 10 LEFT : NODE_PTR ; -- this records predecessor 11 RIGHT : NODE_PTR ; -- this records sucessor 12 NAME : STRING(1..2) ; -- data, the "sort" key 13 PHONE : STRING(1..2) ; -- more tag along data 14 end record ; 15 16 ROOT_PTR : NODE_PTR := new NODE'(0, null, null, "D ", "4 ") ; 17 -- build a dummy root node using qualified aggregate 18 LEFT_PTR : NODE_PTR ; -- just some scratch objects 19 RIGHT_PTR : NODE_PTR ; 20 22 procedure OUTPUT(T : NODE_PTR ) is -- recursive procedure to walk tree 23 begin 24 if T /= null then 25 OUTPUT(T.LEFT) ; -- recursive, down left 26 PUT(T.NAME) ; PUT_LINE(T.PHONE) ; 27 OUTPUT(T.RIGHT) ; -- recursive, down right 28 end if ; 29 end OUTPUT ; 30 31 begin 32 PUT_LINE(" basic access types ") ; 33 -- build a tree manually for testing 34 -- 35 -- D 4 36 -- / \ 37 -- B 2 F 6 first tier 38 -- / \ / \ 39 -- A 1 C 3 E 5 G 7 second tier 40 -- 41 -- first tier 42 LEFT_PTR := new NODE'(0, null, null, "B ", "2 ") ; 43 ROOT_PTR.LEFT := LEFT_PTR ; 44 RIGHT_PTR := new NODE'(0, null, null, "F ", "6 ") ; 45 ROOT_PTR.RIGHT := RIGHT_PTR ; 46 -- second tier 47 LEFT_PTR.LEFT := new NODE'(0, null, null, "A ", "1 ") ; 48 LEFT_PTR.RIGHT := new NODE'(0, null, null, "C ", "3 ") ; 49 RIGHT_PTR.LEFT := new NODE'(0, null, null, "E ", "5 ") ; 50 RIGHT_PTR.RIGHT := new NODE'(0, null, null, "G ", "7 ") ; 52 OUTPUT(ROOT_PTR) ; -- print the tree 54 end MAIN ; basic access types A 1 B 2 C 3 D 4 E 5 F 6 G 7 <-- printed down page