为什么当打印BST的预购遍历时,我的程序什么都不做
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("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);
}
假设调用预购操作,它应该按该顺序打印树,但程序只是终止了,没有打印任何内容,我坚持了下来,请帮助我,并原谅我的英语
我认为您的问题是没有任何东西阻碍程序的退出。。。只需使用cin
int x;
cin >> x;
return 0;
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 我的评分程序无法正常工作
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 反向功能超出了我的 cpp 程序的范围
- 我的二进制搜索程序只是关闭了
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 为什么我的两个 cin 语句没有在程序结束时运行?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- C++机器人程序(我的私人服务器)出现问题
- 用C++创建一个写作应用程序(我的第一个项目)(学习C++)