有没有办法访问从最低级别到较高级别(根)的二叉树?
不是从根级到最低级!!!
(而不是使用水平顺序遍历和堆栈...... !!!)<---它的对面..
太难了......谢谢!
这应该遍历BST并删除每个节点,包括根节点.但是,最后,我收到消息"root仍然有一个左节点".为什么不删除所有节点?
void deleteTree()
{
deleteNode(root);
if(root->right)
cout << "root still has a right node" << endl;
if(root->left)
cout << "root still has a left node" << endl;
root = 0;
}
void deleteNode(node *p)
{
if(p->left)
{
deleteNode(p->left);
p->left = 0;
}
if(p->right)
{
deleteNode(p->right);
p->right = 0;
}
cout << "Deleting node containing " << p->data << endl;
delete p;
}
Run Code Online (Sandbox Code Playgroud) 我搜索了论坛,并尝试在我找到的主题中实现代码.但是我从上午10点左右开始研究这个真正简单的程序,并且无法解决这个问题.我生命的错误.
关于我做错的任何想法都将不胜感激.
BST.h(所有的实现问题都应该在这里,并且已经更新了一些,以反映进一步的开发工作,查看历史记录以查看旧版本.)
#ifndef BST_H_
#define BST_H_
#include <stdexcept>
#include <iostream>
#include "btnode.h"
using namespace std;
/*
A class to represent a templated binary search tree.
*/
template <typename T>
class BST
{
private:
//pointer to the root node in the tree
BTNode<T>* root;
public:
//default constructor to make an empty tree
BST();
/*
You have to document these 4 functions
*/
void insert(T value);
bool search(const T& value) const;
bool search(BTNode<T>* node, const T& value) const;
void printInOrder() const; …Run Code Online (Sandbox Code Playgroud) 我需要一个C++的伪代码,它将搜索树以找到值为"z"的节点.
该函数被赋予树的根节点以开始.树具有每个节点最多具有两个子节点的属性.每个节点都有3个属性:left child,right child和value.
我试图使用链表而不是arraylist创建一个完整的二叉树,而不比较节点值.我的意思是插入一个新值,我不希望比较该值是否小于,大于或等于根值,以便将其添加到左侧链接或右侧链接,但仍然能够创建一个完整的二叉树.
你认为这可能吗?如果是的话,你有任何想法,或者你能指出我可以使用/阅读的东西吗?
编辑:
Here's an example to make my question more clear: I am adding the numbers in the order presented through the insert method: 1 2 3 4 5 6 the insert method takes care of the structure of the tree. 1 becomes the root since it's the first value added. 2 is the left child of 1 3 the right child of 1 4 the left child of 2 5 the right child of 2 6 the left …
我需要帮助来理解如何为任何给定的二叉树编写漂亮的打印机.
我知道第一步是预先订购以获取所有节点.
我知道在前序遍历期间,这就是所有美丽将被实现的地方.
只是不知道如何开始.我有一个预订序遍历功能,但我不知道从哪里开始修改它或我应该只是自己的功能.
没有代码,只是询问别人如何去做的想法.
如果我绝望的话可能是后来的代码:P
具体来说,它应该是这样的:

有人可以解释什么是Disjoint Sets Data Structure是什么?或者将我链接到YouTube视频或解释它的文章.
几分钟前我搜索过它,我得到的只是一些数学课程,其中包含一个看起来像维恩图的图像.也许就是这样,但我不确定,所以任何帮助都会受到赞赏.
简而言之,当我被问到"如何使用二叉树来表示二项式队列中的每个二叉树时"这是指你必须相互堆叠的二叉树.就像B1连接B1一样成为B2,那么两个B2成为B3,依此类推.
algorithm binary-tree disjoint-sets data-structures binomial-heap
假设我们有一个无限的,完整的二叉树,其中节点编号为1,2,3,......它们在树的逐层遍历中的位置.给定树中两个节点u和v的索引,我们如何才能有效地找到它们之间的最短路径?
谢谢!
我将通过基础CS原理的速成课程来学习职业生涯杯指南,并停留在计算二叉树的最小/最大深度的示例上。由于这是我遇到的几乎每个示例都存在的相同问题,我认为我会在此处发布问题。
这些说明将实现一种方法,该方法将检查树是否平衡。为此,您需要比较最小深度和最大深度,并确保它们之间的差异不大于1。此原理一目了然。第15行上的方法旨在做到这一点。
但是,我不了解每个辅助方法(maxDepth和minDepth)的return语句中发生了什么。如何从root.left或得出一个数字root.right?是否Math.max功能简单地假设一个1或一个0是有一个值/空节点,或者,由于没有正在值指定(只是Node对象),并Math.max(maxDepth(root.left), maxDepth(root.right)本身等于0,从而通过增加的返回值1,直到两个节点都为空?
如果是这样,则一般过程用于计算树的最小/最大深度:
minDepth =根是否有子代?是= minDepth = 1,否= minDepth = 0(如果有根)
maxDepth =在两个分支之间循环,直到找到离根最远的叶子。保持计数器去确定叶子。
1 public static int maxDepth(TreeNode root) {
2 if (root == null) {
3 return 0;
4 }
5 return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
6 }
7
8 public static int minDepth(TreeNode root) {
9 if (root == null) {
10 return 0; …Run Code Online (Sandbox Code Playgroud) 当我运行析构函数时,我得到一个运行失败,我不知道为什么这是我的树头
class ExpressionTree {
private:
ExpressionNode* root;
public:
ExpressionTree() :
hashmap(100000),
root(NULL) {
};
virtual ~ExpressionTree(){
helper(root);
}
void helper(ExpressionNode *node) {
if ( !node ) {
return;
} else {
helper( node->getLeft( ) );
helper( node->getRight( ) );
delete node;
}
}
};
Run Code Online (Sandbox Code Playgroud)
和我的节点标题
class ExpressionNode {
private:
ExpressionNode* left;
ExpressionNode* right;
string data;
public:
virtual ~ExpressionNode(){
delete left;
delete right;
}
};
Run Code Online (Sandbox Code Playgroud)
现在一切正常,如果在ExpressionTree类中我只会破坏根,但我相信我会以这种方式泄漏内存.这实际上是正确的方式还是我的递归破坏有问题.