This program demonstarte how to build and print a binary tree of random numbers.
There is a separate process for storing random numbers and sorting them to compare with print tree results.
The code has explanatory comments.
Code:
*&---------------------------------------------------------------------*
*& Report YBINTREE - Build/Print Binary Tree of numbers *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*
REPORT YBINTREE .
types: begin of stree,
value type i,
left type ref to data,
right type ref to data,
end of stree.
data: tree type stree.
data: int type i.
data: begin of rnd occurs 0,
num type i,
end of rnd.
start-of-selection.
do 100 times.
* generate random number between 0 and 100
CALL FUNCTION 'RANDOM_I4'
EXPORTING
RND_MIN = 0
RND_MAX = 100
IMPORTING
RND_VALUE = int.
* store numbers
rnd-num = int.
append rnd.
* build binary tree of random numbers
perform add_value using tree int.
enddo.
* stored numbers are sorted for comparison
sort rnd by num.
* print sorted random numbers
write: / 'Sorted Numbers'.
write: / '=============='.
skip.
loop at rnd.
write: rnd-num.
endloop.
skip.
* print binary tree. This should give the same result
* as the one listed from the internal table
write: / 'Binary Tree List'.
write: / '================'.
skip.
perform print_value using tree.
skip.
*&---------------------------------------------------------------------*
*& Form add_value
*&---------------------------------------------------------------------*
* text - Build tree with value provided
*----------------------------------------------------------------------*
* -->TREE text
* -->VAL text
*----------------------------------------------------------------------*
form add_value using tree type stree val type i.
field-symbols: type any.
data: work type stree.
if tree is initial. "When node has no values
tree-value = val. " assign value
clear: tree-left, tree-right.
create data tree-left type stree. "Create an empty node for left
create data tree-right type stree. "create an empty node for right
else.
if val le tree-value. "if number is less than or equal
assign tree-left->* to . "assign the left node to fs
* call add_value recursively with left node
perform add_value using val.
else. "if number is greater
assign tree-right->* to . "assign the right node to fs
* call add_value recursively with right node
perform add_value using val.
endif.
endif.
endform. "add_value
*&---------------------------------------------------------------------*
*& Form print_value
*&---------------------------------------------------------------------*
* text - traverse tree from left-mid-right order
* automatically this will be sorted list
*----------------------------------------------------------------------*
* -->TREE text
*----------------------------------------------------------------------*
form print_value using tree type stree.
field-symbols: type any.
if tree is initial. "node is empty
else. "non-empty node
assign tree-left->* to . "left node
perform print_value using . "print left
write: tree-value. "print the current value
assign tree-right->* to . "right node
perform print_value using . "print right
endif.
endform. "print_value
Thursday, December 20, 2007
Building A Binary Table
Labels:
SAP ABAP Tips and Tricks
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2007
(952)
-
▼
December
(236)
- Working with events in a Global Class
- Events in Table Maintenance
- Archiving IDocs
- Using Sorted table and Index while processing Inte...
- Highlighting only a particular cell instead of ent...
- Raising exceptions in a method and handling the sa...
- Maintaining Translations for Work item texts and O...
- Understanding "Local Workflow"
- Implementing Enhancements in a view
- Sending recursive mails upon reaching the deadline...
- ABAP Proxy communication
- Creation of a web service in SAP
- Step by Step approach to ceate simple BDC session ...
- General Application Transaction Codes
- Inroduction TO LSMW
- 24 Column Account Managers Report
- sap ABAP report on Column Account Managers Report
- sap ABAP report on Detail Transaction Report (DTR)...
- sap ABAP report on Summary Statement Quick Reference
- free download book on abap reporting on all modules
- ABAP REPORTS
- Report YRS_DOWNLOAD_TRANSPORT_REQUEST
- Report YRS_UPLOAD_TRANSPORT_REQUEST
- Displaying Available Report Variants in sap
- Creating Report Variants in sap
- SAP helpful reports, transactions and tables
- Useful SAP Tables
- ABAP REPORTING
- SAP Solution Manager’s centralized solution
- SAP Solution Manager
- Report Tree Using SARP and SERP
- User Profile Parameters
- XML sample file
- XSLT options
- Create XSLT program
- XML XSLT with ABAP
- Transport Guide
- ABAP Acronyms
- ABAP Code Sample
- ALV Pdf books
- ALV Articles
- Class ALV
- How to do the EXCISE ANNEXURE10 report?
- How to calculate last date of the month?
- How to change the deadline of the workitem program...
- How to convert from one currency value to other?
- How do I display / add the Terms and Conditions to...
- How do I create a long text for a document?*
- Where are the long texts of a document stored and ...
- I am using a SELECT query on a database table. Sin...
- How can I convert numerals into the corresponding ...
- How can I read an Excel file from presentation ser...
- How can I download my internal table into an Excel...
- How can I get the IP address of the system program...
- How do I download data in my internal table in a C...
- How to convert a date to internal or external format?
- How are RANGES different from SELECT-OPTIONS?
- What does R/3 stands for ?
- What does ABAP stand for?
- What is this new transaction 'n' all about?
- Casino Game in SAP ABAP
- Moving Characters in SAP ABAP Report Output
- Database Table Operations- A Performance Considera...
- SAP ABAP Performnace consideration while using ‘fo...
- Applying the having clause
- Avoid nested SELECT-ENDSELECT loops
- Use high-speed array operations with UPDATE, INSER...
- Coding Style
- Building A Binary Table
- how can we change the font for weite statement
- Import graphics to SAP
- Uploading Graphics TTF files into SAp Script Text
- Schedule Background Job
- How to change Package name/Development class for a...
- Useful User Paramters
- Finding a transaction code via SE93
- Standard Conversion Exits
- How to transport Standard Texts
- Search SAP Menu
- A FM which can create internal session
- Procedure for uploading Font
- Save content of internal tables in Excel format du...
- Where all SAP ABAP programs get stored!!
- Debugging a popup window
- Spool Conversion tp PDF
- A tips on error message while writting a BDC program
- Get the next available number in Number Range Object
- SAP ABAP Macro to validate Date
- Adding pushbuttons on the application toolbar of r...
- Display your report output lines inside a Title Box
- Using Subscreens on Report Selection Screens
- Save your ABAP report list output in your desktop ...
- Listbox on Selection Screen
- Get the Report selection Criteria
- What is sequence of event triggered in report?
- What is the difference between Primary key and Uni...
- What are the various types of selection screen event?
- What are standard layouts sets in the SAP Script?
- What are the data types of Internal Tables?
- What is the difference between 'Select single * ' ...
-
▼
December
(236)
No comments:
Post a Comment