C++ 二叉搜索树C++;

C++ 二叉搜索树C++;,c++,data-structures,linked-list,nodes,binary-search-tree,C++,Data Structures,Linked List,Nodes,Binary Search Tree,我试图实现一个简单的二叉搜索树来进行实践。我尝试只添加值并打印节点中的值。但是,我没有在节点中获得正确的值升序。以下是我所拥有的: struct Node { int data; Node* leftN; Node* rightN; }; typedef Node* Node_ptr; Node_ptr head; //INSERT_VALUE FUNCTION Node* insert_value(Node_ptr leaf, int key) { //R

我试图实现一个简单的二叉搜索树来进行实践。我尝试只添加值并打印节点中的值。但是,我没有在节点中获得正确的值升序。以下是我所拥有的:

struct Node
{
    int data;
    Node* leftN;
    Node* rightN;

};

typedef Node* Node_ptr;
Node_ptr head;

//INSERT_VALUE FUNCTION
Node* insert_value(Node_ptr leaf, int key)
{
    //Root case when there is no set value yet  
    if(leaf == NULL)
    {
        leaf = new Node;
        head = leaf;
        cout << "Make the first node" << endl;
        leaf->data = key;
        leaf->leftN = NULL;
        leaf->rightN = NULL;
        return leaf;
    }   
    //Left child Node
    if(key < leaf->data)
    {
        //Search for a spot in the tree to add a Node (left value < root value < right value)
        //This is only true for the left child Node
        if(leaf->leftN != NULL)
            insert_value(leaf, key);
        //We have found a spot in the tree to add a new Node and add the value of key
        else 
        {
            cout << "Insert-left" << endl;
            leaf->leftN = new Node;
            leaf = leaf->leftN;
            leaf->data = key;
            leaf->leftN = NULL;
            leaf->rightN = NULL;
            return leaf;
        }
    }

    //Right child Node
    else if(key >= leaf->data)
    {
        //Search for a spot to add a new Node in the tree (only amongst the right child Nodes)
        if(leaf->rightN != NULL)
            insert_value(leaf, key);    
        //Once we have found a spot to add a new Node, append the new Node
        else
        {
            cout << "Insert-right" << endl;
            leaf->rightN = new Node;
            leaf = leaf->rightN;    
            leaf->data = key;
            leaf->leftN = NULL;
            leaf->rightN = NULL;
            return leaf;
        }
    }   
}

//PRINT FUNCTION
void printTree(Node_ptr leaf)
{
    if(leaf == NULL)
        return;
    printTree(leaf->leftN);
    cout << "Data element: " << leaf->data << endl;
    printTree(leaf->rightN);
}

//MAIN
int main()
{
    Node_ptr root = NULL;
    int i;

    //initialize values
    for(i = 1; i < 12; i+=2)
        root = insert_value(root, i);
    root = head;
    for(i = 0; i < 11; i+=2)
        root = insert_value(root, i);
    root = head;
    printTree(root);

    root = head;
    cout << "Head Node: " << root->data << endl;

    return 0;
}
struct节点
{
int数据;
节点*leftN;
节点*right;
};
typedef节点*Node_ptr;
节点头;
//插入值函数
节点*插入值(节点ptr叶,int键)
{
//没有设置值时的根事例
if(leaf==NULL)
{
叶=新节点;
头=叶;
cout-leftN=NULL;
leaf->rightN=NULL;
回叶;
}   
//左子节点
如果(键data)
{
//在树中搜索点以添加节点(左值<根值<右值)
//这仅适用于左侧子节点
如果(叶->左!=NULL)
插入_值(叶、键);
//我们在树中找到了一个点来添加一个新节点并添加key的值
其他的
{
不能左;
叶->数据=键;
leaf->leftN=NULL;
leaf->rightN=NULL;
回叶;
}
}
//右子节点
否则如果(键>=叶->数据)
{
//搜索要在树中添加新节点的点(仅在右侧子节点中)
如果(叶->右!=NULL)
插入_值(叶、键);
//找到添加新节点的位置后,添加新节点
其他的
{
不能正确;
叶->数据=键;
leaf->leftN=NULL;
leaf->rightN=NULL;
回叶;
}
}   
}
//打印功能
无效打印树(节点\u ptr叶)
{
if(leaf==NULL)
返回;
打印树(叶->左);

cout在leaf->node?!=NULL的情况下,我认为不需要调用

insert_value(leaf, key);
你想说什么

leaf->node? = insert_value(leaf->node?, key)
哪里?当然是L或R

您可能会考虑向方法添加注释,例如:

// Adds the given key to the (sub-)tree rooted at node* then returns the new root
// of that (sub-)tree.
node *insert_value_and_return_root(node *root, int value) { ... }

因为您将插入调用为:

    root = insert_value(root, i);
插入位置始终使用从上次插入开始的子树。除了重新开始添加奇数时,当您开始在头部插入时

如果创建一个包含头指针的
类BinarySearchTree
,以及一个调用
节点::插入(头,值)的采用
int值的插入方法
,则您可以在该类上调用insert,而不向其传递节点,并且它始终可以确保插入使用树的根作为递归的开始


只有我,但我会有一个节点构造函数,它接受一个
int
并将指针初始化为NULL。这样,您就不必在insert方法中这样做了。

Re:
typedef Node*Node\u ptr
。请注意,
Node\u ptr
Node*
更多的是击键,并且仍然设法包含不重要的whiteespace.)