Node switching help

This is a discussion on Node switching help within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to implement a huffman binary tree for class. Each node has 3 pointers: left, right, and ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    42

    Node switching help

    Hi. I am trying to implement a huffman binary tree for class. Each node has 3 pointers: left, right, and back to parent. I cannot for the life of me figure out why my code won't work for switching two nodes:

    Code:
    void huffmanTree::switchNodes(treeNode *& T1, treeNode *& T2)
    {
          
        treeNode * temp1 = T1->back;
        treeNode * temp2 = T2->back;
       
       if(T1 == temp1->right)
            temp1->right = T2;
        else
            temp1->left = T2;
        
        
        if(T2 == temp2->right)
            temp2->right = T1;
        else
            temp2->left = T1;
            
        T1->back = temp2;
        T2->back = temp1;
    
    }
    I tried getting it to work with a three node tree (switching the two external nodes). If i explicitly use the root member pointer

    root->left = T1;
    root->right = T2;

    or whatever, i can get it to work, but i don't see why the above won't.

    EDIT: Delete this thread, found the problem in the above code.
    Last edited by Link_26; 02-15-2009 at 08:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21