在二叉树中插入值的函数?

Function for inserting a value in binary tree?

我有一个函数 insert 用于将值插入二叉树。 但是当我注销 value 时,什么也没有显示。

我知道使用成员函数插入。

根节点的值没有被更新?

有人能告诉我哪里错了吗?

#include <iostream>

using namespace std;

class Node{

 public:
     int value;
     Node* left;
     Node* right;

     Node();

     Node(int data){
     value = data;
     left = NULL;
     right = NULL; 
    } 


};

void insert(Node* root , int val){
     if(root == NULL){
         root = new Node(val);
         return;
     }
    if(root->value > val)
         insert(root->left,val);
     else
        insert(root->right,val);


} 

int main()
   class Node* root  = NULL;
   insert(root,5);
   cout<<root->value;
 }

您在正确的位置插入了位置,但问题是您没有将新插入的节点的 link 创建到它的父节点。

你可以查看作为参考!