Author Topic: How to insert a node in a binary tree?  (Read 1357 times)

kcreddy90

  • Newbie
  • *
  • Posts: 3
How to insert a node in a binary tree?
« on: June 14, 2011, 03:46:34 pm »
How to insert a node in a binary tree?
« Last Edit: June 14, 2011, 03:47:24 pm by Adminstrator »

administrator

  • Administrator
  • Full Member
  • *****
  • Posts: 69
Re: How to insert a node in a binary tree?
« Reply #1 on: June 14, 2011, 03:47:12 pm »
How to insert a node in a binary tree?

It is already posted, look at the link below:

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