二叉树:迭代中序打印

Binary Tree: iterative inorder print

我写了一个红黑树实现,内置了中序遍历(使用嵌套class Iterator)。

我正在寻找一种(迭代,如果可能的话)算法,它使用中序遍历以图形方式打印二叉树。

打印方向不相关,即命令行输出中的树可以像这样定向(格式化):

    2
   / \
  1   4
     / \
    3   5

或者像这样:

 |1
 |
 |
2
 | |3
 | |
 |4
   |
   |5

甚至是颠倒的,但是应该使用 in-oder 遍历打印树,使用下面提供的方法:

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的响应,但是none的算法利用了中序遍历(而且大部分都是递归的) ).

编辑:

按照@nonsensickle 的建议,代码被削减到最低限度。

使用迭代算法进行in-order遍历的规范方法是维护需要打印的节点的堆栈(或后进先出队列)。每个循环迭代做两件事之一:

  1. 如果你不在叶子上,将当前节点压入栈中并移动到最左边child。

  2. 如果你在叶子上,打印它,从堆栈中弹出顶部节点,打印它,然后移动到它最右边的 child.

你继续,直到你的堆栈为空并且你在一片叶子上。

节点间分支的格式和图形表示的生成显然取决于您。请记住,它需要一些额外的状态变量。

编辑

我说的"some extra state variables"是这个意思

要提供 pretty-printing,您需要跟踪三件事:

  1. 您当前的node-to-print在树的第几层(从底部数起)。这会告诉您(部分)缩进多远(或者从 canvas 的边缘偏移它,如果您使用的是 2D 绘图库)。

  2. 你现在的 node-to-print 是左派还是 right-child。这会(再次)告诉您将它从其兄弟缩进多远,以及将它与其 parent.

  3. 连接的分支的方向
  4. 你的节点距离"center"多少个节点。这对于与其 (non-sibling) 个邻居的适当间距也很有用。

也许可以用更少的 iteration-to-iteration 状态,但这对我有用。