繁体   English   中英

二叉树和特殊节点打印

[英]Binary tree and especial node printing

我有一棵这样的树,用二进制表示;

                 1
                /
               2
                \
                 3
                / \
               6   4
                \   \
                 7   5
                    /
                   8
                  /
                 9
                  \
                   10
                     \
                      11

但实际上这不是二叉树,而是

     1
  / | | \
 2  3 4 5
    /\  |
    6 7 8
       /| \
      9 10 11

您能帮我打印出类似的内容吗(子级打印顺序相反)

1 : 5 4 3 2
5 : 8
3 : 7 6
8 : 11 10 9

我的TNode类看起来像。

class TNode {
public:
    unsigned long int data;
    TNode * left;///first child
    TNode * right;/// sibling
    TNode * parent;/// parent

    TNode(unsigned long int d = 0, TNode * p = NULL, TNode * n = NULL)///konstruktors
    {
        left = NULL;
        right = NULL;
        parent = p;
        data = d;
    }
};

这需要堆栈实现吗?

尝试类似的方法:(尚未测试)

printTree(TNode root) {
    queue <TNode> proc;
    proc.push(root);
    while (!proc.empty()) {
        TNode front = proc.front();
        proc.pop();

        // add front's children to a stack
        stack <Tnode> temp;
        Tnode current = front->left;
        while (current != NULL) {
            temp.push(current);
            current = current->right;
        }
        // add the children to the back of queue in reverse order using the stack.
        // at the same time, print.
        printf ("%lld:", front->data);
        while (!temp.empty()) {
            proc.push(temp.top());
            printf(" %lld", temp.top()->data);
            temp.pop();
        }
        printf("\n");
    }

}

我仍在尝试使其更优雅。 感谢您提出有趣的问题!

编辑:将格式说明符更改为lld

这种方法怎么样:以预定顺序遍历树(访问每个节点)。 对于每个节点(前往时),检查其是否有左子节点。 如果是这样(表示原始树中具有子元素的节点),则用“:”打印节点数据,并获取其子树并递归跟随所有正确的儿子(代表所有同级兄弟),然后打印每个正确的儿子数据(您有)以相反的顺序在代码中:

void print_siblings() {
    if (this->right != NULL) {
        this->right->print_siblings();
    }
    cout << data << ", ";
}

void traverse(void) {
    if (this->left != NULL) {
        cout << data << ":";
        this->left->print_siblings();
        cout << endl;
        this->left->traverse();
    }

    if (this->right != NULL) {
        this->right->traverse();
    }
}

编辑:这是一个有序遍历:

void traverse_inorder(void) {
    if (this->left != NULL) {
        this->left->traverse_inorder();
        cout << data << ":";
        this->left->print_siblings();
        cout << endl;
    }

    if (this->right != NULL) {
        this->right->traverse_inorder();
    }
}

预购的输出为:

1:5, 4, 3, 2,
3:7, 6,
5:8,
8:11, 10, 9,

订单的输出将是:

3:7, 6,
8:11, 10, 9,
5:8,
1:5, 4, 3, 2,

Edit2:为了完整起见,还需要后遍历:-)

void traverse_postorder(void) {
    if (this->left != NULL) {
        this->left->traverse_postorder();
    }

    if (this->right != NULL) {
        this->right->traverse_postorder();
    }
    if (this->left != NULL) {
        cout << data << ":";
        this->left->print_siblings();
        cout << endl;
    }

}

及其输出:

8:11, 10, 9,
5:8,
3:7, 6,
1:5, 4, 3, 2,

我做了这样的事情,但是由于while循环,这有点不递归。 有什么办法让它更REC

void mirrorChilds(TNode * root)//
{
    if(!root) return;
    cout << root->data << " ";
    //tmp = tmp->right;
    if(!root->left) return;
    TNode * tmp = root->left;
    while(tmp != NULL)
    {

        root->left = tmp;
        tmp = tmp->right;
    }

   tmp = root->left;
   while(root != tmp)
    {
        cout << tmp->data << " ";
        tmp = tmp->parent;

    }
    cout << endl;
    tmp = root->left;
    while(root != tmp)
    {
        if(tmp->left) mirrorChilds(tmp);
        //if(tmp->left) cout << tmp->left->data << endl;
        tmp = tmp->parent;

    }
}

它完全可以正常工作,但是,我有点想获得O(n * log(n))时间...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM