Shared Top Border

Enterprise Resource
Planning Portal

 

Advertise | Founder BLOG

ERPGenie.COM ABAP Tips and Tricks Database

THE ultimate
ERP website

 

Forums | Vote for us |

Google    Other Search Options

Home arrow Tips and Tricks arrow ABAP Dictionary arrow A Binary Tree Implementation in ABAP
A Binary Tree Implementation in ABAP PDF Print E-mail
User Rating: / 2
PoorBest 
Written by Dharmasiri Amith   
Tuesday, 30 October 2007

This sample code uses dynamic objects to create a binary tree of random numbers. It stores numbers on the left node or right node depending on the value comparison with the current value. There are two recursive subrotines used for the building of the tree and printing  through the tree.

For comparison purpose, the same random numbers are stored and sorted in an internal table and printed.

*&---------------------------------------------------------------------*

*& 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: <ltree> 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 <ltree>. "assign the left node to fs

* call add_value recursively with left node

perform add_value using <ltree> val.

else. "if number is greater

assign tree-right->* to <ltree>. "assign the right node to fs

* call add_value recursively with right node

perform add_value using <ltree> 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: <ltree> type any.

 

if tree is initial. "node is empty

else. "non-empty node

assign tree-left->* to <ltree>. "left node

perform print_value using <ltree>. "print left

write: tree-value. "print the current value

assign tree-right->* to <ltree>. "right node

perform print_value using <ltree>. "print right

endif.

endform. "print_value

Last Updated ( Tuesday, 30 October 2007 )
 
< Prev   Next >

Google Search

Google Ads

Shared Bottom Border

Contact Us | Polls | Add URL | Contribute | Privacy | Terms | Feedback

Discussion Forum | BLOG | Consultants: Post your resume | Companies: Advertise on ERPGenie.COM | Post Job
Financials Consultant | Consultant Review | Gallia Consulting | Supply Chain Project | SAP Financials Forum
GenieHoldings.COM, Inc. | Genie Press | WorkflowGenie | ESAGenie | ERPTopSites | ABAP Tips and Tricks | SAP Solutions Database

EDIGenie | Searching Survivor