TITLE "MP3 by Douglas Jones" ; a function to traverse a binary tree concatenate the strings in it. ; this version is based on the first posted solution to Homework 6. ; this version makes no attempt to free unused space on the heap. ; this version has slight optimization in the style of Chapter 6 USE "hawk.h" USE "stdio.h" USE "string.h" USE "stdlib.h" ; activation record for TRAVERS ;RETAD = 0 P = 4 ; points to a tree node LSTR = 8 ; left_string of P RSTR = 12 ; right_string of P LEN = 16 ; length of the strings ARSIZE = 20 TRAVERS:; expects R3 = p, pointer to the root ; returns nothing ; void travers( node * p ) { STORES R1,R2 ADDI R2,R2,ARSIZE TESTR R3 BZS TRAVEL ; if (p != NULL) { STORE R3,R2,P-ARSIZE ; -- move p LOAD R3,R3,LEFT ; -- param JSR R1,TRAVERS ; left_string = travers( p->left ) STORE R3,R2,LSTR-ARSIZE ; -- param already in R3 LIL R1,STRLEN JSRS R1,R1 STORE R3,R2,LEN-ARSIZE; length = strlen( left_string ) LOAD R3,R2,P-ARSIZE LOAD R3,R3,RIGHT ; -- param JSR R1,TRAVERS ; right_string = travers( p->right ) STORE R3,R2,RSTR-ARSIZE ; -- param already in R3 LIL R1,STRLEN JSRS R1,R1 LOAD R4,R2,LEN-ARSIZE ADD R4,R4,R3 ; length = length + strlen( right_string ) STORE R4,R2,LEN-ARSIZE LOAD R3,R2,P-ARSIZE LOAD R3,R3,DATA ; -- param LIL R1,STRLEN JSRS R1,R1 LOAD R4,R2,LEN-ARSIZE ADD R3,R4,R3 ; length = length + strlen( p->data ) ADDSI R3,1 ; length = length + 1 -- allow for '\0' ; -- param already in R3 LIL R1,MALLOC JSRS R1,R1 ; buf = malloc( length ) LOAD R4,R2,LSTR-ARSIZE LIL R1,STRCPY JSRS R1,R1 ; buf = strcpy( buf, left_string ) LOAD R4,R2,P-ARSIZE LOAD R4,R4,DATA ; -- param LIL R1,STRCAT JSRS R1,R1 ; buf = strcat( buf, p->data ) LOAD R4,R2,RSTR-ARSIZE LIL R1,STRCAT JSRS R1,R1 ; buf = strcat( buf, right_string ) BR TRAVQT TRAVEL: ; } else { -- empty tree LEA R3,EMPTY ; buf = "" TRAVQT: ; } -- end of if ADDI R2,R2,-ARSIZE LOADS R1,R2 ; -- early partial return sequence JUMPS R1 ; return buf ; } -- end of travers EMPTY: ASCII "",0 ; the assignment said to put the following at the very end USE "/mnt/nfs/clasnetappvm/fs3/dwjones/2630mp3.h" END