Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : Bbcbasic

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

0 of 1 people (0%) answered Yes
Recently 0 of 1 people (0%) answered Yes

BBCBASIC: Datastructure: Tree: Binary: Print: Sideways: How to print a binary tree sideways?

Oct 24th, 2003 11:14

Knud van Eeden


----------------------------------------------------------------------
--- Knud van Eeden --- 24 October 2003 - 00:27 am --------------------
BBCBASIC: Datastructure: Tree: Binary: Print: Sideways: How to print a 
binary tree sideways?
---
Steps: Overview:
 1. -Open BBCBASIC
 2. -Copy/paste this program in the program editor
--- cut here ---------------------------------------------------------
PROCTreePrintBinarySidewaysMain( 1, -1, 10, 0 )
END
:
:
:
REM library: tree: print: binary: main [kn, ri, fr, 24-10-2003  
0:27:46]
DEF PROCTreePrintBinarySidewaysMain( rootP%, nilP%, dimmaxI%, 
depthminI% )
PROCTreePrintBinarySidewaysInitDim( dimmaxI% )
PROCTreePrintBinarySidewaysInitData( dimmaxI% )
PROCTreePrintBinarySidewaysSub( rootP%, depthminI% )
ENDPROC
:
DEF PROCTreePrintBinarySidewaysSub( P%, depthI% )
IF P% = nilP% THEN ENDPROC
REM
REM right first traversal, thus right has to come first
PROCTreePrintBinarySidewaysSub( rP%( P% ), depthI% + 1 )
REM
PROCTreePrintBinarySidewaysSubOutput( P% )
REM
PROCTreePrintBinarySidewaysSub( lP%( P% ), depthI% + 1 )
ENDPROC
:
DEF PROCTreePrintBinarySidewaysSubOutput( P% )
PRINT
PRINT; TAB( 2 * depthI% ); s$( P% ); " ";
ENDPROC
:
DEF PROCTreePrintBinarySidewaysInitDim( maxI% )
DIM lP%( maxI% )
DIM rP%( maxI% )
DIM s$( maxI% )
ENDPROC
:
DEF PROCTreePrintBinarySidewaysInitData( maxI% )
LOCAL I%
LOCAL minI%
minI% = 1
FOR I% = minI% TO maxI%
  READ lP%( I% )
  READ rP%( I% )
  READ s$( I% )
NEXT I%
ENDPROC
:
DEF PROCTreePrintBinarySidewaysStoreData
REM
REM here 3 + 4 * 5 is stored in a binary tree
REM
REM ---
REM
REM the binary tree is represented by arrays
REM
REM ---
REM
REM format:
REM
REM leftpointer, rightpointer, value
REM
REM ---
REM
DATA  2,  3, "*"
DATA  4,  5, "+"
DATA -1, -1, "5"
DATA -1, -1, "3"
DATA -1, -1, "4"
DATA -1, -1, ""
DATA -1, -1, ""
DATA -1, -1, ""
DATA -1, -1, ""
DATA -1, -1, ""
ENDPROC
:
--- cut here ---------------------------------------------------------
 3. -Run the program
 4. -That will show for this example the binary tree
     for '3 + 4 * 5' sideways:
       5
     *
         4
       +
         3
     ---
     1. that is the root '*' is most left
     ---
     2. and because of the right first traversal tree walk,
        the right most node comes on the first line
 5. About the printing sideways, the x and y value of where
    to print horizontally respectively vertically are
    controled by PRINT and depth respectively.
    This is controlled in this procedure:
     DEF PROCTreePrintBinarySidewaysSubOutput( P% )
     REM
     REM the total amount of 'PRINT' statements here
     REM control the vertical y-value.
     REM
     REM That is, each PRINT statement increases the 
     REM vertical y-value with 1
     REM
     REM
     PRINT
     REM
     REM ---
     REM
     REM the depth controls the horizontal x-value
     REM
     PRINT; TAB( 2 * depthI% ); s$( P% ); " "; :
     ENDPROC
     :
---
---
Internet: see also:
---
Datastructure: Tree: Binary: Print: Recursion: How to print a binary 
tree?
http://www.faqts.com/knowledge_base/view.phtml/aid/25815/fid/1266
---
Datastructure: Tree: Binary: Print: Sideways: How to print binary tree 
sideways?
http://www.faqts.com/knowledge_base/view.phtml/aid/25817/fid/1266
---
Datastructure: Tree: Binary: Print: Upright: How to print a binary 
tree upright?
http://www.faqts.com/knowledge_base/view.phtml/aid/25819/fid/1266
---
BBCBASIC: Datastructure: Tree: Binary: Print: Upright: How to print a 
binary tree upright?
http://www.faqts.com/knowledge_base/view.phtml/aid/25835/fid/768
----------------------------------------------------------------------



© 1999-2004 Synop Pty Ltd