我写了一个红黑树实现,内置按顺序遍历(使用嵌套class Iterator
)。二叉树:迭代序列打印
我正在寻找一种(迭代的,如果可能的话)算法,它使用按顺序遍历以图形方式打印二叉树。
打印方向是不相关的,即,在命令行输出的树可以被定向(格式化)所示:
2
/\
1 4
/\
3 5
或这样的:
|1
|
|
2
| |3
| |
|4
|
|5
或甚至倒置,但树应使用内部遍历打印,使用以下提供的方法:
void Iteraor::first(); // Traverses to the first node.
void Iterator::next(); // Traverses to the next node.
void Iterator::last(); // Traverses to the last node.
因此有可能因此让这样的事情:
RBTree tree;
/* Tree init. */
Iterator from(&tree), until(&tree);
from.first();
until.last();
for (Iterator i = from; i != until; i.next()) {
// PRINTING.
}
这是原来的代码:
/** A program for Red-Black Tree manipulation: insertion and value retrieval.
* All position relations (first, last, previous, next) are in-order.
*/
class RBTree {
struct Node {
enum class Colour : bool { RED, BLACK };
int value;
Node *left, *right, *parent;
Colour colour;
public:
/* ... */
};
class Iterator {
class Stack {
/* ... */
};
Stack stack;
const RBTree* const tree; // Once set, neither the reference nor the referenced object's attributes can be modified.
Node* pointer;
public:
Iterator(const RBTree*);
void first();
void next();
void last();
/* ... */
Node* getNode() const;
bool operator != (const Iterator&) const;
};
Node *root;
Iterator iterator;
public:
RBTree() : root(nullptr), iterator(this) {}
/* ... */
bool printTree() const;
~RBTree() { deleteTree(); }
};
// TREE // public: //
/* ... */
bool RBTree::printTree() const {
if (root != nullptr) {
// print ??
return true;
}
else
return false;
}
// NODE: Ensures the proper connection. //
void RBTree::Node::setLeft(Node *p_left) {
left = p_left;
if (p_left != nullptr)
p_left->parent = this;
}
void RBTree::Node::setRight(Node *p_right) {
right = p_right;
if (p_right != nullptr)
p_right->parent = this;
}
// ITERATOR //
RBTree::Iterator::Iterator(const RBTree* p_tree) : tree(p_tree), pointer(p_tree->root) {}
// Traverses to the first node (leftmost).
void RBTree::Iterator::first() {
if (pointer != nullptr) {
while (true) {
if (pointer != nullptr) {
stack.push(pointer);
pointer = pointer->left;
}
else {
pointer = stack.peek();
break;
}
}
}
}
// Traverses to next node in-order.
void RBTree::Iterator::next() {
if (pointer != nullptr) {
if (!stack.isEmpty()) {
pointer = stack.pop();
if (pointer->right != nullptr) {
pointer = pointer->right;
first();
}
}
}
}
// Traverses to the last node (rightmost).
void RBTree::Iterator::last() {
pointer = tree->root;
if (pointer != nullptr)
while (pointer->right != nullptr)
pointer = pointer->right;
stack.clear();
}
/* ... */
RBTree::Node* RBTree::Iterator::getNode() const {
return pointer;
}
bool RBTree::Iterator::operator != (const Iterator& p_iterator) const {
return pointer != p_iterator.pointer ? true : false;
}
我已经就读于similar question的答复,但没有的算法,利用在遍历(并且大多数是递归的)。
编辑:
如下因素@ nonsensickle的建议,该代码被裁剪到最低限度。
我不完全确定你的问题是什么。你所做的只是陈述了一些关于你的代码的事实/意见。你要求我们帮助你什么? – nonsensickle
我需要在命令提示符下打印图形二叉树的(可能是迭代的)算法。我会澄清这个问题。 –
好吧,这是更清晰的,但我仍然认为你的问题可以改善一点。特别是,大量的代码需要大量的时间来帮助那些想要帮助的人。将这些代码减少到最低要求将使它更有可能得到回答。请参阅[如何提问](http://stackoverflow.com/help/how-to-ask)和[MCVE](http://stackoverflow.com/help/mcve)。 PS Dobar ti je Engleski ... – nonsensickle