Software & Finance





C++ - Printing Binary Search Tree





 

Here is the C++ code for printing binary search tree along with post order printing.

 

All the nodes to left are less than the current node value and all nodes to the right are greater than the current node value. It would be true for each and every node in the binary search tree. Click here for validating binary search tree.

 

typedef struct _BSTNode

{

    struct _BSTNode *left;

    struct _BSTNode *right;

    int data;

} BSTNode;

 

static BSTNode *root = NULL;

 

void PrintTree(BSTNode *node)

{

    if(node == NULL)

        return;

    PrintTree(node->left);

   

    std::cout << node->data << " ";

 

    PrintTree(node->right);

}

 

void PrintPostOrder(BSTNode *node)

{

    if(node == NULL)

        return;

    PrintPostOrder(node->left);

    PrintPostOrder(node->right);

 

    std::cout << node->data;

}