C++用于填充树右侧的功能

C++ function to populate right side of a tree

本文关键字:功能 用于 填充 C++      更新时间:2023-10-16

问题 我们为您提供了一个类 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的单个节点,
  • 否则,创建一个树,其中根中有currentcurrent+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));
}