【问题标题】:push_back() binary tree into vectorpush_back() 二叉树成向量
【发布时间】:2018-04-22 20:36:57
【问题描述】:

我正在尝试将二叉搜索树中的所有元素按顺序放入一个向量中。这是函数:

编辑:为清楚起见添加说明。 编写一个类来实现一个能够存储数字的简单二叉搜索树。类应该有成员函数:

void insert(double x)
bool search(double x)
void inorder(vector <double> & v)

插入函数不应直接或通过调用递归函数间接使用递归。搜索功能应该通过调用私有递归成员函数来工作

bool search(double x, <double> & v )

中序函数被传递一个初始为空的向量v; if 用存储在二叉搜索树中的数字的有序列表填充 v。使用合适的驱动程序演示类的操作。

编辑:为清楚起见添加了完整代码。

#include "stdafx.h"
#include <iostream>
#include <vector>

class BinaryTree {

private:
struct TreeNode {

    double value;
    TreeNode *left;
    TreeNode *right;
    TreeNode(double value1,
        TreeNode *left1 = nullptr,
        TreeNode *right1 = nullptr) {

        value = value1;
        left = left1;
        right = right1;
    }
};

TreeNode *root;    //pointer to the root of the tree
bool search(double x, TreeNode *t) {

    while (t) {
        std::cout << "running through t." << std::endl;
        if (t->value == x) {
            return true;
        }
        else if (x < t->value) {
            std::cout << "wasn't found, moving left." << std::endl;
            search(x, t->left);
        }
        else {
            std::cout << "wasn't found, moving right." << std::endl;
            search(x, t->right);
        }
    }
    std::cout << "wasn't found." << std::endl;
    return false;
}


public:

std::vector<TreeNode> v;

BinaryTree() {
    root = nullptr;
}

void insert(double x) {
    TreeNode *tree = root;

    if (!tree) {
        std::cout << "Creating tree." << x << std::endl;
        root = new TreeNode(x);
        return;
    }

    while (tree) {

        std::cout << "Adding next value." << std::endl;
        if (tree->value == x) return;

        if (x < tree->value) {
            tree = tree->left;
            tree->value = x;
        }
        else {
            tree = tree->right;
            tree->value = x;
        }
    }

}
bool search(double x) {

    return search(x, root);
}

void inOrder(std::vector <double> & v) {

    {
        if (left)
            left->inOrder(v);
        v.push_back(value);
        if (right)
            right->inOrder(v);
    }
}
TreeNode* left = nullptr;
TreeNode* right = nullptr;
double value;
};

int main() {

    BinaryTree t;

    std::cout << "Inserting the numbers 5, 8, 3, 12, and 9." << std::endl;
    t.insert(5);
    t.insert(8);
    t.insert(3);
    t.insert(12);
    t.insert(9);

    std::cout << "Looking for 12 in tree." << std::endl;
    if (t.search(12)) {
        std::cout << "12 was found." << std::endl;
    }

    std::cout << "Here are the numbers in order." << std::endl;


    return 0;
}

我无法获取要推入向量的值。关于如何实现这一点的任何想法?

【问题讨论】:

    标签: c++11 vector binary-tree inorder


    【解决方案1】:

    您通常会递归地执行此操作:

    #include <vector>    
    
    class TreeNode {
        void inOrder(std::vector<double>& v) const
        {
            if (left)
                left->inOrder(v);
            v.push_back(value);
            if (right)
                right->inOrder(v);
        }
    
        TreeNode* left = nullptr;
        TreeNode* right = nullptr;
        double value;
    };
    

    编辑:添加了#include

    Edit2:我就是这样做的。如有任何问题,请随时提出:

    #include <iostream>
    #include <vector>
    
    class BinaryTree {
    private:
        struct TreeNode {
    
            double value;
            TreeNode *left = nullptr;
            TreeNode *right = nullptr;
    
            TreeNode(double value1)
                : value(value1)
            {}
    
            void inOrder(std::vector <double> & v) {
                if (left)
                    left->inOrder(v);
                v.push_back(value);
                if (right)
                    right->inOrder(v);
            }
        };
    
        TreeNode *root = nullptr;    //pointer to the root of the tree
    
        bool search(double x, TreeNode *t) {
            while (t) {
                std::cout << "running through t." << std::endl;
                if (t->value == x) {
                    return true;
                }
                else if (x < t->value) {
                    std::cout << "wasn't found, moving left." << std::endl;
                    return search(x, t->left);
                }
                else {
                    std::cout << "wasn't found, moving right." << std::endl;
                    return search(x, t->right);
                }
            }
            std::cout << "wasn't found." << std::endl;
            return false;
        }
    public:
    
    
        BinaryTree() {}
    
        void insert(double x) {
            TreeNode *tree = root;
    
            if (!tree) {
                std::cout << "Creating tree." << x << std::endl;
                root = new TreeNode(x);
                return;
            }
    
            while (tree) {
                std::cout << "Adding next value." << std::endl;
                if (tree->value == x) return;
    
                if (x < tree->value) {
                    if (!tree->left)
                    {
                        tree->left = new TreeNode(x);
                        return;
                    }
                    tree = tree->left;
                }
                else {
                    if (!tree->right)
                    {
                        tree->right = new TreeNode(x);
                        return;
                    }
                    tree = tree->right;
                }
            }
        }
        bool search(double x) {
            return search(x, root);
        }
        void inOrder(std::vector<double>& v)
        {
            root->inOrder(v);
        }
    };
    
    
    
    int main() {
    
        BinaryTree t;
    
        std::cout << "Inserting the numbers 5, 8, 3, 12, and 9." << std::endl;
        t.insert(5);
        t.insert(8);
        t.insert(3);
        t.insert(12);
        t.insert(9);
    
        std::cout << "Looking for 12 in tree." << std::endl;
        if (t.search(12)) {
            std::cout << "12 was found." << std::endl;
        }
    
        std::cout << "Here are the numbers in order." << std::endl;
        std::vector<double> v;
    
        t.inOrder(v);
        std::cout << "values in order:";
        for (double val : v)
        {
            std::cout << " " << val;
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    【讨论】:

    • 我最初尝试了它,但无法弄清楚如何让我只有一个参数参数。为了清楚说明说明,我已经编辑了帖子。
    • 由于 C++ 区分大小写,请检查是否所有出现的 'inOrder' 都用大写 O 编写。
    • 我做了,我已经添加了完整的代码。我不得不在没有 inOrder() 函数的情况下上交作业,但仍然想知道我做错了什么。
    • 那么为什么它可以作为私人会员而不是公共会员呢?
    • 有两个函数名为 inOrder。 TreeNode (BinaryTree::TreeNode::inOrder) 中的一个是私有的,而 BinaryTree (BinaryTree::inOrder) 中的另一个是公共的。后者在测试代码中被调用。
    猜你喜欢
    • 2017-03-24
    • 2018-10-15
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多