English 中文(简体)
我的节目在印刷《服贸总协定》的序曲时,为什么没有
原标题:Why when printing the preorder traversal of a BST my program does nothing

我试图制定一部法典,允许我进入树苗,然后表明其先令的渗透,但并不理解发生什么。 我做了什么错误?

这是我的法典:

#include <stdio.h>
#include<iostream>
#include<queue>
#define MAX 100
#include <vector>
#include <stdlib.h>

typedef struct node {
    struct node *parent;
    struct node *left;
    struct node *right;

    int value;
} Node;

typedef struct tree {
    Node *root;
} Tree;

void findPlace(Node *_toAdd, Node *_current) {
    // set this node to the parent so we dont have to do it later (may change next recursion)
    _toAdd->parent = _current;

    // compare value of node we re adding to current node
    if(_toAdd->value < _current->value)
        // add the element to the left subtree
        if(_current->left == NULL)          // if no left subtree yet, add it right here
            _current->left = _toAdd;
        else                                        // otherwise continue recursion to left subtree
            findPlace(_toAdd, _current->left);
    else
        // add the element to the right subtree
        if(_current->right == NULL)     // if no right subtree yet, add it right here
            _current->right = _toAdd;
        else                                        // otherwise, continue recursing to the right subtree
            findPlace(_toAdd, _current->right);
}

Node *addNode(int _val, Tree* _tree) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->value = _val;

    // if the tree is empty, this is the new root
    if(_tree->root == NULL)
        _tree->root = newNode;

    // otherwise, we need to find the right place to put it
    else
        findPlace(newNode, _tree->root);

    // return reference to the node
    return newNode;
}


void preOrder(Node *);


int main() {
    // create a tree to fool around with
    Tree *myTree = (Tree *)malloc(sizeof(Tree));

    // Add some values to the tree
    addNode(7, myTree);
    addNode(3, myTree);
    addNode(7, myTree);
    addNode(11, myTree);
    addNode(6, myTree);
    addNode(8, myTree);
    addNode(12, myTree);
    addNode(0, myTree);
    addNode(2, myTree);
    addNode(9, myTree);

    printf("Pre-Order Traversal: "); preOrder(myTree->root); printf("
");



    return 0;
}

void preOrder(Node *_root)
{
    printf("%d ", _root->value);
    if(_root->left != NULL)
        preOrder(_root->left);
    if(_root->right != NULL)
        preOrder(_root->right);
}

假设要求采取先令行动,则按此顺序印刷树木,但该方案只是终止,不印刷任何东西,因此,请帮助我,并免除我的英文。

最佳回答

我认为,你的问题是,你没有拖延方案退出。

int x;
cin >> x;

return 0;
问题回答

暂无回答




相关问题
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?

热门标签