挿入するノードの値と BST のルートを指定して、バイナリ検索ツリーにノードを追加する単純な C++ 関数を実装しようとしています。
驚いたことに、私はどの要素もプッシュできません。ノードを挿入するステートメントがコンパイラによって入力されていることを確認しましたが、ツリーには追加しようとしているノードがありませんでした。問題は、関数の引数でノードを渡す方法にあると思います。誰でも助けることができますか?ありがとうございました。これが私の Node タイプと関数の実装です。
struct Node{
int value;
Node *left;
Node *right;
};
//this method adds a new node with value v to Binary Search Tree
// node is initially the root of the tree
void Push_To_BST(Node* node,int v)
{
// the right place to add the node
if(node==NULL)
{
node=new Node();
node->value= v;
node->left= NULL;
node->right=NULL;
}
//the value to be added is less than or equal the reached node
else if(v <= node->value)
{
//adding the value to the left subtree
Push_To_BST(node->left,v);
}
//the value to be added is greater than the reached node
else if(v > node->value)
{
//adding the value to the right subtree
Push_To_BST(node->right,v);
}
}