简体   繁体   中英

Binary tree and especial node printing

I have a tree like this in binary representation;

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

But in reality this is not binary tree but like

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

Can you please help me getting printed out something like(childs are printed out in reversed order)

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

My TNode class looks like.

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;
    }
};

Does this need stack implementation?

Try something like: (Not tested yet)

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");
    }

}

I am still trying to make it more elegant. Thanks for the interesting problem!

Edit: Changed the format specifiers to lld

What about this approach: Traverse the tree in preorder (visiting each node). For each node (when travelled to it) check if it has a left child. If so (indicating a node with child elements in original tree) print the nodes data with a ':' and take its subtree and follow all right sons (which are representing all siblings) recursively, afterwards print each right sons data (you have it in reversed order. In Code:

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();
    }
}

Edit: Here is a inorder-traversal:

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();
    }
}

Output for preorder would be:

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

Output for inorder would be:

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

Edit2: Just for completeness, the post-order traversal as well :-)

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;
    }

}

And its output:

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

I've made something like this, but kind'a unrecursive due to while loops. Is there any way making it more 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;

    }
}

It works prefectly fine, but ye, i kinda trying to get O(n*log(n)) time ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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