程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了BST树/C++的void insert(int)方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决BST树/C++的void insert(int)方法?

开发过程中遇到BST树/C++的void insert(int)方法的问题如何解决?下面主要结合日常开发的经验,给出你关于BST树/C++的void insert(int)方法的解决方法建议,希望对你解决BST树/C++的void insert(int)方法有所启发或帮助;

我一直在努力让这个功能工作最长时间。它是在线课程作业的一部分,但似乎无论我提交什么,该函数对于空子测试和左子测试都失败。请参阅下面的代码。 main() 函数被故意注释掉。任何信息。/输入非常感谢。

// C++ binary trees and stuff;
//
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
class BST
{
    public:
        int data;
        BST *left;
        BST *right;
        //BST *root;
        // BST() constructor
        BST (int num)
        {
            data = num;
            left = nullptr;
            right = nullptr;
            root = nullptr;
        }
        // constructors for root node(s),initializing as root when no values exist yet;
        BST() : root (nullptr){}
        BST (BST *rootNode) : root(rootNode){}
        voID insert (int value)
        {
            BST *newNode = new BST();
            newNode = root;
            if (root == nullptr)
            {
                root = new BST (value);
            }
            else
            {
                root->data = value;
            }
            // check if newNode's value equals the passed-in value:
            if (value == root->data)
            {
                //cout << "\nWarning!  Value already exists in tree,so nothing will be done.\n";
                return;
            }
            // check if value is < or > newNode's value:
            if (value <= root->data)
            {
                if (root->left == nullptr)
                {
                    // make a new node as the left child of this node,root->left = new BST(value);
                }
                else
                {
                    // recursively call insert() on tree's left sIDe,root->left->insert(value);
                }
            }
            else
            {
                if (root->right == nullptr)
                {
                    // make a new node as the right child of this node,root->right = new BST(value);
                }
                else
                {
                    // recursively call insert() on tree's right sIDe,root->right->insert(value);
                }
            }
        }
    public:
        BST *root;
};
/*
int main (int argc,char *argv[])
{
    //...insert code here,// create nodes,...
    
    BST rootNode(5);
    BST leftNode(4);
    BST rightNode(6);
    // connect the nodes to the tree via rootNode.left and rootNode.right,..
    
    rootNode.left = &leftNode;
    rootNode.right = &rightNode;
    printf ("\nData (root) value = %i,rootNode.left = %i,and rootNode.right = %i\n",rootNode.data,rootNode.left->data,rootNode.right->data);
    cout << "\n\nHello,Solar System!\n";
    return 0;
}
*/

解决方法

好的,这是我的建议。您需要重新格式化您的代码。你需要两节课。你需要一个 BST,你需要一个 Node。添加/删除/遍历的各种方法是 BST 类的一部分。节点只是节点。

所以:

class BST_Node {
public:
    int value;
    BST_Node * left = nullptr;
    BST_Node * right = nullptr;

    // Define constructors,etc.
};

class BST {
public:
     BST_Node * root = nullptr;

     BST_Node * insert(int value);
     void insertNode(BST_Node *node);
     void insertNodeBelow(BST_Node *nodeToInsert,BST_Node *startingNode);
};

BST_Node * BST::insert(int value) {
    BST_Node * node = new BST_Node(value);
    insertNode(node);
    return node;
}

void BST::insertNode(BST_Node *node) {
    if (node == nullptr) {
        return;
    }
    if (root == nullptr) {
        root = node;
    }
    else {
        insertNodeBelow(node,root);
    }
}

void BST::insertNodeBelow(BST_Node *node,BST_Node *startingNode) {
    if (node == nullptr || startingNode == nullptr) {
        return;
    }
    if (node->value < startingNode->value) {
        if (startingNode->left != nullptr) {
            insertNodeBelow(node,startingNode->left);
        }
        else {
            startingNode->left = node;
        }
    }
    else {
        if (startingNode->right != nullptr) {
            insertNodeBelow(node,startingNode->right);
        }
        else {
            startingNode->right = node;
        }
    }
}

这是如何工作的... 首先,如何存储节点的逻辑在 BST 中。节点不在乎。其次,我创建了插入值或节点的方法。因为我觉得这很方便。这应该很容易理解。

根节点可以为空,如果是这样,那么你插入的节点现在是根节点。否则它调用递归插入函数。现在,您可以稍微简化一下,但我不想太聪明。

所以很简单。我们查看它相对于我们所处的点(最初是根)属于哪里。我们要么进入左分支,要么进入右分支。但是那个分支可能是空的,所以你直接把它放进去。如果它不是空的,那么你递归。

我没有测试过。

大佬总结

以上是大佬教程为你收集整理的BST树/C++的void insert(int)方法全部内容,希望文章能够帮你解决BST树/C++的void insert(int)方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: