English 中文(简体)
2. 原始树木在无再生的情况下从卷宗中创造 C++
原标题:Binary tree creation from file without recursions C++

For starters, I would like to apologize because I didn t know how to word the title appropriately. Here is the issue that I have. I m having an issue while creating a tree from a file. The file looks like this:

0 1 3 4
0 1 4
0 2 3 3 4
0 2 3 4 

我需要创造的树像是这样:

        0
      /   
     1     2
    /     |
   3   4   3
   |      / 
   4     3   4
         |
         4

我用言语解释,在档案的每一行中,我都获得了新的数据集,我应 create树,但如果存在顺序,我就应该遵循这一顺序,直到数据出现中断。

在创造树木时,我使用这种方法来检查是否存在顺序:

void Tree::addNewStack(const string& line) {
istringstream stack(line);
string token;

Node* currentNode = nullptr;

while (stack >> token) {
    Node* newNode = new Node(token);

    if (root == nullptr) {
        root = newNode;
    }
    else {
        // Checking for existing node.
        Node* existingNode = nullptr;
        currentNode = root;

        while (existingNode == nullptr && currentNode != nullptr) {
            if (currentNode->left != nullptr && current->left->data == token) {
                existingNode = currentNode->left;
            }
            else if (currentNode->right != nullptr && current->right->data == token) {
                existingNode = currentNode->right;
            }
            // ****
        }
        // If there is an existingNode move to it, otherwise make a new node.
        if (existingNode != nullptr) {
            currentNode = existingNode;
            currentNode = newNode;
        }
    else {
                currentNode = newNode;
            }
        }
    }
}

我尝试优先放电,然后使用先令的 trav子树,但这似乎更为复杂。

在星号评论的空档中,我可以说明如何通过整整条通道进行搜索。 我需要找到一种途径,更新目前的诺德,以便我通过整个树.。 我用目前的Node = 目前的Node->left,但仅对树的一方进行制衡。 另外,还试图在休息期间进行2次检查,一次检查左边,一次检查右边,但那次检查工作。 任何建议?

最佳回答

There is only 1 root in your tree.
The token corresponding to the root is indeed repeated several times in the file (once for each line) but judging from the parameter being named const string& line, the method will only have to treat it once for each call. It is not mentioned in your question but I have to assume in my answer that another method builds the line strings out of the file in a loop and pass them to Tree::addNewStack one by one.

由于目前编号只需要在每个电话中首次采用,你应处理<>strong>(前)。 <代码>while 选择不在此限(这应当使我们在进入 lo之前先入住。)

其余部分应易于按照您的问题为双向树木(而不是双向search树)流动;我们将在<条码>前填写

stack >> token;
if (!root)
    root = new Node(token);
else if (root->data != token)
    throw "The root specified in file does not match the root of the tree";
Node* currentNode = root;

while (stack >> token) {
    if (!currentNode->left)
        currentNode = currentNode->left = new Node(token);
    else if (currentNode->left->data == token)
        currentNode = currentNode->left;
    else if (!currentNode->right)
        currentNode = currentNode->right = new Node(token);
    else if (currentNode->right->data == token)
        currentNode = currentNode->right;
    else
        throw "Path is incompatible with tree";
}    

如果你想造一个双向搜索树,你需要增加一层逻辑,以便处理:

  • if (token <= currentNode->data) -> Check only the left branch.
  • else -> Check only the right branch.

同样,贵方的树木并不是一种生物浓缩系数,因为<代码>1下的2个节点大于1。 因此,您的档案应当对文件第2行的编号4提出例外(关于编号<1>>>,目前不适用>right 为alreay, 代码>3。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签