Thursday, December 20, 2007

Building A Binary Table

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

No comments:

Blog Archive