C++用于填充树右侧的功能
C++ function to populate right side of a tree
问题 我们为您提供了一个类 TreeNode。这些方法是在 TreeNode.h 中声明的,其中大多数在 TreeNode.cpp 中都有实现。
您的工作: 实现函数 TreeNode *makeFullRight(int n) 构建一个完整的树 n 节点。构建树的方式是,每个双子节点的左子节点将是一个叶子节点。节点的编号应(通过填充其data_字段)从 1 到 n ,根据以下规则:如果节点有数字 我 ,那么任何左边的孩子都会有数字 我 + 1 任何正确的孩子都会有数字 我 + 2 .
把你的代码放在TreeNode.cpp中。
举个例子,makeFullRight(7) 将生成树
1
/
2 3
/
4 5
/
6 7
您应该假设您的makeFullRight函数将被赋予一个奇数作为输入。
您可以使用所需的任何技术来编写此函数,但我们建议使用递归并编写帮助程序函数。
示例运行 您的主.cpp将执行您的代码。
我厌倦了使用递归来做到这一点,但我无法得到答案,现在我对如何使用makeFullRight函数感到迷茫
树节点.cpp- 文件
#include "TreeNode.h"
// Your function here
TreeNode *makeFullRight(int n)
{
}
// Methods and functions we provide following.
// You should not need to edit this code.
TreeNode::TreeNode(int data, TreeNode *left, TreeNode *right) :
data_(data), left_(left), right_(right) {}
TreeNode::~TreeNode() {
if (left_ != NULL)
delete left_;
if (right_ != NULL)
delete right_;
}
bool equal(TreeNode *n1, TreeNode *n2) {
if (n1 == NULL)
return n2 == NULL;
if (n2==NULL)
return false;
return (n1->getData() == n2->getData() &&
equal(n1->getLeft(),n2->getLeft()) &&
equal(n1->getRight(),n2->getRight()));
}
int TreeNode::getData() const {
return data_;
}
TreeNode *TreeNode::getLeft() const {
return left_;
}
TreeNode *TreeNode::getRight() const {
return right_;
}
TreeNode.h--- 新文件
#ifndef _TREENODE_H
#define _TREENODE_H
#include <cstddef>
class TreeNode {
public:
int data_;
TreeNode *left_;
TreeNode *right_;
TreeNode(int data=0, TreeNode *left=NULL, TreeNode *right=NULL);
~TreeNode();
int findMax() const;
int getData() const;
TreeNode *getLeft() const;
TreeNode *getRight() const;
};
// Here is the signature of the code you will write
TreeNode *makeFullRight(int n);
bool equal(TreeNode *n1, TreeNode *n2);
#endif
主文件.cpp ---新建文件
#include <iostream>
#include "TreeNode.h"
using namespace std;
const string RED_TEXT = " 33[1;31m";
const string GREEN_TEXT = " 33[1;32m";
const string RESET_TEXT = " 33[0m";
void print_pass(string message) {
cout<<GREEN_TEXT<<"TEST PASSED"<<RESET_TEXT<<": "<<message<<endl;
}
void print_fail(string message) {
cout<<RED_TEXT<<"TEST FAILED"<<RESET_TEXT<<": "<<message<<endl;
exit(1);
}
int main() {
TreeNode *example =
new TreeNode(1,
new TreeNode(2),
new TreeNode(3,
new TreeNode(4),
new TreeNode(5,
new TreeNode(6),
new TreeNode(7))));
// Try complete
TreeNode *x = makeFullRight(7);
cout << "Result of makeFullRight: " << endl;
if (equal(x,example))
print_pass("");
else
print_fail("");
// Clean up
delete x;
delete example;
}
生成文件---新建文件
EXENAME = main
OBJS = main.o TreeNode.o
CXX = clang++
CXXFLAGS = -std=c++0x -c -g -O0 -Wall -Wextra
LD = clang++
LDFLAGS = -std=c++0x
all: $(EXENAME)
$(EXENAME): $(OBJS)
$(LD) $^ $(LDFLAGS) -o $@
main.o: main.cpp
$(CXX) $< $(CXXFLAGS)
TreeNode.o: TreeNode.cpp TreeNode.h
$(CXX) $< $(CXXFLAGS)
clean:
-rm -f *.o $(EXENAME)
如果您检查树,您将看到每个子树的根中有k
,左侧节点中有具有k+1
的叶子,右侧节点中根中有k+2
的右完整树。
假设您有建议的帮助程序函数 - 我们称之为"fullHelper" - 跟踪"当前值"并生成一个在其根中的树:
// Produce a right-full tree with 'current' at the root, up until 'n'.
TreeNode* fullHelper(int n, int current);
然后你可以写
TreeNode* makeFullRight(int n)
{
return fullHelper(n, 1);
}
现在,您需要的"全部"是编写帮助程序函数。
您需要一个基本情况和一个递归案例:
- 如果
n == current
,则创建具有n
的单个节点, - 否则,创建一个树,其中根中有
current
,current+1
在左叶中,并将根current+2
的右满树作为其右子树。
翻译成C++:
TreeNode* fullHelper(int n, int current)
{
return n == current
? new TreeNode(current)
: new TreeNode(current,
new TreeNode(current+1),
fullHelper(n, current+2));
}
相关文章:
- 在用于格式4的arm模拟器中实现功能时的一个问题
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 删除功能不适用于串行通信后多个循环中的多个实例
- 不正确的比较和交换计数器输出用于快速排序功能
- 私有成员数据不可用于公共成员功能
- 如何编写用于多映射插入和擦除功能的 API?
- '_HAS_CXX17'宏是否可用于自定义项目标头以启用C++17 语言集功能?
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- C++中用于结构的纯数组的类似于TableView/DataFrame的通用功能
- C++用于填充树右侧的功能
- 用于检查我们是否有有效数独的辅助功能
- 如何实现通用 C++ 用于配置功能?
- "no main"用于C++中链接或执行的功能
- 如何使用 Turbo C++并行运行两个功能,一个用于键盘,一个用于鼠标?
- 维护/维持两个代码集的风险,一个用于 CPU,一个用于 GPU,需要执行非常相似的功能
- 是否有用于执行反向字符串拆分器的开箱即用功能?
- 二叉树插入功能不适用于 10 个条目
- STL功能用于确定距离是否在n`中
- 我可以将 AVX/SSE 与旋转功能用于 AoS 布局,而不是 SoA 吗?
- 用户定义的功能用于竞争编程中,以获取输入