Non-Linear Data Structure 535
P
O
V W
(b)
Fig. 15.7{a) Tree representation (b) Linked representation
The structure for a tree node would be as follows.
struct node
{
int num;
struct node *ls;
struct node *rs
>
The pointers *ls and *rs point to the structure node. The pointer *ls contains the address of the left son
and rs is the pointer to the right son. If there are no left son and right son, then the pointers would be
NULL.
15.5 TRAVERSING BINARY TREES
As discussed earlier, three parameters are needed for formation of a binary tree. Passing through every
node of the tree is called traversing. Therefore, traversing of a tree means traversing of a node, left and
right subtrees. Assume that the root is indicated by O, left subtree as L and right subtree as R. Then, the
following traverse combinations are possible.
1) ORL: ROOT-RIGHT-LEFT
2) OLR: ROOT-LEFT-RIGHT
3) LOR: LEFT-ROOT-RIGHT
4) LRO: LEFT -RIGHT- ROOT
5) ROL: RIGHT-ROOT-LEFT
6) RLO: RIGHT-LEFT-ROOT
From the above six methods only three are standard and are discussed below. The right subtree is
always traversed after the left subtree. Hence, the OLR is preorder, LOR is inorder, and LRO is postorder.