Author Topic: How do insert a node in a binary tree?  (Read 901 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
How do insert a node in a binary tree?
« on: June 14, 2011, 03:44:40 pm »
For more details, visit us at: http://www.softwareandfinance.com/Visual_CPP/DataStructues_Algorithms.html

typedef struct _BSTNode
{
    struct _BSTNode *left;
    struct _BSTNode *right;
    int data;
} BSTNode;
 
BSTNode* NewNode(int data)
{
    BSTNode *pNewNode = new BSTNode();
    pNewNode->left = pNewNode->right = NULL;
    pNewNode->data = data;
    return pNewNode;
}


BSTNode* Insert(BSTNode* node, int data)
{
    if(node == NULL)
        return NewNode(data);
 
    if(data <= node->data)
        node->left = Insert(node->left, data);
    else
        node->right = Insert(node->right, data);
    return node;
}


For more details, visit us at: http://www.softwareandfinance.com/Visual_CPP/DataStructues_Algorithms.html

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1