我在这个二叉树程序中做错了什么

What I am doing wrong in this binary tree program

#include <iostream>

using namespace std;

struct node{
    int data;
    node *left;
    node *right;
};

node *root ;
node *newnode;

void insert( node **root , int value){
    newnode = *root;

    if ( newnode == NULL ){
        newnode = new node;
        newnode->left = NULL;
        newnode->right = NULL;
        newnode->data  = value;
    }
    else if ( value < newnode->data ){
        insert( &newnode->left , value );
    }
    else if ( value > newnode->data  ){
        insert ( &newnode->right , value );
    }
}

int main(){

    node *p = root;
    insert ( &root , 4);
    insert ( &root , 5);
    insert ( &root , 2 );

    cout << root->data << endl;
    //inorder( root );
}

出现这个错误

    [main] C:\Users\abc\Documents\QueueUsingLinkedList.exe 1336 (0)      handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
    [main] QueueUsingLinkedList 1336 (0) handle_exceptions: Dumping stack trace to QueueUsingLinkedList.exe.core
    [Finished in 0.7s]

我正在制作过去一小时的二叉树...我在互联网上看到过程序,但我不知道这个程序有什么问题。

这应该可以解决问题:

node *root = NULL;

*rootnode = newnode;

编辑:

试试这个:

#include <iostream>

using namespace std;

struct node{
    int data;
    node *left;
    node *right;
};


void insert( node **rootnode , int value) {

    node* newnode = *rootnode;

    if ( newnode == NULL ) {
        newnode = new node;
        newnode->left = NULL;
        newnode->right = NULL;
        newnode->data  = value;
        *rootnode = newnode; // this was missing!
    }
    else if ( value < newnode->data ){
        insert( &newnode->left , value );
    }
    else if ( value > newnode->data  ){
        insert ( &newnode->right , value );
    }


}




int main(){

    node* root = NULL;

    insert ( &root , 4);
    insert ( &root , 5);
    insert ( &root , 2 );

    cout << root->data << endl;
}

您永远不会更改 insert()root 的值:

if ( newnode == NULL ){
    newnode = new node;
    newnode->left = NULL;
    newnode->right = NULL;
    newnode->data  = value;
    *root = newnode; // the root is the newly create node...
}

同时将root初始化为NULL

如果您对程序进行更多的结构化处理,有些事情会更清楚:

1 - You should use constructors!

structs 和 classes 都可以定义构造函数,所以你应该使用它们:它们会在长 运行.

struct Node {
  int data;
  Node *left;
  Node *right;
  Node(int data) {
    this->data = data;
    left = NULL;
    right = NULL;
  }
  ~Node() {
    delete left;
    delete right;
  }
};

2 - You should have a Tree class of some sort!

如果你有一棵树 class,你不需要担心传递根 - 如果你定义了正确的行为,class 会为你处理它。 ;)

class BinaryTree {
  public:
    BinaryTree() { root = NULL; }
    ~BinaryTree() { delete root; }
    bool insert(int inserted);
    Node* get_root() const { return root; }
  private:
    Node* root;
};

3 - Your insert should return a bool

对于 insertsearchremove 到 return 和 bool 这样的操作很典型 - 这让调用者知道它是否成功.在这种情况下,我将成功定义为 inserting 具有唯一值的节点。

bool BinaryTree::insert(int inserted) {

    if(!root) {
        root = new Node(inserted);
        return true; // This is a unique value
    }
    else {
        Node *temp = root;

        while(temp) {
            if(inserted > temp->data) {
                if(temp->right)
                    temp = temp->right;
                else {
                    temp->right = new Node(inserted); // This is a unique value
                    return true;
                }
            } else if(inserted < temp->data) {
                if(temp->left) 
                    temp = temp->left;
                else {
                    temp->left = new Node(inserted); // This is a unique value
                    return true;
                }
            } else
                return false; // Not unique
        }
    }
    return false; // Just in case
}

4 - A few minor things

我建议 structs 和 classes 使用大写名称。事实上,传统上使用 UpperCamelCase 来命名它们。变量应该是 lowercasesnake_case - 我对代码做了一些更改。更多款式推荐,请查看the Google Style Guide for C++。真好看。

Here's the ideone. 祝其余的实施顺利。 :)