 .major .clear/window=1 ( .title/start=1,center/bold Tree Tutorial .box/start=3,32/end=5,43/bold  .label/start=4,33 population .box/start=7,16/end=9,22/bold  .label/start=8,17 maine  .box/start=7,50/end=9,59/bold  .label/start=8,51 new york .line/start=5,38/end=6,38/bold .line/start=6,19/end=6,55/bold .line/start=6,19/end=7,19/bold .line/start=6,55/end=7,55/bold .box/start=11,15/end=13,23/bold  .label/start=12,16 augusta .line/start=9,19/end=11,19/bold  .box/start=11,38/end=13,45/bold  .label/start=12,39 albany  .box/start=11,47/end=13,55/bold  .label/start=12,48 buffalo .box/start=11,57/end=13,71/bold   .label/start=12,58 new york city .line/start=9,55/end=10,55/bold   .line/start=10,42/end=10,65/bold  .line/start=10,42/end=11,42/bold  .line/start=10,52/end=11,52/bold  .line/start=10,65/end=11,65/bold" .label/start=14,17/underline 21945# .label/start=14,39/underline 101727 # .label/start=14,48/underline 357870 $ .label/start=14,61/underline 7071030   .text/start=16,2I A tree is a unique data structure to SCAN.  The closest parallel in other I programming languages is an array.  Both data structures are used to hold K multiple values of the same type.  You reference a node in a tree much like N an array element.  The declaration of a tree is much like an array declaration< in PASCAL.  There are, however, quite a few differences too.	 .end_text  .clear/start=21,1 
 .end_frame .text/start=16,2I The differences include: a SCAN tree can hold a varying number of values, G their subscripts need not be contiguous, and the subscripts need not be  integers.     3 Above we have a pictorial representation of a tree. 	 .end_text  .clear/start=21,1 
 .end_frame   .text/start=16,2P This example is designed to hold the population of various cities.  The ~b root~T of the tree, ~u population~, appears at the top of tree.  The first ~b level~ of theQ tree has a ~b node~ for each state of interest.  The second ~b level~ of the tree O has a ~b node~ for each city for which we have a population.  The values of the N tree are the integer populations of each city (underlined at the bottom of theN tree).  Only the ~b leaf nodes~, those at the bottom of the tree, have values.	 .end_text 
 .end_frame  + .label/start=4,51 DECLARE ~bu population~:  . .label/start=5,60 TREE(~r STRING~,~rb STRING~)! .label/start=6,65 OF ~u INTEGER~; ! .label/start=4,33 ~bu population~  .label/start=8,17 ~r maine~  .label/start=8,51 ~r new york~ .label/start=12,16 ~rb augusta~  .label/start=12,39 ~rb albany~ .label/start=12,48 ~rb buffalo~ % .label/start=12,58 ~rb new york city~    .text/start=16,2P The declaration above declare the tree ~u population~.  We've also tried to showH the corresponding parts of the declaration and the diagram.  The name ofM the tree is the root.  The list of types in parentheses following the keyword Q ~b TREE~ gives the number of levels and the type of the subscripts at each level. H Following the keyword ~b OF~ is the type of the values held by the tree.	 .end_text  .clear/start=21,1 
 .end_frame   .clear/start=4,51/end=4,80 .clear/start=5,60/end=5,80 .label/start=6,65/end=6,80   .label/start=4,33 population .clear/start=6,1M .label/start=10,center DECLARE population: TREE( STRING, STRING ) OF INTEGER;    .text/start=16,2K As we mentioned earlier, the number of values in a tree varies.  Initially, I the tree has no nodes or values.  You create nodes and values in the tree * using assignment statements.  For example:  2        population( 'texas', 'houston' ) = 1232802;	 .end_text 
 .end_frame   .clear/start=10,1/end=10,80  .box/start=7,50/end=9,56/bold  .label/start=8,51 texas  .line/start=5,38/end=6,38/bold .line/start=6,38/end=6,53/bold .line/start=6,53/end=7,53/bold .box/start=11,55/end=13,63/bold  .label/start=12,56 houston .line/start=9,53/end=10,53/bold   .line/start=10,53/end=10,59/bold  .line/start=10,59/end=11,59/bold$ .label/start=14,56/underline 1232802
 .end_frame   .text/start=16,21 Observe the effects of the following assignments:   3        population( 'georgia', 'atlanta' ) = 476973; )        population( 'texas', 'austin' ) =  9              population( 'georgia', 'atlanta' ) - 225165;   	 .end_text 
 .end_frame   .box/start=7,16/end=9,24/bold  .label/start=8,17 georgia  .line/start=6,20/end=6,38/bold .line/start=6,20/end=7,20/bold .box/start=11,16/end=13,24/bold  .label/start=12,17 altanta .line/start=9,20/end=11,20/bold # .label/start=14,17/underline 476973    .box/start=11,45/end=13,52/bold  .label/start=12,46 austin   .line/start=10,48/end=10,53/bold  .line/start=10,48/end=11,48/bold# .label/start=14,46/underline 251808 
 .end_frame   .text/start=16,20 Trees have the following very useful properties:  . 	o they can hold an arbitrary number of values. 	o they provide an associative lookup function? 	o the subscripts of the nodes at a particular level are sorted   	 .end_text 
 .end_frame   .major .clear/start=3,2 .text/start=4,2 / 	DECLARE dictionary: TREE( STRING ) OF INTEGER; $ 	SET alpha	( 'a'..'z' OR 'A'..'Z' ); 	TOKEN word	{ alpha... }; % 	MACRO find_word TRIGGER { w: word }; 3 	    w = LOWER( w );			! make word case insensitive A 	    IF NOT EXISTS( dictionary( w ) )	! check if word not in tree 	 	    THEN ' 		dictionary( w ) = 0;		! if not add it  	    END IF;+ 	    dictionary( w ) = dictionary( w ) + 1;  	END MACRO; 	 .end_text  .text/start=16,2K The arbitrary number of nodes makes it simple to write a small program that O counts the number of unique words in a file.  Here the tree ~u dictionary~ is a M one level tree.  The subscripts of the nodes are the words found in the file. @ The value of each node is the number of times the word occurred.	 .end_text 
 .end_frame   .major .text/start=4,2 / 	DECLARE dictionary: TREE( STRING ) OF INTEGER; 2 	DECLARE tree_ptr:   TREEPTR( STRING ) TO INTEGER;  ? 	tree_ptr = FIRST( dictionary );		! get a pointer to first node 5 	WHILE tree_ptr <> NIL;			! loop til run out of nodes < 	    WRITE SUBSCRIPT( tree_ptr ), 	! write subscript of node) 		  ' occurred ', 		!   which is the word 1 		  VALUE( tree_ptr ),		! write value of the node & 		  ' times';			!   which is the count8 	    tree_ptr = NEXT( tree_ptr );	! advance to next node 	END WHILE; 	 .end_text  .clear/start=15,1  .text/start=16,2J Since the subscripts of the tree dictionary are sorted, we can use severalM built-in function to produce a list of the words found in alphabetical order. O This example uses a unique SCAN data type called a ~b TREEPTR~ (pronounced tree N pointer) to point to the nodes of the tree and to obtain information about theO nodes.  FIRST gets the first node of a tree; NEXT gets the next node; SUBSCRIPT G returns the subscript of a node; and VALUE returns the value of a node. 	 .end_text 
 .end_frame   .major .text/start=4,2 , 	DECLARE keyword: TREE( STRING ) OF INTEGER;4 	keyword( 'if' ) = 0;			! place keywords in the tree 	keyword( 'then' ) = 0; 4 	keyword( 'else' ) = 0;			! there could be many more% 	MACRO find_word TRIGGER { w: word }; 3 	    w = LOWER( w );			! make word case insensitive < 	    IF EXISTS( keyword( w ) )		! check if word is a keyword' 	    THEN				! if it is increment count ( 		dictionary( w ) = dictionary( w ) + 1; 	    END IF; 	END MACRO; 	 .end_text  .clear/start=15,1  .text/start=16,2L This example uses the associative lookup property of a tree, ie. the abilityO to see if an arbitrary string is the subscript of a node.  We make the keywords P we are interested in counting the subscripts of the tree ~u keyword~.  The valueM of each node is the number of times that keyword occurred. In a macro that is O fired when a word is encountered, we check if the word is one of the subscripts ? in tree.  If it is we increment the value of that keyword node. 	 .end_text 
 .end_frame .clear/start=4,13 .label/start=10,center/reverse End of Tree Tutorial 
 .end_frame .end_script   