为什么在打印 BST 的前序遍历时我的程序不执行任何操作

发布于 2024-12-27 02:56:28 字数 2541 浏览 2 评论 0原文

我正在尝试编写一个代码,允许我输入一个树节点,然后指示其先序遍历,但不明白会发生什么。我做错了什么?

这是我的代码:

#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("\n");



    return 0;
}

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

假设调用预购操作,它应该按该顺序打印树,但程序只是终止并且不打印任何内容,我坚持这个,请帮助我,请原谅我的英语

I'm trying to make a code that allows me to enter a tree node and then indicate its preorder traversal, but do not understand what happens. What am I doing wrong?

This is my code:

#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("\n");



    return 0;
}

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

It is assumed that calling preorder action, it should print the tree in that order but the program simply terminates and does not print anything, im stuck with this please help me, and excuse my English

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2025-01-03 02:56:28

我认为你的问题是你没有任何东西阻止程序的退出......只需使用 cin

int x;
cin >> x;

return 0;

I think your issue is that you don't have anything stalling the exiting of the program...just use cin

int x;
cin >> x;

return 0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文