简体   繁体   English

递归插入二叉树(geeksforgeeks)

[英]Inserting into a Binary Tree (geeksforgeeks) recursively

I'm trying to implement the insertion function used on geeksforgeeks.com but am running into some problems trying to work it into my current code.我正在尝试实现在 geeksforgeeks.com 上使用的插入 function 但在尝试将其应用于我当前的代码时遇到了一些问题。

I have a vector with the data I need to put into the binary tree.我有一个向量,其中包含我需要放入二叉树的数据。 I use this function to pass the numbers into the insertion function:我使用这个 function 将数字传递到插入 function 中:

void populateTree(vector<string> dataVec) {
    for (int i = 0; i < dataVec.size(); i++) {
        insert(stoi(dataVec[i]), root);
    }
}

This is the insertion function:这是插入 function:

node* insert(int x, node* node) {

    if (node == nullptr)
        return newNode(x);
    if (x < node->data)
        node->left = insert(x, node->left);
    else
        node->right = insert(x, node->right);

    return root;
}

New node function:新节点 function:

node* newNode(int num) {

node* temp = new node;
temp->data = num;
temp->left = temp->right = nullptr;
temp->level = 1;
return temp;

} }

Root is a private member within the class which is initialized to nullptr. Root 是 class 中的私有成员,初始化为 nullptr。 I'm not sure how I should go about making the first node that comes in from the vector as the root and then keep inserting things beginning from there recursively.我不确定我应该如何 go 将来自向量的第一个节点作为根,然后递归地从那里开始插入东西。 Thanks!谢谢!

The problem in your is related to use of pointer.您的问题与指针的使用有关。

Instead of using node* insert(int x, node* node) you should use node* insert(int x, node** node) or node* insert(int x, node*& node) and adopt your code accordingly.而不是使用node* insert(int x, node* node)您应该使用node* insert(int x, node** node)node* insert(int x, node*& node)并相应地采用您的代码。

Following is corrected sample code.以下是更正的示例代码。 See it in execution here :在此处查看它的执行情况

#include <iostream>
#include <vector>
using namespace std;

struct Node
{
    int val;
    Node* left;
    Node* right;

    Node(int v)
    {
        val = v;
        left = right = nullptr;
    }
};


class Tree
{
    Node* root;

    Tree()
    {
        root = nullptr;
    }

    public:

    static void insert(int x, Node*& node)
    {
        if (node == nullptr)
        {
            node = new Node(x);
        }
        else
        {
            if (x < node->val)
                insert(x, node->left);
            else
                insert(x, node->right);
        }
    }

    static Tree* populateTree(vector<string> dataVec)
    {
        Tree* t= new Tree();
        for (int i = 0; i < dataVec.size(); i++)
        {
            insert(stoi(dataVec[i]), t->root);
        }
        return t;
    }

    static void printTree(Node* node, string s)
    {
        if(node == nullptr) return;
        cout<<s<< "+"<<node->val <<endl;
        s += "----";
        printTree(node->left,s);
        printTree(node->right, s);
    }

    static void printTree(Tree* t)
    {
        if(t)
        {
            printTree(t->root, "");
        }
    }
};

int main() {
    Tree* t = Tree::populateTree({"70", "2", "7", "20", "41", "28", "20", "51", "91"});
    Tree::printTree(t);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM