原始树的子树
Subtrees of Original Tree
本文关键字:原始 更新时间:2023-10-16
我有问题,当我试图:(1)在原树的根的左子树中找到最大的info字段(2)在原树的根的右子树中找到最小的info字段。
我的代码编译,但它有错误,当它执行,我不清楚发生了什么在我的maxleftsubtree()和minrightsubtree()函数。如有任何建议,不胜感激。
我的当前代码:
#include <iostream>
#include <string>
using namespace std;
class Tnode {
public:
Tnode *left;
string info;
Tnode *right;
Tnode(string info = "", Tnode* left = NULL, Tnode* right = NULL) :
info(info), left(left), right(right) {}
};
class BST {
public:
BST() : theroot(NULL) {}
void insert(string x);
void inorderprint();
void preorderprint();
void postorderprint();
void maxstring();
void minstring();
void maxleftsubtree();
void minrightsubtree();
private:
void inorderprint(Tnode *p);
void preorderprint(Tnode *p);
void postorderprint(Tnode *p);
void maxstring(Tnode *p);
void minstring(Tnode *p);
void maxleftsubtree(Tnode *p);
void minrightsubtree(Tnode *p);
void insertleft(Tnode *place, string newval);
void insertright(Tnode *place, string newval);
Tnode *theroot;
};
// add a new node (with x as info) to tree that has theroot as root
void BST::insert(string x)
{
// if the tree is initially empty, put x at the root
if (theroot==NULL) {
theroot = new Tnode(x);
return;
}
Tnode *p, *q;
// otherwise, find where x belongs in the tree
p = theroot;
q = theroot;
while ( q != NULL) {
p = q;
if (x < p-> info)
q = p-> left;
else
q = p-> right;
}
// to get here, we found the correct place to store x,
// as a child of node p Q: is it left or right?
if (x < p-> info)
insertleft(p,x);
else
insertright(p,x);
return;
}
//insert a new node (with info newval) as left child of place
void BST::insertleft(Tnode *place, string newval)
{
Tnode *p = new Tnode(newval);
place -> left = p;
return;
}
//insert a new node (with info newval) as right child of place
void BST::insertright(Tnode *place, string newval)
{
Tnode *p = new Tnode(newval);
place -> right = p;
return;
}
......................
...............
...............
//
//
void BST::maxleftsubtree()
{
maxleftsubtree(theroot);
}
//
//
void BST::minrightsubtree()
{
minrightsubtree(theroot);
}
.....................................
.................................
.........................
//
//
void BST::maxleftsubtree(Tnode *p)
{
while (p -> left)
p = p -> right;
cout << p -> info << " n";
return;
}
//
//
void BST::minrightsubtree(Tnode *p)
{
while (p -> right)
p = p -> left;
cout << p -> info << " n";
return;
}
您的maxleftsubtree
(maxtrightsubtree
)函数出现错误。首先应该选择根的左(右)子树,沿着右(左)分支走到末尾。以下是修改后的版本:
void BST::maxleftsubtree(Tnode *p)
{
Tnode* left = NULL;
if (p != NULL) {
left = p->left;
}
if (left != NULL) {
while (left->right)
left = left -> right;
cout << left -> info << " n";
}
return;
}
相关文章:
- 将浮动的heightmap数组导出为16位原始值
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 如何将原始字节附加到 std::vector?
- 从堆栈分配的原始指针构造智能指针
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- 将unique_ptr分配给原始指针
- 是否可以从 OpenGL 缓冲区获取原始大小的像素?
- 使用 OpenSSL 从内存中读取原始 SSL/TLS 证书
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- 从原始字节解码协议缓冲区(以 C++为单位)
- C ++如何在原始抽象类中创建一个函数,该函数接受派生类的输入
- C++:尝试使用等效的 STL 算法消除原始循环
- 如何在连接器 C++ 中将原始字节转换为字符串
- 通过引用传递类不会更改原始类
- 实现使用模板化类而不使原始类成为模板的类
- 如何在 2d 矢量 c++ 中复制元素并将其放在原始元素旁边
- 打印原始阵列和更改的阵列
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?